blob: 76d8e58724fc430411839abd00b29924de6a868e [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 {
Maros Ondrejickaf719adf2022-12-20 15:10:50 +0100134 volumeMap := elem["volume"].(VolumeConfig)
135 hostDir := volumeMap["host-dir"].(string)
136 s.volumes = append(s.volumes, hostDir)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100137 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000138
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100139 s.containers = make(map[string]*Container)
140 for _, elem := range yamlTopo.Containers {
141 newContainer, err := NewContainer(elem)
142 if err != nil {
143 s.T().Fatalf("config error: %v", err)
144 }
Maros Ondrejicka87531802022-12-19 20:35:27 +0100145 s.log(newContainer.getRunCommand())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100146 s.containers[newContainer.name] = newContainer
147 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000148}
149
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000150func setupSuite(s *suite.Suite, topologyName string) func() {
151 t := s.T()
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100152 topology, err := LoadTopology(NetworkTopologyDir, topologyName)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000153 if err != nil {
154 t.Fatalf("error on loading topology '%s': %v", topologyName, err)
155 }
156 err = topology.Configure()
157 if err != nil {
158 t.Fatalf("failed to configure %s: %v", topologyName, err)
159 }
160
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000161 return func() {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100162 if IsPersistent() {
163 return
164 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000165 topology.Unconfigure()
166 }
167}
168
169func TestTapSuite(t *testing.T) {
170 var m TapSuite
171 suite.Run(t, &m)
172}
173
174func TestNs(t *testing.T) {
175 var m NsSuite
176 suite.Run(t, &m)
177}
178
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100179func TestVeths(t *testing.T) {
180 var m VethsSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000181 suite.Run(t, &m)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000182}
Filip Tehlarc204c872022-12-21 08:59:16 +0100183
184func TestNoTopo(t *testing.T) {
185 var m NoTopoSuite
186 suite.Run(t, &m)
187}