From d06c5d3aa3aa48e2bb2c9035612f91927342b845 Mon Sep 17 00:00:00 2001 From: Andrew Brudnak Date: Tue, 22 Oct 2024 16:13:55 -0700 Subject: [PATCH] fix vai tests --- .../steve/vai/pod_filter_test_cases.go | 2 +- .../v2/validation/steve/vai/scripts/script.sh | 14 +- tests/v2/validation/steve/vai/vai.go | 14 -- tests/v2/validation/steve/vai/vai_test.go | 150 +++++++++--------- 4 files changed, 83 insertions(+), 97 deletions(-) diff --git a/tests/v2/validation/steve/vai/pod_filter_test_cases.go b/tests/v2/validation/steve/vai/pod_filter_test_cases.go index ee99add7b3d..a8bcd2e59e9 100644 --- a/tests/v2/validation/steve/vai/pod_filter_test_cases.go +++ b/tests/v2/validation/steve/vai/pod_filter_test_cases.go @@ -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} diff --git a/tests/v2/validation/steve/vai/scripts/script.sh b/tests/v2/validation/steve/vai/scripts/script.sh index ce60fb1f46c..34047ba9638 100644 --- a/tests/v2/validation/steve/vai/scripts/script.sh +++ b/tests/v2/validation/steve/vai/scripts/script.sh @@ -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 { diff --git a/tests/v2/validation/steve/vai/vai.go b/tests/v2/validation/steve/vai/vai.go index 735308c2c6c..b83bd2c81ef 100644 --- a/tests/v2/validation/steve/vai/vai.go +++ b/tests/v2/validation/steve/vai/vai.go @@ -3,9 +3,7 @@ package vai import ( "fmt" "github.com/rancher/shepherd/clients/rancher" - "github.com/rancher/shepherd/extensions/vai" "strings" - "sync" ) const ( @@ -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 diff --git a/tests/v2/validation/steve/vai/vai_test.go b/tests/v2/validation/steve/vai/vai_test.go index 6e3f8ded59b..90b9ea5f273 100644 --- a/tests/v2/validation/steve/vai/vai_test.go +++ b/tests/v2/validation/steve/vai/vai_test.go @@ -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 @@ -33,7 +33,6 @@ type VaiTestSuite struct { session *session.Session cluster management.Cluster vaiEnabled bool - once sync.Once } func (v *VaiTestSuite) SetupSuite() { @@ -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 } @@ -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) }) @@ -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() { @@ -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) @@ -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 { @@ -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)) -}