Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 4 | "bufio" |
Filip Tehlar | 4b3598e | 2023-09-02 08:54:21 +0200 | [diff] [blame] | 5 | "errors" |
Filip Tehlar | 671cf51 | 2023-01-31 10:34:18 +0100 | [diff] [blame] | 6 | "flag" |
Filip Tehlar | 4b3598e | 2023-09-02 08:54:21 +0200 | [diff] [blame] | 7 | "fmt" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 8 | "os" |
Filip Tehlar | 31eaea9 | 2023-06-15 10:06:57 +0200 | [diff] [blame] | 9 | "os/exec" |
| 10 | "strings" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 11 | "time" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 12 | |
| 13 | "github.com/edwarnicke/exechelper" |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | "github.com/stretchr/testify/suite" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 16 | "gopkg.in/yaml.v3" |
| 17 | ) |
| 18 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 19 | const ( |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 20 | DEFAULT_NETWORK_NUM int = 1 |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 21 | ) |
| 22 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 23 | var isPersistent = flag.Bool("persist", false, "persists topology config") |
| 24 | var isVerbose = flag.Bool("verbose", false, "verbose test output") |
| 25 | var isUnconfiguring = flag.Bool("unconfigure", false, "remove topology") |
| 26 | var isVppDebug = flag.Bool("debug", false, "attach gdb to vpp") |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 27 | var nConfiguredCpus = flag.Int("cpus", 1, "number of CPUs assigned to vpp") |
Filip Tehlar | 109f3ce | 2023-09-05 15:36:28 +0200 | [diff] [blame] | 28 | var vppSourceFileDir = flag.String("vppsrc", "", "vpp source file directory") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 29 | |
| 30 | type HstSuite struct { |
| 31 | suite.Suite |
Filip Tehlar | 3a910ab | 2023-06-08 17:39:39 +0200 | [diff] [blame] | 32 | containers map[string]*Container |
| 33 | volumes []string |
| 34 | netConfigs []NetConfig |
| 35 | netInterfaces map[string]*NetInterface |
| 36 | ip4AddrAllocator *Ip4AddressAllocator |
| 37 | testIds map[string]string |
| 38 | cpuAllocator *CpuAllocatorT |
| 39 | cpuContexts []*CpuContext |
| 40 | cpuPerVpp int |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 41 | pid string |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | func (s *HstSuite) SetupSuite() { |
| 45 | var err error |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 46 | s.pid = fmt.Sprint(os.Getpid()) |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 47 | s.cpuAllocator, err = CpuAllocator() |
| 48 | if err != nil { |
| 49 | s.FailNow("failed to init cpu allocator: %v", err) |
| 50 | } |
| 51 | s.cpuPerVpp = *nConfiguredCpus |
| 52 | } |
| 53 | |
| 54 | func (s *HstSuite) AllocateCpus() []int { |
| 55 | cpuCtx, err := s.cpuAllocator.Allocate(s.cpuPerVpp) |
| 56 | s.assertNil(err) |
| 57 | s.AddCpuContext(cpuCtx) |
| 58 | return cpuCtx.cpus |
| 59 | } |
| 60 | |
| 61 | func (s *HstSuite) AddCpuContext(cpuCtx *CpuContext) { |
| 62 | s.cpuContexts = append(s.cpuContexts, cpuCtx) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | func (s *HstSuite) TearDownSuite() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 66 | s.unconfigureNetworkTopology() |
| 67 | } |
| 68 | |
| 69 | func (s *HstSuite) TearDownTest() { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 70 | if *isPersistent { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 71 | return |
| 72 | } |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 73 | for _, c := range s.cpuContexts { |
| 74 | c.Release() |
| 75 | } |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 76 | s.resetContainers() |
| 77 | s.removeVolumes() |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 78 | s.ip4AddrAllocator.deleteIpAddresses() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 81 | func (s *HstSuite) skipIfUnconfiguring() { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 82 | if *isUnconfiguring { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 83 | s.skip("skipping to unconfigure") |
| 84 | } |
| 85 | } |
| 86 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 87 | func (s *HstSuite) SetupTest() { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 88 | s.skipIfUnconfiguring() |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 89 | s.setupVolumes() |
| 90 | s.setupContainers() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 93 | func (s *HstSuite) setupVolumes() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 94 | for _, volume := range s.volumes { |
| 95 | cmd := "docker volume create --name=" + volume |
| 96 | s.log(cmd) |
| 97 | exechelper.Run(cmd) |
| 98 | } |
| 99 | } |
| 100 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 101 | func (s *HstSuite) setupContainers() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 102 | for _, container := range s.containers { |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 103 | if !container.isOptional { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 104 | container.run() |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 109 | func logVppInstance(container *Container, maxLines int){ |
| 110 | if container.vppInstance == nil{ |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | logSource := container.getHostWorkDir() + defaultLogFilePath |
| 115 | file, err := os.Open(logSource) |
| 116 | |
| 117 | if err != nil{ |
| 118 | return |
| 119 | } |
| 120 | defer file.Close() |
| 121 | |
| 122 | scanner := bufio.NewScanner(file) |
| 123 | var lines []string |
| 124 | var counter int |
| 125 | |
| 126 | for scanner.Scan(){ |
| 127 | lines = append(lines, scanner.Text()) |
| 128 | counter++ |
| 129 | if counter > maxLines { |
| 130 | lines = lines[1:] |
| 131 | counter-- |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fmt.Println("vvvvvvvvvvvvvvv " + container.name + " [VPP instance]:") |
| 136 | for _, line := range lines{ |
| 137 | fmt.Println(line) |
| 138 | } |
| 139 | fmt.Printf("^^^^^^^^^^^^^^^\n\n") |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 142 | func (s *HstSuite) hstFail() { |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 143 | fmt.Println("Containers: " + fmt.Sprint(s.containers)) |
| 144 | for _, container := range s.containers{ |
| 145 | out, err := container.log(20) |
| 146 | if err != nil{ |
| 147 | fmt.Printf("An error occured while obtaining '%s' container logs: %s\n", container.name, fmt.Sprint(err)) |
| 148 | break |
| 149 | } |
| 150 | fmt.Printf("\nvvvvvvvvvvvvvvv " + |
| 151 | container.name + ":\n" + |
| 152 | out + |
| 153 | "^^^^^^^^^^^^^^^\n\n") |
| 154 | logVppInstance(container, 20) |
| 155 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 156 | s.T().FailNow() |
| 157 | } |
| 158 | |
| 159 | func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) { |
| 160 | if !assert.Nil(s.T(), object, msgAndArgs...) { |
| 161 | s.hstFail() |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) { |
| 166 | if !assert.NotNil(s.T(), object, msgAndArgs...) { |
| 167 | s.hstFail() |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 172 | if !assert.Equal(s.T(), expected, actual, msgAndArgs...) { |
| 173 | s.hstFail() |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 178 | if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) { |
| 179 | s.hstFail() |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 184 | if !assert.Contains(s.T(), testString, contains, msgAndArgs...) { |
| 185 | s.hstFail() |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 190 | if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) { |
| 191 | s.hstFail() |
| 192 | } |
| 193 | } |
| 194 | |
Maros Ondrejicka | 2ddb2fd | 2023-02-15 17:44:46 +0100 | [diff] [blame] | 195 | func (s *HstSuite) assertNotEmpty(object interface{}, msgAndArgs ...interface{}) { |
| 196 | if !assert.NotEmpty(s.T(), object, msgAndArgs...) { |
| 197 | s.hstFail() |
| 198 | } |
| 199 | } |
| 200 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 201 | func (s *HstSuite) log(args ...any) { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 202 | if *isVerbose { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 203 | s.T().Helper() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 204 | s.T().Log(args...) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | func (s *HstSuite) skip(args ...any) { |
| 209 | s.log(args...) |
| 210 | s.T().SkipNow() |
| 211 | } |
| 212 | |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 213 | func (s *HstSuite) SkipIfMultiWorker(args ...any) { |
| 214 | if *nConfiguredCpus > 1 { |
| 215 | s.skip("test case not supported with multiple vpp workers") |
| 216 | } |
| 217 | } |
| 218 | |
Filip Tehlar | 31eaea9 | 2023-06-15 10:06:57 +0200 | [diff] [blame] | 219 | func (s *HstSuite) SkipUnlessExtendedTestsBuilt() { |
| 220 | imageName := "hs-test/nginx-http3" |
| 221 | |
| 222 | cmd := exec.Command("docker", "images", imageName) |
| 223 | byteOutput, err := cmd.CombinedOutput() |
| 224 | if err != nil { |
| 225 | s.log("error while searching for docker image") |
| 226 | return |
| 227 | } |
| 228 | if !strings.Contains(string(byteOutput), imageName) { |
| 229 | s.skip("extended tests not built") |
| 230 | } |
| 231 | } |
| 232 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 233 | func (s *HstSuite) resetContainers() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 234 | for _, container := range s.containers { |
| 235 | container.stop() |
| 236 | } |
| 237 | } |
| 238 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 239 | func (s *HstSuite) removeVolumes() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 240 | for _, volumeName := range s.volumes { |
| 241 | cmd := "docker volume rm " + volumeName |
| 242 | exechelper.Run(cmd) |
| 243 | os.RemoveAll(volumeName) |
| 244 | } |
| 245 | } |
| 246 | |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 247 | func (s *HstSuite) getNetNamespaceByName(name string) string { |
| 248 | return name + s.pid |
| 249 | } |
| 250 | |
| 251 | func (s *HstSuite) getInterfaceByName(name string) *NetInterface { |
| 252 | return s.netInterfaces[name + s.pid] |
| 253 | } |
| 254 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 255 | func (s *HstSuite) getContainerByName(name string) *Container { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 256 | return s.containers[name + s.pid] |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 259 | /* |
| 260 | * Create a copy and return its address, so that individial tests which call this |
| 261 | * are not able to modify the original container and affect other tests by doing that |
| 262 | */ |
| 263 | func (s *HstSuite) getTransientContainerByName(name string) *Container { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 264 | containerCopy := *s.containers[name + s.pid] |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 265 | return &containerCopy |
| 266 | } |
| 267 | |
| 268 | func (s *HstSuite) loadContainerTopology(topologyName string) { |
adrianvillin | 707210c | 2024-01-24 01:45:59 -0500 | [diff] [blame] | 269 | data, err := os.ReadFile(containerTopologyDir + topologyName + ".yaml") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 270 | if err != nil { |
| 271 | s.T().Fatalf("read error: %v", err) |
| 272 | } |
| 273 | var yamlTopo YamlTopology |
| 274 | err = yaml.Unmarshal(data, &yamlTopo) |
| 275 | if err != nil { |
| 276 | s.T().Fatalf("unmarshal error: %v", err) |
| 277 | } |
| 278 | |
| 279 | for _, elem := range yamlTopo.Volumes { |
| 280 | volumeMap := elem["volume"].(VolumeConfig) |
| 281 | hostDir := volumeMap["host-dir"].(string) |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 282 | workingVolumeDir := logDir + s.T().Name() + s.pid + volumeDir |
Filip Tehlar | a1bd50c | 2024-01-24 11:59:44 +0100 | [diff] [blame] | 283 | volDirReplacer := strings.NewReplacer("$HST_VOLUME_DIR", workingVolumeDir) |
| 284 | hostDir = volDirReplacer.Replace(hostDir) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 285 | s.volumes = append(s.volumes, hostDir) |
| 286 | } |
| 287 | |
| 288 | s.containers = make(map[string]*Container) |
| 289 | for _, elem := range yamlTopo.Containers { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 290 | newContainer, err := newContainer(s, elem, s.pid) |
| 291 | newContainer.suite = s |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 292 | if err != nil { |
| 293 | s.T().Fatalf("container config error: %v", err) |
| 294 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 295 | s.containers[newContainer.name] = newContainer |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | func (s *HstSuite) loadNetworkTopology(topologyName string) { |
adrianvillin | 707210c | 2024-01-24 01:45:59 -0500 | [diff] [blame] | 300 | data, err := os.ReadFile(networkTopologyDir + topologyName + ".yaml") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 301 | if err != nil { |
| 302 | s.T().Fatalf("read error: %v", err) |
| 303 | } |
| 304 | var yamlTopo YamlTopology |
| 305 | err = yaml.Unmarshal(data, &yamlTopo) |
| 306 | if err != nil { |
| 307 | s.T().Fatalf("unmarshal error: %v", err) |
| 308 | } |
| 309 | |
Filip Tehlar | 3a910ab | 2023-06-08 17:39:39 +0200 | [diff] [blame] | 310 | s.ip4AddrAllocator = NewIp4AddressAllocator() |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 311 | s.netInterfaces = make(map[string]*NetInterface) |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 312 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 313 | for _, elem := range yamlTopo.Devices { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 314 | if _, ok := elem["name"]; ok { |
| 315 | elem["name"] = elem["name"].(string) + s.pid |
| 316 | } |
| 317 | |
| 318 | if peer, ok := elem["peer"].(NetDevConfig); ok { |
| 319 | if peer["name"].(string) != ""{ |
| 320 | peer["name"] = peer["name"].(string) + s.pid |
| 321 | } |
| 322 | if _, ok := peer["netns"]; ok{ |
| 323 | peer["netns"] = peer["netns"].(string) + s.pid |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if _, ok := elem["netns"]; ok { |
| 328 | elem["netns"] = elem["netns"].(string) + s.pid |
| 329 | } |
| 330 | |
| 331 | if _, ok := elem["interfaces"]; ok { |
| 332 | interfaceCount := len(elem["interfaces"].([]interface{})) |
| 333 | for i := 0; i < interfaceCount; i++ { |
| 334 | elem["interfaces"].([]interface{})[i] = elem["interfaces"].([]interface{})[i].(string) + s.pid |
| 335 | } |
| 336 | } |
| 337 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 338 | switch elem["type"].(string) { |
| 339 | case NetNs: |
| 340 | { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 341 | if namespace, err := newNetNamespace(elem); err == nil { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 342 | s.netConfigs = append(s.netConfigs, &namespace) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 343 | } else { |
| 344 | s.T().Fatalf("network config error: %v", err) |
| 345 | } |
| 346 | } |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 347 | case Veth, Tap: |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 348 | { |
Filip Tehlar | 3a910ab | 2023-06-08 17:39:39 +0200 | [diff] [blame] | 349 | if netIf, err := newNetworkInterface(elem, s.ip4AddrAllocator); err == nil { |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 350 | s.netConfigs = append(s.netConfigs, netIf) |
| 351 | s.netInterfaces[netIf.Name()] = netIf |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 352 | } else { |
| 353 | s.T().Fatalf("network config error: %v", err) |
| 354 | } |
| 355 | } |
| 356 | case Bridge: |
| 357 | { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 358 | if bridge, err := newBridge(elem); err == nil { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 359 | s.netConfigs = append(s.netConfigs, &bridge) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 360 | } else { |
| 361 | s.T().Fatalf("network config error: %v", err) |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | func (s *HstSuite) configureNetworkTopology(topologyName string) { |
| 369 | s.loadNetworkTopology(topologyName) |
| 370 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 371 | if *isUnconfiguring { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 372 | return |
| 373 | } |
| 374 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 375 | for _, nc := range s.netConfigs { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 376 | if err := nc.configure(); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 377 | s.T().Fatalf("network config error: %v", err) |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | func (s *HstSuite) unconfigureNetworkTopology() { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 383 | if *isPersistent { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 384 | return |
| 385 | } |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 386 | for _, nc := range s.netConfigs { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 387 | nc.unconfigure() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 391 | func (s *HstSuite) getTestId() string { |
| 392 | testName := s.T().Name() |
| 393 | |
| 394 | if s.testIds == nil { |
| 395 | s.testIds = map[string]string{} |
| 396 | } |
| 397 | |
| 398 | if _, ok := s.testIds[testName]; !ok { |
Filip Tehlar | 75776f0 | 2023-03-24 13:47:45 +0100 | [diff] [blame] | 399 | s.testIds[testName] = time.Now().Format("2006-01-02_15-04-05") |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | return s.testIds[testName] |
| 403 | } |
Filip Tehlar | 4b3598e | 2023-09-02 08:54:21 +0200 | [diff] [blame] | 404 | |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 405 | // Returns last 4 digits of PID |
| 406 | func (s *HstSuite) getPortFromPid() string { |
| 407 | port := s.pid |
| 408 | for len(port) < 4 { |
| 409 | port += "0" |
| 410 | } |
| 411 | return port[len(port)-4:] |
| 412 | } |
| 413 | |
Filip Tehlar | 4b3598e | 2023-09-02 08:54:21 +0200 | [diff] [blame] | 414 | func (s *HstSuite) startServerApp(running chan error, done chan struct{}, env []string) { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 415 | cmd := exec.Command("iperf3", "-4", "-s", "-p", s.getPortFromPid()) |
Filip Tehlar | 4b3598e | 2023-09-02 08:54:21 +0200 | [diff] [blame] | 416 | if env != nil { |
| 417 | cmd.Env = env |
| 418 | } |
| 419 | s.log(cmd) |
| 420 | err := cmd.Start() |
| 421 | if err != nil { |
| 422 | msg := fmt.Errorf("failed to start iperf server: %v", err) |
| 423 | running <- msg |
| 424 | return |
| 425 | } |
| 426 | running <- nil |
| 427 | <-done |
| 428 | cmd.Process.Kill() |
| 429 | } |
| 430 | |
| 431 | func (s *HstSuite) startClientApp(ipAddress string, env []string, clnCh chan error, clnRes chan string) { |
| 432 | defer func() { |
| 433 | clnCh <- nil |
| 434 | }() |
| 435 | |
| 436 | nTries := 0 |
| 437 | |
| 438 | for { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 439 | cmd := exec.Command("iperf3", "-c", ipAddress, "-u", "-l", "1460", "-b", "10g", "-p", s.getPortFromPid()) |
Filip Tehlar | 4b3598e | 2023-09-02 08:54:21 +0200 | [diff] [blame] | 440 | if env != nil { |
| 441 | cmd.Env = env |
| 442 | } |
| 443 | s.log(cmd) |
| 444 | o, err := cmd.CombinedOutput() |
| 445 | if err != nil { |
| 446 | if nTries > 5 { |
| 447 | clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o) |
| 448 | return |
| 449 | } |
| 450 | time.Sleep(1 * time.Second) |
| 451 | nTries++ |
| 452 | continue |
| 453 | } else { |
| 454 | clnRes <- fmt.Sprintf("Client output: %s", o) |
| 455 | } |
| 456 | break |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | func (s *HstSuite) startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 461 | cmd := newCommand([]string{"./http_server", addressPort, s.pid}, netNs) |
Filip Tehlar | 4b3598e | 2023-09-02 08:54:21 +0200 | [diff] [blame] | 462 | err := cmd.Start() |
| 463 | s.log(cmd) |
| 464 | if err != nil { |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 465 | fmt.Println("Failed to start http server: " + fmt.Sprint(err)) |
Filip Tehlar | 4b3598e | 2023-09-02 08:54:21 +0200 | [diff] [blame] | 466 | return |
| 467 | } |
| 468 | running <- struct{}{} |
| 469 | <-done |
| 470 | cmd.Process.Kill() |
| 471 | } |
| 472 | |
| 473 | func (s *HstSuite) startWget(finished chan error, server_ip, port, query, netNs string) { |
| 474 | defer func() { |
| 475 | finished <- errors.New("wget error") |
| 476 | }() |
| 477 | |
| 478 | cmd := newCommand([]string{"wget", "--timeout=10", "--no-proxy", "--tries=5", "-O", "/dev/null", server_ip + ":" + port + "/" + query}, |
| 479 | netNs) |
| 480 | s.log(cmd) |
| 481 | o, err := cmd.CombinedOutput() |
| 482 | if err != nil { |
| 483 | finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o) |
| 484 | return |
| 485 | } else if !strings.Contains(string(o), "200 OK") { |
| 486 | finished <- fmt.Errorf("wget error: response not 200 OK") |
| 487 | return |
| 488 | } |
| 489 | finished <- nil |
| 490 | } |