16 "github.com/edwarnicke/exechelper"
19 const configTemplate = `unix {
21 log %[1]s/var/log/vpp/vpp.log
23 cli-listen %[1]s/var/run/vpp/cli.sock
24 runtime-dir %[1]s/var/run
37 socket-name %[1]s/var/run/vpp/api.sock
41 socket-name %[1]s/var/run/vpp/stats.sock
45 plugin unittest_plugin.so { enable }
46 plugin dpdk_plugin.so { disable }
47 plugin crypto_aesni_plugin.so { enable }
48 plugin quic_plugin.so { enable }
53 const TopologyDir string = "topo/"
60 type ActionResult struct {
67 type JsonResult struct {
74 func StartServerApp(running chan error, done chan struct{}, env []string) {
75 cmd := exec.Command("iperf3", "-4", "-s")
81 msg := fmt.Errorf("failed to start iperf server: %v", err)
90 func StartClientApp(env []string, clnCh chan error) {
98 cmd := exec.Command("iperf3", "-c", "10.10.10.1", "-u", "-l", "1460", "-b", "10g")
102 o, err := cmd.CombinedOutput()
105 clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
108 time.Sleep(1 * time.Second)
112 fmt.Printf("Client output: %s", o)
118 // run vpphelper in docker
119 func hstExec(args string, instance string) (string, error) {
120 syncFile := fmt.Sprintf("/tmp/%s/sync/rc", instance)
123 c := "docker exec -d " + instance + " /hs-test " + args
124 err := exechelper.Run(c)
129 res, err := waitForSyncFile(syncFile)
132 return "", fmt.Errorf("failed to read sync file while executing './hs-test %s': %v", args, err)
135 o := res.StdOutput + res.ErrOutput
137 return o, fmt.Errorf("cmd resulted in non-zero value %d: %s", res.Code, res.Desc)
142 func waitForSyncFile(fname string) (*JsonResult, error) {
145 for i := 0; i < 60; i++ {
146 f, err := os.Open(fname)
150 data, err := ioutil.ReadFile(fname)
152 return nil, fmt.Errorf("read error: %v", err)
154 err = json.Unmarshal(data, &res)
156 return nil, fmt.Errorf("json unmarshal error: %v", err)
160 time.Sleep(1 * time.Second)
162 return nil, fmt.Errorf("no sync file found")
165 func dockerRun(instance, args string) error {
166 exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", instance))
167 syncPath := fmt.Sprintf("-v /tmp/%s/sync:/tmp/sync", instance)
168 cmd := "docker run --cap-add=all -d --privileged --network host --rm "
171 cmd += " --name " + instance + " hs-test/vpp"
173 return exechelper.Run(cmd)
176 func assertFileSize(f1, f2 string) error {
177 fi1, err := os.Stat(f1)
182 fi2, err1 := os.Stat(f2)
187 if fi1.Size() != fi2.Size() {
188 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
193 func dockerExec(cmd string, instance string) ([]byte, error) {
194 c := "docker exec -d " + instance + " " + cmd
195 return exechelper.CombinedOutput(c)
198 func startEnvoy(ctx context.Context, dockerInstance string) <-chan error {
199 errCh := make(chan error)
200 wd, err := os.Getwd()
206 c := []string{"docker", "run", "--rm", "--name", "envoy",
207 "-v", fmt.Sprintf("%s/envoy/proxy.yaml:/etc/envoy/envoy.yaml", wd),
208 "-v", fmt.Sprintf("shared-vol:/tmp/%s", dockerInstance),
209 "-v", fmt.Sprintf("%s/envoy:/tmp", wd),
211 "-e", "VCL_CONFIG=/tmp/vcl.conf",
212 "envoyproxy/envoy-contrib:v1.21-latest"}
215 go func(errCh chan error) {
219 cmd = NewCommand(c, "")
225 errCh <- fmt.Errorf("failed to start envoy docker after %d attempts", count)
232 errCh <- fmt.Errorf("failed to start docker: %v", err)
240 func setupEnvoy(t *testing.T, ctx context.Context, dockerInstance string) error {
241 errCh := startEnvoy(ctx, dockerInstance)
248 go func(ctx context.Context, errCh <-chan error) {
251 // handle cancel() call from outside to gracefully stop the routine
257 fmt.Printf("error while running envoy: %v", err)
266 func configureVppProxy() error {
267 _, err := dockerExec("vppctl test proxy server server-uri tcp://10.0.0.2/555 client-uri tcp://10.0.1.1/666",
270 return fmt.Errorf("error while configuring vpp proxy test: %v", err)
275 func startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
276 cmd := NewCommand([]string{"./http_server", addressPort}, netNs)
279 fmt.Println("Failed to start http server")
282 running <- struct{}{}
287 func startWget(finished chan error, server_ip, port string, netNs string) {
288 fname := "test_file_10M"
290 finished <- errors.New("wget error")
293 cmd := NewCommand([]string{"wget", "--tries=5", "-q", "-O", "/dev/null", server_ip + ":" + port + "/" + fname},
295 o, err := cmd.CombinedOutput()
297 fmt.Printf("wget error: '%s'.\n%s", err, o)
300 fmt.Printf("Client output: %s", o)
304 func (c *Stanza) NewStanza(name string) *Stanza {
305 c.Append("\n" + name + " {")
310 func (c *Stanza) Append(name string) *Stanza {
311 c.content += strings.Repeat(" ", c.pad)
312 c.content += name + "\n"
316 func (c *Stanza) Close() *Stanza {
322 func (s *Stanza) ToString() string {
326 func (s *Stanza) SaveToFile(fileName string) error {
327 fo, err := os.Create(fileName)
333 _, err = io.Copy(fo, strings.NewReader(s.content))