Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 4 | "io/ioutil" |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 5 | "os" |
Filip Tehlar | bb98aac | 2022-12-21 14:40:35 +0100 | [diff] [blame] | 6 | "testing" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 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" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 11 | "gopkg.in/yaml.v3" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 12 | ) |
| 13 | |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 14 | func IsPersistent() bool { |
Filip Tehlar | bb98aac | 2022-12-21 14:40:35 +0100 | [diff] [blame] | 15 | return os.Getenv("HST_PERSIST") == "1" |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | func IsVerbose() bool { |
Filip Tehlar | bb98aac | 2022-12-21 14:40:35 +0100 | [diff] [blame] | 19 | return os.Getenv("HST_VERBOSE") == "1" |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 20 | } |
| 21 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 22 | type HstSuite struct { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 23 | suite.Suite |
| 24 | teardownSuite func() |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 25 | containers map[string]*Container |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 26 | volumes []string |
| 27 | } |
| 28 | |
| 29 | func (s *HstSuite) TearDownSuite() { |
| 30 | s.teardownSuite() |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func (s *HstSuite) TearDownTest() { |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 34 | if IsPersistent() { |
| 35 | return |
| 36 | } |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 37 | s.ResetContainers() |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 38 | s.RemoveVolumes() |
| 39 | } |
| 40 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 41 | func (s *HstSuite) SetupTest() { |
| 42 | for _, volume := range s.volumes { |
| 43 | cmd := "docker volume create --name=" + volume |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 44 | s.log(cmd) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 45 | exechelper.Run(cmd) |
| 46 | } |
| 47 | for _, container := range s.containers { |
| 48 | if container.isOptional == false { |
| 49 | container.run() |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 54 | func (s *HstSuite) hstFail() { |
| 55 | s.T().FailNow() |
| 56 | } |
| 57 | |
| 58 | func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) { |
| 59 | if !assert.Nil(s.T(), object, msgAndArgs...) { |
| 60 | s.hstFail() |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) { |
| 65 | if !assert.NotNil(s.T(), object, msgAndArgs...) { |
| 66 | s.hstFail() |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 71 | if !assert.Equal(s.T(), expected, actual, msgAndArgs...) { |
| 72 | s.hstFail() |
| 73 | } |
| 74 | } |
| 75 | |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 76 | func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) { |
| 77 | if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) { |
| 78 | s.hstFail() |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 83 | if !assert.Contains(s.T(), testString, contains, msgAndArgs...) { |
| 84 | s.hstFail() |
| 85 | } |
| 86 | } |
| 87 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 88 | func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) { |
| 89 | if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) { |
| 90 | s.hstFail() |
| 91 | } |
| 92 | } |
| 93 | |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 94 | func (s *HstSuite) log(args ...any) { |
| 95 | if IsVerbose() { |
| 96 | s.T().Log(args...) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func (s *HstSuite) skip(args ...any) { |
| 101 | s.log(args...) |
| 102 | s.T().SkipNow() |
| 103 | } |
| 104 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 105 | func (s *HstSuite) ResetContainers() { |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 106 | for _, container := range s.containers { |
| 107 | container.stop() |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | func (s *HstSuite) RemoveVolumes() { |
| 112 | for _, volumeName := range s.volumes { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 113 | cmd := "docker volume rm " + volumeName |
| 114 | exechelper.Run(cmd) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 118 | func (s *HstSuite) getContainerByName(name string) *Container { |
| 119 | return s.containers[name] |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 122 | func (s *HstSuite) loadContainerTopology(topologyName string) { |
| 123 | data, err := ioutil.ReadFile(ContainerTopologyDir + topologyName + ".yaml") |
| 124 | if err != nil { |
| 125 | s.T().Fatalf("read error: %v", err) |
| 126 | } |
| 127 | var yamlTopo YamlTopology |
| 128 | err = yaml.Unmarshal(data, &yamlTopo) |
| 129 | if err != nil { |
| 130 | s.T().Fatalf("unmarshal error: %v", err) |
| 131 | } |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 132 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 133 | for _, elem := range yamlTopo.Volumes { |
| 134 | s.volumes = append(s.volumes, elem) |
| 135 | } |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 136 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 137 | s.containers = make(map[string]*Container) |
| 138 | for _, elem := range yamlTopo.Containers { |
| 139 | newContainer, err := NewContainer(elem) |
| 140 | if err != nil { |
| 141 | s.T().Fatalf("config error: %v", err) |
| 142 | } |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 143 | s.log(newContainer.getRunCommand()) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 144 | s.containers[newContainer.name] = newContainer |
| 145 | } |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 148 | func setupSuite(s *suite.Suite, topologyName string) func() { |
| 149 | t := s.T() |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 150 | topology, err := LoadTopology(NetworkTopologyDir, topologyName) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 151 | if err != nil { |
| 152 | t.Fatalf("error on loading topology '%s': %v", topologyName, err) |
| 153 | } |
| 154 | err = topology.Configure() |
| 155 | if err != nil { |
| 156 | t.Fatalf("failed to configure %s: %v", topologyName, err) |
| 157 | } |
| 158 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 159 | return func() { |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 160 | if IsPersistent() { |
| 161 | return |
| 162 | } |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 163 | topology.Unconfigure() |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestTapSuite(t *testing.T) { |
| 168 | var m TapSuite |
| 169 | suite.Run(t, &m) |
| 170 | } |
| 171 | |
| 172 | func TestNs(t *testing.T) { |
| 173 | var m NsSuite |
| 174 | suite.Run(t, &m) |
| 175 | } |
| 176 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 177 | func TestVeths(t *testing.T) { |
| 178 | var m VethsSuite |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 179 | suite.Run(t, &m) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 180 | } |