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 | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 16 | containers []*Container |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 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 | |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 48 | func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 49 | if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) { |
| 50 | s.hstFail() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 55 | if !assert.Contains(s.T(), testString, contains, msgAndArgs...) { |
| 56 | s.hstFail() |
| 57 | } |
| 58 | } |
| 59 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 60 | func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 61 | if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) { |
| 62 | s.hstFail() |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func (s *HstSuite) NewContainer(name string) (*Container, error) { |
| 67 | if name == "" { |
| 68 | return nil, fmt.Errorf("creating container failed: name must not be blank") |
| 69 | } |
| 70 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 71 | container := new(Container) |
| 72 | container.name = name |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 73 | |
| 74 | s.containers = append(s.containers, container) |
| 75 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 76 | return container, nil |
| 77 | } |
| 78 | |
| 79 | func (s *HstSuite) StopContainers() { |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 80 | for _, container := range s.containers { |
| 81 | container.stop() |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 85 | func (s *HstSuite) NewVolume(name string) error { |
| 86 | err := exechelper.Run(fmt.Sprintf("docker volume create --name=%s", name)) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | s.volumes = append(s.volumes, name) |
| 92 | return nil |
| 93 | } |
| 94 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 95 | func (s *HstSuite) RemoveVolumes() { |
| 96 | for _, volumeName := range s.volumes { |
| 97 | exechelper.Run("docker volume rm " + volumeName) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | type TapSuite struct { |
| 102 | HstSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func (s *TapSuite) SetupSuite() { |
| 106 | time.Sleep(1 * time.Second) |
| 107 | s.teardownSuite = setupSuite(&s.Suite, "tap") |
| 108 | } |
| 109 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 110 | type VethsSuite struct { |
| 111 | HstSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 114 | func (s *VethsSuite) SetupSuite() { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 115 | time.Sleep(1 * time.Second) |
| 116 | s.teardownSuite = setupSuite(&s.Suite, "2peerVeth") |
| 117 | } |
| 118 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 119 | type NsSuite struct { |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 120 | HstSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func (s *NsSuite) SetupSuite() { |
| 124 | s.teardownSuite = setupSuite(&s.Suite, "ns") |
| 125 | } |
| 126 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 127 | func setupSuite(s *suite.Suite, topologyName string) func() { |
| 128 | t := s.T() |
| 129 | topology, err := LoadTopology(TopologyDir, topologyName) |
| 130 | if err != nil { |
| 131 | t.Fatalf("error on loading topology '%s': %v", topologyName, err) |
| 132 | } |
| 133 | err = topology.Configure() |
| 134 | if err != nil { |
| 135 | t.Fatalf("failed to configure %s: %v", topologyName, err) |
| 136 | } |
| 137 | |
| 138 | t.Logf("topo %s loaded", topologyName) |
| 139 | return func() { |
| 140 | topology.Unconfigure() |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestTapSuite(t *testing.T) { |
| 145 | var m TapSuite |
| 146 | suite.Run(t, &m) |
| 147 | } |
| 148 | |
| 149 | func TestNs(t *testing.T) { |
| 150 | var m NsSuite |
| 151 | suite.Run(t, &m) |
| 152 | } |
| 153 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 154 | func TestVeths(t *testing.T) { |
| 155 | var m VethsSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 156 | suite.Run(t, &m) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 157 | } |