github.com/mponton/terratest@v0.44.0/modules/docker/run.go (about) 1 package docker 2 3 import ( 4 "github.com/mponton/terratest/modules/logger" 5 "github.com/mponton/terratest/modules/shell" 6 "github.com/mponton/terratest/modules/testing" 7 "github.com/stretchr/testify/require" 8 ) 9 10 // RunOptions defines options that can be passed to the 'docker run' command. 11 type RunOptions struct { 12 // Override the default COMMAND of the Docker image 13 Command []string 14 15 // If set to true, pass the --detach flag to 'docker run' to run the container in the background 16 Detach bool 17 18 // Override the default ENTRYPOINT of the Docker image 19 Entrypoint string 20 21 // Set environment variables 22 EnvironmentVariables []string 23 24 // If set to true, pass the --init flag to 'docker run' to run an init inside the container that forwards signals 25 // and reaps processes 26 Init bool 27 28 // Assign a name to the container 29 Name string 30 31 // If set to true, pass the --privileged flag to 'docker run' to give extended privileges to the container 32 Privileged bool 33 34 // If set to true, pass the --rm flag to 'docker run' to automatically remove the container when it exits 35 Remove bool 36 37 // If set to true, pass the -tty flag to 'docker run' to allocate a pseudo-TTY 38 Tty bool 39 40 // Username or UID 41 User string 42 43 // Bind mount these volume(s) when running the container 44 Volumes []string 45 46 // Custom CLI options that will be passed as-is to the 'docker run' command. This is an "escape hatch" that allows 47 // Terratest to not have to support every single command-line option offered by the 'docker run' command, and 48 // solely focus on the most important ones. 49 OtherOptions []string 50 51 // Set a logger that should be used. See the logger package for more info. 52 Logger *logger.Logger 53 } 54 55 // Run runs the 'docker run' command on the given image with the given options and return stdout/stderr. This method 56 // fails the test if there are any errors. 57 func Run(t testing.TestingT, image string, options *RunOptions) string { 58 out, err := RunE(t, image, options) 59 require.NoError(t, err) 60 return out 61 } 62 63 // RunE runs the 'docker run' command on the given image with the given options and return stdout/stderr, or any error. 64 func RunE(t testing.TestingT, image string, options *RunOptions) (string, error) { 65 options.Logger.Logf(t, "Running 'docker run' on image '%s'", image) 66 67 args, err := formatDockerRunArgs(image, options) 68 if err != nil { 69 return "", err 70 } 71 72 cmd := shell.Command{ 73 Command: "docker", 74 Args: args, 75 Logger: options.Logger, 76 } 77 78 return shell.RunCommandAndGetOutputE(t, cmd) 79 } 80 81 // RunAndGetID runs the 'docker run' command on the given image with the given options and returns the container ID 82 // that is returned in stdout. This method fails the test if there are any errors. 83 func RunAndGetID(t testing.TestingT, image string, options *RunOptions) string { 84 out, err := RunAndGetIDE(t, image, options) 85 require.NoError(t, err) 86 return out 87 } 88 89 // RunAndGetIDE runs the 'docker run' command on the given image with the given options and returns the container ID 90 // that is returned in stdout, or any error. 91 func RunAndGetIDE(t testing.TestingT, image string, options *RunOptions) (string, error) { 92 options.Logger.Logf(t, "Running 'docker run' on image '%s', returning stdout", image) 93 94 args, err := formatDockerRunArgs(image, options) 95 if err != nil { 96 return "", err 97 } 98 99 cmd := shell.Command{ 100 Command: "docker", 101 Args: args, 102 Logger: options.Logger, 103 } 104 105 return shell.RunCommandAndGetStdOutE(t, cmd) 106 } 107 108 // formatDockerRunArgs formats the arguments for the 'docker run' command. 109 func formatDockerRunArgs(image string, options *RunOptions) ([]string, error) { 110 args := []string{"run"} 111 112 if options.Detach { 113 args = append(args, "--detach") 114 } 115 116 if options.Entrypoint != "" { 117 args = append(args, "--entrypoint", options.Entrypoint) 118 } 119 120 for _, envVar := range options.EnvironmentVariables { 121 args = append(args, "--env", envVar) 122 } 123 124 if options.Init { 125 args = append(args, "--init") 126 } 127 128 if options.Name != "" { 129 args = append(args, "--name", options.Name) 130 } 131 132 if options.Privileged { 133 args = append(args, "--privileged") 134 } 135 136 if options.Remove { 137 args = append(args, "--rm") 138 } 139 140 if options.Tty { 141 args = append(args, "--tty") 142 } 143 144 if options.User != "" { 145 args = append(args, "--user", options.User) 146 } 147 148 for _, volume := range options.Volumes { 149 args = append(args, "--volume", volume) 150 } 151 152 args = append(args, options.OtherOptions...) 153 154 args = append(args, image) 155 156 args = append(args, options.Command...) 157 158 return args, nil 159 }