hs-test: manage containers and volumes within test suite
[vpp.git] / extras / hs-test / container.go
1 package main
2
3 import (
4         "fmt"
5
6         "github.com/edwarnicke/exechelper"
7 )
8
9 type Volume struct {
10         name string
11         path string
12 }
13
14 type Container struct {
15         name    string
16         volumes []*Volume
17 }
18
19 func (c *Container) run() error {
20         if c.name == "" {
21                 return fmt.Errorf("create volume failed: container name is blank")
22         }
23
24         exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", c.name))
25         syncPath := fmt.Sprintf("-v /tmp/%s/sync:/tmp/sync", c.name)
26         cmd := "docker run --cap-add=all -d --privileged --network host --rm "
27         cmd += syncPath
28         cmd += c.getVolumes()
29         cmd += " --name " + c.name + " hs-test/vpp"
30         fmt.Println(cmd)
31         err := exechelper.Run(cmd)
32         if err != nil {
33                 return fmt.Errorf("create volume failed: %s", err)
34         }
35
36         return nil
37 }
38
39 func (c *Container) addVolume(name string, containerPath string) {
40         c.volumes = append(c.volumes, &Volume{name, containerPath})
41 }
42
43 func (c *Container) getVolumes() string {
44         dockerOption := ""
45
46         if len(c.volumes) > 0 {
47                 for _, volume := range c.volumes {
48                         dockerOption += fmt.Sprintf(" -v %s:%s", volume.name, volume.path)
49                 }
50         }
51
52         return dockerOption
53 }
54
55 func (c *Container) stop() error {
56         return exechelper.Run("docker stop " + c.name)
57 }