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 vclTemplate = `vcl {
58 namespace-secret %[2]s
63 const TopologyDir string = "topo/"
70 type ActionResult struct {
77 type JsonResult struct {
84 func StartServerApp(running chan error, done chan struct{}, env []string) {
85 cmd := exec.Command("iperf3", "-4", "-s")
91 msg := fmt.Errorf("failed to start iperf server: %v", err)
100 func StartClientApp(env []string, clnCh chan error) {
108 cmd := exec.Command("iperf3", "-c", "10.10.10.1", "-u", "-l", "1460", "-b", "10g")
112 o, err := cmd.CombinedOutput()
115 clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
118 time.Sleep(1 * time.Second)
122 fmt.Printf("Client output: %s", o)
128 // run vpphelper in docker
129 func hstExec(args string, instance string) (string, error) {
130 syncFile := fmt.Sprintf("/tmp/%s/sync/rc", instance)
133 c := "docker exec -d " + instance + " /hs-test " + args
134 err := exechelper.Run(c)
139 res, err := waitForSyncFile(syncFile)
142 return "", fmt.Errorf("failed to read sync file while executing './hs-test %s': %v", args, err)
145 o := res.StdOutput + res.ErrOutput
147 return o, fmt.Errorf("cmd resulted in non-zero value %d: %s", res.Code, res.Desc)
152 func waitForSyncFile(fname string) (*JsonResult, error) {
155 for i := 0; i < 360; i++ {
156 f, err := os.Open(fname)
160 data, err := ioutil.ReadFile(fname)
162 return nil, fmt.Errorf("read error: %v", err)
164 err = json.Unmarshal(data, &res)
166 return nil, fmt.Errorf("json unmarshal error: %v", err)
170 time.Sleep(1 * time.Second)
172 return nil, fmt.Errorf("no sync file found")
175 func dockerRun(instance, args string) error {
176 exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", instance))
177 syncPath := fmt.Sprintf("-v /tmp/%s/sync:/tmp/sync", instance)
178 cmd := "docker run --cap-add=all -d --privileged --network host --rm "
181 cmd += " --name " + instance + " hs-test/vpp"
183 return exechelper.Run(cmd)
186 func assertFileSize(f1, f2 string) error {
187 fi1, err := os.Stat(f1)
192 fi2, err1 := os.Stat(f2)
197 if fi1.Size() != fi2.Size() {
198 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
203 func dockerExec(cmd string, instance string) ([]byte, error) {
204 c := "docker exec -d " + instance + " " + cmd
205 return exechelper.CombinedOutput(c)
208 func startEnvoy(ctx context.Context, dockerInstance string) <-chan error {
209 errCh := make(chan error)
210 wd, err := os.Getwd()
216 c := []string{"docker", "run", "--rm", "--name", "envoy",
217 "-v", fmt.Sprintf("%s/envoy/proxy.yaml:/etc/envoy/envoy.yaml", wd),
218 "-v", fmt.Sprintf("shared-vol:/tmp/%s", dockerInstance),
219 "-v", fmt.Sprintf("%s/envoy:/tmp", wd),
221 "-e", "VCL_CONFIG=/tmp/vcl.conf",
222 "envoyproxy/envoy-contrib:v1.21-latest"}
225 go func(errCh chan error) {
229 cmd = NewCommand(c, "")
235 errCh <- fmt.Errorf("failed to start envoy docker after %d attempts", count)
242 errCh <- fmt.Errorf("failed to start docker: %v", err)
250 func setupEnvoy(t *testing.T, ctx context.Context, dockerInstance string) error {
251 errCh := startEnvoy(ctx, dockerInstance)
258 go func(ctx context.Context, errCh <-chan error) {
261 // handle cancel() call from outside to gracefully stop the routine
267 fmt.Printf("error while running envoy: %v", err)
276 func configureVppProxy() error {
277 _, err := dockerExec("vppctl test proxy server server-uri tcp://10.0.0.2/555 client-uri tcp://10.0.1.1/666",
280 return fmt.Errorf("error while configuring vpp proxy test: %v", err)
285 func startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
286 cmd := NewCommand([]string{"./http_server", addressPort}, netNs)
289 fmt.Println("Failed to start http server")
292 running <- struct{}{}
297 func startWget(finished chan error, server_ip, port string, netNs string) {
298 fname := "test_file_10M"
300 finished <- errors.New("wget error")
303 cmd := NewCommand([]string{"wget", "--tries=5", "-q", "-O", "/dev/null", server_ip + ":" + port + "/" + fname},
305 o, err := cmd.CombinedOutput()
307 fmt.Printf("wget error: '%s'.\n%s", err, o)
310 fmt.Printf("Client output: %s", o)
314 func (c *Stanza) NewStanza(name string) *Stanza {
315 c.Append("\n" + name + " {")
320 func (c *Stanza) Append(name string) *Stanza {
321 c.content += strings.Repeat(" ", c.pad)
322 c.content += name + "\n"
326 func (c *Stanza) Close() *Stanza {
332 func (s *Stanza) ToString() string {
336 func (s *Stanza) SaveToFile(fileName string) error {
337 fo, err := os.Create(fileName)
343 _, err = io.Copy(fo, strings.NewReader(s.content))