blob: cd43cfe4612ce890fa792a5a555356f656f2f4e6 [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Maros Ondrejickadb823ed2022-12-14 16:30:04 +01004 "io/ioutil"
Maros Ondrejicka87531802022-12-19 20:35:27 +01005 "os"
Filip Tehlarbb98aac2022-12-21 14:40:35 +01006 "testing"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00007
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01008 "github.com/edwarnicke/exechelper"
9 "github.com/stretchr/testify/assert"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000010 "github.com/stretchr/testify/suite"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010011 "gopkg.in/yaml.v3"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000012)
13
Maros Ondrejicka87531802022-12-19 20:35:27 +010014func IsPersistent() bool {
Filip Tehlarbb98aac2022-12-21 14:40:35 +010015 return os.Getenv("HST_PERSIST") == "1"
Maros Ondrejicka87531802022-12-19 20:35:27 +010016}
17
18func IsVerbose() bool {
Filip Tehlarbb98aac2022-12-21 14:40:35 +010019 return os.Getenv("HST_VERBOSE") == "1"
Maros Ondrejicka87531802022-12-19 20:35:27 +010020}
21
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010022type HstSuite struct {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000023 suite.Suite
24 teardownSuite func()
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010025 containers map[string]*Container
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010026 volumes []string
27}
28
29func (s *HstSuite) TearDownSuite() {
30 s.teardownSuite()
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010031}
32
33func (s *HstSuite) TearDownTest() {
Maros Ondrejicka87531802022-12-19 20:35:27 +010034 if IsPersistent() {
35 return
36 }
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010037 s.ResetContainers()
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010038 s.RemoveVolumes()
39}
40
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010041func (s *HstSuite) SetupTest() {
42 for _, volume := range s.volumes {
43 cmd := "docker volume create --name=" + volume
Maros Ondrejicka87531802022-12-19 20:35:27 +010044 s.log(cmd)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010045 exechelper.Run(cmd)
46 }
47 for _, container := range s.containers {
48 if container.isOptional == false {
49 container.run()
50 }
51 }
52}
53
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010054func (s *HstSuite) hstFail() {
55 s.T().FailNow()
56}
57
58func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) {
59 if !assert.Nil(s.T(), object, msgAndArgs...) {
60 s.hstFail()
61 }
62}
63
64func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) {
65 if !assert.NotNil(s.T(), object, msgAndArgs...) {
66 s.hstFail()
67 }
68}
69
70func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
71 if !assert.Equal(s.T(), expected, actual, msgAndArgs...) {
72 s.hstFail()
73 }
74}
75
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010076func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
77 if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) {
78 s.hstFail()
79 }
80}
81
82func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) {
83 if !assert.Contains(s.T(), testString, contains, msgAndArgs...) {
84 s.hstFail()
85 }
86}
87
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010088func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
89 if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
90 s.hstFail()
91 }
92}
93
Maros Ondrejicka87531802022-12-19 20:35:27 +010094func (s *HstSuite) log(args ...any) {
95 if IsVerbose() {
96 s.T().Log(args...)
97 }
98}
99
100func (s *HstSuite) skip(args ...any) {
101 s.log(args...)
102 s.T().SkipNow()
103}
104
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100105func (s *HstSuite) ResetContainers() {
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100106 for _, container := range s.containers {
107 container.stop()
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100108 }
109}
110
111func (s *HstSuite) RemoveVolumes() {
112 for _, volumeName := range s.volumes {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100113 cmd := "docker volume rm " + volumeName
114 exechelper.Run(cmd)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100115 }
116}
117
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100118func (s *HstSuite) getContainerByName(name string) *Container {
119 return s.containers[name]
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000120}
121
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100122func (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 Tehlar229f5fc2022-08-09 14:44:47 +0000132
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100133 for _, elem := range yamlTopo.Volumes {
134 s.volumes = append(s.volumes, elem)
135 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000136
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100137 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 Ondrejicka87531802022-12-19 20:35:27 +0100143 s.log(newContainer.getRunCommand())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100144 s.containers[newContainer.name] = newContainer
145 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000146}
147
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000148func setupSuite(s *suite.Suite, topologyName string) func() {
149 t := s.T()
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100150 topology, err := LoadTopology(NetworkTopologyDir, topologyName)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000151 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 Tehlar229f5fc2022-08-09 14:44:47 +0000159 return func() {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100160 if IsPersistent() {
161 return
162 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000163 topology.Unconfigure()
164 }
165}
166
167func TestTapSuite(t *testing.T) {
168 var m TapSuite
169 suite.Run(t, &m)
170}
171
172func TestNs(t *testing.T) {
173 var m NsSuite
174 suite.Run(t, &m)
175}
176
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100177func TestVeths(t *testing.T) {
178 var m VethsSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000179 suite.Run(t, &m)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000180}