|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI 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 compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/compose-spec/compose-go/v2/types" |
| 26 | + "github.com/docker/cli/cli/streams" |
| 27 | + logrustest "github.com/sirupsen/logrus/hooks/test" |
| 28 | + "go.uber.org/mock/gomock" |
| 29 | + "gotest.tools/v3/assert" |
| 30 | + |
| 31 | + "github.com/docker/compose/v5/pkg/mocks" |
| 32 | +) |
| 33 | + |
| 34 | +func projectDir(t *testing.T, composeContent string) string { |
| 35 | + t.Helper() |
| 36 | + dir := t.TempDir() |
| 37 | + if composeContent != "" { |
| 38 | + assert.NilError(t, os.WriteFile(filepath.Join(dir, "compose.yaml"), []byte(composeContent), 0o600)) |
| 39 | + } |
| 40 | + return dir |
| 41 | +} |
| 42 | + |
| 43 | +func resolutionCli(t *testing.T) *mocks.MockCli { |
| 44 | + t.Helper() |
| 45 | + ctrl := gomock.NewController(t) |
| 46 | + cli := mocks.NewMockCli(ctrl) |
| 47 | + cli.EXPECT().Out().Return(streams.NewOut(os.Stdout)).AnyTimes() |
| 48 | + cli.EXPECT().Err().Return(streams.NewOut(os.Stderr)).AnyTimes() |
| 49 | + return cli |
| 50 | +} |
| 51 | + |
| 52 | +const validCompose = "services:\n web:\n image: alpine\n gated:\n image: alpine\n profiles: [debug]\n" |
| 53 | + |
| 54 | +// The resolution matrix of projectOrName: one name precedence |
| 55 | +// (--project-name, then COMPOSE_PROJECT_NAME, then the model), a hard error |
| 56 | +// for an explicit --file that cannot be read, and a label-based fallback |
| 57 | +// only when COMPOSE_PROJECT_NAME provides a name. |
| 58 | +func TestProjectOrNameResolution(t *testing.T) { |
| 59 | + unsetEnv := func(t *testing.T) { |
| 60 | + t.Setenv(ComposeProjectName, "") |
| 61 | + assert.NilError(t, os.Unsetenv(ComposeProjectName)) |
| 62 | + } |
| 63 | + |
| 64 | + t.Run("broken implicit file falls back to COMPOSE_PROJECT_NAME", func(t *testing.T) { |
| 65 | + t.Setenv(ComposeProjectName, "fallback") |
| 66 | + dir := projectDir(t, "services: {invalid") |
| 67 | + opts := ProjectOptions{ProjectDir: dir} |
| 68 | + hook := logrustest.NewGlobal() |
| 69 | + |
| 70 | + project, name, err := opts.projectOrName(t.Context(), resolutionCli(t)) |
| 71 | + assert.NilError(t, err) |
| 72 | + assert.Equal(t, name, "fallback") |
| 73 | + assert.Assert(t, project == nil) |
| 74 | + assert.Equal(t, len(hook.AllEntries()), 1) |
| 75 | + assert.Assert(t, strings.Contains(hook.LastEntry().Message, "falling back to label-based mode"), hook.LastEntry().Message) |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("broken explicit --file is a hard error even with COMPOSE_PROJECT_NAME", func(t *testing.T) { |
| 79 | + t.Setenv(ComposeProjectName, "fallback") |
| 80 | + dir := projectDir(t, "services: {invalid") |
| 81 | + opts := ProjectOptions{ConfigPaths: []string{filepath.Join(dir, "compose.yaml")}, ProjectDir: dir} |
| 82 | + |
| 83 | + _, _, err := opts.projectOrName(t.Context(), resolutionCli(t)) |
| 84 | + assert.ErrorContains(t, err, "yaml") |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("no file at all with COMPOSE_PROJECT_NAME is the file-less workflow", func(t *testing.T) { |
| 88 | + t.Setenv(ComposeProjectName, "labels-only") |
| 89 | + dir := projectDir(t, "") |
| 90 | + opts := ProjectOptions{ProjectDir: dir} |
| 91 | + hook := logrustest.NewGlobal() |
| 92 | + |
| 93 | + project, name, err := opts.projectOrName(t.Context(), resolutionCli(t)) |
| 94 | + assert.NilError(t, err) |
| 95 | + assert.Equal(t, name, "labels-only") |
| 96 | + assert.Assert(t, project == nil) |
| 97 | + // no file exists at all, so the "found but could not be loaded" |
| 98 | + // warning would be factually wrong here — this is the normal |
| 99 | + // file-less workflow, not a fallback from a broken file. |
| 100 | + assert.Equal(t, len(hook.AllEntries()), 0, hook.AllEntries()) |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("no file and no name errors", func(t *testing.T) { |
| 104 | + unsetEnv(t) |
| 105 | + dir := projectDir(t, "") |
| 106 | + opts := ProjectOptions{ProjectDir: dir} |
| 107 | + |
| 108 | + _, _, err := opts.projectOrName(t.Context(), resolutionCli(t)) |
| 109 | + assert.Assert(t, err != nil) |
| 110 | + }) |
| 111 | + |
| 112 | + t.Run("COMPOSE_PROJECT_NAME overrides the loaded model's name", func(t *testing.T) { |
| 113 | + t.Setenv(ComposeProjectName, "from-env") |
| 114 | + dir := projectDir(t, validCompose) |
| 115 | + opts := ProjectOptions{ProjectDir: dir} |
| 116 | + |
| 117 | + project, name, err := opts.projectOrName(t.Context(), resolutionCli(t)) |
| 118 | + assert.NilError(t, err) |
| 119 | + assert.Equal(t, name, "from-env") |
| 120 | + assert.Assert(t, project != nil) |
| 121 | + }) |
| 122 | + |
| 123 | + t.Run("--project-name without --file skips loading, even a broken file", func(t *testing.T) { |
| 124 | + unsetEnv(t) |
| 125 | + dir := projectDir(t, "services: {invalid") |
| 126 | + opts := ProjectOptions{ProjectName: "explicit", ProjectDir: dir} |
| 127 | + |
| 128 | + project, name, err := opts.projectOrName(t.Context(), resolutionCli(t)) |
| 129 | + assert.NilError(t, err) |
| 130 | + assert.Equal(t, name, "explicit") |
| 131 | + assert.Assert(t, project == nil) |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("unknown requested service is rejected by the load itself", func(t *testing.T) { |
| 135 | + unsetEnv(t) |
| 136 | + dir := projectDir(t, validCompose) |
| 137 | + opts := ProjectOptions{ProjectDir: dir} |
| 138 | + |
| 139 | + _, _, err := opts.projectOrName(t.Context(), resolutionCli(t), "typo") |
| 140 | + assert.ErrorContains(t, err, "no such service") |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +// validateServiceNames backs the commands that don't pass their service |
| 145 | +// arguments through the load-time selection (restart, wait): strict when a |
| 146 | +// model is available, no-op in label-based mode. |
| 147 | +func TestValidateServiceNames(t *testing.T) { |
| 148 | + project := &types.Project{ |
| 149 | + Services: types.Services{"web": {Name: "web"}}, |
| 150 | + DisabledServices: types.Services{"gated": {Name: "gated"}}, |
| 151 | + Jobs: types.Jobs{"migrate": {Name: "migrate"}}, |
| 152 | + } |
| 153 | + |
| 154 | + assert.NilError(t, validateServiceNames(nil, []string{"anything"})) |
| 155 | + assert.NilError(t, validateServiceNames(project, []string{"web"})) |
| 156 | + // profile-disabled services are legitimate targets: restart enables them |
| 157 | + assert.NilError(t, validateServiceNames(project, []string{"gated"})) |
| 158 | + assert.Error(t, validateServiceNames(project, []string{"typo"}), "no such service: typo") |
| 159 | + // restart/wait pass no services to projectOrName, so a job target never |
| 160 | + // reaches jobTargetErr there -- validateServiceNames must recognize it |
| 161 | + // itself instead of falling through to the generic "no such service". |
| 162 | + assert.Error(t, validateServiceNames(project, []string{"migrate"}), `job "migrate" can only be triggered with "docker compose run"`) |
| 163 | +} |
0 commit comments