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