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