2022-08-09 14:44:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-01 09:56:37 +01:00
|
|
|
"fmt"
|
2022-08-09 14:44:47 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
"github.com/edwarnicke/exechelper"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-08-09 14:44:47 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
type HstSuite struct {
|
2022-08-09 14:44:47 +00:00
|
|
|
suite.Suite
|
|
|
|
teardownSuite func()
|
2022-12-01 09:56:37 +01:00
|
|
|
containers []string
|
|
|
|
volumes []string
|
2022-08-09 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
func (s *HstSuite) TearDownSuite() {
|
|
|
|
s.teardownSuite()
|
|
|
|
s.StopContainers()
|
|
|
|
s.RemoveVolumes()
|
2022-08-09 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
func (s *HstSuite) hstFail() {
|
|
|
|
s.T().FailNow()
|
2022-08-09 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
func (s *HstSuite) assertNil(object interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if !assert.Nil(s.T(), object, msgAndArgs...) {
|
|
|
|
s.hstFail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HstSuite) assertNotNil(object interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if !assert.NotNil(s.T(), object, msgAndArgs...) {
|
|
|
|
s.hstFail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HstSuite) assertEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if !assert.Equal(s.T(), expected, actual, msgAndArgs...) {
|
|
|
|
s.hstFail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
|
|
|
|
s.hstFail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HstSuite) NewContainer(name string) (*Container, error) {
|
|
|
|
if name == "" {
|
|
|
|
return nil, fmt.Errorf("creating container failed: name must not be blank")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.containers = append(s.containers, name)
|
|
|
|
|
|
|
|
container := new(Container)
|
|
|
|
container.name = name
|
|
|
|
return container, nil
|
2022-08-09 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
func (s *HstSuite) StopContainers() {
|
|
|
|
for _, containerName := range s.containers {
|
|
|
|
exechelper.Run("docker stop " + containerName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HstSuite) RemoveVolumes() {
|
|
|
|
for _, volumeName := range s.volumes {
|
|
|
|
exechelper.Run("docker volume rm " + volumeName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type TapSuite struct {
|
|
|
|
HstSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TapSuite) SetupSuite() {
|
2022-08-09 14:44:47 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2022-12-01 09:56:37 +01:00
|
|
|
s.teardownSuite = setupSuite(&s.Suite, "tap")
|
2022-08-09 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
type VethsSuite struct {
|
|
|
|
HstSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VethsSuite) SetupSuite() {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
s.teardownSuite = setupSuite(&s.Suite, "2peerVeth")
|
2022-08-09 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NsSuite struct {
|
2022-12-01 09:56:37 +01:00
|
|
|
HstSuite
|
2022-08-09 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NsSuite) SetupSuite() {
|
|
|
|
s.teardownSuite = setupSuite(&s.Suite, "ns")
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupSuite(s *suite.Suite, topologyName string) func() {
|
|
|
|
t := s.T()
|
|
|
|
topology, err := LoadTopology(TopologyDir, topologyName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error on loading topology '%s': %v", topologyName, err)
|
|
|
|
}
|
|
|
|
err = topology.Configure()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to configure %s: %v", topologyName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("topo %s loaded", topologyName)
|
|
|
|
return func() {
|
|
|
|
topology.Unconfigure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTapSuite(t *testing.T) {
|
|
|
|
var m TapSuite
|
|
|
|
suite.Run(t, &m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNs(t *testing.T) {
|
|
|
|
var m NsSuite
|
|
|
|
suite.Run(t, &m)
|
|
|
|
}
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
func TestVeths(t *testing.T) {
|
|
|
|
var m VethsSuite
|
2022-08-09 14:44:47 +00:00
|
|
|
suite.Run(t, &m)
|
|
|
|
}
|