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