14 "github.com/edwarnicke/exechelper"
15 . "github.com/onsi/ginkgo/v2"
16 . "github.com/onsi/gomega"
21 DEFAULT_NETWORK_NUM int = 1
24 var isPersistent = flag.Bool("persist", false, "persists topology config")
25 var isVerbose = flag.Bool("verbose", false, "verbose test output")
26 var isUnconfiguring = flag.Bool("unconfigure", false, "remove topology")
27 var isVppDebug = flag.Bool("debug", false, "attach gdb to vpp")
28 var nConfiguredCpus = flag.Int("cpus", 1, "number of CPUs assigned to vpp")
29 var vppSourceFileDir = flag.String("vppsrc", "", "vpp source file directory")
31 type HstSuite struct {
32 containers map[string]*Container
34 netConfigs []NetConfig
35 netInterfaces map[string]*NetInterface
36 ip4AddrAllocator *Ip4AddressAllocator
37 testIds map[string]string
38 cpuAllocator *CpuAllocatorT
39 cpuContexts []*CpuContext
44 func (s *HstSuite) SetupSuite() {
46 s.pid = fmt.Sprint(os.Getpid())
47 s.cpuAllocator, err = CpuAllocator()
49 Fail("failed to init cpu allocator: " + fmt.Sprint(err))
51 s.cpuPerVpp = *nConfiguredCpus
54 func (s *HstSuite) AllocateCpus() []int {
55 cpuCtx, err := s.cpuAllocator.Allocate(s.cpuPerVpp)
57 s.AddCpuContext(cpuCtx)
61 func (s *HstSuite) AddCpuContext(cpuCtx *CpuContext) {
62 s.cpuContexts = append(s.cpuContexts, cpuCtx)
65 func (s *HstSuite) TearDownSuite() {
66 s.unconfigureNetworkTopology()
69 func (s *HstSuite) TearDownTest() {
73 for _, c := range s.cpuContexts {
78 s.ip4AddrAllocator.deleteIpAddresses()
81 func (s *HstSuite) skipIfUnconfiguring() {
83 s.skip("skipping to unconfigure")
87 func (s *HstSuite) SetupTest() {
88 RegisterFailHandler(func(message string, callerSkip ...int) {
90 Fail(message, callerSkip...)
92 s.skipIfUnconfiguring()
97 func (s *HstSuite) setupVolumes() {
98 for _, volume := range s.volumes {
99 cmd := "docker volume create --name=" + volume
105 func (s *HstSuite) setupContainers() {
106 for _, container := range s.containers {
107 if !container.isOptional {
113 func logVppInstance(container *Container, maxLines int) {
114 if container.vppInstance == nil {
118 logSource := container.getHostWorkDir() + defaultLogFilePath
119 file, err := os.Open(logSource)
126 scanner := bufio.NewScanner(file)
131 lines = append(lines, scanner.Text())
133 if counter > maxLines {
139 fmt.Println("vvvvvvvvvvvvvvv " + container.name + " [VPP instance]:")
140 for _, line := range lines {
143 fmt.Printf("^^^^^^^^^^^^^^^\n\n")
146 func (s *HstSuite) hstFail() {
147 fmt.Println("Containers: " + fmt.Sprint(s.containers))
148 for _, container := range s.containers {
149 out, err := container.log(20)
151 fmt.Printf("An error occured while obtaining '%s' container logs: %s\n", container.name, fmt.Sprint(err))
154 fmt.Printf("\nvvvvvvvvvvvvvvv " +
155 container.name + ":\n" +
157 "^^^^^^^^^^^^^^^\n\n")
158 logVppInstance(container, 20)
162 func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) {
163 Expect(object).To(BeNil(), msgAndArgs...)
166 func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) {
167 Expect(object).ToNot(BeNil(), msgAndArgs...)
170 func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
171 Expect(actual).To(Equal(expected), msgAndArgs...)
174 func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
175 Expect(actual).ToNot(Equal(expected), msgAndArgs...)
178 func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) {
179 Expect(testString).To(ContainSubstring(fmt.Sprint(contains)), msgAndArgs...)
182 func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
183 Expect(testString).ToNot(ContainSubstring(fmt.Sprint(contains)), msgAndArgs...)
186 func (s *HstSuite) assertNotEmpty(object interface{}, msgAndArgs ...interface{}) {
187 Expect(object).ToNot(BeEmpty(), msgAndArgs...)
190 func (s *HstSuite) log(arg any) {
192 slog.Info(fmt.Sprint(arg))
196 func (s *HstSuite) skip(args string) {
200 func (s *HstSuite) SkipIfMultiWorker(args ...any) {
201 if *nConfiguredCpus > 1 {
202 s.skip("test case not supported with multiple vpp workers")
206 func (s *HstSuite) SkipUnlessExtendedTestsBuilt() {
207 imageName := "hs-test/nginx-http3"
209 cmd := exec.Command("docker", "images", imageName)
210 byteOutput, err := cmd.CombinedOutput()
212 s.log("error while searching for docker image")
215 if !strings.Contains(string(byteOutput), imageName) {
216 s.skip("extended tests not built")
220 func (s *HstSuite) resetContainers() {
221 for _, container := range s.containers {
226 func (s *HstSuite) removeVolumes() {
227 for _, volumeName := range s.volumes {
228 cmd := "docker volume rm " + volumeName
230 os.RemoveAll(volumeName)
234 func (s *HstSuite) getNetNamespaceByName(name string) string {
238 func (s *HstSuite) getInterfaceByName(name string) *NetInterface {
239 return s.netInterfaces[name+s.pid]
242 func (s *HstSuite) getContainerByName(name string) *Container {
243 return s.containers[name+s.pid]
247 * Create a copy and return its address, so that individial tests which call this
248 * are not able to modify the original container and affect other tests by doing that
250 func (s *HstSuite) getTransientContainerByName(name string) *Container {
251 containerCopy := *s.containers[name+s.pid]
252 return &containerCopy
255 func (s *HstSuite) loadContainerTopology(topologyName string) {
256 data, err := os.ReadFile(containerTopologyDir + topologyName + ".yaml")
258 Fail("read error: " + fmt.Sprint(err))
260 var yamlTopo YamlTopology
261 err = yaml.Unmarshal(data, &yamlTopo)
263 Fail("unmarshal error: " + fmt.Sprint(err))
266 for _, elem := range yamlTopo.Volumes {
267 volumeMap := elem["volume"].(VolumeConfig)
268 hostDir := volumeMap["host-dir"].(string)
269 workingVolumeDir := logDir + CurrentSpecReport().LeafNodeText + s.pid + volumeDir
270 volDirReplacer := strings.NewReplacer("$HST_VOLUME_DIR", workingVolumeDir)
271 hostDir = volDirReplacer.Replace(hostDir)
272 s.volumes = append(s.volumes, hostDir)
275 s.containers = make(map[string]*Container)
276 for _, elem := range yamlTopo.Containers {
277 newContainer, err := newContainer(s, elem)
278 newContainer.suite = s
279 newContainer.name += newContainer.suite.pid
281 Fail("container config error: " + fmt.Sprint(err))
283 s.containers[newContainer.name] = newContainer
287 func (s *HstSuite) loadNetworkTopology(topologyName string) {
288 data, err := os.ReadFile(networkTopologyDir + topologyName + ".yaml")
290 Fail("read error: " + fmt.Sprint(err))
292 var yamlTopo YamlTopology
293 err = yaml.Unmarshal(data, &yamlTopo)
295 Fail("unmarshal error: " + fmt.Sprint(err))
298 s.ip4AddrAllocator = NewIp4AddressAllocator()
299 s.netInterfaces = make(map[string]*NetInterface)
301 for _, elem := range yamlTopo.Devices {
302 if _, ok := elem["name"]; ok {
303 elem["name"] = elem["name"].(string) + s.pid
306 if peer, ok := elem["peer"].(NetDevConfig); ok {
307 if peer["name"].(string) != "" {
308 peer["name"] = peer["name"].(string) + s.pid
310 if _, ok := peer["netns"]; ok {
311 peer["netns"] = peer["netns"].(string) + s.pid
315 if _, ok := elem["netns"]; ok {
316 elem["netns"] = elem["netns"].(string) + s.pid
319 if _, ok := elem["interfaces"]; ok {
320 interfaceCount := len(elem["interfaces"].([]interface{}))
321 for i := 0; i < interfaceCount; i++ {
322 elem["interfaces"].([]interface{})[i] = elem["interfaces"].([]interface{})[i].(string) + s.pid
326 switch elem["type"].(string) {
329 if namespace, err := newNetNamespace(elem); err == nil {
330 s.netConfigs = append(s.netConfigs, &namespace)
332 Fail("network config error: " + fmt.Sprint(err))
337 if netIf, err := newNetworkInterface(elem, s.ip4AddrAllocator); err == nil {
338 s.netConfigs = append(s.netConfigs, netIf)
339 s.netInterfaces[netIf.Name()] = netIf
341 Fail("network config error: " + fmt.Sprint(err))
346 if bridge, err := newBridge(elem); err == nil {
347 s.netConfigs = append(s.netConfigs, &bridge)
349 Fail("network config error: " + fmt.Sprint(err))
356 func (s *HstSuite) configureNetworkTopology(topologyName string) {
357 s.loadNetworkTopology(topologyName)
359 if *isUnconfiguring {
363 for _, nc := range s.netConfigs {
364 if err := nc.configure(); err != nil {
365 Fail("Network config error: " + fmt.Sprint(err))
370 func (s *HstSuite) unconfigureNetworkTopology() {
374 for _, nc := range s.netConfigs {
379 func (s *HstSuite) getTestId() string {
380 testName := CurrentSpecReport().LeafNodeText
382 if s.testIds == nil {
383 s.testIds = map[string]string{}
386 if _, ok := s.testIds[testName]; !ok {
387 s.testIds[testName] = time.Now().Format("2006-01-02_15-04-05")
390 return s.testIds[testName]
393 // Returns last 4 digits of PID
394 func (s *HstSuite) getPortFromPid() string {
399 return port[len(port)-4:]
402 func (s *HstSuite) startServerApp(running chan error, done chan struct{}, env []string) {
403 cmd := exec.Command("iperf3", "-4", "-s", "-p", s.getPortFromPid())
410 msg := fmt.Errorf("failed to start iperf server: %v", err)
419 func (s *HstSuite) startClientApp(ipAddress string, env []string, clnCh chan error, clnRes chan string) {
427 cmd := exec.Command("iperf3", "-c", ipAddress, "-u", "-l", "1460", "-b", "10g", "-p", s.getPortFromPid())
432 o, err := cmd.CombinedOutput()
435 clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
438 time.Sleep(1 * time.Second)
442 clnRes <- fmt.Sprintf("Client output: %s", o)
448 func (s *HstSuite) startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
449 cmd := newCommand([]string{"./http_server", addressPort, s.pid}, netNs)
453 fmt.Println("Failed to start http server: " + fmt.Sprint(err))
456 running <- struct{}{}
461 func (s *HstSuite) startWget(finished chan error, server_ip, port, query, netNs string) {
463 finished <- errors.New("wget error")
466 cmd := newCommand([]string{"wget", "--timeout=10", "--no-proxy", "--tries=5", "-O", "/dev/null", server_ip + ":" + port + "/" + query},
469 o, err := cmd.CombinedOutput()
471 finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o)
473 } else if !strings.Contains(string(o), "200 OK") {
474 finished <- fmt.Errorf("wget error: response not 200 OK")