blob: 7f93b15f50a52cf67466a35c36b731c1957ea7b2 [file] [log] [blame]
Maros Ondrejickaffa3f602023-01-26 10:07:29 +01001package main
2
3import (
Filip Tehlar4b3598e2023-09-02 08:54:21 +02004 "errors"
Filip Tehlar671cf512023-01-31 10:34:18 +01005 "flag"
Filip Tehlar4b3598e2023-09-02 08:54:21 +02006 "fmt"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +01007 "os"
Filip Tehlar31eaea92023-06-15 10:06:57 +02008 "os/exec"
9 "strings"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010010 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010011
12 "github.com/edwarnicke/exechelper"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/suite"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010015 "gopkg.in/yaml.v3"
16)
17
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010018const (
Filip Tehlar608d0062023-04-28 10:29:47 +020019 DEFAULT_NETWORK_NUM int = 1
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010020)
21
Maros Ondrejickae7625d02023-02-28 16:55:01 +010022var isPersistent = flag.Bool("persist", false, "persists topology config")
23var isVerbose = flag.Bool("verbose", false, "verbose test output")
24var isUnconfiguring = flag.Bool("unconfigure", false, "remove topology")
25var isVppDebug = flag.Bool("debug", false, "attach gdb to vpp")
Filip Tehlar608d0062023-04-28 10:29:47 +020026var nConfiguredCpus = flag.Int("cpus", 1, "number of CPUs assigned to vpp")
Filip Tehlar109f3ce2023-09-05 15:36:28 +020027var vppSourceFileDir = flag.String("vppsrc", "", "vpp source file directory")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010028
29type HstSuite struct {
30 suite.Suite
Filip Tehlar3a910ab2023-06-08 17:39:39 +020031 containers map[string]*Container
32 volumes []string
33 netConfigs []NetConfig
34 netInterfaces map[string]*NetInterface
35 ip4AddrAllocator *Ip4AddressAllocator
36 testIds map[string]string
37 cpuAllocator *CpuAllocatorT
38 cpuContexts []*CpuContext
39 cpuPerVpp int
Filip Tehlar608d0062023-04-28 10:29:47 +020040}
41
42func (s *HstSuite) SetupSuite() {
43 var err error
44 s.cpuAllocator, err = CpuAllocator()
45 if err != nil {
46 s.FailNow("failed to init cpu allocator: %v", err)
47 }
48 s.cpuPerVpp = *nConfiguredCpus
49}
50
51func (s *HstSuite) AllocateCpus() []int {
52 cpuCtx, err := s.cpuAllocator.Allocate(s.cpuPerVpp)
53 s.assertNil(err)
54 s.AddCpuContext(cpuCtx)
55 return cpuCtx.cpus
56}
57
58func (s *HstSuite) AddCpuContext(cpuCtx *CpuContext) {
59 s.cpuContexts = append(s.cpuContexts, cpuCtx)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010060}
61
62func (s *HstSuite) TearDownSuite() {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010063 s.unconfigureNetworkTopology()
64}
65
66func (s *HstSuite) TearDownTest() {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010067 if *isPersistent {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010068 return
69 }
Filip Tehlar608d0062023-04-28 10:29:47 +020070 for _, c := range s.cpuContexts {
71 c.Release()
72 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +010073 s.resetContainers()
74 s.removeVolumes()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010075}
76
Maros Ondrejickaaf004dd2023-02-27 16:52:57 +010077func (s *HstSuite) skipIfUnconfiguring() {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010078 if *isUnconfiguring {
Maros Ondrejickaaf004dd2023-02-27 16:52:57 +010079 s.skip("skipping to unconfigure")
80 }
81}
82
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010083func (s *HstSuite) SetupTest() {
Maros Ondrejickaaf004dd2023-02-27 16:52:57 +010084 s.skipIfUnconfiguring()
Maros Ondrejickae7625d02023-02-28 16:55:01 +010085 s.setupVolumes()
86 s.setupContainers()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010087}
88
Maros Ondrejickae7625d02023-02-28 16:55:01 +010089func (s *HstSuite) setupVolumes() {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010090 for _, volume := range s.volumes {
91 cmd := "docker volume create --name=" + volume
92 s.log(cmd)
93 exechelper.Run(cmd)
94 }
95}
96
Maros Ondrejickae7625d02023-02-28 16:55:01 +010097func (s *HstSuite) setupContainers() {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010098 for _, container := range s.containers {
Filip Tehlar608d0062023-04-28 10:29:47 +020099 if !container.isOptional {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100100 container.run()
101 }
102 }
103}
104
105func (s *HstSuite) hstFail() {
106 s.T().FailNow()
107}
108
109func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) {
110 if !assert.Nil(s.T(), object, msgAndArgs...) {
111 s.hstFail()
112 }
113}
114
115func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) {
116 if !assert.NotNil(s.T(), object, msgAndArgs...) {
117 s.hstFail()
118 }
119}
120
121func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
122 if !assert.Equal(s.T(), expected, actual, msgAndArgs...) {
123 s.hstFail()
124 }
125}
126
127func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
128 if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) {
129 s.hstFail()
130 }
131}
132
133func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) {
134 if !assert.Contains(s.T(), testString, contains, msgAndArgs...) {
135 s.hstFail()
136 }
137}
138
139func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
140 if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
141 s.hstFail()
142 }
143}
144
Maros Ondrejicka2ddb2fd2023-02-15 17:44:46 +0100145func (s *HstSuite) assertNotEmpty(object interface{}, msgAndArgs ...interface{}) {
146 if !assert.NotEmpty(s.T(), object, msgAndArgs...) {
147 s.hstFail()
148 }
149}
150
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100151func (s *HstSuite) log(args ...any) {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100152 if *isVerbose {
Maros Ondrejickaaf004dd2023-02-27 16:52:57 +0100153 s.T().Helper()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100154 s.T().Log(args...)
155 }
156}
157
158func (s *HstSuite) skip(args ...any) {
159 s.log(args...)
160 s.T().SkipNow()
161}
162
Filip Tehlar608d0062023-04-28 10:29:47 +0200163func (s *HstSuite) SkipIfMultiWorker(args ...any) {
164 if *nConfiguredCpus > 1 {
165 s.skip("test case not supported with multiple vpp workers")
166 }
167}
168
Filip Tehlar31eaea92023-06-15 10:06:57 +0200169func (s *HstSuite) SkipUnlessExtendedTestsBuilt() {
170 imageName := "hs-test/nginx-http3"
171
172 cmd := exec.Command("docker", "images", imageName)
173 byteOutput, err := cmd.CombinedOutput()
174 if err != nil {
175 s.log("error while searching for docker image")
176 return
177 }
178 if !strings.Contains(string(byteOutput), imageName) {
179 s.skip("extended tests not built")
180 }
181}
182
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100183func (s *HstSuite) resetContainers() {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100184 for _, container := range s.containers {
185 container.stop()
186 }
187}
188
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100189func (s *HstSuite) removeVolumes() {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100190 for _, volumeName := range s.volumes {
191 cmd := "docker volume rm " + volumeName
192 exechelper.Run(cmd)
193 os.RemoveAll(volumeName)
194 }
195}
196
197func (s *HstSuite) getContainerByName(name string) *Container {
198 return s.containers[name]
199}
200
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100201/*
202 * Create a copy and return its address, so that individial tests which call this
203 * are not able to modify the original container and affect other tests by doing that
204 */
205func (s *HstSuite) getTransientContainerByName(name string) *Container {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100206 containerCopy := *s.containers[name]
207 return &containerCopy
208}
209
210func (s *HstSuite) loadContainerTopology(topologyName string) {
adrianvillin707210c2024-01-24 01:45:59 -0500211 data, err := os.ReadFile(containerTopologyDir + topologyName + ".yaml")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100212 if err != nil {
213 s.T().Fatalf("read error: %v", err)
214 }
215 var yamlTopo YamlTopology
216 err = yaml.Unmarshal(data, &yamlTopo)
217 if err != nil {
218 s.T().Fatalf("unmarshal error: %v", err)
219 }
220
221 for _, elem := range yamlTopo.Volumes {
222 volumeMap := elem["volume"].(VolumeConfig)
223 hostDir := volumeMap["host-dir"].(string)
Filip Tehlara1bd50c2024-01-24 11:59:44 +0100224 workingVolumeDir := logDir + s.T().Name() + volumeDir
225 volDirReplacer := strings.NewReplacer("$HST_VOLUME_DIR", workingVolumeDir)
226 hostDir = volDirReplacer.Replace(hostDir)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100227 s.volumes = append(s.volumes, hostDir)
228 }
229
230 s.containers = make(map[string]*Container)
231 for _, elem := range yamlTopo.Containers {
Filip Tehlara1bd50c2024-01-24 11:59:44 +0100232 newContainer, err := newContainer(s, elem)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100233 if err != nil {
234 s.T().Fatalf("container config error: %v", err)
235 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100236 s.containers[newContainer.name] = newContainer
237 }
238}
239
240func (s *HstSuite) loadNetworkTopology(topologyName string) {
adrianvillin707210c2024-01-24 01:45:59 -0500241 data, err := os.ReadFile(networkTopologyDir + topologyName + ".yaml")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100242 if err != nil {
243 s.T().Fatalf("read error: %v", err)
244 }
245 var yamlTopo YamlTopology
246 err = yaml.Unmarshal(data, &yamlTopo)
247 if err != nil {
248 s.T().Fatalf("unmarshal error: %v", err)
249 }
250
Filip Tehlar3a910ab2023-06-08 17:39:39 +0200251 s.ip4AddrAllocator = NewIp4AddressAllocator()
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100252 s.netInterfaces = make(map[string]*NetInterface)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100253 for _, elem := range yamlTopo.Devices {
254 switch elem["type"].(string) {
255 case NetNs:
256 {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100257 if namespace, err := newNetNamespace(elem); err == nil {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100258 s.netConfigs = append(s.netConfigs, &namespace)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100259 } else {
260 s.T().Fatalf("network config error: %v", err)
261 }
262 }
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100263 case Veth, Tap:
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100264 {
Filip Tehlar3a910ab2023-06-08 17:39:39 +0200265 if netIf, err := newNetworkInterface(elem, s.ip4AddrAllocator); err == nil {
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100266 s.netConfigs = append(s.netConfigs, netIf)
267 s.netInterfaces[netIf.Name()] = netIf
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100268 } else {
269 s.T().Fatalf("network config error: %v", err)
270 }
271 }
272 case Bridge:
273 {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100274 if bridge, err := newBridge(elem); err == nil {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100275 s.netConfigs = append(s.netConfigs, &bridge)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100276 } else {
277 s.T().Fatalf("network config error: %v", err)
278 }
279 }
280 }
281 }
282}
283
284func (s *HstSuite) configureNetworkTopology(topologyName string) {
285 s.loadNetworkTopology(topologyName)
286
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100287 if *isUnconfiguring {
Maros Ondrejickaaf004dd2023-02-27 16:52:57 +0100288 return
289 }
290
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100291 for _, nc := range s.netConfigs {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100292 if err := nc.configure(); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100293 s.T().Fatalf("network config error: %v", err)
294 }
295 }
296}
297
298func (s *HstSuite) unconfigureNetworkTopology() {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100299 if *isPersistent {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100300 return
301 }
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100302 for _, nc := range s.netConfigs {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100303 nc.unconfigure()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100304 }
305}
306
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100307func (s *HstSuite) getTestId() string {
308 testName := s.T().Name()
309
310 if s.testIds == nil {
311 s.testIds = map[string]string{}
312 }
313
314 if _, ok := s.testIds[testName]; !ok {
Filip Tehlar75776f02023-03-24 13:47:45 +0100315 s.testIds[testName] = time.Now().Format("2006-01-02_15-04-05")
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100316 }
317
318 return s.testIds[testName]
319}
Filip Tehlar4b3598e2023-09-02 08:54:21 +0200320
321func (s *HstSuite) startServerApp(running chan error, done chan struct{}, env []string) {
322 cmd := exec.Command("iperf3", "-4", "-s")
323 if env != nil {
324 cmd.Env = env
325 }
326 s.log(cmd)
327 err := cmd.Start()
328 if err != nil {
329 msg := fmt.Errorf("failed to start iperf server: %v", err)
330 running <- msg
331 return
332 }
333 running <- nil
334 <-done
335 cmd.Process.Kill()
336}
337
338func (s *HstSuite) startClientApp(ipAddress string, env []string, clnCh chan error, clnRes chan string) {
339 defer func() {
340 clnCh <- nil
341 }()
342
343 nTries := 0
344
345 for {
346 cmd := exec.Command("iperf3", "-c", ipAddress, "-u", "-l", "1460", "-b", "10g")
347 if env != nil {
348 cmd.Env = env
349 }
350 s.log(cmd)
351 o, err := cmd.CombinedOutput()
352 if err != nil {
353 if nTries > 5 {
354 clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
355 return
356 }
357 time.Sleep(1 * time.Second)
358 nTries++
359 continue
360 } else {
361 clnRes <- fmt.Sprintf("Client output: %s", o)
362 }
363 break
364 }
365}
366
367func (s *HstSuite) startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
368 cmd := newCommand([]string{"./http_server", addressPort}, netNs)
369 err := cmd.Start()
370 s.log(cmd)
371 if err != nil {
372 fmt.Println("Failed to start http server")
373 return
374 }
375 running <- struct{}{}
376 <-done
377 cmd.Process.Kill()
378}
379
380func (s *HstSuite) startWget(finished chan error, server_ip, port, query, netNs string) {
381 defer func() {
382 finished <- errors.New("wget error")
383 }()
384
385 cmd := newCommand([]string{"wget", "--timeout=10", "--no-proxy", "--tries=5", "-O", "/dev/null", server_ip + ":" + port + "/" + query},
386 netNs)
387 s.log(cmd)
388 o, err := cmd.CombinedOutput()
389 if err != nil {
390 finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o)
391 return
392 } else if !strings.Contains(string(o), "200 OK") {
393 finished <- fmt.Errorf("wget error: response not 200 OK")
394 return
395 }
396 finished <- nil
397}