skip_win_ci_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "strings"
  22. "syscall"
  23. "testing"
  24. "time"
  25. "gotest.tools/golden"
  26. "gotest.tools/v3/assert"
  27. "gotest.tools/v3/icmd"
  28. "gotest.tools/v3/poll"
  29. "github.com/docker/compose-cli/cli/cmd"
  30. . "github.com/docker/compose-cli/tests/framework"
  31. )
  32. var binDir string
  33. func TestMain(m *testing.M) {
  34. p, cleanup, err := SetupExistingCLI()
  35. if err != nil {
  36. fmt.Println(err)
  37. os.Exit(1)
  38. }
  39. binDir = p
  40. exitCode := m.Run()
  41. cleanup()
  42. os.Exit(exitCode)
  43. }
  44. func TestKillChildProcess(t *testing.T) {
  45. c := NewParallelE2eCLI(t, binDir)
  46. image := "test-sleep-image"
  47. pCmd := icmd.Command("ps", "-x")
  48. if runtime.GOOS == "windows" {
  49. pCmd = icmd.Command("tasklist")
  50. }
  51. pRes := icmd.RunCmd(pCmd)
  52. pRes.Assert(t, icmd.Success)
  53. assert.Assert(t, !strings.Contains(pRes.Combined(), image))
  54. d := writeDockerfile(t)
  55. buildArgs := []string{"build", "--no-cache", "-t", image, "."}
  56. cmd := c.NewDockerCmd(buildArgs...)
  57. cmd.Dir = d
  58. res := icmd.StartCmd(cmd)
  59. buildRunning := func(t poll.LogT) poll.Result {
  60. res := icmd.RunCmd(pCmd)
  61. if strings.Contains(res.Combined(), strings.Join(buildArgs, " ")) {
  62. return poll.Success()
  63. }
  64. return poll.Continue("waiting for child process to be running")
  65. }
  66. poll.WaitOn(t, buildRunning, poll.WithDelay(1*time.Second))
  67. if runtime.GOOS == "windows" {
  68. err := res.Cmd.Process.Kill()
  69. assert.NilError(t, err)
  70. } else {
  71. err := res.Cmd.Process.Signal(syscall.SIGTERM)
  72. assert.NilError(t, err)
  73. }
  74. buildStopped := func(t poll.LogT) poll.Result {
  75. res := icmd.RunCmd(pCmd)
  76. if !strings.Contains(res.Combined(), strings.Join(buildArgs, " ")) {
  77. return poll.Success()
  78. }
  79. return poll.Continue("waiting for child process to be killed")
  80. }
  81. poll.WaitOn(t, buildStopped, poll.WithDelay(1*time.Second), poll.WithTimeout(60*time.Second))
  82. }
  83. // no linux containers on GHA Windows CI nodes (windows server)
  84. func TestLocalContainers(t *testing.T) {
  85. c := NewParallelE2eCLI(t, binDir)
  86. c.RunDockerCmd("context", "create", "local", "test-local")
  87. res := c.RunDockerCmd("context", "use", "test-local")
  88. res.Assert(t, icmd.Expected{Out: "test-local"})
  89. t.Run("use", func(t *testing.T) {
  90. res := c.RunDockerCmd("context", "show")
  91. res.Assert(t, icmd.Expected{Out: "test-local"})
  92. res = c.RunDockerCmd("context", "ls")
  93. golden.Assert(t, res.Stdout(), GoldenFile("ls-out-test-local"))
  94. })
  95. var nginxContainerName string
  96. t.Run("run", func(t *testing.T) {
  97. res := c.RunDockerCmd("run", "-d", "-p", "85:80", "nginx")
  98. nginxContainerName = strings.TrimSpace(res.Stdout())
  99. })
  100. defer c.RunDockerOrExitError("rm", "-f", nginxContainerName)
  101. var nginxID string
  102. t.Run("inspect", func(t *testing.T) {
  103. res = c.RunDockerCmd("inspect", nginxContainerName)
  104. inspect := &cmd.ContainerInspectView{}
  105. err := json.Unmarshal([]byte(res.Stdout()), inspect)
  106. assert.NilError(t, err)
  107. nginxID = inspect.ID
  108. })
  109. t.Run("ps", func(t *testing.T) {
  110. res = c.RunDockerCmd("ps")
  111. lines := Lines(res.Stdout())
  112. nginxFound := false
  113. for _, line := range lines {
  114. fields := strings.Fields(line)
  115. if fields[0] == nginxID {
  116. nginxFound = true
  117. assert.Equal(t, fields[1], "nginx")
  118. assert.Equal(t, fields[2], "/docker-entrypoint.sh")
  119. }
  120. }
  121. assert.Assert(t, nginxFound, res.Stdout())
  122. res = c.RunDockerCmd("ps", "--format", "json")
  123. res.Assert(t, icmd.Expected{Out: `"Image":"nginx","Status":"Up Less than a second","Command":"/docker-entrypoint.sh nginx -g 'daemon off;'","Ports":["0.0.0.0:85->80/tcp"`})
  124. res = c.RunDockerCmd("ps", "--quiet")
  125. res.Assert(t, icmd.Expected{Out: nginxID + "\n"})
  126. })
  127. }
  128. func writeDockerfile(t *testing.T) string {
  129. d, err := ioutil.TempDir("", "")
  130. assert.NilError(t, err)
  131. t.Cleanup(func() {
  132. _ = os.RemoveAll(d)
  133. })
  134. err = ioutil.WriteFile(filepath.Join(d, "Dockerfile"), []byte(`FROM alpine:3.10
  135. RUN sleep 100`), 0644)
  136. assert.NilError(t, err)
  137. return d
  138. }