hs-test: remove exec flags from source files
[vpp.git] / extras / hs-test / framework_test.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "testing"
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         return os.Getenv("HST_PERSIST") == "1"
16 }
17
18 func IsVerbose() bool {
19         return os.Getenv("HST_VERBOSE") == "1"
20 }
21
22 type HstSuite struct {
23         suite.Suite
24         teardownSuite func()
25         containers    map[string]*Container
26         volumes       []string
27 }
28
29 func (s *HstSuite) TearDownSuite() {
30         s.teardownSuite()
31 }
32
33 func (s *HstSuite) TearDownTest() {
34         if IsPersistent() {
35                 return
36         }
37         s.ResetContainers()
38         s.RemoveVolumes()
39 }
40
41 func (s *HstSuite) SetupTest() {
42         for _, volume := range s.volumes {
43                 cmd := "docker volume create --name=" + volume
44                 s.log(cmd)
45                 exechelper.Run(cmd)
46         }
47         for _, container := range s.containers {
48                 if container.isOptional == false {
49                         container.run()
50                 }
51         }
52 }
53
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
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
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
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
105 func (s *HstSuite) ResetContainers() {
106         for _, container := range s.containers {
107                 container.stop()
108         }
109 }
110
111 func (s *HstSuite) RemoveVolumes() {
112         for _, volumeName := range s.volumes {
113                 cmd := "docker volume rm " + volumeName
114                 exechelper.Run(cmd)
115         }
116 }
117
118 func (s *HstSuite) getContainerByName(name string) *Container {
119         return s.containers[name]
120 }
121
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         }
132
133         for _, elem := range yamlTopo.Volumes {
134                 s.volumes = append(s.volumes, elem)
135         }
136
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                 }
143                 s.log(newContainer.getRunCommand())
144                 s.containers[newContainer.name] = newContainer
145         }
146 }
147
148 func setupSuite(s *suite.Suite, topologyName string) func() {
149         t := s.T()
150         topology, err := LoadTopology(NetworkTopologyDir, topologyName)
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
159         return func() {
160                 if IsPersistent() {
161                         return
162                 }
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
177 func TestVeths(t *testing.T) {
178         var m VethsSuite
179         suite.Run(t, &m)
180 }
181
182 func TestNoTopo(t *testing.T) {
183         var m NoTopoSuite
184         suite.Run(t, &m)
185 }