2021-02-24 10:53:06 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-05-31 14:59:18 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-02-24 10:53:06 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
2022-05-31 14:59:18 +02:00
|
|
|
"gotest.tools/v3/icmd"
|
2021-02-24 10:53:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocalComposeVolume(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2021-02-24 10:53:06 +01:00
|
|
|
|
|
|
|
const projectName = "compose-e2e-volume"
|
|
|
|
|
|
|
|
t.Run("up with build and no image name, volume", func(t *testing.T) {
|
|
|
|
// ensure local test run does not reuse previously build image
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerOrExitError(t, "rmi", "compose-e2e-volume_nginx")
|
|
|
|
c.RunDockerOrExitError(t, "volume", "rm", projectName+"_staticVol")
|
|
|
|
c.RunDockerOrExitError(t, "volume", "rm", "myvolume")
|
|
|
|
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up",
|
|
|
|
"-d")
|
2021-02-24 10:53:06 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("access bind mount data", func(t *testing.T) {
|
|
|
|
output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)
|
|
|
|
assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check container volume specs", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "inspect", "compose-e2e-volume-nginx2-1", "--format", "{{ json .Mounts }}")
|
2021-02-24 10:53:06 +01:00
|
|
|
output := res.Stdout()
|
2022-07-13 19:20:40 +02:00
|
|
|
//nolint
|
2021-04-27 12:18:12 +02:00
|
|
|
assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"z","RW":true,"Propagation":""`), output)
|
2021-02-24 10:53:06 +01:00
|
|
|
assert.Assert(t, strings.Contains(output, `"Destination":"/myconfig","Mode":"","RW":false,"Propagation":"rprivate"`), output)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check config content", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
output := c.RunDockerCmd(t, "exec", "compose-e2e-volume-nginx2-1", "cat", "/myconfig").Stdout()
|
2021-02-24 10:53:06 +01:00
|
|
|
assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check secrets content", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
output := c.RunDockerCmd(t, "exec", "compose-e2e-volume-nginx2-1", "cat", "/run/secrets/mysecret").Stdout()
|
2021-02-24 10:53:06 +01:00
|
|
|
assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check container bind-mounts specs", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "inspect", "compose-e2e-volume-nginx-1", "--format", "{{ json .Mounts }}")
|
2021-02-24 10:53:06 +01:00
|
|
|
output := res.Stdout()
|
2022-07-13 19:20:40 +02:00
|
|
|
//nolint
|
2021-02-24 10:53:06 +01:00
|
|
|
assert.Assert(t, strings.Contains(output, `"Type":"bind"`))
|
2021-04-27 12:18:12 +02:00
|
|
|
assert.Assert(t, strings.Contains(output, `"Destination":"/usr/share/nginx/html"`))
|
2021-02-24 10:53:06 +01:00
|
|
|
})
|
|
|
|
|
2021-06-22 14:37:52 +02:00
|
|
|
t.Run("should inherit anonymous volumes", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "touch", "/usr/src/app/node_modules/test")
|
2022-06-15 21:32:00 +02:00
|
|
|
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "-d")
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "ls", "/usr/src/app/node_modules/test")
|
2021-06-22 14:37:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should renew anonymous volumes", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "touch", "/usr/src/app/node_modules/test")
|
2022-06-15 21:32:00 +02:00
|
|
|
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "--renew-anon-volumes", "-d")
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "ls", "/usr/src/app/node_modules/test")
|
2021-06-22 14:37:52 +02:00
|
|
|
})
|
|
|
|
|
2021-02-24 10:53:06 +01:00
|
|
|
t.Run("cleanup volume project", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "--volumes")
|
|
|
|
ls := c.RunDockerCmd(t, "volume", "ls").Stdout()
|
2022-01-03 09:08:41 +01:00
|
|
|
assert.Assert(t, !strings.Contains(ls, projectName+"_staticVol"))
|
|
|
|
assert.Assert(t, !strings.Contains(ls, "myvolume"))
|
2021-02-24 10:53:06 +01:00
|
|
|
})
|
|
|
|
}
|
2022-05-31 14:59:18 +02:00
|
|
|
|
|
|
|
func TestProjectVolumeBind(t *testing.T) {
|
|
|
|
if composeStandaloneMode {
|
|
|
|
t.Skip()
|
|
|
|
}
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2022-05-31 14:59:18 +02:00
|
|
|
const projectName = "compose-e2e-project-volume-bind"
|
|
|
|
|
|
|
|
t.Run("up on project volume with bind specification", func(t *testing.T) {
|
|
|
|
tmpDir, err := os.MkdirTemp("", projectName)
|
|
|
|
assert.NilError(t, err)
|
2022-07-13 19:20:40 +02:00
|
|
|
defer os.RemoveAll(tmpDir) //nolint
|
2022-05-31 14:59:18 +02:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2022-05-31 14:59:18 +02:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerOrExitError(t, "volume", "rm", "-f", projectName+"_project_data").Assert(t, icmd.Success)
|
2022-05-31 14:59:18 +02:00
|
|
|
cmd := c.NewCmdWithEnv([]string{"TEST_DIR=" + tmpDir},
|
|
|
|
"docker", "compose", "--project-directory", "fixtures/project-volume-bind-test", "--project-name", projectName, "up", "-d")
|
|
|
|
icmd.RunCmd(cmd).Assert(t, icmd.Success)
|
2022-06-15 21:55:58 +02:00
|
|
|
defer c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2022-05-31 14:59:18 +02:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunCmd(t, "sh", "-c", "echo SUCCESS > "+filepath.Join(tmpDir, "resultfile")).Assert(t, icmd.Success)
|
2022-05-31 14:59:18 +02:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
ret := c.RunDockerOrExitError(t, "exec", "frontend", "bash", "-c", "cat /data/resultfile").Assert(t, icmd.Success)
|
2022-05-31 14:59:18 +02:00
|
|
|
assert.Assert(t, strings.Contains(ret.Stdout(), "SUCCESS"))
|
|
|
|
})
|
|
|
|
}
|