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