10 "github.com/edwarnicke/exechelper"
14 logDir string = "/tmp/hs-test/"
18 workDir, _ = os.Getwd()
27 type Container struct {
33 extraRunningArgs string
34 volumes map[string]Volume
35 envVars map[string]string
36 vppInstance *VppInstance
39 func newContainer(yamlInput ContainerConfig) (*Container, error) {
40 containerName := yamlInput["name"].(string)
41 if len(containerName) == 0 {
42 err := fmt.Errorf("container name must not be blank")
46 var container = new(Container)
47 container.volumes = make(map[string]Volume)
48 container.envVars = make(map[string]string)
49 container.name = containerName
51 if image, ok := yamlInput["image"]; ok {
52 container.image = image.(string)
54 container.image = "hs-test/vpp"
57 if args, ok := yamlInput["extra-args"]; ok {
58 container.extraRunningArgs = args.(string)
60 container.extraRunningArgs = ""
63 if isOptional, ok := yamlInput["is-optional"]; ok {
64 container.isOptional = isOptional.(bool)
66 container.isOptional = false
69 if runDetached, ok := yamlInput["run-detached"]; ok {
70 container.runDetached = runDetached.(bool)
72 container.runDetached = true
75 if _, ok := yamlInput["volumes"]; ok {
76 r := strings.NewReplacer("$HST_DIR", workDir)
77 for _, volu := range yamlInput["volumes"].([]interface{}) {
78 volumeMap := volu.(ContainerConfig)
79 hostDir := r.Replace(volumeMap["host-dir"].(string))
80 containerDir := volumeMap["container-dir"].(string)
81 isDefaultWorkDir := false
83 if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
84 isDefaultWorkDir = isDefault.(bool)
87 container.addVolume(hostDir, containerDir, isDefaultWorkDir)
92 if _, ok := yamlInput["vars"]; ok {
93 for _, envVar := range yamlInput["vars"].([]interface{}) {
94 envVarMap := envVar.(ContainerConfig)
95 name := envVarMap["name"].(string)
96 value := envVarMap["value"].(string)
97 container.addEnvVar(name, value)
100 return container, nil
103 func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
104 for _, v := range c.volumes {
105 if v.isDefaultWorkDir {
114 func (c *Container) getHostWorkDir() (res string) {
115 if v, ok := c.getWorkDirVolume(); ok {
121 func (c *Container) getContainerWorkDir() (res string) {
122 if v, ok := c.getWorkDirVolume(); ok {
128 func (c *Container) getContainerArguments() string {
129 args := "--ulimit nofile=90000:90000 --cap-add=all --privileged --network host --rm"
130 args += c.getVolumesAsCliOption()
131 args += c.getEnvVarsAsCliOption()
132 args += " --name " + c.name + " " + c.image
133 args += " " + c.extraRunningArgs
137 func (c *Container) create() error {
138 cmd := "docker create " + c.getContainerArguments()
140 return exechelper.Run(cmd)
143 func (c *Container) start() error {
144 cmd := "docker start " + c.name
146 return exechelper.Run(cmd)
149 func (c *Container) prepareCommand() (string, error) {
151 return "", fmt.Errorf("run container failed: name is blank")
158 cmd += " " + c.getContainerArguments()
164 func (c *Container) combinedOutput() (string, error) {
165 cmd, err := c.prepareCommand()
170 byteOutput, err := exechelper.CombinedOutput(cmd)
171 return string(byteOutput), err
174 func (c *Container) run() error {
175 cmd, err := c.prepareCommand()
180 return exechelper.Run(cmd)
183 func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
185 volume.hostDir = hostDir
186 volume.containerDir = containerDir
187 volume.isDefaultWorkDir = isDefaultWorkDir
188 c.volumes[hostDir] = volume
191 func (c *Container) getVolumesAsCliOption() string {
194 if len(c.volumes) > 0 {
195 for _, volume := range c.volumes {
196 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
203 func (c *Container) addEnvVar(name string, value string) {
204 c.envVars[name] = value
207 func (c *Container) getEnvVarsAsCliOption() string {
209 if len(c.envVars) == 0 {
213 for name, value := range c.envVars {
214 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
219 func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
220 vpp := new(VppInstance)
223 if len(additionalConfig) > 0 {
224 vpp.additionalConfig = additionalConfig[0]
232 func (c *Container) copy(sourceFileName string, targetFileName string) error {
233 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
237 func (c *Container) createFile(destFileName string, content string) error {
238 f, err := os.CreateTemp("/tmp", "hst-config")
242 defer os.Remove(f.Name())
244 if _, err := f.Write([]byte(content)); err != nil {
247 if err := f.Close(); err != nil {
250 c.copy(f.Name(), destFileName)
255 * Executes in detached mode so that the started application can continue to run
256 * without blocking execution of test
258 func (c *Container) execServer(command string, arguments ...any) {
259 serverCommand := fmt.Sprintf(command, arguments...)
260 containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
261 " " + c.name + " " + serverCommand
263 c.suite.log(containerExecCommand)
264 c.suite.assertNil(exechelper.Run(containerExecCommand))
267 func (c *Container) exec(command string, arguments ...any) string {
268 cliCommand := fmt.Sprintf(command, arguments...)
269 containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
270 " " + c.name + " " + cliCommand
272 c.suite.log(containerExecCommand)
273 byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
274 c.suite.assertNil(err)
275 return string(byteOutput)
278 func (c *Container) getLogDirPath() string {
279 testId := c.suite.getTestId()
280 testName := c.suite.T().Name()
281 logDirPath := logDir + testName + "/" + testId + "/"
283 cmd := exec.Command("mkdir", "-p", logDirPath)
284 if err := cmd.Run(); err != nil {
285 c.suite.T().Fatalf("mkdir error: %v", err)
291 func (c *Container) saveLogs() {
292 cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name)
293 if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") {
297 testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log"
299 cmd = exec.Command("docker", "logs", "--details", "-t", c.name)
300 output, err := cmd.CombinedOutput()
302 c.suite.T().Fatalf("fetching logs error: %v", err)
305 f, err := os.Create(testLogFilePath)
307 c.suite.T().Fatalf("file create error: %v", err)
309 fmt.Fprint(f, string(output))
313 func (c *Container) log() string {
314 cmd := "docker logs " + c.name
316 o, err := exechelper.CombinedOutput(cmd)
317 c.suite.assertNil(err)
321 func (c *Container) stop() error {
322 if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
323 c.vppInstance.saveLogs()
324 c.vppInstance.disconnect()
328 return exechelper.Run("docker stop " + c.name + " -t 0")
331 func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
332 template := template.Must(template.ParseFiles(templateName))
334 f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
335 c.suite.assertNil(err)
336 defer os.Remove(f.Name())
338 err = template.Execute(f, values)
339 c.suite.assertNil(err)
342 c.suite.assertNil(err)
344 c.copy(f.Name(), targetConfigName)
348 cmd := exec.Command("mkdir", "-p", logDir)
349 if err := cmd.Run(); err != nil {