hs-test: use assert-like approach in test cases 54/37754/3
authorMaros Ondrejicka <maros.ondrejicka@pantheon.tech>
Tue, 6 Dec 2022 14:38:05 +0000 (15:38 +0100)
committerFlorin Coras <florin.coras@gmail.com>
Wed, 7 Dec 2022 16:05:20 +0000 (16:05 +0000)
Type: test
Signed-off-by: Maros Ondrejicka <maros.ondrejicka@pantheon.tech>
Change-Id: I1653001461d4dfc52f1fb3a9e0cf458a506b8324

extras/hs-test/framework_test.go
extras/hs-test/http_test.go
extras/hs-test/ldp_test.go
extras/hs-test/linux_iperf_test.go
extras/hs-test/proxy_test.go
extras/hs-test/utils.go
extras/hs-test/vcl_test.go

index cfc3801..3df509f 100755 (executable)
@@ -45,6 +45,18 @@ func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...inter
        }
 }
 
+func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
+       if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) {
+               s.hstFail()
+       }
+}
+
+func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) {
+       if !assert.Contains(s.T(), testString, contains, msgAndArgs...) {
+               s.hstFail()
+       }
+}
+
 func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
        if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
                s.hstFail()
index 99b509f..d2fb5a3 100755 (executable)
@@ -1,8 +1,6 @@
 package main
 
 import (
-       "strings"
-
        "github.com/edwarnicke/exechelper"
 )
 
@@ -15,26 +13,17 @@ func (s *NsSuite) TestHttpTps() {
 
        t.Log("starting vpp..")
 
-       err := dockerRun(dockerInstance, "")
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       s.assertNil(dockerRun(dockerInstance, ""), "failed to start docker")
        defer func() { exechelper.Run("docker stop " + dockerInstance) }()
 
        // start & configure vpp in the container
-       _, err = hstExec("ConfigureHttpTps", dockerInstance)
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       _, err := hstExec("ConfigureHttpTps", dockerInstance)
+       s.assertNil(err)
 
        go startWget(finished, server_ip, port, "client")
        // wait for client
        err = <-finished
-       if err != nil {
-               t.Errorf("%v", err)
-       }
+       s.assertNil(err)
 }
 
 func (s *VethsSuite) TestHttpCli() {
@@ -42,49 +31,27 @@ func (s *VethsSuite) TestHttpCli() {
 
        srvInstance := "http-cli-srv"
        clnInstance := "http-cli-cln"
-       err := dockerRun(srvInstance, "")
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       s.assertNil(dockerRun(srvInstance, ""), "failed to start docker (srv)")
        defer func() { exechelper.Run("docker stop " + srvInstance) }()
 
-       err = dockerRun(clnInstance, "")
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       s.assertNil(dockerRun(clnInstance, ""), "failed to start docker (cln)")
        defer func() { exechelper.Run("docker stop " + clnInstance) }()
 
-       _, err = hstExec("Configure2Veths srv", srvInstance)
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       _, err := hstExec("Configure2Veths srv", srvInstance)
+       s.assertNil(err)
 
        _, err = hstExec("Configure2Veths cln", clnInstance)
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       s.assertNil(err)
 
        t.Log("configured IPs...")
 
        _, err = hstExec("RunHttpCliSrv", srvInstance)
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       s.assertNil(err)
 
        t.Log("configured http server")
 
        o, err := hstExec("RunHttpCliCln /show/version", clnInstance)
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
-
-       if strings.Index(o, "<html>") < 0 {
-               t.Error("<html> not found in the result!")
-       }
+       s.assertNil(err)
+
+       s.assertContains(o, "<html>", "<html> not found in the result!")
 }
index c219c82..683c6a3 100755 (executable)
@@ -9,7 +9,6 @@ import (
 )
 
 func (s *VethsSuite) TestLDPreloadIperfVpp() {
-       t := s.T()
        var clnVclConf, srvVclConf Stanza
 
        srvInstance := "vpp-ldp-srv"
@@ -23,7 +22,7 @@ func (s *VethsSuite) TestLDPreloadIperfVpp() {
        exechelper.Run("mkdir " + clnPath)
 
        ldpreload := os.Getenv("HST_LDPRELOAD")
-       s.Assert().NotEqual("", ldpreload)
+       s.assertNotEqual("", ldpreload)
 
        ldpreload = "LD_PRELOAD=" + ldpreload
 
@@ -33,31 +32,17 @@ func (s *VethsSuite) TestLDPreloadIperfVpp() {
 
        fmt.Println("starting VPPs")
 
-       err := dockerRun(srvInstance, fmt.Sprintf("-v /tmp/%s:/tmp", srvInstance))
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       s.assertNil(dockerRun(srvInstance, fmt.Sprintf("-v /tmp/%s:/tmp", srvInstance)), "failed to start docker (srv)")
        defer func() { exechelper.Run("docker stop " + srvInstance) }()
 
-       err = dockerRun(clnInstance, fmt.Sprintf("-v /tmp/%s:/tmp", clnInstance))
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       s.assertNil(dockerRun(clnInstance, fmt.Sprintf("-v /tmp/%s:/tmp", clnInstance)), "failed to start docker (cln)")
        defer func() { exechelper.Run("docker stop " + clnInstance) }()
 
-       _, err = hstExec("Configure2Veths srv", srvInstance)
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       _, err := hstExec("Configure2Veths srv", srvInstance)
+       s.assertNil(err)
 
        _, err = hstExec("Configure2Veths cln", clnInstance)
-       if err != nil {
-               t.Errorf("%v", err)
-               return
-       }
+       s.assertNil(err)
 
        err = clnVclConf.
                NewStanza("vcl").
@@ -68,10 +53,7 @@ func (s *VethsSuite) TestLDPreloadIperfVpp() {
                Append("use-mq-eventfd").
                Append(fmt.Sprintf("app-socket-api /tmp/%s/Configure2Veths/var/run/app_ns_sockets/2", clnInstance)).Close().
                SaveToFile(clnVcl)
-       if err != nil {
-               t.Errorf("%v", err)
-               t.FailNow()
-       }
+       s.assertNil(err)
 
        err = srvVclConf.
                NewStanza("vcl").
@@ -82,10 +64,8 @@ func (s *VethsSuite) TestLDPreloadIperfVpp() {
                Append("use-mq-eventfd").
                Append(fmt.Sprintf("app-socket-api /tmp/%s/Configure2Veths/var/run/app_ns_sockets/1", srvInstance)).Close().
                SaveToFile(srvVcl)
-       if err != nil {
-               t.Errorf("%v", err)
-               t.FailNow()
-       }
+       s.assertNil(err)
+
        fmt.Printf("attaching server to vpp")
 
        // FIXME
@@ -95,9 +75,7 @@ func (s *VethsSuite) TestLDPreloadIperfVpp() {
        go StartServerApp(srvCh, stopServerCh, srvEnv)
 
        err = <-srvCh
-       if err != nil {
-               s.FailNow("vcl server", "%v", err)
-       }
+       s.assertNil(err)
 
        fmt.Println("attaching client to vpp")
        clnEnv := append(os.Environ(), ldpreload, "VCL_CONFIG="+clnVcl)
@@ -105,9 +83,7 @@ func (s *VethsSuite) TestLDPreloadIperfVpp() {
 
        // wait for client's result
        err = <-clnCh
-       if err != nil {
-               s.Failf("client", "%v", err)
-       }
+       s.assertNil(err)
 
        // stop server
        stopServerCh <- struct{}{}
index 92a85cf..bef07fb 100755 (executable)
@@ -11,16 +11,11 @@ func (s *TapSuite) TestLinuxIperf() {
 
        go StartServerApp(srvCh, stopServerCh, nil)
        err := <-srvCh
-       if err != nil {
-               t.Errorf("%v", err)
-               t.FailNow()
-       }
+       s.assertNil(err)
        t.Log("server running")
        go StartClientApp(nil, clnCh)
        t.Log("client running")
        err = <-clnCh
-       if err != nil {
-               s.Failf("client", "%v", err)
-       }
+       s.assertNil(err)
        t.Log("Test completed")
 }
index d426b61..0ada6fa 100755 (executable)
@@ -4,42 +4,32 @@ import (
        "context"
        "fmt"
        "os"
-       "testing"
 
        "github.com/edwarnicke/exechelper"
 )
 
-func testProxyHttpTcp(t *testing.T, dockerInstance, action string, proxySetup func() error) error {
+func testProxyHttpTcp(s *NsSuite, dockerInstance, action string, proxySetup func() error) error {
        const outputFile = "test.data"
        const srcFile = "10M"
        stopServer := make(chan struct{}, 1)
        serverRunning := make(chan struct{}, 1)
 
        volumeArgs := fmt.Sprintf("-v shared-vol:/tmp/%s", dockerInstance)
-       err := dockerRun(dockerInstance, volumeArgs)
-       if err != nil {
-               return fmt.Errorf("failed to start container: %v", err)
-       }
+       s.assertNil(dockerRun(dockerInstance, volumeArgs), "failed to start container")
        defer func() { exechelper.Run("docker stop " + dockerInstance) }()
 
        // start & configure vpp in the container
-       _, err = hstExec(action, dockerInstance)
-       if err != nil {
-               return fmt.Errorf("error starting vpp in container: %v", err)
-       }
+       _, err := hstExec(action, dockerInstance)
+       s.assertNil(err)
 
        fmt.Println("VPP running and configured...")
 
-       if err := proxySetup(); err != nil {
-               return fmt.Errorf("failed to setup proxy: %v", err)
-       }
+       s.assertNil(proxySetup(), "failed to setup proxy")
        fmt.Println("Proxy configured...")
 
        // create test file
        err = exechelper.Run(fmt.Sprintf("ip netns exec server truncate -s %s %s", srcFile, srcFile))
-       if err != nil {
-               return fmt.Errorf("failed to run truncate command")
-       }
+       s.assertNil(err, "failed to run truncate command")
        defer func() { os.Remove(srcFile) }()
 
        fmt.Println("Test file created...")
@@ -56,30 +46,48 @@ func testProxyHttpTcp(t *testing.T, dockerInstance, action string, proxySetup fu
 
        c := fmt.Sprintf("ip netns exec client wget --retry-connrefused --retry-on-http-error=503 --tries=10 -O %s 10.0.0.2:555/%s", outputFile, srcFile)
        _, err = exechelper.CombinedOutput(c)
-       if err != nil {
-               return fmt.Errorf("failed to run wget: %v", err)
-       }
+       s.assertNil(err, "failed to run wget")
        stopServer <- struct{}{}
 
        defer func() { os.Remove(outputFile) }()
 
-       if err = assertFileSize(outputFile, srcFile); err != nil {
+       s.assertNil(assertFileSize(outputFile, srcFile))
+       return nil
+}
+
+func setupEnvoy(ctx context.Context, dockerInstance string) error {
+       errCh := startEnvoy(ctx, dockerInstance)
+       select {
+       case err := <-errCh:
                return err
+       default:
        }
+
+       go func(ctx context.Context, errCh <-chan error) {
+               for {
+                       select {
+                       // handle cancel() call from outside to gracefully stop the routine
+                       case <-ctx.Done():
+                               return
+                       default:
+                               select {
+                               case err := <-errCh:
+                                       fmt.Printf("error while running envoy: %v", err)
+                               default:
+                               }
+                       }
+               }
+       }(ctx, errCh)
        return nil
 }
 
 func (s *NsSuite) TestVppProxyHttpTcp() {
-       t := s.T()
        dockerInstance := "vpp-proxy"
-       err := testProxyHttpTcp(t, dockerInstance, "ConfigureVppProxy", configureVppProxy)
-       if err != nil {
-               t.Errorf("%v", err)
-       }
+       err := testProxyHttpTcp(s, dockerInstance, "ConfigureVppProxy", configureVppProxy)
+       s.assertNil(err)
 }
 
 func (s *NsSuite) TestEnvoyProxyHttpTcp() {
-       t := s.T()
        exechelper.Run("docker volume create --name=shared-vol")
        defer func() {
                exechelper.Run("docker stop envoy")
@@ -88,11 +96,9 @@ func (s *NsSuite) TestEnvoyProxyHttpTcp() {
        ctx, cancel := context.WithCancel(context.Background())
 
        dockerInstance := "vpp-envoy"
-       err := testProxyHttpTcp(t, dockerInstance, "ConfigureEnvoyProxy", func() error {
-               return setupEnvoy(t, ctx, dockerInstance)
+       err := testProxyHttpTcp(s, dockerInstance, "ConfigureEnvoyProxy", func() error {
+               return setupEnvoy(ctx, dockerInstance)
        })
-       if err != nil {
-               t.Errorf("%v", err)
-       }
+       s.assertNil(err)
        cancel()
 }
index 4dda4e4..581b846 100755 (executable)
@@ -10,7 +10,6 @@ import (
        "os"
        "os/exec"
        "strings"
-       "testing"
        "time"
 
        "github.com/edwarnicke/exechelper"
@@ -249,32 +248,6 @@ func startEnvoy(ctx context.Context, dockerInstance string) <-chan error {
        return errCh
 }
 
-func setupEnvoy(t *testing.T, ctx context.Context, dockerInstance string) error {
-       errCh := startEnvoy(ctx, dockerInstance)
-       select {
-       case err := <-errCh:
-               return err
-       default:
-       }
-
-       go func(ctx context.Context, errCh <-chan error) {
-               for {
-                       select {
-                       // handle cancel() call from outside to gracefully stop the routine
-                       case <-ctx.Done():
-                               return
-                       default:
-                               select {
-                               case err := <-errCh:
-                                       fmt.Printf("error while running envoy: %v", err)
-                               default:
-                               }
-                       }
-               }
-       }(ctx, errCh)
-       return nil
-}
-
 func configureVppProxy() error {
        _, err := dockerExec("vppctl test proxy server server-uri tcp://10.0.0.2/555 client-uri tcp://10.0.1.1/666",
                "vpp-proxy")
index 9febe8b..8fd64f1 100755 (executable)
@@ -31,25 +31,25 @@ func (s *VethsSuite) testVclEcho(proto string) {
        serverVppContainer, err := s.NewContainer(srvInstance)
        s.assertNil(err)
        serverVppContainer.addVolume(serverVolume, "/tmp/Configure2Veths")
-       serverVppContainer.run()
+       s.assertNil(serverVppContainer.run())
 
        clnInstance := "vpp-vcl-test-cln"
        clientVppContainer, err := s.NewContainer(clnInstance)
        s.assertNil(err)
        clientVppContainer.addVolume(clientVolume, "/tmp/Configure2Veths")
-       clientVppContainer.run();
+       s.assertNil(clientVppContainer.run())
 
        echoSrv := "echo-srv"
        serverEchoContainer, err := s.NewContainer(echoSrv)
        s.assertNil(err)
        serverEchoContainer.addVolume(serverVolume, "/tmp/" + echoSrv)
-       serverEchoContainer.run()
+       s.assertNil(serverEchoContainer.run())
 
        echoCln := "echo-cln"
        clientEchoContainer, err := s.NewContainer(echoCln)
        s.assertNil(err)
        clientEchoContainer.addVolume(clientVolume, "/tmp/" + echoCln)
-       clientEchoContainer.run()
+       s.assertNil(clientEchoContainer.run())
 
        _, err = hstExec("Configure2Veths srv", srvInstance)
        s.assertNil(err)
@@ -63,6 +63,7 @@ func (s *VethsSuite) testVclEcho(proto string) {
 
        o, err := hstExec("RunEchoClient "+proto, echoCln)
        s.assertNil(err)
+
        fmt.Println(o)
 }
 
@@ -82,25 +83,25 @@ func (s *VethsSuite) testRetryAttach(proto string) {
        serverVppContainer, err := s.NewContainer(srvInstance)
        s.assertNil(err)
        serverVppContainer.addVolume(serverVolume, "/tmp/Configure2Veths")
-       serverVppContainer.run()
+       s.assertNil(serverVppContainer.run())
 
        clnInstance := "vpp-vcl-test-cln"
        clientVppContainer, err := s.NewContainer(clnInstance)
        s.assertNil(err)
        clientVppContainer.addVolume(clientVolume, "/tmp/Configure2Veths")
-       clientVppContainer.run();
+       s.assertNil(clientVppContainer.run())
 
        echoSrv := "echo-srv"
        serverEchoContainer, err := s.NewContainer(echoSrv)
        s.assertNil(err)
        serverEchoContainer.addVolume(serverVolume, "/tmp/" + echoSrv)
-       serverEchoContainer.run()
+       s.assertNil(serverEchoContainer.run())
 
        echoCln := "echo-cln"
        clientEchoContainer, err := s.NewContainer(echoCln)
        s.assertNil(err)
        clientEchoContainer.addVolume(clientVolume, "/tmp/" + echoCln)
-       clientEchoContainer.run()
+       s.assertNil(clientEchoContainer.run())
 
        _, err = hstExec("Configure2Veths srv-with-preset-hw-addr", srvInstance)
        s.assertNil(err)