hs-test: added filenames to test names 65/40965/4
authorAdrian Villin <[email protected]>
Fri, 7 Jun 2024 10:45:48 +0000 (06:45 -0400)
committerDave Wallace <[email protected]>
Mon, 10 Jun 2024 16:42:38 +0000 (16:42 +0000)
- It is now possible to only run tests that are in a certain file

Type: test

Change-Id: I41665dd2bc0942c283be36a5af3e560fd65e9d03
Signed-off-by: Adrian Villin <[email protected]>
extras/hs-test/README.rst
extras/hs-test/framework_test.go
extras/hs-test/suite_nginx_test.go
extras/hs-test/suite_no_topo_test.go
extras/hs-test/suite_ns_test.go
extras/hs-test/suite_tap_test.go
extras/hs-test/suite_veth_test.go

index 1dc1039..a88251a 100644 (file)
@@ -89,6 +89,30 @@ This can be put in file ``extras/hs-test/my_test.go`` and run with command ``mak
                 s.log(result)
         }
 
+
+Filtering test cases
+--------------------
+
+The framework allows us to filter test cases in a few different ways, using ``make test TEST=``:
+* Suite name
+* File name
+* Test name
+* All of the above as long as they are ordered properly, e.g. ``make test TEST=VethsSuite.http_test.go.HeaderServerTest``
+
+**Names are case sensitive!**
+
+Names don't have to be complete, as long as they are last:
+This is valid and will run all tests in every ``http`` file (if there is more than one):
+``make test TEST=VethsSuite.http``
+This is not valid:
+``make test TEST=Veths.http``
+
+They can also be left out:
+``make test TEST=http_test.go`` will run every test in ``http_test.go``
+``make test TEST=Nginx`` will run everything that has 'Nginx' in its name - suites, files and tests.
+``make test TEST=HeaderServerTest`` will only run the header server test
+
+
 Modifying the framework
 -----------------------
 
index 8cbf936..8d7e2ed 100644 (file)
@@ -1,6 +1,8 @@
 package main
 
 import (
+       "path/filepath"
+       "runtime"
        "testing"
        "time"
 
@@ -10,6 +12,11 @@ import (
 
 var suiteTimeout time.Duration
 
+func getTestFilename() string {
+       _, filename, _, _ := runtime.Caller(2)
+       return filepath.Base(filename)
+}
+
 func TestHst(t *testing.T) {
        if *isVppDebug {
                // 30 minute timeout so that the framework won't timeout while debugging
index 4c6e9db..7caf16e 100644 (file)
@@ -17,18 +17,18 @@ const (
        mirroringServerInterfaceName = "hstsrv"
 )
 
-var nginxTests = []func(s *NginxSuite){}
-var nginxSoloTests = []func(s *NginxSuite){}
+var nginxTests = map[string][]func(s *NginxSuite){}
+var nginxSoloTests = map[string][]func(s *NginxSuite){}
 
 type NginxSuite struct {
        HstSuite
 }
 
 func registerNginxTests(tests ...func(s *NginxSuite)) {
-       nginxTests = append(nginxTests, tests...)
+       nginxTests[getTestFilename()] = tests
 }
 func registerNginxSoloTests(tests ...func(s *NginxSuite)) {
-       nginxSoloTests = append(nginxSoloTests, tests...)
+       nginxSoloTests[getTestFilename()] = tests
 }
 
 func (s *NginxSuite) SetupSuite() {
@@ -92,15 +92,18 @@ var _ = Describe("NginxSuite", Ordered, ContinueOnFailure, func() {
        AfterEach(func() {
                s.TearDownTest()
        })
-       for _, test := range nginxTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+
+       for filename, tests := range nginxTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
 
@@ -119,14 +122,16 @@ var _ = Describe("NginxSuiteSolo", Ordered, ContinueOnFailure, Serial, func() {
                s.TearDownTest()
        })
 
-       for _, test := range nginxSoloTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, Label("SOLO"), func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range nginxSoloTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, Label("SOLO"), func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
index 260fc1d..af0cf9a 100644 (file)
@@ -14,18 +14,18 @@ const (
        tapInterfaceName         = "htaphost"
 )
 
-var noTopoTests = []func(s *NoTopoSuite){}
-var noTopoSoloTests = []func(s *NoTopoSuite){}
+var noTopoTests = map[string][]func(s *NoTopoSuite){}
+var noTopoSoloTests = map[string][]func(s *NoTopoSuite){}
 
 type NoTopoSuite struct {
        HstSuite
 }
 
 func registerNoTopoTests(tests ...func(s *NoTopoSuite)) {
-       noTopoTests = append(noTopoTests, tests...)
+       noTopoTests[getTestFilename()] = tests
 }
 func registerNoTopoSoloTests(tests ...func(s *NoTopoSuite)) {
-       noTopoSoloTests = append(noTopoSoloTests, tests...)
+       noTopoSoloTests[getTestFilename()] = tests
 }
 
 func (s *NoTopoSuite) SetupSuite() {
@@ -68,15 +68,17 @@ var _ = Describe("NoTopoSuite", Ordered, ContinueOnFailure, func() {
                s.TearDownTest()
        })
 
-       for _, test := range noTopoTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range noTopoTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
 
@@ -95,14 +97,16 @@ var _ = Describe("NoTopoSuiteSolo", Ordered, ContinueOnFailure, Serial, func() {
                s.TearDownTest()
        })
 
-       for _, test := range noTopoSoloTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, Label("SOLO"), func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range noTopoSoloTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, Label("SOLO"), func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
index 7bdb90b..b4fa771 100644 (file)
@@ -15,18 +15,18 @@ const (
        serverInterface = "hsrvvpp"
 )
 
-var nsTests = []func(s *NsSuite){}
-var nsSoloTests = []func(s *NsSuite){}
+var nsTests = map[string][]func(s *NsSuite){}
+var nsSoloTests = map[string][]func(s *NsSuite){}
 
 type NsSuite struct {
        HstSuite
 }
 
 func registerNsTests(tests ...func(s *NsSuite)) {
-       nsTests = append(nsTests, tests...)
+       nsTests[getTestFilename()] = tests
 }
 func registerNsSoloTests(tests ...func(s *NsSuite)) {
-       nsSoloTests = append(nsSoloTests, tests...)
+       nsSoloTests[getTestFilename()] = tests
 }
 
 func (s *NsSuite) SetupSuite() {
@@ -77,15 +77,17 @@ var _ = Describe("NsSuite", Ordered, ContinueOnFailure, func() {
                s.TearDownTest()
        })
 
-       for _, test := range nsTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range nsTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
 
@@ -104,14 +106,16 @@ var _ = Describe("NsSuiteSolo", Ordered, ContinueOnFailure, Serial, func() {
                s.TearDownTest()
        })
 
-       for _, test := range nsSoloTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, Label("SOLO"), func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range nsSoloTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, Label("SOLO"), func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
index cb06533..bb7082d 100644 (file)
@@ -13,14 +13,14 @@ type TapSuite struct {
        HstSuite
 }
 
-var tapTests = []func(s *TapSuite){}
-var tapSoloTests = []func(s *TapSuite){}
+var tapTests = map[string][]func(s *TapSuite){}
+var tapSoloTests = map[string][]func(s *TapSuite){}
 
 func registerTapTests(tests ...func(s *TapSuite)) {
-       tapTests = append(tapTests, tests...)
+       tapTests[getTestFilename()] = tests
 }
 func registerTapSoloTests(tests ...func(s *TapSuite)) {
-       tapSoloTests = append(tapSoloTests, tests...)
+       tapSoloTests[getTestFilename()] = tests
 }
 
 func (s *TapSuite) SetupSuite() {
@@ -44,15 +44,17 @@ var _ = Describe("TapSuite", Ordered, ContinueOnFailure, func() {
                s.TearDownTest()
        })
 
-       for _, test := range tapTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range tapTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
 
@@ -71,14 +73,16 @@ var _ = Describe("TapSuiteSolo", Ordered, ContinueOnFailure, Serial, func() {
                s.TearDownTest()
        })
 
-       for _, test := range tapSoloTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, Label("SOLO"), func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range tapSoloTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, Label("SOLO"), func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
index 13ef5ef..49a36ca 100644 (file)
@@ -16,18 +16,18 @@ const (
        clientInterfaceName = "cln"
 )
 
-var vethTests = []func(s *VethsSuite){}
-var vethSoloTests = []func(s *VethsSuite){}
+var vethTests = map[string][]func(s *VethsSuite){}
+var vethSoloTests = map[string][]func(s *VethsSuite){}
 
 type VethsSuite struct {
        HstSuite
 }
 
 func registerVethTests(tests ...func(s *VethsSuite)) {
-       vethTests = append(vethTests, tests...)
+       vethTests[getTestFilename()] = tests
 }
 func registerSoloVethTests(tests ...func(s *VethsSuite)) {
-       vethSoloTests = append(vethSoloTests, tests...)
+       vethSoloTests[getTestFilename()] = tests
 }
 
 func (s *VethsSuite) SetupSuite() {
@@ -101,15 +101,17 @@ var _ = Describe("VethsSuite", Ordered, ContinueOnFailure, func() {
        })
 
        // https://onsi.github.io/ginkgo/#dynamically-generating-specs
-       for _, test := range vethTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range vethTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })
 
@@ -129,14 +131,16 @@ var _ = Describe("VethsSuiteSolo", Ordered, ContinueOnFailure, Serial, func() {
        })
 
        // https://onsi.github.io/ginkgo/#dynamically-generating-specs
-       for _, test := range vethSoloTests {
-               test := test
-               pc := reflect.ValueOf(test).Pointer()
-               funcValue := runtime.FuncForPC(pc)
-               testName := strings.Split(funcValue.Name(), ".")[2]
-               It(testName, Label("SOLO"), func(ctx SpecContext) {
-                       s.log(testName + ": BEGIN")
-                       test(&s)
-               }, SpecTimeout(suiteTimeout))
+       for filename, tests := range vethSoloTests {
+               for _, test := range tests {
+                       test := test
+                       pc := reflect.ValueOf(test).Pointer()
+                       funcValue := runtime.FuncForPC(pc)
+                       testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
+                       It(testName, Label("SOLO"), func(ctx SpecContext) {
+                               s.log(testName + ": BEGIN")
+                               test(&s)
+                       }, SpecTimeout(suiteTimeout))
+               }
        }
 })