16 "github.com/edwarnicke/exechelper"
19 // TODO remove `configTemplate` once its usage has been replaced everywhere with VppConfig
20 const configTemplate = `unix {
22 log %[1]s/var/log/vpp/vpp.log
24 cli-listen %[1]s/var/run/vpp/cli.sock
25 runtime-dir %[1]s/var/run
38 socket-name %[1]s/var/run/vpp/api.sock
42 socket-name %[1]s/var/run/vpp/stats.sock
46 plugin unittest_plugin.so { enable }
47 plugin dpdk_plugin.so { disable }
48 plugin crypto_aesni_plugin.so { enable }
49 plugin quic_plugin.so { enable }
50 plugin crypto_ipsecmb_plugin.so { disable }
55 const vclTemplate = `vcl {
60 namespace-secret %[2]s
65 const TopologyDir string = "topo/"
72 type ActionResult struct {
79 type JsonResult struct {
86 func StartServerApp(running chan error, done chan struct{}, env []string) {
87 cmd := exec.Command("iperf3", "-4", "-s")
93 msg := fmt.Errorf("failed to start iperf server: %v", err)
102 func StartClientApp(env []string, clnCh chan error) {
110 cmd := exec.Command("iperf3", "-c", "10.10.10.1", "-u", "-l", "1460", "-b", "10g")
114 o, err := cmd.CombinedOutput()
117 clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
120 time.Sleep(1 * time.Second)
124 fmt.Printf("Client output: %s", o)
130 // run vpphelper in docker
131 func hstExec(args string, instance string) (string, error) {
132 syncFile := fmt.Sprintf("/tmp/%s/sync/rc", instance)
135 c := "docker exec -d " + instance + " /hs-test " + args
136 err := exechelper.Run(c)
141 res, err := waitForSyncFile(syncFile)
144 return "", fmt.Errorf("failed to read sync file while executing './hs-test %s': %v", args, err)
147 o := res.StdOutput + res.ErrOutput
149 return o, fmt.Errorf("cmd resulted in non-zero value %d: %s", res.Code, res.Desc)
154 func waitForSyncFile(fname string) (*JsonResult, error) {
157 for i := 0; i < 360; i++ {
158 f, err := os.Open(fname)
162 data, err := ioutil.ReadFile(fname)
164 return nil, fmt.Errorf("read error: %v", err)
166 err = json.Unmarshal(data, &res)
168 return nil, fmt.Errorf("json unmarshal error: %v", err)
172 time.Sleep(1 * time.Second)
174 return nil, fmt.Errorf("no sync file found")
177 func dockerRun(instance, args string) error {
178 exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", instance))
179 syncPath := fmt.Sprintf("-v /tmp/%s/sync:/tmp/sync", instance)
180 cmd := "docker run --cap-add=all -d --privileged --network host --rm "
183 cmd += " --name " + instance + " hs-test/vpp"
185 return exechelper.Run(cmd)
188 func assertFileSize(f1, f2 string) error {
189 fi1, err := os.Stat(f1)
194 fi2, err1 := os.Stat(f2)
199 if fi1.Size() != fi2.Size() {
200 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
205 func dockerExec(cmd string, instance string) ([]byte, error) {
206 c := "docker exec -d " + instance + " " + cmd
207 return exechelper.CombinedOutput(c)
210 func startEnvoy(ctx context.Context, dockerInstance string) <-chan error {
211 errCh := make(chan error)
212 wd, err := os.Getwd()
218 c := []string{"docker", "run", "--rm", "--name", "envoy",
219 "-v", fmt.Sprintf("%s/envoy/proxy.yaml:/etc/envoy/envoy.yaml", wd),
220 "-v", fmt.Sprintf("shared-vol:/tmp/%s", dockerInstance),
221 "-v", fmt.Sprintf("%s/envoy:/tmp", wd),
223 "-e", "VCL_CONFIG=/tmp/vcl.conf",
224 "envoyproxy/envoy-contrib:v1.21-latest"}
227 go func(errCh chan error) {
231 cmd = NewCommand(c, "")
237 errCh <- fmt.Errorf("failed to start envoy docker after %d attempts", count)
244 errCh <- fmt.Errorf("failed to start docker: %v", err)
252 func setupEnvoy(t *testing.T, ctx context.Context, dockerInstance string) error {
253 errCh := startEnvoy(ctx, dockerInstance)
260 go func(ctx context.Context, errCh <-chan error) {
263 // handle cancel() call from outside to gracefully stop the routine
269 fmt.Printf("error while running envoy: %v", err)
278 func configureVppProxy() error {
279 _, err := dockerExec("vppctl test proxy server server-uri tcp://10.0.0.2/555 client-uri tcp://10.0.1.1/666",
282 return fmt.Errorf("error while configuring vpp proxy test: %v", err)
287 func startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
288 cmd := NewCommand([]string{"./http_server", addressPort}, netNs)
291 fmt.Println("Failed to start http server")
294 running <- struct{}{}
299 func startWget(finished chan error, server_ip, port string, netNs string) {
300 fname := "test_file_10M"
302 finished <- errors.New("wget error")
305 cmd := NewCommand([]string{"wget", "--tries=5", "-q", "-O", "/dev/null", server_ip + ":" + port + "/" + fname},
307 o, err := cmd.CombinedOutput()
309 fmt.Printf("wget error: '%s'.\n%s", err, o)
312 fmt.Printf("Client output: %s", o)
316 func (c *Stanza) NewStanza(name string) *Stanza {
317 c.Append("\n" + name + " {")
322 func (c *Stanza) Append(name string) *Stanza {
323 c.content += strings.Repeat(" ", c.pad)
324 c.content += name + "\n"
328 func (c *Stanza) Close() *Stanza {
334 func (s *Stanza) ToString() string {
338 func (s *Stanza) SaveToFile(fileName string) error {
339 fo, err := os.Create(fileName)
345 _, err = io.Copy(fo, strings.NewReader(s.content))