blob: 3df509f30c1d47883cc4137df41b464a99675213 [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01004 "fmt"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00005 "testing"
6 "time"
7
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"
11)
12
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010013type HstSuite struct {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000014 suite.Suite
15 teardownSuite func()
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010016 containers []*Container
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010017 volumes []string
18}
19
20func (s *HstSuite) TearDownSuite() {
21 s.teardownSuite()
22 s.StopContainers()
23 s.RemoveVolumes()
24}
25
26func (s *HstSuite) hstFail() {
27 s.T().FailNow()
28}
29
30func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) {
31 if !assert.Nil(s.T(), object, msgAndArgs...) {
32 s.hstFail()
33 }
34}
35
36func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) {
37 if !assert.NotNil(s.T(), object, msgAndArgs...) {
38 s.hstFail()
39 }
40}
41
42func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
43 if !assert.Equal(s.T(), expected, actual, msgAndArgs...) {
44 s.hstFail()
45 }
46}
47
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010048func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
49 if !assert.NotEqual(s.T(), expected, actual, msgAndArgs...) {
50 s.hstFail()
51 }
52}
53
54func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) {
55 if !assert.Contains(s.T(), testString, contains, msgAndArgs...) {
56 s.hstFail()
57 }
58}
59
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010060func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
61 if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
62 s.hstFail()
63 }
64}
65
66func (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
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010071 container := new(Container)
72 container.name = name
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010073
74 s.containers = append(s.containers, container)
75
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010076 return container, nil
77}
78
79func (s *HstSuite) StopContainers() {
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010080 for _, container := range s.containers {
81 container.stop()
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010082 }
83}
84
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010085func (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
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010095func (s *HstSuite) RemoveVolumes() {
96 for _, volumeName := range s.volumes {
97 exechelper.Run("docker volume rm " + volumeName)
98 }
99}
100
101type TapSuite struct {
102 HstSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000103}
104
105func (s *TapSuite) SetupSuite() {
106 time.Sleep(1 * time.Second)
107 s.teardownSuite = setupSuite(&s.Suite, "tap")
108}
109
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100110type VethsSuite struct {
111 HstSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000112}
113
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100114func (s *VethsSuite) SetupSuite() {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000115 time.Sleep(1 * time.Second)
116 s.teardownSuite = setupSuite(&s.Suite, "2peerVeth")
117}
118
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000119type NsSuite struct {
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100120 HstSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000121}
122
123func (s *NsSuite) SetupSuite() {
124 s.teardownSuite = setupSuite(&s.Suite, "ns")
125}
126
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000127func 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
144func TestTapSuite(t *testing.T) {
145 var m TapSuite
146 suite.Run(t, &m)
147}
148
149func TestNs(t *testing.T) {
150 var m NsSuite
151 suite.Run(t, &m)
152}
153
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100154func TestVeths(t *testing.T) {
155 var m VethsSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000156 suite.Run(t, &m)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000157}