hs-test: abstract away topology from test cases
[vpp.git] / extras / hs-test / container.go
1 package main
2
3 import (
4         "fmt"
5         "os"
6         "strings"
7
8         "github.com/edwarnicke/exechelper"
9 )
10
11 type Volume struct {
12         hostDir      string
13         containerDir string
14 }
15
16 type Container struct {
17         isOptional bool
18         name       string
19         image      string
20         workDir    string
21         volumes    map[string]Volume
22         envVars    map[string]string
23 }
24
25 func NewContainer(yamlInput ContainerConfig) (*Container, error) {
26         containerName := yamlInput["name"].(string)
27         if len(containerName) == 0 {
28                 err := fmt.Errorf("container name must not be blank")
29                 return nil, err
30         }
31
32         var container = new(Container)
33         container.volumes = make(map[string]Volume)
34         container.envVars = make(map[string]string)
35         container.name = containerName
36
37         if image, ok := yamlInput["image"]; ok {
38                 container.image = image.(string)
39         } else {
40                 container.image = "hs-test/vpp"
41         }
42
43         if isOptional, ok := yamlInput["is-optional"]; ok {
44                 container.isOptional = isOptional.(bool)
45         } else {
46                 container.isOptional = false
47         }
48
49         if _, ok := yamlInput["volumes"]; ok {
50                 r:= strings.NewReplacer("$HST_DIR", workDir)
51                 for _, volu := range yamlInput["volumes"].([]interface{}) {
52                         volumeMap := volu.(ContainerConfig)
53                         hostDir := r.Replace(volumeMap["host-dir"].(string))
54                         containerDir := volumeMap["container-dir"].(string)
55                         container.addVolume(hostDir, containerDir)
56
57                         if isDefaultWorkDir, ok := volumeMap["is-default-work-dir"]; ok &&
58                         isDefaultWorkDir.(bool) &&
59                         len(container.workDir) == 0 {
60                                 container.workDir = containerDir
61                         }
62
63                 }
64         }
65
66         if _, ok := yamlInput["vars"]; ok {
67                 for _, envVar := range yamlInput["vars"].([]interface{}) {
68                         container.addEnvVar(envVar)
69                 }
70         }
71         return container, nil
72 }
73
74 func (c *Container) run() error {
75         if c.name == "" {
76                 return fmt.Errorf("create volume failed: container name is blank")
77         }
78
79         exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", c.name))
80         syncPath := fmt.Sprintf(" -v %s:/tmp/sync", c.getSyncPath())
81         cmd := "docker run --cap-add=all -d --privileged --network host --rm"
82         cmd += syncPath
83         cmd += c.getVolumesAsCliOption()
84         cmd += c.getEnvVarsAsCliOption()
85         cmd += " --name " + c.name + " " + c.image
86         fmt.Println(cmd)
87         err := exechelper.Run(cmd)
88         if err != nil {
89                 return fmt.Errorf("container run failed: %s", err)
90         }
91
92         return nil
93 }
94
95 func (c *Container) addVolume(hostDir string, containerDir string) {
96         var volume Volume
97         volume.hostDir = hostDir
98         volume.containerDir = containerDir
99         c.volumes[hostDir] = volume
100 }
101
102 func (c *Container) getVolumeByHostDir(hostDir string) Volume {
103         return c.volumes[hostDir]
104 }
105
106 func (c *Container) getVolumesAsCliOption() string {
107         cliOption := ""
108
109         if len(c.volumes) > 0 {
110                 for _, volume := range c.volumes {
111                         cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
112                 }
113         }
114
115         return cliOption
116 }
117
118 func (c *Container) getWorkDirAsCliOption() string {
119         if len(c.workDir) == 0 {
120                 return ""
121         }
122         return fmt.Sprintf(" --workdir=\"%s\"", c.workDir)
123 }
124
125 func (c *Container) addEnvVar(envVar interface{}) {
126         envVarMap := envVar.(ContainerConfig)
127         name := envVarMap["name"].(string)
128         value := envVarMap["value"].(string)
129         c.envVars[name] = value
130 }
131
132 func (c *Container) getEnvVarsAsCliOption() string {
133         cliOption := ""
134         if len(c.envVars) == 0 {
135                 return cliOption
136         }
137
138         for name, value := range c.envVars {
139                 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
140         }
141         return cliOption
142 }
143
144 func (c *Container) getSyncPath() string {
145         return fmt.Sprintf("/tmp/%s/sync", c.name)
146 }
147
148 func (c *Container) exec(command string) (string, error) {
149         cliCommand := "docker exec -d " + c.name + " " + command
150         byteOutput, err := exechelper.CombinedOutput(cliCommand)
151         return string(byteOutput), err
152 }
153
154 func (c *Container) execAction(args string) (string, error) {
155         syncFile := c.getSyncPath() + "/rc"
156         os.Remove(syncFile)
157
158         workDir := c.getWorkDirAsCliOption()
159         cmd := fmt.Sprintf("docker exec -d %s %s hs-test %s",
160                 workDir,
161                 c.name,
162                 args)
163         err := exechelper.Run(cmd)
164         if err != nil {
165                 return "", err
166         }
167         res, err := waitForSyncFile(syncFile)
168         if err != nil {
169                 return "", fmt.Errorf("failed to read sync file while executing 'hs-test %s': %v", args, err)
170         }
171         o := res.StdOutput + res.ErrOutput
172         if res.Code != 0 {
173                 return o, fmt.Errorf("cmd resulted in non-zero value %d: %s", res.Code, res.Desc)
174         }
175         return o, err
176 }
177
178 func (c *Container) stop() error {
179         return exechelper.Run("docker stop " + c.name)
180 }