Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Filip Tehlar | 671cf51 | 2023-01-31 10:34:18 +0100 | [diff] [blame] | 4 | "flag" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 5 | "io/ioutil" |
| 6 | "os" |
Filip Tehlar | 31eaea9 | 2023-06-15 10:06:57 +0200 | [diff] [blame] | 7 | "os/exec" |
| 8 | "strings" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 9 | "time" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 10 | |
| 11 | "github.com/edwarnicke/exechelper" |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | "github.com/stretchr/testify/suite" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 14 | "gopkg.in/yaml.v3" |
| 15 | ) |
| 16 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 17 | const ( |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 18 | DEFAULT_NETWORK_NUM int = 1 |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 19 | ) |
| 20 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 21 | var isPersistent = flag.Bool("persist", false, "persists topology config") |
| 22 | var isVerbose = flag.Bool("verbose", false, "verbose test output") |
| 23 | var isUnconfiguring = flag.Bool("unconfigure", false, "remove topology") |
| 24 | var isVppDebug = flag.Bool("debug", false, "attach gdb to vpp") |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 25 | var nConfiguredCpus = flag.Int("cpus", 1, "number of CPUs assigned to vpp") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 26 | |
| 27 | type HstSuite struct { |
| 28 | suite.Suite |
Filip Tehlar | 3a910ab | 2023-06-08 17:39:39 +0200 | [diff] [blame] | 29 | containers map[string]*Container |
| 30 | volumes []string |
| 31 | netConfigs []NetConfig |
| 32 | netInterfaces map[string]*NetInterface |
| 33 | ip4AddrAllocator *Ip4AddressAllocator |
| 34 | testIds map[string]string |
| 35 | cpuAllocator *CpuAllocatorT |
| 36 | cpuContexts []*CpuContext |
| 37 | cpuPerVpp int |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | func (s *HstSuite) SetupSuite() { |
| 41 | var err error |
| 42 | s.cpuAllocator, err = CpuAllocator() |
| 43 | if err != nil { |
| 44 | s.FailNow("failed to init cpu allocator: %v", err) |
| 45 | } |
| 46 | s.cpuPerVpp = *nConfiguredCpus |
| 47 | } |
| 48 | |
| 49 | func (s *HstSuite) AllocateCpus() []int { |
| 50 | cpuCtx, err := s.cpuAllocator.Allocate(s.cpuPerVpp) |
| 51 | s.assertNil(err) |
| 52 | s.AddCpuContext(cpuCtx) |
| 53 | return cpuCtx.cpus |
| 54 | } |
| 55 | |
| 56 | func (s *HstSuite) AddCpuContext(cpuCtx *CpuContext) { |
| 57 | s.cpuContexts = append(s.cpuContexts, cpuCtx) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func (s *HstSuite) TearDownSuite() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 61 | s.unconfigureNetworkTopology() |
| 62 | } |
| 63 | |
| 64 | func (s *HstSuite) TearDownTest() { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 65 | if *isPersistent { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 66 | return |
| 67 | } |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 68 | for _, c := range s.cpuContexts { |
| 69 | c.Release() |
| 70 | } |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 71 | s.resetContainers() |
| 72 | s.removeVolumes() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 75 | func (s *HstSuite) skipIfUnconfiguring() { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 76 | if *isUnconfiguring { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 77 | s.skip("skipping to unconfigure") |
| 78 | } |
| 79 | } |
| 80 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 81 | func (s *HstSuite) SetupTest() { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 82 | s.skipIfUnconfiguring() |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 83 | s.setupVolumes() |
| 84 | s.setupContainers() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 87 | func (s *HstSuite) setupVolumes() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 88 | for _, volume := range s.volumes { |
| 89 | cmd := "docker volume create --name=" + volume |
| 90 | s.log(cmd) |
| 91 | exechelper.Run(cmd) |
| 92 | } |
| 93 | } |
| 94 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 95 | func (s *HstSuite) setupContainers() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 96 | for _, container := range s.containers { |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 97 | if !container.isOptional { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 98 | container.run() |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func (s *HstSuite) hstFail() { |
| 104 | s.T().FailNow() |
| 105 | } |
| 106 | |
| 107 | func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) { |
| 108 | if !assert.Nil(s.T(), object, msgAndArgs...) { |
| 109 | s.hstFail() |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) { |
| 114 | if !assert.NotNil(s.T(), object, msgAndArgs...) { |
| 115 | s.hstFail() |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 120 | if !assert.Equal(s.T(), expected, actual, msgAndArgs...) { |
| 121 | s.hstFail() |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 126 | if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) { |
| 127 | s.hstFail() |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 132 | if !assert.Contains(s.T(), testString, contains, msgAndArgs...) { |
| 133 | s.hstFail() |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 138 | if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) { |
| 139 | s.hstFail() |
| 140 | } |
| 141 | } |
| 142 | |
Maros Ondrejicka | 2ddb2fd | 2023-02-15 17:44:46 +0100 | [diff] [blame] | 143 | func (s *HstSuite) assertNotEmpty(object interface{}, msgAndArgs ...interface{}) { |
| 144 | if !assert.NotEmpty(s.T(), object, msgAndArgs...) { |
| 145 | s.hstFail() |
| 146 | } |
| 147 | } |
| 148 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 149 | func (s *HstSuite) log(args ...any) { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 150 | if *isVerbose { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 151 | s.T().Helper() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 152 | s.T().Log(args...) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func (s *HstSuite) skip(args ...any) { |
| 157 | s.log(args...) |
| 158 | s.T().SkipNow() |
| 159 | } |
| 160 | |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 161 | func (s *HstSuite) SkipIfMultiWorker(args ...any) { |
| 162 | if *nConfiguredCpus > 1 { |
| 163 | s.skip("test case not supported with multiple vpp workers") |
| 164 | } |
| 165 | } |
| 166 | |
Filip Tehlar | 31eaea9 | 2023-06-15 10:06:57 +0200 | [diff] [blame] | 167 | func (s *HstSuite) SkipUnlessExtendedTestsBuilt() { |
| 168 | imageName := "hs-test/nginx-http3" |
| 169 | |
| 170 | cmd := exec.Command("docker", "images", imageName) |
| 171 | byteOutput, err := cmd.CombinedOutput() |
| 172 | if err != nil { |
| 173 | s.log("error while searching for docker image") |
| 174 | return |
| 175 | } |
| 176 | if !strings.Contains(string(byteOutput), imageName) { |
| 177 | s.skip("extended tests not built") |
| 178 | } |
| 179 | } |
| 180 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 181 | func (s *HstSuite) resetContainers() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 182 | for _, container := range s.containers { |
| 183 | container.stop() |
| 184 | } |
| 185 | } |
| 186 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 187 | func (s *HstSuite) removeVolumes() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 188 | for _, volumeName := range s.volumes { |
| 189 | cmd := "docker volume rm " + volumeName |
| 190 | exechelper.Run(cmd) |
| 191 | os.RemoveAll(volumeName) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func (s *HstSuite) getContainerByName(name string) *Container { |
| 196 | return s.containers[name] |
| 197 | } |
| 198 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 199 | /* |
| 200 | * Create a copy and return its address, so that individial tests which call this |
| 201 | * are not able to modify the original container and affect other tests by doing that |
| 202 | */ |
| 203 | func (s *HstSuite) getTransientContainerByName(name string) *Container { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 204 | containerCopy := *s.containers[name] |
| 205 | return &containerCopy |
| 206 | } |
| 207 | |
| 208 | func (s *HstSuite) loadContainerTopology(topologyName string) { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 209 | data, err := ioutil.ReadFile(containerTopologyDir + topologyName + ".yaml") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 210 | if err != nil { |
| 211 | s.T().Fatalf("read error: %v", err) |
| 212 | } |
| 213 | var yamlTopo YamlTopology |
| 214 | err = yaml.Unmarshal(data, &yamlTopo) |
| 215 | if err != nil { |
| 216 | s.T().Fatalf("unmarshal error: %v", err) |
| 217 | } |
| 218 | |
| 219 | for _, elem := range yamlTopo.Volumes { |
| 220 | volumeMap := elem["volume"].(VolumeConfig) |
| 221 | hostDir := volumeMap["host-dir"].(string) |
| 222 | s.volumes = append(s.volumes, hostDir) |
| 223 | } |
| 224 | |
| 225 | s.containers = make(map[string]*Container) |
| 226 | for _, elem := range yamlTopo.Containers { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 227 | newContainer, err := newContainer(elem) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 228 | newContainer.suite = s |
| 229 | if err != nil { |
| 230 | s.T().Fatalf("container config error: %v", err) |
| 231 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 232 | s.containers[newContainer.name] = newContainer |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func (s *HstSuite) loadNetworkTopology(topologyName string) { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 237 | data, err := ioutil.ReadFile(networkTopologyDir + topologyName + ".yaml") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 238 | if err != nil { |
| 239 | s.T().Fatalf("read error: %v", err) |
| 240 | } |
| 241 | var yamlTopo YamlTopology |
| 242 | err = yaml.Unmarshal(data, &yamlTopo) |
| 243 | if err != nil { |
| 244 | s.T().Fatalf("unmarshal error: %v", err) |
| 245 | } |
| 246 | |
Filip Tehlar | 3a910ab | 2023-06-08 17:39:39 +0200 | [diff] [blame] | 247 | s.ip4AddrAllocator = NewIp4AddressAllocator() |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 248 | s.netInterfaces = make(map[string]*NetInterface) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 249 | for _, elem := range yamlTopo.Devices { |
| 250 | switch elem["type"].(string) { |
| 251 | case NetNs: |
| 252 | { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 253 | if namespace, err := newNetNamespace(elem); err == nil { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 254 | s.netConfigs = append(s.netConfigs, &namespace) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 255 | } else { |
| 256 | s.T().Fatalf("network config error: %v", err) |
| 257 | } |
| 258 | } |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 259 | case Veth, Tap: |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 260 | { |
Filip Tehlar | 3a910ab | 2023-06-08 17:39:39 +0200 | [diff] [blame] | 261 | if netIf, err := newNetworkInterface(elem, s.ip4AddrAllocator); err == nil { |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 262 | s.netConfigs = append(s.netConfigs, netIf) |
| 263 | s.netInterfaces[netIf.Name()] = netIf |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 264 | } else { |
| 265 | s.T().Fatalf("network config error: %v", err) |
| 266 | } |
| 267 | } |
| 268 | case Bridge: |
| 269 | { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 270 | if bridge, err := newBridge(elem); err == nil { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 271 | s.netConfigs = append(s.netConfigs, &bridge) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 272 | } else { |
| 273 | s.T().Fatalf("network config error: %v", err) |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func (s *HstSuite) configureNetworkTopology(topologyName string) { |
| 281 | s.loadNetworkTopology(topologyName) |
| 282 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 283 | if *isUnconfiguring { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 284 | return |
| 285 | } |
| 286 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 287 | for _, nc := range s.netConfigs { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 288 | if err := nc.configure(); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 289 | s.T().Fatalf("network config error: %v", err) |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | func (s *HstSuite) unconfigureNetworkTopology() { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 295 | if *isPersistent { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 296 | return |
| 297 | } |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 298 | for _, nc := range s.netConfigs { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 299 | nc.unconfigure() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 303 | func (s *HstSuite) getTestId() string { |
| 304 | testName := s.T().Name() |
| 305 | |
| 306 | if s.testIds == nil { |
| 307 | s.testIds = map[string]string{} |
| 308 | } |
| 309 | |
| 310 | if _, ok := s.testIds[testName]; !ok { |
Filip Tehlar | 75776f0 | 2023-03-24 13:47:45 +0100 | [diff] [blame] | 311 | s.testIds[testName] = time.Now().Format("2006-01-02_15-04-05") |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | return s.testIds[testName] |
| 315 | } |