From 82636a3ea445d902ac330c100dbb3dfa2041012e Mon Sep 17 00:00:00 2001 From: Anand Francis Joseph Date: Tue, 11 Jul 2023 20:43:21 +0530 Subject: [PATCH 1/2] Added support for test excludes Signed-off-by: Anand Francis Joseph --- pkg/apis/testharness/v1beta1/test_types.go | 4 ++++ .../v1beta1/zz_generated.deepcopy.go | 5 +++++ pkg/test/harness.go | 22 +++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/apis/testharness/v1beta1/test_types.go b/pkg/apis/testharness/v1beta1/test_types.go index 92981426..2c1d38a5 100644 --- a/pkg/apis/testharness/v1beta1/test_types.go +++ b/pkg/apis/testharness/v1beta1/test_types.go @@ -77,6 +77,10 @@ type TestSuite struct { Suppress []string `json:"suppress"` Config *RestConfig `json:"config,omitempty"` + + // Test sub directories that needs to be excluded from the test run that may otherwise be included with `TestDirs`. + // Include all directories specified in `TestDirs`` and exclude those specified in `TestExcludeDirs`. + TestExcludeDirs []string `json:"testExcludeDirs,omitempty"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/pkg/apis/testharness/v1beta1/zz_generated.deepcopy.go b/pkg/apis/testharness/v1beta1/zz_generated.deepcopy.go index 68f8d6c1..a8cc2fbd 100644 --- a/pkg/apis/testharness/v1beta1/zz_generated.deepcopy.go +++ b/pkg/apis/testharness/v1beta1/zz_generated.deepcopy.go @@ -239,6 +239,11 @@ func (in *TestSuite) DeepCopyInto(out *TestSuite) { in, out := &in.Config, &out.Config *out = (*in).DeepCopy() } + if in.TestExcludeDirs != nil { + in, out := &in.TestExcludeDirs, &out.TestExcludeDirs + *out = make([]string, len(*in)) + copy(*out, *in) + } return } diff --git a/pkg/test/harness.go b/pkg/test/harness.go index b221a5bf..87c1667c 100644 --- a/pkg/test/harness.go +++ b/pkg/test/harness.go @@ -56,7 +56,7 @@ type Harness struct { } // LoadTests loads all of the tests in a given directory. -func (h *Harness) LoadTests(dir string) ([]*Case, error) { +func (h *Harness) LoadTests(dir string, testExcludes []string) ([]*Case, error) { dir, err := filepath.Abs(dir) if err != nil { return nil, err @@ -77,6 +77,11 @@ func (h *Harness) LoadTests(dir string) ([]*Case, error) { continue } + if isTestExcluded(file.Name(), testExcludes) { + h.T.Logf("skipping test dir %s as its excluded by excludeDirs", file.Name()) + continue + } + tests = append(tests, &Case{ Timeout: timeout, Steps: []*Step{}, @@ -360,12 +365,13 @@ func (h *Harness) RunTests() { h.T.Log("running tests") testDirs := h.testPreProcessing() + testExcludeDirs := h.TestSuite.TestExcludeDirs //todo: testsuite + testsuites (extend case to have what we need (need testdir here) // TestSuite is a TestSuiteCollection and should be renamed for v1beta2 realTestSuite := make(map[string][]*Case) for _, testDir := range testDirs { - tempTests, err := h.LoadTests(testDir) + tempTests, err := h.LoadTests(testDir, testExcludeDirs) if err != nil { h.T.Fatal(err) } @@ -637,3 +643,15 @@ func (h *Harness) loadKindConfig(path string) (*kindConfig.Cluster, error) { } return cluster, nil } + +func isTestExcluded(testDirName string, testExcludes []string) bool { + if testExcludes == nil || len(testExcludes) == 0 { + return false + } + for _, testExclude := range testExcludes { + if strings.Contains(testDirName, testExclude) { + return true + } + } + return false +} From 33757764354c313d163a46c6db7932742d08cbad Mon Sep 17 00:00:00 2001 From: Anand Francis Joseph Date: Tue, 11 Jul 2023 22:11:28 +0530 Subject: [PATCH 2/2] Added better comments in the newly added code Signed-off-by: Anand Francis Joseph --- pkg/apis/testharness/v1beta1/test_types.go | 2 +- pkg/test/harness.go | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/apis/testharness/v1beta1/test_types.go b/pkg/apis/testharness/v1beta1/test_types.go index 2c1d38a5..1020cd7e 100644 --- a/pkg/apis/testharness/v1beta1/test_types.go +++ b/pkg/apis/testharness/v1beta1/test_types.go @@ -79,7 +79,7 @@ type TestSuite struct { Config *RestConfig `json:"config,omitempty"` // Test sub directories that needs to be excluded from the test run that may otherwise be included with `TestDirs`. - // Include all directories specified in `TestDirs`` and exclude those specified in `TestExcludeDirs`. + // Include all directories specified in `TestDirs` and exclude those specified in `TestExcludeDirs`. TestExcludeDirs []string `json:"testExcludeDirs,omitempty"` } diff --git a/pkg/test/harness.go b/pkg/test/harness.go index 87c1667c..76c5afff 100644 --- a/pkg/test/harness.go +++ b/pkg/test/harness.go @@ -77,11 +77,11 @@ func (h *Harness) LoadTests(dir string, testExcludes []string) ([]*Case, error) continue } + // If test excludes are specified, check and add tests that are not excluded explicitly. if isTestExcluded(file.Name(), testExcludes) { h.T.Logf("skipping test dir %s as its excluded by excludeDirs", file.Name()) continue } - tests = append(tests, &Case{ Timeout: timeout, Steps: []*Step{}, @@ -365,13 +365,13 @@ func (h *Harness) RunTests() { h.T.Log("running tests") testDirs := h.testPreProcessing() - testExcludeDirs := h.TestSuite.TestExcludeDirs + testExcludes := h.TestSuite.TestExcludeDirs //todo: testsuite + testsuites (extend case to have what we need (need testdir here) // TestSuite is a TestSuiteCollection and should be renamed for v1beta2 realTestSuite := make(map[string][]*Case) for _, testDir := range testDirs { - tempTests, err := h.LoadTests(testDir, testExcludeDirs) + tempTests, err := h.LoadTests(testDir, testExcludes) if err != nil { h.T.Fatal(err) } @@ -644,12 +644,15 @@ func (h *Harness) loadKindConfig(path string) (*kindConfig.Cluster, error) { return cluster, nil } +// isTestExcluded returns true if a test is excluded explicitly, false otherwise. func isTestExcluded(testDirName string, testExcludes []string) bool { - if testExcludes == nil || len(testExcludes) == 0 { + if len(testExcludes) <= 0 { + // Test excludes not specified. Include all tests by default. return false } for _, testExclude := range testExcludes { - if strings.Contains(testDirName, testExclude) { + // TODO: support regex based comparison in future + if strings.Compare(testDirName, testExclude) == 0 { return true } }