blob: fc186f35180553aed357b0da3666c869ac2e5fc9 [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 Ondrejicka11a03e92022-12-01 09:56:37 +010016 containers []string
17 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
48func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
49 if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
50 s.hstFail()
51 }
52}
53
54func (s *HstSuite) NewContainer(name string) (*Container, error) {
55 if name == "" {
56 return nil, fmt.Errorf("creating container failed: name must not be blank")
57 }
58
59 s.containers = append(s.containers, name)
60
61 container := new(Container)
62 container.name = name
63 return container, nil
64}
65
66func (s *HstSuite) StopContainers() {
67 for _, containerName := range s.containers {
68 exechelper.Run("docker stop " + containerName)
69 }
70}
71
72func (s *HstSuite) RemoveVolumes() {
73 for _, volumeName := range s.volumes {
74 exechelper.Run("docker volume rm " + volumeName)
75 }
76}
77
78type TapSuite struct {
79 HstSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +000080}
81
82func (s *TapSuite) SetupSuite() {
83 time.Sleep(1 * time.Second)
84 s.teardownSuite = setupSuite(&s.Suite, "tap")
85}
86
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010087type VethsSuite struct {
88 HstSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +000089}
90
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010091func (s *VethsSuite) SetupSuite() {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000092 time.Sleep(1 * time.Second)
93 s.teardownSuite = setupSuite(&s.Suite, "2peerVeth")
94}
95
Filip Tehlar229f5fc2022-08-09 14:44:47 +000096type NsSuite struct {
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010097 HstSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +000098}
99
100func (s *NsSuite) SetupSuite() {
101 s.teardownSuite = setupSuite(&s.Suite, "ns")
102}
103
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000104func setupSuite(s *suite.Suite, topologyName string) func() {
105 t := s.T()
106 topology, err := LoadTopology(TopologyDir, topologyName)
107 if err != nil {
108 t.Fatalf("error on loading topology '%s': %v", topologyName, err)
109 }
110 err = topology.Configure()
111 if err != nil {
112 t.Fatalf("failed to configure %s: %v", topologyName, err)
113 }
114
115 t.Logf("topo %s loaded", topologyName)
116 return func() {
117 topology.Unconfigure()
118 }
119}
120
121func TestTapSuite(t *testing.T) {
122 var m TapSuite
123 suite.Run(t, &m)
124}
125
126func TestNs(t *testing.T) {
127 var m NsSuite
128 suite.Run(t, &m)
129}
130
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100131func TestVeths(t *testing.T) {
132 var m VethsSuite
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000133 suite.Run(t, &m)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000134}