Skip to content

Commit

Permalink
fix vai tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brudnak committed Jan 13, 2025
1 parent dae07ed commit d06c5d3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 97 deletions.
2 changes: 1 addition & 1 deletion tests/v2/validation/steve/vai/pod_filter_test_cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var podFilterTestCases = []podFilterTestCase{
pods[1].Spec.NodeName = "node2"
pods[2].Spec.NodeName = "node1"

expectedNames := []string{}
var expectedNames []string
allNamespaces := []string{ns1, ns2}
expectedNamespaces := []string{ns1, ns2}

Expand Down
14 changes: 10 additions & 4 deletions tests/v2/validation/steve/vai/scripts/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ import (
)
func main() {
tableName := strings.ReplaceAll(os.Getenv("TABLE_NAME"), "\"", "")
resourceName := os.Getenv("RESOURCE_NAME")
db, err := sql.Open("sqlite", "/var/lib/rancher/informer_object_fields.db")
db, err := sql.Open("sqlite", "/var/lib/rancher/informer_object_cache.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
fmt.Println("Creating database snapshot...")
_, err = db.Exec("VACUUM INTO '/tmp/snapshot.db'")
if err != nil {
log.Fatal(err)
}
tableName := strings.ReplaceAll(os.Getenv("TABLE_NAME"), "\"", "")
resourceName := os.Getenv("RESOURCE_NAME")
query := fmt.Sprintf("SELECT \"metadata.name\" FROM \"%s\" WHERE \"metadata.name\" = ?", tableName)
stmt, err := db.Prepare(query)
if err != nil {
Expand Down
14 changes: 0 additions & 14 deletions tests/v2/validation/steve/vai/vai.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package vai
import (
"fmt"
"github.com/rancher/shepherd/clients/rancher"
"github.com/rancher/shepherd/extensions/vai"
"strings"
"sync"
)

const (
Expand Down Expand Up @@ -45,18 +43,6 @@ func isVaiEnabled(client *rancher.Client) (bool, error) {
return value, nil
}

func enableVAI(client *rancher.Client, vaiEnabled *bool, once *sync.Once) error {
var enableErr error
once.Do(func() {
if err := vai.EnableVaiCaching(client); err != nil {
enableErr = fmt.Errorf("failed to enable VAI caching: %w", err)
return
}
*vaiEnabled = true
})
return enableErr
}

func filterTestCases[T SupportedWithVai](testCases []T, vaiEnabled bool) []T {
if !vaiEnabled {
return testCases
Expand Down
150 changes: 72 additions & 78 deletions tests/v2/validation/steve/vai/vai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/url"
"strings"
"sync"
"testing"
"time"
)

const scriptURL = "https://raw.githubusercontent.com/rancher/rancher/main/tests/v2/validation/steve/vai/scripts/script.sh"
// TODO: update
const scriptURL = "https://raw.githubusercontent.com/brudnak/rancher/refs/heads/brudnak-vai/tests/v2/validation/steve/vai/scripts/script.sh"

type VaiTestSuite struct {
suite.Suite
Expand All @@ -33,7 +33,6 @@ type VaiTestSuite struct {
session *session.Session
cluster management.Cluster
vaiEnabled bool
once sync.Once
}

func (v *VaiTestSuite) SetupSuite() {
Expand All @@ -46,8 +45,8 @@ func (v *VaiTestSuite) SetupSuite() {
v.client = client
v.steveClient = client.Steve

// Check initial VAI state
enabled, err := isVaiEnabled(v.client)

require.NoError(v.T(), err)
v.vaiEnabled = enabled
}
Expand All @@ -56,89 +55,55 @@ func (v *VaiTestSuite) TearDownSuite() {
v.session.Cleanup()
}

func (v *VaiTestSuite) enableVai() error {
logrus.Info("Enabling VAI caching")
startTime := time.Now()
err := enableVAI(v.client, &v.vaiEnabled, &v.once)
if err != nil {
return err
func (v *VaiTestSuite) ensureVaiEnabled() {
if !v.vaiEnabled {
logrus.Info("VAI is disabled, enabling it for test...")
err := vai.EnableVaiCaching(v.client)
require.NoError(v.T(), err)

// Verify VAI is now enabled
enabled, err := isVaiEnabled(v.client)
require.NoError(v.T(), err)
require.True(v.T(), enabled, "VAI should be enabled")
v.vaiEnabled = true
}
duration := time.Since(startTime)
logrus.Infof("Enabling VAI took %s", formatDuration(duration))
v.vaiEnabled = true
return nil
}

func (v *VaiTestSuite) disableVai() error {
logrus.Info("Disabling VAI caching")
startTime := time.Now()
err := vai.DisableVaiCaching(v.client)
if err != nil {
return err
func (v *VaiTestSuite) ensureVaiDisabled() {
if v.vaiEnabled {
logrus.Info("VAI is enabled, disabling it for test...")
err := vai.DisableVaiCaching(v.client)
require.NoError(v.T(), err)

// Verify VAI is now disabled
enabled, err := isVaiEnabled(v.client)
require.NoError(v.T(), err)
require.False(v.T(), enabled, "VAI should be disabled")
v.vaiEnabled = false
}
duration := time.Since(startTime)
logrus.Infof("Disabling VAI took %s", formatDuration(duration))
v.vaiEnabled = false
return nil
}

func formatDuration(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
if h > 0 {
return fmt.Sprintf("%d hours %d minutes %d seconds", h, m, s)
} else if m > 0 {
return fmt.Sprintf("%d minutes %d seconds", m, s)
} else {
return fmt.Sprintf("%d seconds", s)
}
}

func (v *VaiTestSuite) TestVAI() {
v.Run("InitialState", func() {
if v.vaiEnabled {
v.Run("TestWithVaiInitiallyEnabled", v.testWithVaiEnabled)
} else {
v.Run("TestWithVaiInitiallyDisabled", v.testWithVaiDisabled)
}
})

v.Run("ToggleVaiAndRetest", func() {
initialState := v.vaiEnabled
if initialState {
err := v.disableVai()
require.NoError(v.T(), err)
v.testWithVaiDisabled()
} else {
err := v.enableVai()
require.NoError(v.T(), err)
v.testWithVaiEnabled()
}
})
}
// TestVaiEnabled runs all tests with VAI enabled
func (v *VaiTestSuite) TestVaiEnabled() {
v.ensureVaiEnabled()

func (v *VaiTestSuite) testWithVaiEnabled() {
v.Run("SecretFilters", func() {
supportedWithVai := filterTestCases(secretFilterTestCases, v.vaiEnabled)
supportedWithVai := filterTestCases(secretFilterTestCases, true)
v.runSecretFilterTestCases(supportedWithVai)
})

v.Run("PodFilters", func() {
supportedWithVai := filterTestCases(podFilterTestCases, v.vaiEnabled)
supportedWithVai := filterTestCases(podFilterTestCases, true)
v.runPodFilterTestCases(supportedWithVai)
})

v.Run("SecretSorting", func() {
supportedWithVai := filterTestCases(secretSortTestCases, v.vaiEnabled)
supportedWithVai := filterTestCases(secretSortTestCases, true)
v.runSecretSortTestCases(supportedWithVai)
})

v.Run("SecretLimit", func() {
supportedWithVai := filterTestCases(secretLimitTestCases, v.vaiEnabled)
supportedWithVai := filterTestCases(secretLimitTestCases, true)
v.runSecretLimitTestCases(supportedWithVai)
})

Expand All @@ -147,28 +112,55 @@ func (v *VaiTestSuite) testWithVaiEnabled() {
v.Run("CheckNamespaceInAllVAIDatabases", v.checkNamespaceInAllVAIDatabases)
}

func (v *VaiTestSuite) testWithVaiDisabled() {
// TestVaiDisabled runs all tests with VAI disabled
func (v *VaiTestSuite) TestVaiDisabled() {
v.ensureVaiDisabled()

v.Run("SecretFilters", func() {
unsupportedWithVai := filterTestCases(secretFilterTestCases, v.vaiEnabled)
unsupportedWithVai := filterTestCases(secretFilterTestCases, false)
v.runSecretFilterTestCases(unsupportedWithVai)
})

v.Run("PodFilters", func() {
unsupportedWithVai := filterTestCases(podFilterTestCases, v.vaiEnabled)
unsupportedWithVai := filterTestCases(podFilterTestCases, false)
v.runPodFilterTestCases(unsupportedWithVai)
})

v.Run("SecretSorting", func() {
unsupportedWithVai := filterTestCases(secretSortTestCases, v.vaiEnabled)
unsupportedWithVai := filterTestCases(secretSortTestCases, false)
v.runSecretSortTestCases(unsupportedWithVai)
})

v.Run("SecretLimit", func() {
unsupportedWithVai := filterTestCases(secretLimitTestCases, v.vaiEnabled)
unsupportedWithVai := filterTestCases(secretLimitTestCases, false)
v.runSecretLimitTestCases(unsupportedWithVai)
})

v.Run("NormalOperations", v.testNormalOperationsWithVaiDisabled)
v.Run("NormalOperations", func() {
pods, err := v.client.Steve.SteveType("pod").List(nil)
require.NoError(v.T(), err)
require.NotEmpty(v.T(), pods.Data, "Should be able to list pods even with VAI disabled")
})
}

func TestVaiTestSuite(t *testing.T) {
suite.Run(t, new(VaiTestSuite))
}

func formatDuration(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
if h > 0 {
return fmt.Sprintf("%d hours %d minutes %d seconds", h, m, s)
} else if m > 0 {
return fmt.Sprintf("%d minutes %d seconds", m, s)
} else {
return fmt.Sprintf("%d seconds", s)
}
}

func (v *VaiTestSuite) testNormalOperationsWithVaiDisabled() {
Expand Down Expand Up @@ -387,7 +379,7 @@ func (v *VaiTestSuite) runSecretLimitTestCases(testCases []secretLimitTestCase)
}

func (v *VaiTestSuite) checkDBFilesInPods() {
expectedDBFiles := []string{"informer_object_cache.db", "informer_object_fields.db"}
expectedDBFiles := []string{"informer_object_cache.db", "informer_object_cache.db-wal", "informer_object_cache.db-shm"}

rancherPods, err := listRancherPods(v.client)
require.NoError(v.T(), err)
Expand All @@ -407,6 +399,12 @@ func (v *VaiTestSuite) checkDBFilesInPods() {
if strings.HasSuffix(file, ".db") {
dbFiles = append(dbFiles, file)
}
if strings.HasSuffix(file, ".db-wal") {
dbFiles = append(dbFiles, file)
}
if strings.HasSuffix(file, ".db-shm") {
dbFiles = append(dbFiles, file)
}
}

for _, expectedFile := range expectedDBFiles {
Expand Down Expand Up @@ -559,7 +557,3 @@ func (v *VaiTestSuite) checkNamespaceInAllVAIDatabases() {
namespaceName, namespaceFoundCount, len(rancherPods)))
v.T().Log("TestCheckNamespaceInAllVAIDatabases test completed")
}

func TestVaiTestSuite(t *testing.T) {
suite.Run(t, new(VaiTestSuite))
}

0 comments on commit d06c5d3

Please sign in to comment.