Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 4 | "fmt" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 5 | "testing" |
| 6 | "time" |
| 7 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 8 | "github.com/edwarnicke/exechelper" |
| 9 | "github.com/stretchr/testify/assert" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 10 | "github.com/stretchr/testify/suite" |
| 11 | ) |
| 12 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 13 | type HstSuite struct { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 14 | suite.Suite |
| 15 | teardownSuite func() |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 16 | containers []string |
| 17 | volumes []string |
| 18 | } |
| 19 | |
| 20 | func (s *HstSuite) TearDownSuite() { |
| 21 | s.teardownSuite() |
| 22 | s.StopContainers() |
| 23 | s.RemoveVolumes() |
| 24 | } |
| 25 | |
| 26 | func (s *HstSuite) hstFail() { |
| 27 | s.T().FailNow() |
| 28 | } |
| 29 | |
| 30 | func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) { |
| 31 | if !assert.Nil(s.T(), object, msgAndArgs...) { |
| 32 | s.hstFail() |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) { |
| 37 | if !assert.NotNil(s.T(), object, msgAndArgs...) { |
| 38 | s.hstFail() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 43 | if !assert.Equal(s.T(), expected, actual, msgAndArgs...) { |
| 44 | s.hstFail() |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 49 | if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) { |
| 50 | s.hstFail() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func (s *HstSuite) NewContainer(name string) (*Container, error) { |
| 55 | if name == "" { |
| 56 | return nil, fmt.Errorf("creating container failed: name must not be blank") |
| 57 | } |
| 58 | |
| 59 | s.containers = append(s.containers, name) |
| 60 | |
| 61 | container := new(Container) |
| 62 | container.name = name |
| 63 | return container, nil |
| 64 | } |
| 65 | |
| 66 | func (s *HstSuite) StopContainers() { |
| 67 | for _, containerName := range s.containers { |
| 68 | exechelper.Run("docker stop " + containerName) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func (s *HstSuite) RemoveVolumes() { |
| 73 | for _, volumeName := range s.volumes { |
| 74 | exechelper.Run("docker volume rm " + volumeName) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | type TapSuite struct { |
| 79 | HstSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func (s *TapSuite) SetupSuite() { |
| 83 | time.Sleep(1 * time.Second) |
| 84 | s.teardownSuite = setupSuite(&s.Suite, "tap") |
| 85 | } |
| 86 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 87 | type VethsSuite struct { |
| 88 | HstSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 91 | func (s *VethsSuite) SetupSuite() { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 92 | time.Sleep(1 * time.Second) |
| 93 | s.teardownSuite = setupSuite(&s.Suite, "2peerVeth") |
| 94 | } |
| 95 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 96 | type NsSuite struct { |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 97 | HstSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | func (s *NsSuite) SetupSuite() { |
| 101 | s.teardownSuite = setupSuite(&s.Suite, "ns") |
| 102 | } |
| 103 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 104 | func setupSuite(s *suite.Suite, topologyName string) func() { |
| 105 | t := s.T() |
| 106 | topology, err := LoadTopology(TopologyDir, topologyName) |
| 107 | if err != nil { |
| 108 | t.Fatalf("error on loading topology '%s': %v", topologyName, err) |
| 109 | } |
| 110 | err = topology.Configure() |
| 111 | if err != nil { |
| 112 | t.Fatalf("failed to configure %s: %v", topologyName, err) |
| 113 | } |
| 114 | |
| 115 | t.Logf("topo %s loaded", topologyName) |
| 116 | return func() { |
| 117 | topology.Unconfigure() |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestTapSuite(t *testing.T) { |
| 122 | var m TapSuite |
| 123 | suite.Run(t, &m) |
| 124 | } |
| 125 | |
| 126 | func TestNs(t *testing.T) { |
| 127 | var m NsSuite |
| 128 | suite.Run(t, &m) |
| 129 | } |
| 130 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 131 | func TestVeths(t *testing.T) { |
| 132 | var m VethsSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 133 | suite.Run(t, &m) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 134 | } |