github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_config_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "os" 22 "path/filepath" 23 "testing" 24 25 "github.com/containerd/nerdctl/pkg/testutil" 26 "gotest.tools/v3/assert" 27 ) 28 29 func TestComposeConfig(t *testing.T) { 30 base := testutil.NewBase(t) 31 32 var dockerComposeYAML = ` 33 services: 34 hello: 35 image: alpine:3.13 36 ` 37 38 comp := testutil.NewComposeDir(t, dockerComposeYAML) 39 defer comp.CleanUp() 40 41 base.ComposeCmd("-f", comp.YAMLFullPath(), "config").AssertOutContains("hello:") 42 } 43 44 func TestComposeConfigWithPrintService(t *testing.T) { 45 base := testutil.NewBase(t) 46 47 var dockerComposeYAML = ` 48 services: 49 hello1: 50 image: alpine:3.13 51 ` 52 53 comp := testutil.NewComposeDir(t, dockerComposeYAML) 54 defer comp.CleanUp() 55 56 base.ComposeCmd("-f", comp.YAMLFullPath(), "config", "--services").AssertOutExactly("hello1\n") 57 } 58 59 func TestComposeConfigWithPrintServiceHash(t *testing.T) { 60 base := testutil.NewBase(t) 61 62 var dockerComposeYAML = ` 63 services: 64 hello1: 65 image: alpine:%s 66 ` 67 68 comp := testutil.NewComposeDir(t, fmt.Sprintf(dockerComposeYAML, "3.13")) 69 defer comp.CleanUp() 70 71 // `--hash=*` is broken in Docker Compose v2.23.0: https://github.com/docker/compose/issues/11145 72 if base.Target == testutil.Nerdctl { 73 base.ComposeCmd("-f", comp.YAMLFullPath(), "config", "--hash=*").AssertOutContains("hello1") 74 } 75 76 hash := base.ComposeCmd("-f", comp.YAMLFullPath(), "config", "--hash=hello1").Out() 77 78 newComp := testutil.NewComposeDir(t, fmt.Sprintf(dockerComposeYAML, "3.14")) 79 defer newComp.CleanUp() 80 81 newHash := base.ComposeCmd("-f", newComp.YAMLFullPath(), "config", "--hash=hello1").Out() 82 assert.Assert(t, hash != newHash) 83 } 84 85 func TestComposeConfigWithMultipleFile(t *testing.T) { 86 base := testutil.NewBase(t) 87 88 var dockerComposeYAML = ` 89 services: 90 hello1: 91 image: alpine:3.13 92 ` 93 94 comp := testutil.NewComposeDir(t, dockerComposeYAML) 95 defer comp.CleanUp() 96 97 comp.WriteFile("docker-compose.test.yml", ` 98 services: 99 hello2: 100 image: alpine:3.14 101 `) 102 comp.WriteFile("docker-compose.override.yml", ` 103 services: 104 hello1: 105 image: alpine:3.14 106 `) 107 108 base.ComposeCmd("-f", comp.YAMLFullPath(), "-f", filepath.Join(comp.Dir(), "docker-compose.test.yml"), "config").AssertOutContains("alpine:3.14") 109 base.ComposeCmd("--project-directory", comp.Dir(), "config", "--services").AssertOutExactly("hello1\n") 110 base.ComposeCmd("--project-directory", comp.Dir(), "config").AssertOutContains("alpine:3.14") 111 } 112 113 func TestComposeConfigWithComposeFileEnv(t *testing.T) { 114 base := testutil.NewBase(t) 115 116 var dockerComposeYAML = ` 117 services: 118 hello1: 119 image: alpine:3.13 120 ` 121 122 comp := testutil.NewComposeDir(t, dockerComposeYAML) 123 defer comp.CleanUp() 124 125 comp.WriteFile("docker-compose.test.yml", ` 126 services: 127 hello2: 128 image: alpine:3.14 129 `) 130 131 base.Env = append(os.Environ(), "COMPOSE_FILE="+comp.YAMLFullPath()+","+filepath.Join(comp.Dir(), "docker-compose.test.yml"), "COMPOSE_PATH_SEPARATOR=,") 132 133 base.ComposeCmd("config").AssertOutContains("alpine:3.14") 134 base.ComposeCmd("--project-directory", comp.Dir(), "config", "--services").AssertOutContainsAll("hello1\n", "hello2\n") 135 base.ComposeCmd("--project-directory", comp.Dir(), "config").AssertOutContains("alpine:3.14") 136 } 137 138 func TestComposeConfigWithEnvFile(t *testing.T) { 139 base := testutil.NewBase(t) 140 141 const dockerComposeYAML = ` 142 services: 143 hello: 144 image: ${image} 145 ` 146 147 comp := testutil.NewComposeDir(t, dockerComposeYAML) 148 defer comp.CleanUp() 149 150 envFile := filepath.Join(comp.Dir(), "env") 151 const envFileContent = ` 152 image: hello-world 153 ` 154 assert.NilError(t, os.WriteFile(envFile, []byte(envFileContent), 0644)) 155 156 base.ComposeCmd("-f", comp.YAMLFullPath(), "--env-file", envFile, "config").AssertOutContains("image: hello-world") 157 }