blob: 583dc4aff543c0986bc79e54e2131b0e5f3a7967 [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Filip Tehlar229f5fc2022-08-09 14:44:47 +00004 "encoding/json"
5 "errors"
6 "fmt"
7 "io"
8 "io/ioutil"
9 "os"
10 "os/exec"
11 "strings"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000012 "time"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000013)
14
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010015// TODO remove `configTemplate` once its usage has been replaced everywhere with VppConfig
Filip Tehlar229f5fc2022-08-09 14:44:47 +000016const configTemplate = `unix {
17 nodaemon
18 log %[1]s/var/log/vpp/vpp.log
19 full-coredump
20 cli-listen %[1]s/var/run/vpp/cli.sock
21 runtime-dir %[1]s/var/run
22 gid vpp
23}
24
25api-trace {
26 on
27}
28
29api-segment {
30 gid vpp
31}
32
33socksvr {
34 socket-name %[1]s/var/run/vpp/api.sock
35}
36
37statseg {
38 socket-name %[1]s/var/run/vpp/stats.sock
39}
40
41plugins {
42 plugin unittest_plugin.so { enable }
43 plugin dpdk_plugin.so { disable }
44 plugin crypto_aesni_plugin.so { enable }
45 plugin quic_plugin.so { enable }
Filip Tehlarb15a0002022-11-10 12:34:17 +010046 plugin crypto_ipsecmb_plugin.so { disable }
Filip Tehlar229f5fc2022-08-09 14:44:47 +000047}
48
49`
50
Maros Ondrejicka0db15752022-10-12 22:58:01 +020051const vclTemplate = `vcl {
52 app-socket-api %[1]s
53 app-scope-global
54 app-scope-local
55 namespace-id %[2]s
56 namespace-secret %[2]s
57 use-mq-eventfd
58}
59`
60
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010061const NetworkTopologyDir string = "topo-network/"
62const ContainerTopologyDir string = "topo-containers/"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000063
64type Stanza struct {
65 content string
66 pad int
67}
68
69type ActionResult struct {
70 Err error
71 Desc string
72 ErrOutput string
73 StdOutput string
74}
75
76type JsonResult struct {
77 Code int
78 Desc string
79 ErrOutput string
80 StdOutput string
81}
82
83func StartServerApp(running chan error, done chan struct{}, env []string) {
84 cmd := exec.Command("iperf3", "-4", "-s")
85 if env != nil {
86 cmd.Env = env
87 }
88 err := cmd.Start()
89 if err != nil {
90 msg := fmt.Errorf("failed to start iperf server: %v", err)
91 running <- msg
92 return
93 }
94 running <- nil
95 <-done
96 cmd.Process.Kill()
97}
98
Maros Ondrejicka87531802022-12-19 20:35:27 +010099func StartClientApp(env []string, clnCh chan error, clnRes chan string) {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000100 defer func() {
101 clnCh <- nil
102 }()
103
104 nTries := 0
105
106 for {
107 cmd := exec.Command("iperf3", "-c", "10.10.10.1", "-u", "-l", "1460", "-b", "10g")
108 if env != nil {
109 cmd.Env = env
110 }
111 o, err := cmd.CombinedOutput()
112 if err != nil {
113 if nTries > 5 {
114 clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
115 return
116 }
117 time.Sleep(1 * time.Second)
118 nTries++
119 continue
120 } else {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100121 clnRes <- fmt.Sprintf("Client output: %s", o)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000122 }
123 break
124 }
125}
126
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000127func waitForSyncFile(fname string) (*JsonResult, error) {
128 var res JsonResult
129
Maros Ondrejicka0db15752022-10-12 22:58:01 +0200130 for i := 0; i < 360; i++ {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000131 f, err := os.Open(fname)
132 if err == nil {
133 defer f.Close()
134
135 data, err := ioutil.ReadFile(fname)
136 if err != nil {
137 return nil, fmt.Errorf("read error: %v", err)
138 }
139 err = json.Unmarshal(data, &res)
140 if err != nil {
141 return nil, fmt.Errorf("json unmarshal error: %v", err)
142 }
143 return &res, nil
144 }
145 time.Sleep(1 * time.Second)
146 }
147 return nil, fmt.Errorf("no sync file found")
148}
149
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000150func assertFileSize(f1, f2 string) error {
151 fi1, err := os.Stat(f1)
152 if err != nil {
153 return err
154 }
155
156 fi2, err1 := os.Stat(f2)
157 if err1 != nil {
158 return err1
159 }
160
161 if fi1.Size() != fi2.Size() {
162 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
163 }
164 return nil
165}
166
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000167func startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
168 cmd := NewCommand([]string{"./http_server", addressPort}, netNs)
169 err := cmd.Start()
170 if err != nil {
171 fmt.Println("Failed to start http server")
172 return
173 }
174 running <- struct{}{}
175 <-done
176 cmd.Process.Kill()
177}
178
179func startWget(finished chan error, server_ip, port string, netNs string) {
180 fname := "test_file_10M"
181 defer func() {
182 finished <- errors.New("wget error")
183 }()
184
185 cmd := NewCommand([]string{"wget", "--tries=5", "-q", "-O", "/dev/null", server_ip + ":" + port + "/" + fname},
186 netNs)
187 o, err := cmd.CombinedOutput()
188 if err != nil {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100189 finished <- errors.New(fmt.Sprintf("wget error: '%s'.\n%s", err, o))
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000190 return
191 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000192 finished <- nil
193}
194
195func (c *Stanza) NewStanza(name string) *Stanza {
196 c.Append("\n" + name + " {")
197 c.pad += 2
198 return c
199}
200
201func (c *Stanza) Append(name string) *Stanza {
202 c.content += strings.Repeat(" ", c.pad)
203 c.content += name + "\n"
204 return c
205}
206
207func (c *Stanza) Close() *Stanza {
208 c.content += "}\n"
209 c.pad -= 2
210 return c
211}
212
213func (s *Stanza) ToString() string {
214 return s.content
215}
216
217func (s *Stanza) SaveToFile(fileName string) error {
218 fo, err := os.Create(fileName)
219 if err != nil {
220 return err
221 }
222 defer fo.Close()
223
224 _, err = io.Copy(fo, strings.NewReader(s.content))
225 return err
226}