compose/pkg/e2e/cp_test.go
Nicolas De Loof d38fa3431a test(e2e): cp, environment and config scenarios
- cp: the twelve copy directions become one scenario; Dir() joins the
  Scenario API for actions that exchange files with the host, and
  FileContains observes what landed there. The files copied in and out
  live in the txtar archive instead of writing into the fixture tree.
- environment: TestEnvPriority splits into three scenarios, one per
  compose model (environment section, bare, env_file section), each
  declaring its .env variants inline; StdoutContains joins the
  vocabulary so container output cannot collide with progress noise
  (the unused framework predicate of the same name is removed). The
  fixed 'env-compose-priority' image name becomes project-scoped.
- config: rendering assertions move to scenario steps; the config
  output is the observable here by definition.
  TestConfigHashMatchesContainerLabel stays legacy: it cross-references
  hashes captured at runtime, which checks cannot hold.
- Fixtures cp-test/, environment/ and config/ are removed.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-08-18 16:49:56 +02:00

60 lines
2.7 KiB
Go

/*
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 (
"path/filepath"
"testing"
)
func TestCopy(t *testing.T) {
s := NewScenario(t, "cp must target all replicas by default, one replica with --index, and copy both ways")
dir := s.Dir()
s.Step("up starts five replicas",
ComposeCmd("up", "--scale", "nginx=5", "-d"),
ServiceScale("nginx", 5)).
Step("cp to the service reaches every replica by default",
ComposeCmd("cp", filepath.Join(dir, "cp-me.txt"), "nginx:/tmp/default.txt")).
Step("replica #1 got the file",
DockerCmd("exec", s.Project()+"-nginx-1", "cat", "/tmp/default.txt"),
OutputContains("hello world")).
Step("replica #3 got the file too",
DockerCmd("exec", s.Project()+"-nginx-3", "cat", "/tmp/default.txt"),
OutputContains("hello world")).
Step("cp --index targets a single replica",
ComposeCmd("cp", "--index=3", filepath.Join(dir, "cp-me.txt"), "nginx:/tmp/indexed.txt")).
Step("the indexed replica got the file",
DockerCmd("exec", s.Project()+"-nginx-3", "cat", "/tmp/indexed.txt"),
OutputContains("hello world")).
Step("the other replicas did not",
DockerCmd("exec", s.Project()+"-nginx-2", "cat", "/tmp/indexed.txt").MayFail(),
ExitCode(1)).
Step("cp from the service reads from replica #1 by default",
ComposeCmd("cp", "nginx:/tmp/default.txt", filepath.Join(dir, "from-default.txt")),
FileContains(filepath.Join(dir, "from-default.txt"), "hello world")).
Step("cp --index reads from the chosen replica",
ComposeCmd("cp", "--index=3", "nginx:/tmp/indexed.txt", filepath.Join(dir, "from-indexed.txt")),
FileContains(filepath.Join(dir, "from-indexed.txt"), "hello world")).
Step("cp copies a folder into the container",
ComposeCmd("cp", filepath.Join(dir, "cp-folder"), "nginx:/tmp")).
Step("the folder's content is in the container",
DockerCmd("exec", s.Project()+"-nginx-1", "cat", "/tmp/cp-folder/cp-me.txt"),
OutputContains("hello world from folder")).
Step("cp copies a folder out of the container",
ComposeCmd("cp", "nginx:/tmp/cp-folder", filepath.Join(dir, "cp-folder2")),
FileContains(filepath.Join(dir, "cp-folder2", "cp-me.txt"), "hello world from folder"))
}