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