github.com/someshkoli/terratest@v0.41.1/modules/docker/stop_test.go (about) 1 package docker 2 3 import ( 4 "crypto/tls" 5 "fmt" 6 "strconv" 7 "strings" 8 "testing" 9 "time" 10 11 http_helper "github.com/gruntwork-io/terratest/modules/http-helper" 12 "github.com/gruntwork-io/terratest/modules/shell" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestStop(t *testing.T) { 17 t.Parallel() 18 19 // appending timestamp to container name to run tests in parallel 20 name := "test-nginx" + strconv.FormatInt(time.Now().UnixNano(), 10) 21 22 // choosing a unique port since 80 may not fly well on test machines 23 port := "13030" 24 host := GetDockerHost() 25 26 testURL := fmt.Sprintf("http://%s:%s", host, port) 27 28 // for testing the stopping of a docker container 29 // we got to run a container first and then stop it 30 runOpts := &RunOptions{ 31 Detach: true, 32 Name: name, 33 Remove: true, 34 OtherOptions: []string{"-p", port + ":80"}, 35 } 36 Run(t, "nginx:1.17-alpine", runOpts) 37 38 // verify nginx is running 39 http_helper.HttpGetWithRetryWithCustomValidation(t, testURL, &tls.Config{}, 60, 2*time.Second, verifyNginxIsUp) 40 41 // try to stop it now 42 out := Stop(t, []string{name}, &StopOptions{}) 43 require.Contains(t, out, name) 44 45 // verify nginx is down 46 // run a docker ps with name filter 47 command := shell.Command{ 48 Command: "docker", 49 Args: []string{"ps", "-q", "--filter", "name=" + name}, 50 } 51 output := shell.RunCommandAndGetStdOut(t, command) 52 require.Empty(t, output) 53 } 54 55 func verifyNginxIsUp(statusCode int, body string) bool { 56 return statusCode == 200 && strings.Contains(body, "nginx!") 57 }