go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/connection/container/docker_engine/command_test.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 //go:build debugtest 5 // +build debugtest 6 7 package docker_engine 8 9 import ( 10 "context" 11 "io" 12 "os" 13 "testing" 14 15 docker_types "github.com/docker/docker/api/types" 16 "github.com/docker/docker/api/types/container" 17 "github.com/docker/docker/client" 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 ) 21 22 func startContainer() (*client.Client, string, error) { 23 // os.Setenv("DOCKER_API_VERSION", "1.26") 24 // Start new docker container 25 ctx := context.Background() 26 var err error 27 dockerClient, err := client.NewClientWithOpts(client.FromEnv) 28 if err != nil { 29 return nil, "", err 30 } 31 32 // ensure we kill container if something went wrong during assertion 33 // we can ignore errors here 34 dockerClient.ContainerKill(ctx, "motor-docker-test", "SIGKILL") 35 dockerClient.ContainerRemove(ctx, "motor-docker-test", docker_types.ContainerRemoveOptions{Force: true}) 36 37 imageName := "ubuntu" 38 39 out, err := dockerClient.ImagePull(ctx, imageName, docker_types.ImagePullOptions{}) 40 if err != nil { 41 return nil, "", err 42 } 43 io.Copy(os.Stdout, out) 44 45 resp, err := dockerClient.ContainerCreate(ctx, &container.Config{ 46 Image: imageName, 47 Cmd: []string{"/bin/bash"}, 48 Tty: true, 49 }, nil, nil, "motor-docker-test") 50 if err != nil { 51 return nil, "", err 52 } 53 54 if err := dockerClient.ContainerStart(ctx, resp.ID, docker_types.ContainerStartOptions{}); err != nil { 55 return nil, "", err 56 } 57 return dockerClient, resp.ID, nil 58 } 59 60 func tearDownContainer(dockerClient *client.Client, containerID string) error { 61 // Stop Container 62 return dockerClient.ContainerKill(context.Background(), containerID, "SIGKILL") 63 } 64 65 func TestDockerCommand(t *testing.T) { 66 dockerClient, containerID, err := startContainer() 67 require.NoError(t, err) 68 defer tearDownContainer(dockerClient, containerID) 69 70 // Execute tests 71 t.Run("echo", func(t *testing.T) { 72 c := &Command{dockerClient: dockerClient, Container: containerID} 73 cmd, err := c.Exec("echo 'test'") 74 require.NoError(t, err) 75 assert.Equal(t, "echo 'test'", cmd.Command, "they should be equal") 76 assert.Equal(t, nil, err, "should execute without error") 77 78 stdout, _ := io.ReadAll(cmd.Stdout) 79 assert.Equal(t, "test\n", string(stdout), "output should be correct") 80 stderr, _ := io.ReadAll(cmd.Stderr) 81 assert.Equal(t, "", string(stderr), "output should be correct") 82 }) 83 84 t.Run("echo pipe", func(t *testing.T) { 85 cErr := &Command{dockerClient: dockerClient, Container: containerID} 86 87 cmd, err := cErr.Exec("echo 'This message goes to stderr' >&2") 88 require.NoError(t, err) 89 90 assert.Equal(t, "echo 'This message goes to stderr' >&2", cmd.Command, "they should be equal") 91 assert.Equal(t, nil, err, "should execute without error") 92 93 stdout, _ := io.ReadAll(cmd.Stdout) 94 assert.Equal(t, "", string(stdout), "output should be correct") 95 96 stderr, _ := io.ReadAll(cmd.Stderr) 97 assert.Equal(t, "This message goes to stderr\n", string(stderr), "output should be correct") 98 }) 99 }