Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1 | package hst |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 5 | "flag" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "log" |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 9 | "net/http" |
| 10 | "net/http/httputil" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
| 14 | "runtime" |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 15 | "strconv" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 16 | "strings" |
| 17 | "time" |
| 18 | |
Adrian Villin | 5a4c7a9 | 2024-09-26 11:24:34 +0200 | [diff] [blame] | 19 | "github.com/edwarnicke/exechelper" |
| 20 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 21 | containerTypes "github.com/docker/docker/api/types/container" |
| 22 | "github.com/docker/docker/client" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 23 | "github.com/onsi/gomega/gmeasure" |
| 24 | "gopkg.in/yaml.v3" |
| 25 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 26 | . "github.com/onsi/ginkgo/v2" |
| 27 | . "github.com/onsi/gomega" |
| 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | DEFAULT_NETWORK_NUM int = 1 |
| 32 | ) |
| 33 | |
| 34 | var IsPersistent = flag.Bool("persist", false, "persists topology config") |
| 35 | var IsVerbose = flag.Bool("verbose", false, "verbose test output") |
| 36 | var IsUnconfiguring = flag.Bool("unconfigure", false, "remove topology") |
| 37 | var IsVppDebug = flag.Bool("debug", false, "attach gdb to vpp") |
| 38 | var NConfiguredCpus = flag.Int("cpus", 1, "number of CPUs assigned to vpp") |
| 39 | var VppSourceFileDir = flag.String("vppsrc", "", "vpp source file directory") |
Adrian Villin | b4516bb | 2024-06-19 06:20:00 -0400 | [diff] [blame] | 40 | var IsDebugBuild = flag.Bool("debug_build", false, "some paths are different with debug build") |
Adrian Villin | 5d171eb | 2024-06-17 08:51:27 +0200 | [diff] [blame] | 41 | var UseCpu0 = flag.Bool("cpu0", false, "use cpu0") |
Matus Fabian | e99d266 | 2024-07-19 16:04:09 +0200 | [diff] [blame] | 42 | var IsLeakCheck = flag.Bool("leak_check", false, "run leak-check tests") |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 43 | var ParallelTotal = flag.Lookup("ginkgo.parallel.total") |
Adrian Villin | 5d171eb | 2024-06-17 08:51:27 +0200 | [diff] [blame] | 44 | var NumaAwareCpuAlloc bool |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 45 | var SuiteTimeout time.Duration |
| 46 | |
| 47 | type HstSuite struct { |
| 48 | Containers map[string]*Container |
| 49 | StartedContainers []*Container |
| 50 | Volumes []string |
| 51 | NetConfigs []NetConfig |
| 52 | NetInterfaces map[string]*NetInterface |
| 53 | Ip4AddrAllocator *Ip4AddressAllocator |
| 54 | TestIds map[string]string |
| 55 | CpuAllocator *CpuAllocatorT |
| 56 | CpuContexts []*CpuContext |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 57 | CpuCount int |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 58 | Ppid string |
| 59 | ProcessIndex string |
| 60 | Logger *log.Logger |
| 61 | LogFile *os.File |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 62 | Docker *client.Client |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 65 | // used for colorful ReportEntry |
| 66 | type StringerStruct struct { |
| 67 | Label string |
| 68 | } |
| 69 | |
| 70 | // ColorableString for ReportEntry to use |
| 71 | func (s StringerStruct) ColorableString() string { |
| 72 | return fmt.Sprintf("{{red}}%s{{/}}", s.Label) |
| 73 | } |
| 74 | |
| 75 | // non-colorable String() is used by go's string formatting support but ignored by ReportEntry |
| 76 | func (s StringerStruct) String() string { |
| 77 | return s.Label |
| 78 | } |
| 79 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 80 | func getTestFilename() string { |
| 81 | _, filename, _, _ := runtime.Caller(2) |
| 82 | return filepath.Base(filename) |
| 83 | } |
| 84 | |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 85 | func (s *HstSuite) getLogDirPath() string { |
| 86 | testId := s.GetTestId() |
| 87 | testName := s.GetCurrentTestName() |
| 88 | logDirPath := logDir + testName + "/" + testId + "/" |
| 89 | |
| 90 | cmd := exec.Command("mkdir", "-p", logDirPath) |
| 91 | if err := cmd.Run(); err != nil { |
| 92 | Fail("mkdir error: " + fmt.Sprint(err)) |
| 93 | } |
| 94 | |
| 95 | return logDirPath |
| 96 | } |
| 97 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 98 | func (s *HstSuite) newDockerClient() { |
| 99 | var err error |
| 100 | s.Docker, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) |
| 101 | s.AssertNil(err) |
| 102 | s.Log("docker client created") |
| 103 | } |
| 104 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 105 | func (s *HstSuite) SetupSuite() { |
| 106 | s.CreateLogger() |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 107 | s.newDockerClient() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 108 | s.Log("Suite Setup") |
| 109 | RegisterFailHandler(func(message string, callerSkip ...int) { |
| 110 | s.HstFail() |
| 111 | Fail(message, callerSkip...) |
| 112 | }) |
| 113 | var err error |
| 114 | s.Ppid = fmt.Sprint(os.Getppid()) |
| 115 | // remove last number so we have space to prepend a process index (interfaces have a char limit) |
| 116 | s.Ppid = s.Ppid[:len(s.Ppid)-1] |
| 117 | s.ProcessIndex = fmt.Sprint(GinkgoParallelProcess()) |
| 118 | s.CpuAllocator, err = CpuAllocator() |
| 119 | if err != nil { |
| 120 | Fail("failed to init cpu allocator: " + fmt.Sprint(err)) |
| 121 | } |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 122 | s.CpuCount = *NConfiguredCpus |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | func (s *HstSuite) AllocateCpus() []int { |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 126 | cpuCtx, err := s.CpuAllocator.Allocate(len(s.StartedContainers), s.CpuCount) |
Adrian Villin | 5d171eb | 2024-06-17 08:51:27 +0200 | [diff] [blame] | 127 | // using Fail instead of AssertNil to make error message more readable |
| 128 | if err != nil { |
| 129 | Fail(fmt.Sprint(err)) |
| 130 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 131 | s.AddCpuContext(cpuCtx) |
| 132 | return cpuCtx.cpus |
| 133 | } |
| 134 | |
| 135 | func (s *HstSuite) AddCpuContext(cpuCtx *CpuContext) { |
| 136 | s.CpuContexts = append(s.CpuContexts, cpuCtx) |
| 137 | } |
| 138 | |
| 139 | func (s *HstSuite) TearDownSuite() { |
| 140 | defer s.LogFile.Close() |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 141 | defer s.Docker.Close() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 142 | s.Log("Suite Teardown") |
| 143 | s.UnconfigureNetworkTopology() |
| 144 | } |
| 145 | |
| 146 | func (s *HstSuite) TearDownTest() { |
| 147 | s.Log("Test Teardown") |
| 148 | if *IsPersistent { |
| 149 | return |
| 150 | } |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 151 | s.WaitForCoreDump() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 152 | s.ResetContainers() |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 153 | |
| 154 | if s.Ip4AddrAllocator != nil { |
| 155 | s.Ip4AddrAllocator.DeleteIpAddresses() |
| 156 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | func (s *HstSuite) SkipIfUnconfiguring() { |
| 160 | if *IsUnconfiguring { |
| 161 | s.Skip("skipping to unconfigure") |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func (s *HstSuite) SetupTest() { |
| 166 | s.Log("Test Setup") |
| 167 | s.StartedContainers = s.StartedContainers[:0] |
| 168 | s.SkipIfUnconfiguring() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 169 | s.SetupContainers() |
| 170 | } |
| 171 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 172 | func (s *HstSuite) SetupContainers() { |
| 173 | for _, container := range s.Containers { |
| 174 | if !container.IsOptional { |
| 175 | container.Run() |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func (s *HstSuite) LogVppInstance(container *Container, maxLines int) { |
| 181 | if container.VppInstance == nil { |
| 182 | return |
| 183 | } |
| 184 | |
| 185 | logSource := container.GetHostWorkDir() + defaultLogFilePath |
| 186 | file, err := os.Open(logSource) |
| 187 | |
| 188 | if err != nil { |
| 189 | return |
| 190 | } |
| 191 | defer file.Close() |
| 192 | |
| 193 | scanner := bufio.NewScanner(file) |
| 194 | var lines []string |
| 195 | var counter int |
| 196 | |
| 197 | for scanner.Scan() { |
| 198 | lines = append(lines, scanner.Text()) |
| 199 | counter++ |
| 200 | if counter > maxLines { |
| 201 | lines = lines[1:] |
| 202 | counter-- |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | s.Log("vvvvvvvvvvvvvvv " + container.Name + " [VPP instance]:") |
| 207 | for _, line := range lines { |
| 208 | s.Log(line) |
| 209 | } |
| 210 | s.Log("^^^^^^^^^^^^^^^\n\n") |
| 211 | } |
| 212 | |
| 213 | func (s *HstSuite) HstFail() { |
| 214 | for _, container := range s.StartedContainers { |
| 215 | out, err := container.log(20) |
| 216 | if err != nil { |
| 217 | s.Log("An error occured while obtaining '" + container.Name + "' container logs: " + fmt.Sprint(err)) |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 218 | s.Log("The container might not be running - check logs in " + s.getLogDirPath()) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 219 | continue |
| 220 | } |
| 221 | s.Log("\nvvvvvvvvvvvvvvv " + |
| 222 | container.Name + ":\n" + |
| 223 | out + |
| 224 | "^^^^^^^^^^^^^^^\n\n") |
| 225 | s.LogVppInstance(container, 20) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func (s *HstSuite) AssertNil(object interface{}, msgAndArgs ...interface{}) { |
Matus Fabian | 225e6f8 | 2024-08-22 15:32:44 +0200 | [diff] [blame] | 230 | ExpectWithOffset(2, object).To(BeNil(), msgAndArgs...) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | func (s *HstSuite) AssertNotNil(object interface{}, msgAndArgs ...interface{}) { |
Matus Fabian | 225e6f8 | 2024-08-22 15:32:44 +0200 | [diff] [blame] | 234 | ExpectWithOffset(2, object).ToNot(BeNil(), msgAndArgs...) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | func (s *HstSuite) AssertEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
Matus Fabian | 225e6f8 | 2024-08-22 15:32:44 +0200 | [diff] [blame] | 238 | ExpectWithOffset(2, actual).To(Equal(expected), msgAndArgs...) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | func (s *HstSuite) AssertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
Matus Fabian | 225e6f8 | 2024-08-22 15:32:44 +0200 | [diff] [blame] | 242 | ExpectWithOffset(2, actual).ToNot(Equal(expected), msgAndArgs...) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | func (s *HstSuite) AssertContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
Matus Fabian | 225e6f8 | 2024-08-22 15:32:44 +0200 | [diff] [blame] | 246 | ExpectWithOffset(2, testString).To(ContainSubstring(fmt.Sprint(contains)), msgAndArgs...) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | func (s *HstSuite) AssertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
Matus Fabian | 225e6f8 | 2024-08-22 15:32:44 +0200 | [diff] [blame] | 250 | ExpectWithOffset(2, testString).ToNot(ContainSubstring(fmt.Sprint(contains)), msgAndArgs...) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 253 | func (s *HstSuite) AssertEmpty(object interface{}, msgAndArgs ...interface{}) { |
Matus Fabian | 225e6f8 | 2024-08-22 15:32:44 +0200 | [diff] [blame] | 254 | ExpectWithOffset(2, object).To(BeEmpty(), msgAndArgs...) |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 257 | func (s *HstSuite) AssertNotEmpty(object interface{}, msgAndArgs ...interface{}) { |
Matus Fabian | 225e6f8 | 2024-08-22 15:32:44 +0200 | [diff] [blame] | 258 | ExpectWithOffset(2, object).ToNot(BeEmpty(), msgAndArgs...) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 261 | func (s *HstSuite) AssertMatchError(actual, expected error, msgAndArgs ...interface{}) { |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 262 | ExpectWithOffset(2, actual).To(MatchError(expected), msgAndArgs...) |
| 263 | } |
| 264 | |
| 265 | func (s *HstSuite) AssertGreaterThan(actual, expected interface{}, msgAndArgs ...interface{}) { |
| 266 | ExpectWithOffset(2, actual).Should(BeNumerically(">=", expected), msgAndArgs...) |
| 267 | } |
| 268 | |
| 269 | func (s *HstSuite) AssertTimeEqualWithinThreshold(actual, expected time.Time, threshold time.Duration, msgAndArgs ...interface{}) { |
| 270 | ExpectWithOffset(2, actual).Should(BeTemporally("~", expected, threshold), msgAndArgs...) |
| 271 | } |
| 272 | |
| 273 | func (s *HstSuite) AssertHttpStatus(resp *http.Response, expectedStatus int, msgAndArgs ...interface{}) { |
| 274 | ExpectWithOffset(2, resp).To(HaveHTTPStatus(expectedStatus), msgAndArgs...) |
| 275 | } |
| 276 | |
| 277 | func (s *HstSuite) AssertHttpHeaderWithValue(resp *http.Response, key string, value interface{}, msgAndArgs ...interface{}) { |
| 278 | ExpectWithOffset(2, resp).To(HaveHTTPHeaderWithValue(key, value), msgAndArgs...) |
| 279 | } |
| 280 | |
| 281 | func (s *HstSuite) AssertHttpHeaderNotPresent(resp *http.Response, key string, msgAndArgs ...interface{}) { |
| 282 | ExpectWithOffset(2, resp.Header.Get(key)).To(BeEmpty(), msgAndArgs...) |
| 283 | } |
| 284 | |
| 285 | func (s *HstSuite) AssertHttpContentLength(resp *http.Response, expectedContentLen int64, msgAndArgs ...interface{}) { |
| 286 | ExpectWithOffset(2, resp).To(HaveHTTPHeaderWithValue("Content-Length", strconv.FormatInt(expectedContentLen, 10)), msgAndArgs...) |
| 287 | } |
| 288 | |
| 289 | func (s *HstSuite) AssertHttpBody(resp *http.Response, expectedBody string, msgAndArgs ...interface{}) { |
| 290 | ExpectWithOffset(2, resp).To(HaveHTTPBody(expectedBody), msgAndArgs...) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 293 | func (s *HstSuite) CreateLogger() { |
| 294 | suiteName := s.GetCurrentSuiteName() |
| 295 | var err error |
| 296 | s.LogFile, err = os.Create("summary/" + suiteName + ".log") |
| 297 | if err != nil { |
| 298 | Fail("Unable to create log file.") |
| 299 | } |
| 300 | s.Logger = log.New(io.Writer(s.LogFile), "", log.LstdFlags) |
| 301 | } |
| 302 | |
| 303 | // Logs to files by default, logs to stdout when VERBOSE=true with GinkgoWriter |
| 304 | // to keep console tidy |
| 305 | func (s *HstSuite) Log(arg any) { |
| 306 | logs := strings.Split(fmt.Sprint(arg), "\n") |
| 307 | for _, line := range logs { |
| 308 | s.Logger.Println(line) |
| 309 | } |
| 310 | if *IsVerbose { |
| 311 | GinkgoWriter.Println(arg) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | func (s *HstSuite) Skip(args string) { |
| 316 | Skip(args) |
| 317 | } |
| 318 | |
| 319 | func (s *HstSuite) SkipIfMultiWorker(args ...any) { |
| 320 | if *NConfiguredCpus > 1 { |
| 321 | s.Skip("test case not supported with multiple vpp workers") |
| 322 | } |
| 323 | } |
| 324 | |
Adrian Villin | 46ab0b2 | 2024-09-19 17:19:39 +0200 | [diff] [blame] | 325 | func (s *HstSuite) SkipIfNotEnoughAvailableCpus() { |
| 326 | var maxRequestedCpu int |
| 327 | availableCpus := len(s.CpuAllocator.cpus) - 1 |
| 328 | |
| 329 | if *UseCpu0 { |
| 330 | availableCpus++ |
| 331 | } |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 332 | |
| 333 | if s.CpuAllocator.runningInCi { |
Adrian Villin | 46ab0b2 | 2024-09-19 17:19:39 +0200 | [diff] [blame] | 334 | maxRequestedCpu = ((s.CpuAllocator.buildNumber + 1) * s.CpuAllocator.maxContainerCount * s.CpuCount) |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 335 | } else { |
Adrian Villin | 46ab0b2 | 2024-09-19 17:19:39 +0200 | [diff] [blame] | 336 | maxRequestedCpu = (GinkgoParallelProcess() * s.CpuAllocator.maxContainerCount * s.CpuCount) |
Adrian Villin | b69ee00 | 2024-07-17 14:38:48 +0200 | [diff] [blame] | 337 | } |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 338 | |
Adrian Villin | 46ab0b2 | 2024-09-19 17:19:39 +0200 | [diff] [blame] | 339 | if availableCpus < maxRequestedCpu { |
| 340 | s.Skip(fmt.Sprintf("Test case cannot allocate requested cpus "+ |
| 341 | "(%d cpus * %d containers, %d available). Try using 'CPU0=true'", |
| 342 | s.CpuCount, s.CpuAllocator.maxContainerCount, availableCpus)) |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 343 | } |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Matus Fabian | e99d266 | 2024-07-19 16:04:09 +0200 | [diff] [blame] | 346 | func (s *HstSuite) SkipUnlessLeakCheck() { |
| 347 | if !*IsLeakCheck { |
| 348 | s.Skip("leak-check tests excluded") |
| 349 | } |
| 350 | } |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 351 | |
| 352 | func (s *HstSuite) WaitForCoreDump() { |
| 353 | var filename string |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 354 | dir, err := os.Open(s.getLogDirPath()) |
| 355 | if err != nil { |
| 356 | s.Log(err) |
| 357 | return |
| 358 | } |
| 359 | defer dir.Close() |
| 360 | |
| 361 | files, err := dir.Readdirnames(0) |
| 362 | if err != nil { |
| 363 | s.Log(err) |
| 364 | return |
| 365 | } |
| 366 | for _, file := range files { |
| 367 | if strings.Contains(file, "core") { |
| 368 | filename = file |
| 369 | } |
| 370 | } |
| 371 | timeout := 60 |
| 372 | waitTime := 5 |
| 373 | |
| 374 | if filename != "" { |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 375 | corePath := s.getLogDirPath() + filename |
| 376 | s.Log(fmt.Sprintf("WAITING FOR CORE DUMP (%s)", corePath)) |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 377 | for i := waitTime; i <= timeout; i += waitTime { |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 378 | fileInfo, err := os.Stat(corePath) |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 379 | if err != nil { |
| 380 | s.Log("Error while reading file info: " + fmt.Sprint(err)) |
| 381 | return |
| 382 | } |
| 383 | currSize := fileInfo.Size() |
| 384 | s.Log(fmt.Sprintf("Waiting %ds/%ds...", i, timeout)) |
| 385 | time.Sleep(time.Duration(waitTime) * time.Second) |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 386 | fileInfo, _ = os.Stat(corePath) |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 387 | |
| 388 | if currSize == fileInfo.Size() { |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 389 | debug := "" |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 390 | if *IsDebugBuild { |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 391 | debug = "_debug" |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 392 | } |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 393 | vppBinPath := fmt.Sprintf("../../build-root/build-vpp%s-native/vpp/bin/vpp", debug) |
| 394 | pluginsLibPath := fmt.Sprintf("build-root/build-vpp%s-native/vpp/lib/x86_64-linux-gnu/vpp_plugins", debug) |
| 395 | cmd := fmt.Sprintf("sudo gdb %s -c %s -ex 'set solib-search-path %s/%s' -ex 'bt full' -batch", vppBinPath, corePath, *VppSourceFileDir, pluginsLibPath) |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 396 | s.Log(cmd) |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 397 | output, _ := exechelper.Output(cmd) |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 398 | AddReportEntry("VPP Backtrace", StringerStruct{Label: string(output)}) |
| 399 | os.WriteFile(s.getLogDirPath()+"backtrace.log", output, os.FileMode(0644)) |
| 400 | if s.CpuAllocator.runningInCi { |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 401 | err = os.Remove(corePath) |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 402 | if err == nil { |
Matus Fabian | 4306a3e | 2024-08-23 15:52:54 +0200 | [diff] [blame] | 403 | s.Log("removed " + corePath) |
Adrian Villin | 4995d0d | 2024-07-29 17:54:58 +0200 | [diff] [blame] | 404 | } else { |
| 405 | s.Log(err) |
| 406 | } |
| 407 | } |
| 408 | return |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 414 | func (s *HstSuite) ResetContainers() { |
| 415 | for _, container := range s.StartedContainers { |
| 416 | container.stop() |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 417 | s.Log("Removing container " + container.Name) |
| 418 | if err := s.Docker.ContainerRemove(container.ctx, container.ID, containerTypes.RemoveOptions{RemoveVolumes: true}); err != nil { |
| 419 | s.Log(err) |
| 420 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | func (s *HstSuite) GetNetNamespaceByName(name string) string { |
| 425 | return s.ProcessIndex + name + s.Ppid |
| 426 | } |
| 427 | |
| 428 | func (s *HstSuite) GetInterfaceByName(name string) *NetInterface { |
| 429 | return s.NetInterfaces[s.ProcessIndex+name+s.Ppid] |
| 430 | } |
| 431 | |
| 432 | func (s *HstSuite) GetContainerByName(name string) *Container { |
| 433 | return s.Containers[s.ProcessIndex+name+s.Ppid] |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Create a copy and return its address, so that individial tests which call this |
| 438 | * are not able to modify the original container and affect other tests by doing that |
| 439 | */ |
| 440 | func (s *HstSuite) GetTransientContainerByName(name string) *Container { |
| 441 | containerCopy := *s.Containers[s.ProcessIndex+name+s.Ppid] |
| 442 | return &containerCopy |
| 443 | } |
| 444 | |
| 445 | func (s *HstSuite) LoadContainerTopology(topologyName string) { |
| 446 | data, err := os.ReadFile(containerTopologyDir + topologyName + ".yaml") |
| 447 | if err != nil { |
| 448 | Fail("read error: " + fmt.Sprint(err)) |
| 449 | } |
| 450 | var yamlTopo YamlTopology |
| 451 | err = yaml.Unmarshal(data, &yamlTopo) |
| 452 | if err != nil { |
| 453 | Fail("unmarshal error: " + fmt.Sprint(err)) |
| 454 | } |
| 455 | |
| 456 | for _, elem := range yamlTopo.Volumes { |
| 457 | volumeMap := elem["volume"].(VolumeConfig) |
| 458 | hostDir := volumeMap["host-dir"].(string) |
| 459 | workingVolumeDir := logDir + s.GetCurrentTestName() + volumeDir |
| 460 | volDirReplacer := strings.NewReplacer("$HST_VOLUME_DIR", workingVolumeDir) |
| 461 | hostDir = volDirReplacer.Replace(hostDir) |
| 462 | s.Volumes = append(s.Volumes, hostDir) |
| 463 | } |
| 464 | |
| 465 | s.Containers = make(map[string]*Container) |
| 466 | for _, elem := range yamlTopo.Containers { |
| 467 | newContainer, err := newContainer(s, elem) |
| 468 | newContainer.Suite = s |
| 469 | newContainer.Name = newContainer.Suite.ProcessIndex + newContainer.Name + newContainer.Suite.Ppid |
| 470 | if err != nil { |
| 471 | Fail("container config error: " + fmt.Sprint(err)) |
| 472 | } |
| 473 | s.Containers[newContainer.Name] = newContainer |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func (s *HstSuite) LoadNetworkTopology(topologyName string) { |
| 478 | data, err := os.ReadFile(networkTopologyDir + topologyName + ".yaml") |
| 479 | if err != nil { |
| 480 | Fail("read error: " + fmt.Sprint(err)) |
| 481 | } |
| 482 | var yamlTopo YamlTopology |
| 483 | err = yaml.Unmarshal(data, &yamlTopo) |
| 484 | if err != nil { |
| 485 | Fail("unmarshal error: " + fmt.Sprint(err)) |
| 486 | } |
| 487 | |
| 488 | s.Ip4AddrAllocator = NewIp4AddressAllocator() |
| 489 | s.NetInterfaces = make(map[string]*NetInterface) |
| 490 | |
| 491 | for _, elem := range yamlTopo.Devices { |
| 492 | if _, ok := elem["name"]; ok { |
| 493 | elem["name"] = s.ProcessIndex + elem["name"].(string) + s.Ppid |
| 494 | } |
| 495 | |
| 496 | if peer, ok := elem["peer"].(NetDevConfig); ok { |
| 497 | if peer["name"].(string) != "" { |
| 498 | peer["name"] = s.ProcessIndex + peer["name"].(string) + s.Ppid |
| 499 | } |
| 500 | if _, ok := peer["netns"]; ok { |
| 501 | peer["netns"] = s.ProcessIndex + peer["netns"].(string) + s.Ppid |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if _, ok := elem["netns"]; ok { |
| 506 | elem["netns"] = s.ProcessIndex + elem["netns"].(string) + s.Ppid |
| 507 | } |
| 508 | |
| 509 | if _, ok := elem["interfaces"]; ok { |
| 510 | interfaceCount := len(elem["interfaces"].([]interface{})) |
| 511 | for i := 0; i < interfaceCount; i++ { |
| 512 | elem["interfaces"].([]interface{})[i] = s.ProcessIndex + elem["interfaces"].([]interface{})[i].(string) + s.Ppid |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | switch elem["type"].(string) { |
| 517 | case NetNs: |
| 518 | { |
| 519 | if namespace, err := newNetNamespace(elem); err == nil { |
| 520 | s.NetConfigs = append(s.NetConfigs, &namespace) |
| 521 | } else { |
| 522 | Fail("network config error: " + fmt.Sprint(err)) |
| 523 | } |
| 524 | } |
| 525 | case Veth, Tap: |
| 526 | { |
| 527 | if netIf, err := newNetworkInterface(elem, s.Ip4AddrAllocator); err == nil { |
| 528 | s.NetConfigs = append(s.NetConfigs, netIf) |
| 529 | s.NetInterfaces[netIf.Name()] = netIf |
| 530 | } else { |
| 531 | Fail("network config error: " + fmt.Sprint(err)) |
| 532 | } |
| 533 | } |
| 534 | case Bridge: |
| 535 | { |
| 536 | if bridge, err := newBridge(elem); err == nil { |
| 537 | s.NetConfigs = append(s.NetConfigs, &bridge) |
| 538 | } else { |
| 539 | Fail("network config error: " + fmt.Sprint(err)) |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | func (s *HstSuite) ConfigureNetworkTopology(topologyName string) { |
| 547 | s.LoadNetworkTopology(topologyName) |
| 548 | |
| 549 | if *IsUnconfiguring { |
| 550 | return |
| 551 | } |
| 552 | |
| 553 | for _, nc := range s.NetConfigs { |
| 554 | s.Log(nc.Name()) |
| 555 | if err := nc.configure(); err != nil { |
| 556 | Fail("Network config error: " + fmt.Sprint(err)) |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | func (s *HstSuite) UnconfigureNetworkTopology() { |
| 562 | if *IsPersistent { |
| 563 | return |
| 564 | } |
| 565 | for _, nc := range s.NetConfigs { |
| 566 | nc.unconfigure() |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | func (s *HstSuite) GetTestId() string { |
| 571 | testName := s.GetCurrentTestName() |
| 572 | |
| 573 | if s.TestIds == nil { |
| 574 | s.TestIds = map[string]string{} |
| 575 | } |
| 576 | |
| 577 | if _, ok := s.TestIds[testName]; !ok { |
| 578 | s.TestIds[testName] = time.Now().Format("2006-01-02_15-04-05") |
| 579 | } |
| 580 | |
| 581 | return s.TestIds[testName] |
| 582 | } |
| 583 | |
| 584 | func (s *HstSuite) GetCurrentTestName() string { |
| 585 | return strings.Split(CurrentSpecReport().LeafNodeText, "/")[1] |
| 586 | } |
| 587 | |
| 588 | func (s *HstSuite) GetCurrentSuiteName() string { |
| 589 | return CurrentSpecReport().ContainerHierarchyTexts[0] |
| 590 | } |
| 591 | |
| 592 | // Returns last 3 digits of PID + Ginkgo process index as the 4th digit |
| 593 | func (s *HstSuite) GetPortFromPpid() string { |
| 594 | port := s.Ppid |
| 595 | for len(port) < 3 { |
| 596 | port += "0" |
| 597 | } |
| 598 | return port[len(port)-3:] + s.ProcessIndex |
| 599 | } |
| 600 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 601 | /* |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 602 | RunBenchmark creates Gomega's experiment with the passed-in name and samples the passed-in callback repeatedly (samplesNum times), |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 603 | passing in suite context, experiment and your data. |
| 604 | |
| 605 | You can also instruct runBenchmark to run with multiple concurrent workers. |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 606 | Note that if running in parallel Gomega returns from Sample when spins up all samples and does not wait until all finished. |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 607 | You can record multiple named measurements (float64 or duration) within passed-in callback. |
| 608 | runBenchmark then produces report to show statistical distribution of measurements. |
| 609 | */ |
| 610 | func (s *HstSuite) RunBenchmark(name string, samplesNum, parallelNum int, callback func(s *HstSuite, e *gmeasure.Experiment, data interface{}), data interface{}) { |
| 611 | experiment := gmeasure.NewExperiment(name) |
| 612 | |
| 613 | experiment.Sample(func(idx int) { |
| 614 | defer GinkgoRecover() |
| 615 | callback(s, experiment, data) |
| 616 | }, gmeasure.SamplingConfig{N: samplesNum, NumParallel: parallelNum}) |
| 617 | AddReportEntry(experiment.Name, experiment) |
| 618 | } |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 619 | |
| 620 | /* |
| 621 | LogHttpReq is Gomega's ghttp server handler which logs received HTTP request. |
| 622 | |
| 623 | You should put it at the first place, so request is logged always. |
| 624 | */ |
| 625 | func (s *HstSuite) LogHttpReq(body bool) http.HandlerFunc { |
| 626 | return func(w http.ResponseWriter, req *http.Request) { |
| 627 | dump, err := httputil.DumpRequest(req, body) |
| 628 | if err == nil { |
| 629 | s.Log("\n> Received request (" + req.RemoteAddr + "):\n" + |
| 630 | string(dump) + |
| 631 | "\n------------------------------\n") |
| 632 | } |
| 633 | } |
| 634 | } |