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