hs-test: use assert-like approach in test cases
[vpp.git] / extras / hs-test / framework_test.go
1 package main
2
3 import (
4         "fmt"
5         "testing"
6         "time"
7
8         "github.com/edwarnicke/exechelper"
9         "github.com/stretchr/testify/assert"
10         "github.com/stretchr/testify/suite"
11 )
12
13 type HstSuite struct {
14         suite.Suite
15         teardownSuite func()
16         containers    []*Container
17         volumes       []string
18 }
19
20 func (s *HstSuite) TearDownSuite() {
21         s.teardownSuite()
22         s.StopContainers()
23         s.RemoveVolumes()
24 }
25
26 func (s *HstSuite) hstFail() {
27         s.T().FailNow()
28 }
29
30 func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) {
31         if !assert.Nil(s.T(), object, msgAndArgs...) {
32                 s.hstFail()
33         }
34 }
35
36 func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) {
37         if !assert.NotNil(s.T(), object, msgAndArgs...) {
38                 s.hstFail()
39         }
40 }
41
42 func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
43         if !assert.Equal(s.T(), expected, actual, msgAndArgs...) {
44                 s.hstFail()
45         }
46 }
47
48 func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
49         if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) {
50                 s.hstFail()
51         }
52 }
53
54 func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) {
55         if !assert.Contains(s.T(), testString, contains, msgAndArgs...) {
56                 s.hstFail()
57         }
58 }
59
60 func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
61         if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
62                 s.hstFail()
63         }
64 }
65
66 func (s *HstSuite) NewContainer(name string) (*Container, error) {
67         if name == "" {
68                 return nil, fmt.Errorf("creating container failed: name must not be blank")
69         }
70
71         container := new(Container)
72         container.name = name
73
74         s.containers = append(s.containers, container)
75
76         return container, nil
77 }
78
79 func (s *HstSuite) StopContainers() {
80         for _, container := range s.containers {
81                 container.stop()
82         }
83 }
84
85 func (s *HstSuite) NewVolume(name string) error {
86         err := exechelper.Run(fmt.Sprintf("docker volume create --name=%s", name))
87         if err != nil {
88                 return err
89         }
90
91         s.volumes = append(s.volumes, name)
92         return nil
93 }
94
95 func (s *HstSuite) RemoveVolumes() {
96         for _, volumeName := range s.volumes {
97                 exechelper.Run("docker volume rm " + volumeName)
98         }
99 }
100
101 type TapSuite struct {
102         HstSuite
103 }
104
105 func (s *TapSuite) SetupSuite() {
106         time.Sleep(1 * time.Second)
107         s.teardownSuite = setupSuite(&s.Suite, "tap")
108 }
109
110 type VethsSuite struct {
111         HstSuite
112 }
113
114 func (s *VethsSuite) SetupSuite() {
115         time.Sleep(1 * time.Second)
116         s.teardownSuite = setupSuite(&s.Suite, "2peerVeth")
117 }
118
119 type NsSuite struct {
120         HstSuite
121 }
122
123 func (s *NsSuite) SetupSuite() {
124         s.teardownSuite = setupSuite(&s.Suite, "ns")
125 }
126
127 func setupSuite(s *suite.Suite, topologyName string) func() {
128         t := s.T()
129         topology, err := LoadTopology(TopologyDir, topologyName)
130         if err != nil {
131                 t.Fatalf("error on loading topology '%s': %v", topologyName, err)
132         }
133         err = topology.Configure()
134         if err != nil {
135                 t.Fatalf("failed to configure %s: %v", topologyName, err)
136         }
137
138         t.Logf("topo %s loaded", topologyName)
139         return func() {
140                 topology.Unconfigure()
141         }
142 }
143
144 func TestTapSuite(t *testing.T) {
145         var m TapSuite
146         suite.Run(t, &m)
147 }
148
149 func TestNs(t *testing.T) {
150         var m NsSuite
151         suite.Run(t, &m)
152 }
153
154 func TestVeths(t *testing.T) {
155         var m VethsSuite
156         suite.Run(t, &m)
157 }