hs-test: log external apps

Type: test

Change-Id: Id2b0d408bd46f20b81422506f9db4eb655feddac
Signed-off-by: Filip Tehlar <ftehlar@cisco.com>
diff --git a/extras/hs-test/hst_suite.go b/extras/hs-test/hst_suite.go
index 093bca0..d74934a 100644
--- a/extras/hs-test/hst_suite.go
+++ b/extras/hs-test/hst_suite.go
@@ -1,7 +1,9 @@
 package main
 
 import (
+	"errors"
 	"flag"
+	"fmt"
 	"io/ioutil"
 	"os"
 	"os/exec"
@@ -313,3 +315,81 @@
 
 	return s.testIds[testName]
 }
+
+func (s *HstSuite) startServerApp(running chan error, done chan struct{}, env []string) {
+	cmd := exec.Command("iperf3", "-4", "-s")
+	if env != nil {
+		cmd.Env = env
+	}
+	s.log(cmd)
+	err := cmd.Start()
+	if err != nil {
+		msg := fmt.Errorf("failed to start iperf server: %v", err)
+		running <- msg
+		return
+	}
+	running <- nil
+	<-done
+	cmd.Process.Kill()
+}
+
+func (s *HstSuite) startClientApp(ipAddress string, env []string, clnCh chan error, clnRes chan string) {
+	defer func() {
+		clnCh <- nil
+	}()
+
+	nTries := 0
+
+	for {
+		cmd := exec.Command("iperf3", "-c", ipAddress, "-u", "-l", "1460", "-b", "10g")
+		if env != nil {
+			cmd.Env = env
+		}
+		s.log(cmd)
+		o, err := cmd.CombinedOutput()
+		if err != nil {
+			if nTries > 5 {
+				clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
+				return
+			}
+			time.Sleep(1 * time.Second)
+			nTries++
+			continue
+		} else {
+			clnRes <- fmt.Sprintf("Client output: %s", o)
+		}
+		break
+	}
+}
+
+func (s *HstSuite) startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
+	cmd := newCommand([]string{"./http_server", addressPort}, netNs)
+	err := cmd.Start()
+	s.log(cmd)
+	if err != nil {
+		fmt.Println("Failed to start http server")
+		return
+	}
+	running <- struct{}{}
+	<-done
+	cmd.Process.Kill()
+}
+
+func (s *HstSuite) startWget(finished chan error, server_ip, port, query, netNs string) {
+	defer func() {
+		finished <- errors.New("wget error")
+	}()
+
+	cmd := newCommand([]string{"wget", "--timeout=10", "--no-proxy", "--tries=5", "-O", "/dev/null", server_ip + ":" + port + "/" + query},
+		netNs)
+	s.log(cmd)
+	o, err := cmd.CombinedOutput()
+	if err != nil {
+		finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o)
+		return
+	} else if !strings.Contains(string(o), "200 OK") {
+		finished <- fmt.Errorf("wget error: response not 200 OK")
+		return
+	}
+	finished <- nil
+}
diff --git a/extras/hs-test/http_test.go b/extras/hs-test/http_test.go
index 503f42a..4aab553 100644
--- a/extras/hs-test/http_test.go
+++ b/extras/hs-test/http_test.go
@@ -17,7 +17,7 @@
 	// configure vpp in the container
 	container.vppInstance.vppctl("http tps uri tcp://0.0.0.0/8080")
 
-	go startWget(finished, client_ip, port, "test_file_10M", "client")
+	go s.startWget(finished, client_ip, port, "test_file_10M", "client")
 	// wait for client
 	err := <-finished
 	s.assertNil(err)
@@ -73,7 +73,7 @@
 	serverAddress := s.netInterfaces[tapInterfaceName].peer.ip4AddressString()
 
 	defer func() { os.Remove(query) }()
-	go startWget(finished, serverAddress, "80", query, "")
+	go s.startWget(finished, serverAddress, "80", query, "")
 	s.assertNil(<-finished)
 }
 
diff --git a/extras/hs-test/ldp_test.go b/extras/hs-test/ldp_test.go
index c2d6f33..1906d6b 100644
--- a/extras/hs-test/ldp_test.go
+++ b/extras/hs-test/ldp_test.go
@@ -54,7 +54,7 @@
 	s.log("attaching server to vpp")
 
 	srvEnv := append(os.Environ(), ldpreload, "VCL_CONFIG="+serverVclFileName)
-	go startServerApp(srvCh, stopServerCh, srvEnv)
+	go s.startServerApp(srvCh, stopServerCh, srvEnv)
 
 	err = <-srvCh
 	s.assertNil(err)
@@ -63,7 +63,7 @@
 	var clnRes = make(chan string, 1)
 	clnEnv := append(os.Environ(), ldpreload, "VCL_CONFIG="+clientVclFileName)
 	serverVethAddress := s.netInterfaces[serverInterfaceName].ip4AddressString()
-	go startClientApp(serverVethAddress, clnEnv, clnCh, clnRes)
+	go s.startClientApp(serverVethAddress, clnEnv, clnCh, clnRes)
 	s.log(<-clnRes)
 
 	// wait for client's result
diff --git a/extras/hs-test/linux_iperf_test.go b/extras/hs-test/linux_iperf_test.go
index 04556a8..c03403c 100644
--- a/extras/hs-test/linux_iperf_test.go
+++ b/extras/hs-test/linux_iperf_test.go
@@ -9,13 +9,13 @@
 		stopServerCh <- struct{}{}
 	}()
 
-	go startServerApp(srvCh, stopServerCh, nil)
+	go s.startServerApp(srvCh, stopServerCh, nil)
 	err := <-srvCh
 	s.assertNil(err)
 	s.log("server running")
 
 	ipAddress := s.netInterfaces[tapInterfaceName].ip4AddressString()
-	go startClientApp(ipAddress, nil, clnCh, clnRes)
+	go s.startClientApp(ipAddress, nil, clnCh, clnRes)
 	s.log("client running")
 	s.log(<-clnRes)
 	err = <-clnCh
diff --git a/extras/hs-test/proxy_test.go b/extras/hs-test/proxy_test.go
index a7a6509..9e72303 100644
--- a/extras/hs-test/proxy_test.go
+++ b/extras/hs-test/proxy_test.go
@@ -20,7 +20,7 @@
 
 	s.log("test file created...")
 
-	go startHttpServer(serverRunning, stopServer, ":666", "server")
+	go s.startHttpServer(serverRunning, stopServer, ":666", "server")
 	// TODO better error handling and recovery
 	<-serverRunning
 
diff --git a/extras/hs-test/utils.go b/extras/hs-test/utils.go
index 4261f4d..304dd4c 100644
--- a/extras/hs-test/utils.go
+++ b/extras/hs-test/utils.go
@@ -1,25 +1,12 @@
 package main
 
 import (
-	"errors"
 	"fmt"
 	"io"
 	"os"
-	"os/exec"
 	"strings"
-	"time"
 )
 
-const vclTemplate = `vcl {
-  app-socket-api %[1]s/var/run/app_ns_sockets/%[2]s
-  app-scope-global
-  app-scope-local
-  namespace-id %[2]s
-  namespace-secret %[2]s
-  use-mq-eventfd
-}
-`
-
 const networkTopologyDir string = "topo-network/"
 const containerTopologyDir string = "topo-containers/"
 
@@ -42,50 +29,6 @@
 	StdOutput string
 }
 
-func startServerApp(running chan error, done chan struct{}, env []string) {
-	cmd := exec.Command("iperf3", "-4", "-s")
-	if env != nil {
-		cmd.Env = env
-	}
-	err := cmd.Start()
-	if err != nil {
-		msg := fmt.Errorf("failed to start iperf server: %v", err)
-		running <- msg
-		return
-	}
-	running <- nil
-	<-done
-	cmd.Process.Kill()
-}
-
-func startClientApp(ipAddress string, env []string, clnCh chan error, clnRes chan string) {
-	defer func() {
-		clnCh <- nil
-	}()
-
-	nTries := 0
-
-	for {
-		cmd := exec.Command("iperf3", "-c", ipAddress, "-u", "-l", "1460", "-b", "10g")
-		if env != nil {
-			cmd.Env = env
-		}
-		o, err := cmd.CombinedOutput()
-		if err != nil {
-			if nTries > 5 {
-				clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
-				return
-			}
-			time.Sleep(1 * time.Second)
-			nTries++
-			continue
-		} else {
-			clnRes <- fmt.Sprintf("Client output: %s", o)
-		}
-		break
-	}
-}
-
 func assertFileSize(f1, f2 string) error {
 	fi1, err := os.Stat(f1)
 	if err != nil {
@@ -103,36 +46,6 @@
 	return nil
 }
 
-func startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
-	cmd := newCommand([]string{"./http_server", addressPort}, netNs)
-	err := cmd.Start()
-	if err != nil {
-		fmt.Println("Failed to start http server")
-		return
-	}
-	running <- struct{}{}
-	<-done
-	cmd.Process.Kill()
-}
-
-func startWget(finished chan error, server_ip, port, query, netNs string) {
-	defer func() {
-		finished <- errors.New("wget error")
-	}()
-
-	cmd := newCommand([]string{"wget", "--timeout=10", "--no-proxy", "--tries=5", "-O", "/dev/null", server_ip + ":" + port + "/" + query},
-		netNs)
-	o, err := cmd.CombinedOutput()
-	if err != nil {
-		finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o)
-		return
-	} else if !strings.Contains(string(o), "200 OK") {
-		finished <- fmt.Errorf("wget error: response not 200 OK")
-		return
-	}
-	finished <- nil
-}
-
 func (c *Stanza) newStanza(name string) *Stanza {
 	c.append("\n" + name + " {")
 	c.pad += 2
diff --git a/extras/hs-test/vcl_test.go b/extras/hs-test/vcl_test.go
index 109b242..cf4c32e 100644
--- a/extras/hs-test/vcl_test.go
+++ b/extras/hs-test/vcl_test.go
@@ -5,6 +5,16 @@
 	"time"
 )
 
+const vclTemplate = `vcl {
+  app-socket-api %[1]s/var/run/app_ns_sockets/%[2]s
+  app-scope-global
+  app-scope-local
+  namespace-id %[2]s
+  namespace-secret %[2]s
+  use-mq-eventfd
+}
+`
+
 func (s *VethsSuite) testVclEcho(proto string) {
 	port := "12345"
 	srvVppCont := s.getContainerByName("server-vpp")