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