package main
import (
- "fmt"
+ "io/ioutil"
+ "os"
"testing"
- "time"
"github.com/edwarnicke/exechelper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
+ "gopkg.in/yaml.v3"
)
+func IsPersistent() bool {
+ return os.Getenv("HST_PERSIST") == "1"
+}
+
+func IsVerbose() bool {
+ return os.Getenv("HST_VERBOSE") == "1"
+}
+
type HstSuite struct {
suite.Suite
teardownSuite func()
- containers []*Container
+ containers map[string]*Container
volumes []string
}
func (s *HstSuite) TearDownSuite() {
s.teardownSuite()
- s.StopContainers()
+}
+
+func (s *HstSuite) TearDownTest() {
+ if IsPersistent() {
+ return
+ }
+ s.ResetContainers()
s.RemoveVolumes()
}
+func (s *HstSuite) SetupTest() {
+ for _, volume := range s.volumes {
+ cmd := "docker volume create --name=" + volume
+ s.log(cmd)
+ exechelper.Run(cmd)
+ }
+ for _, container := range s.containers {
+ if container.isOptional == false {
+ container.run()
+ }
+ }
+}
+
func (s *HstSuite) hstFail() {
s.T().FailNow()
}
}
}
-func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
- if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
+func (s *HstSuite) assertNotEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
+ if !assert.NotEqual(s.T(), expected, actual, 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")
+func (s *HstSuite) assertContains(testString, contains interface{}, msgAndArgs ...interface{}) {
+ if !assert.Contains(s.T(), testString, contains, msgAndArgs...) {
+ s.hstFail()
}
+}
- container := new(Container)
- container.name = name
+func (s *HstSuite) assertNotContains(testString, contains interface{}, msgAndArgs ...interface{}) {
+ if !assert.NotContains(s.T(), testString, contains, msgAndArgs...) {
+ s.hstFail()
+ }
+}
- s.containers = append(s.containers, container)
+func (s *HstSuite) log(args ...any) {
+ if IsVerbose() {
+ s.T().Log(args...)
+ }
+}
- return container, nil
+func (s *HstSuite) skip(args ...any) {
+ s.log(args...)
+ s.T().SkipNow()
}
-func (s *HstSuite) StopContainers() {
+func (s *HstSuite) ResetContainers() {
for _, container := range s.containers {
container.stop()
}
}
-func (s *HstSuite) NewVolume(name string) error {
- err := exechelper.Run(fmt.Sprintf("docker volume create --name=%s", name))
- if err != nil {
- return err
- }
-
- s.volumes = append(s.volumes, name)
- return nil
-}
-
func (s *HstSuite) RemoveVolumes() {
for _, volumeName := range s.volumes {
- exechelper.Run("docker volume rm " + volumeName)
+ cmd := "docker volume rm " + volumeName
+ exechelper.Run(cmd)
}
}
-type TapSuite struct {
- HstSuite
+func (s *HstSuite) getContainerByName(name string) *Container {
+ return s.containers[name]
}
-func (s *TapSuite) SetupSuite() {
- time.Sleep(1 * time.Second)
- s.teardownSuite = setupSuite(&s.Suite, "tap")
-}
-
-type VethsSuite struct {
- HstSuite
-}
-
-func (s *VethsSuite) SetupSuite() {
- time.Sleep(1 * time.Second)
- s.teardownSuite = setupSuite(&s.Suite, "2peerVeth")
-}
+func (s *HstSuite) loadContainerTopology(topologyName string) {
+ data, err := ioutil.ReadFile(ContainerTopologyDir + topologyName + ".yaml")
+ if err != nil {
+ s.T().Fatalf("read error: %v", err)
+ }
+ var yamlTopo YamlTopology
+ err = yaml.Unmarshal(data, &yamlTopo)
+ if err != nil {
+ s.T().Fatalf("unmarshal error: %v", err)
+ }
-type NsSuite struct {
- HstSuite
-}
+ for _, elem := range yamlTopo.Volumes {
+ s.volumes = append(s.volumes, elem)
+ }
-func (s *NsSuite) SetupSuite() {
- s.teardownSuite = setupSuite(&s.Suite, "ns")
+ s.containers = make(map[string]*Container)
+ for _, elem := range yamlTopo.Containers {
+ newContainer, err := NewContainer(elem)
+ if err != nil {
+ s.T().Fatalf("config error: %v", err)
+ }
+ s.log(newContainer.getRunCommand())
+ s.containers[newContainer.name] = newContainer
+ }
}
func setupSuite(s *suite.Suite, topologyName string) func() {
t := s.T()
- topology, err := LoadTopology(TopologyDir, topologyName)
+ topology, err := LoadTopology(NetworkTopologyDir, topologyName)
if err != nil {
t.Fatalf("error on loading topology '%s': %v", topologyName, err)
}
t.Fatalf("failed to configure %s: %v", topologyName, err)
}
- t.Logf("topo %s loaded", topologyName)
return func() {
+ if IsPersistent() {
+ return
+ }
topology.Unconfigure()
}
}
var m VethsSuite
suite.Run(t, &m)
}
+
+func TestNoTopo(t *testing.T) {
+ var m NoTopoSuite
+ suite.Run(t, &m)
+}