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 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "os" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 8 | "time" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 9 | |
| 10 | "github.com/edwarnicke/exechelper" |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | "github.com/stretchr/testify/suite" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 13 | "gopkg.in/yaml.v3" |
| 14 | ) |
| 15 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 16 | const ( |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 17 | defaultNetworkNumber int = 1 |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 18 | ) |
| 19 | |
Filip Tehlar | 671cf51 | 2023-01-31 10:34:18 +0100 | [diff] [blame] | 20 | var IsPersistent = flag.Bool("persist", false, "persists topology config") |
| 21 | var IsVerbose = flag.Bool("verbose", false, "verbose test output") |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 22 | var IsUnconfiguring = flag.Bool("unconfigure", false, "remove topology") |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame^] | 23 | var IsVppDebug = flag.Bool("debug", false, "attach gdb to vpp") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 24 | |
| 25 | type HstSuite struct { |
| 26 | suite.Suite |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 27 | containers map[string]*Container |
| 28 | volumes []string |
| 29 | netConfigs []NetConfig |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 30 | netInterfaces map[string]*NetInterface |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 31 | addresser *Addresser |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 32 | testIds map[string]string |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | func (s *HstSuite) TearDownSuite() { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 36 | s.unconfigureNetworkTopology() |
| 37 | } |
| 38 | |
| 39 | func (s *HstSuite) TearDownTest() { |
Filip Tehlar | 671cf51 | 2023-01-31 10:34:18 +0100 | [diff] [blame] | 40 | if *IsPersistent { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 41 | return |
| 42 | } |
| 43 | s.ResetContainers() |
| 44 | s.RemoveVolumes() |
| 45 | } |
| 46 | |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 47 | func (s *HstSuite) skipIfUnconfiguring() { |
| 48 | if *IsUnconfiguring { |
| 49 | s.skip("skipping to unconfigure") |
| 50 | } |
| 51 | } |
| 52 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 53 | func (s *HstSuite) SetupTest() { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 54 | s.skipIfUnconfiguring() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 55 | s.SetupVolumes() |
| 56 | s.SetupContainers() |
| 57 | } |
| 58 | |
| 59 | func (s *HstSuite) SetupVolumes() { |
| 60 | for _, volume := range s.volumes { |
| 61 | cmd := "docker volume create --name=" + volume |
| 62 | s.log(cmd) |
| 63 | exechelper.Run(cmd) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (s *HstSuite) SetupContainers() { |
| 68 | for _, container := range s.containers { |
| 69 | if container.isOptional == false { |
| 70 | container.run() |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func (s *HstSuite) hstFail() { |
| 76 | s.T().FailNow() |
| 77 | } |
| 78 | |
| 79 | func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) { |
| 80 | if !assert.Nil(s.T(), object, msgAndArgs...) { |
| 81 | s.hstFail() |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) { |
| 86 | if !assert.NotNil(s.T(), object, msgAndArgs...) { |
| 87 | s.hstFail() |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 92 | if !assert.Equal(s.T(), expected, actual, msgAndArgs...) { |
| 93 | s.hstFail() |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 98 | if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) { |
| 99 | s.hstFail() |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 104 | if !assert.Contains(s.T(), testString, contains, msgAndArgs...) { |
| 105 | s.hstFail() |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 110 | if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) { |
| 111 | s.hstFail() |
| 112 | } |
| 113 | } |
| 114 | |
Maros Ondrejicka | 2ddb2fd | 2023-02-15 17:44:46 +0100 | [diff] [blame] | 115 | func (s *HstSuite) assertNotEmpty(object interface{}, msgAndArgs ...interface{}) { |
| 116 | if !assert.NotEmpty(s.T(), object, msgAndArgs...) { |
| 117 | s.hstFail() |
| 118 | } |
| 119 | } |
| 120 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 121 | func (s *HstSuite) log(args ...any) { |
Filip Tehlar | 671cf51 | 2023-01-31 10:34:18 +0100 | [diff] [blame] | 122 | if *IsVerbose { |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 123 | s.T().Helper() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 124 | s.T().Log(args...) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func (s *HstSuite) skip(args ...any) { |
| 129 | s.log(args...) |
| 130 | s.T().SkipNow() |
| 131 | } |
| 132 | |
| 133 | func (s *HstSuite) ResetContainers() { |
| 134 | for _, container := range s.containers { |
| 135 | container.stop() |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func (s *HstSuite) RemoveVolumes() { |
| 140 | for _, volumeName := range s.volumes { |
| 141 | cmd := "docker volume rm " + volumeName |
| 142 | exechelper.Run(cmd) |
| 143 | os.RemoveAll(volumeName) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func (s *HstSuite) getContainerByName(name string) *Container { |
| 148 | return s.containers[name] |
| 149 | } |
| 150 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 151 | /* |
| 152 | * Create a copy and return its address, so that individial tests which call this |
| 153 | * are not able to modify the original container and affect other tests by doing that |
| 154 | */ |
| 155 | func (s *HstSuite) getTransientContainerByName(name string) *Container { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 156 | containerCopy := *s.containers[name] |
| 157 | return &containerCopy |
| 158 | } |
| 159 | |
| 160 | func (s *HstSuite) loadContainerTopology(topologyName string) { |
| 161 | data, err := ioutil.ReadFile(ContainerTopologyDir + topologyName + ".yaml") |
| 162 | if err != nil { |
| 163 | s.T().Fatalf("read error: %v", err) |
| 164 | } |
| 165 | var yamlTopo YamlTopology |
| 166 | err = yaml.Unmarshal(data, &yamlTopo) |
| 167 | if err != nil { |
| 168 | s.T().Fatalf("unmarshal error: %v", err) |
| 169 | } |
| 170 | |
| 171 | for _, elem := range yamlTopo.Volumes { |
| 172 | volumeMap := elem["volume"].(VolumeConfig) |
| 173 | hostDir := volumeMap["host-dir"].(string) |
| 174 | s.volumes = append(s.volumes, hostDir) |
| 175 | } |
| 176 | |
| 177 | s.containers = make(map[string]*Container) |
| 178 | for _, elem := range yamlTopo.Containers { |
| 179 | newContainer, err := NewContainer(elem) |
| 180 | newContainer.suite = s |
| 181 | if err != nil { |
| 182 | s.T().Fatalf("container config error: %v", err) |
| 183 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 184 | s.containers[newContainer.name] = newContainer |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func (s *HstSuite) loadNetworkTopology(topologyName string) { |
| 189 | data, err := ioutil.ReadFile(NetworkTopologyDir + topologyName + ".yaml") |
| 190 | if err != nil { |
| 191 | s.T().Fatalf("read error: %v", err) |
| 192 | } |
| 193 | var yamlTopo YamlTopology |
| 194 | err = yaml.Unmarshal(data, &yamlTopo) |
| 195 | if err != nil { |
| 196 | s.T().Fatalf("unmarshal error: %v", err) |
| 197 | } |
| 198 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 199 | s.addresser = NewAddresser(s) |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 200 | s.netInterfaces = make(map[string]*NetInterface) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 201 | for _, elem := range yamlTopo.Devices { |
| 202 | switch elem["type"].(string) { |
| 203 | case NetNs: |
| 204 | { |
| 205 | if namespace, err := NewNetNamespace(elem); err == nil { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 206 | s.netConfigs = append(s.netConfigs, &namespace) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 207 | } else { |
| 208 | s.T().Fatalf("network config error: %v", err) |
| 209 | } |
| 210 | } |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 211 | case Veth, Tap: |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 212 | { |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 213 | if netIf, err := NewNetworkInterface(elem, s.addresser); err == nil { |
| 214 | s.netConfigs = append(s.netConfigs, netIf) |
| 215 | s.netInterfaces[netIf.Name()] = netIf |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 216 | } else { |
| 217 | s.T().Fatalf("network config error: %v", err) |
| 218 | } |
| 219 | } |
| 220 | case Bridge: |
| 221 | { |
| 222 | if bridge, err := NewBridge(elem); err == nil { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 223 | s.netConfigs = append(s.netConfigs, &bridge) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 224 | } else { |
| 225 | s.T().Fatalf("network config error: %v", err) |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func (s *HstSuite) configureNetworkTopology(topologyName string) { |
| 233 | s.loadNetworkTopology(topologyName) |
| 234 | |
Maros Ondrejicka | af004dd | 2023-02-27 16:52:57 +0100 | [diff] [blame] | 235 | if *IsUnconfiguring { |
| 236 | return |
| 237 | } |
| 238 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 239 | for _, nc := range s.netConfigs { |
| 240 | if err := nc.Configure(); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 241 | s.T().Fatalf("network config error: %v", err) |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func (s *HstSuite) unconfigureNetworkTopology() { |
Filip Tehlar | 671cf51 | 2023-01-31 10:34:18 +0100 | [diff] [blame] | 247 | if *IsPersistent { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 248 | return |
| 249 | } |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 250 | for _, nc := range s.netConfigs { |
| 251 | nc.Unconfigure() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 255 | func (s *HstSuite) getTestId() string { |
| 256 | testName := s.T().Name() |
| 257 | |
| 258 | if s.testIds == nil { |
| 259 | s.testIds = map[string]string{} |
| 260 | } |
| 261 | |
| 262 | if _, ok := s.testIds[testName]; !ok { |
| 263 | s.testIds[testName] = time.Now().Format(time.RFC3339) |
| 264 | } |
| 265 | |
| 266 | return s.testIds[testName] |
| 267 | } |
| 268 | |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 269 | type AddressCounter = int |
| 270 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 271 | type Addresser struct { |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 272 | networks map[int]AddressCounter |
| 273 | suite *HstSuite |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 276 | func (a *Addresser) AddNetwork(networkNumber int) { |
| 277 | a.networks[networkNumber] = 1 |
| 278 | } |
| 279 | |
| 280 | func (a *Addresser) NewIp4Address(inputNetworkNumber ...int) (string, error) { |
| 281 | var networkNumber int = 0 |
| 282 | if len(inputNetworkNumber) > 0 { |
| 283 | networkNumber = inputNetworkNumber[0] |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 286 | if _, ok := a.networks[networkNumber]; !ok { |
| 287 | a.AddNetwork(networkNumber) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 288 | } |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 289 | |
| 290 | numberOfAddresses := a.networks[networkNumber] |
| 291 | |
| 292 | if numberOfAddresses == 254 { |
| 293 | return "", fmt.Errorf("no available IPv4 addresses") |
| 294 | } |
| 295 | |
| 296 | address := fmt.Sprintf("10.10.%v.%v/24", networkNumber, numberOfAddresses) |
| 297 | a.networks[networkNumber] = numberOfAddresses + 1 |
| 298 | |
| 299 | return address, nil |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 300 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 301 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 302 | func NewAddresser(suite *HstSuite) *Addresser { |
| 303 | var addresser = new(Addresser) |
| 304 | addresser.suite = suite |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 305 | addresser.networks = make(map[int]AddressCounter) |
| 306 | addresser.AddNetwork(0) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 307 | return addresser |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 308 | } |