-
Notifications
You must be signed in to change notification settings - Fork 5.8k
cli: one project-resolution story — single name precedence, no swallowed load error, strict service validation #14151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ndeloof
wants to merge
1
commit into
docker:main
Choose a base branch
from
ndeloof:f4-project-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| 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 compose | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/compose-spec/compose-go/v2/types" | ||
| "github.com/docker/cli/cli/streams" | ||
| logrustest "github.com/sirupsen/logrus/hooks/test" | ||
| "go.uber.org/mock/gomock" | ||
| "gotest.tools/v3/assert" | ||
|
|
||
| "github.com/docker/compose/v5/pkg/mocks" | ||
| ) | ||
|
|
||
| func projectDir(t *testing.T, composeContent string) string { | ||
| t.Helper() | ||
| dir := t.TempDir() | ||
| if composeContent != "" { | ||
| assert.NilError(t, os.WriteFile(filepath.Join(dir, "compose.yaml"), []byte(composeContent), 0o600)) | ||
| } | ||
| return dir | ||
| } | ||
|
|
||
| func resolutionCli(t *testing.T) *mocks.MockCli { | ||
| t.Helper() | ||
| ctrl := gomock.NewController(t) | ||
| cli := mocks.NewMockCli(ctrl) | ||
| cli.EXPECT().Out().Return(streams.NewOut(os.Stdout)).AnyTimes() | ||
| cli.EXPECT().Err().Return(streams.NewOut(os.Stderr)).AnyTimes() | ||
| return cli | ||
| } | ||
|
|
||
| const validCompose = "services:\n web:\n image: alpine\n gated:\n image: alpine\n profiles: [debug]\n" | ||
|
|
||
| // The resolution matrix of projectOrName: one name precedence | ||
| // (--project-name, then COMPOSE_PROJECT_NAME, then the model), a hard error | ||
| // for an explicit --file that cannot be read, and a label-based fallback | ||
| // only when COMPOSE_PROJECT_NAME provides a name. | ||
| func TestProjectOrNameResolution(t *testing.T) { | ||
| unsetEnv := func(t *testing.T) { | ||
| t.Setenv(ComposeProjectName, "") | ||
| assert.NilError(t, os.Unsetenv(ComposeProjectName)) | ||
| } | ||
|
|
||
| t.Run("broken implicit file falls back to COMPOSE_PROJECT_NAME", func(t *testing.T) { | ||
| t.Setenv(ComposeProjectName, "fallback") | ||
| dir := projectDir(t, "services: {invalid") | ||
| opts := ProjectOptions{ProjectDir: dir} | ||
| hook := logrustest.NewGlobal() | ||
|
|
||
| project, name, err := opts.projectOrName(t.Context(), resolutionCli(t)) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, name, "fallback") | ||
| assert.Assert(t, project == nil) | ||
| assert.Equal(t, len(hook.AllEntries()), 1) | ||
| assert.Assert(t, strings.Contains(hook.LastEntry().Message, "falling back to label-based mode"), hook.LastEntry().Message) | ||
| }) | ||
|
|
||
| t.Run("broken explicit --file is a hard error even with COMPOSE_PROJECT_NAME", func(t *testing.T) { | ||
| t.Setenv(ComposeProjectName, "fallback") | ||
| dir := projectDir(t, "services: {invalid") | ||
| opts := ProjectOptions{ConfigPaths: []string{filepath.Join(dir, "compose.yaml")}, ProjectDir: dir} | ||
|
|
||
| _, _, err := opts.projectOrName(t.Context(), resolutionCli(t)) | ||
| assert.ErrorContains(t, err, "yaml") | ||
| }) | ||
|
|
||
| t.Run("no file at all with COMPOSE_PROJECT_NAME is the file-less workflow", func(t *testing.T) { | ||
| t.Setenv(ComposeProjectName, "labels-only") | ||
| dir := projectDir(t, "") | ||
| opts := ProjectOptions{ProjectDir: dir} | ||
| hook := logrustest.NewGlobal() | ||
|
|
||
| project, name, err := opts.projectOrName(t.Context(), resolutionCli(t)) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, name, "labels-only") | ||
| assert.Assert(t, project == nil) | ||
| // no file exists at all, so the "found but could not be loaded" | ||
| // warning would be factually wrong here — this is the normal | ||
| // file-less workflow, not a fallback from a broken file. | ||
| assert.Equal(t, len(hook.AllEntries()), 0, hook.AllEntries()) | ||
| }) | ||
|
|
||
| t.Run("no file and no name errors", func(t *testing.T) { | ||
| unsetEnv(t) | ||
| dir := projectDir(t, "") | ||
| opts := ProjectOptions{ProjectDir: dir} | ||
|
|
||
| _, _, err := opts.projectOrName(t.Context(), resolutionCli(t)) | ||
| assert.Assert(t, err != nil) | ||
| }) | ||
|
|
||
| t.Run("COMPOSE_PROJECT_NAME overrides the loaded model's name", func(t *testing.T) { | ||
| t.Setenv(ComposeProjectName, "from-env") | ||
| dir := projectDir(t, validCompose) | ||
| opts := ProjectOptions{ProjectDir: dir} | ||
|
|
||
| project, name, err := opts.projectOrName(t.Context(), resolutionCli(t)) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, name, "from-env") | ||
| assert.Assert(t, project != nil) | ||
| }) | ||
|
|
||
| t.Run("--project-name without --file skips loading, even a broken file", func(t *testing.T) { | ||
| unsetEnv(t) | ||
| dir := projectDir(t, "services: {invalid") | ||
| opts := ProjectOptions{ProjectName: "explicit", ProjectDir: dir} | ||
|
|
||
| project, name, err := opts.projectOrName(t.Context(), resolutionCli(t)) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, name, "explicit") | ||
| assert.Assert(t, project == nil) | ||
| }) | ||
|
|
||
| t.Run("unknown requested service is rejected by the load itself", func(t *testing.T) { | ||
| unsetEnv(t) | ||
| dir := projectDir(t, validCompose) | ||
| opts := ProjectOptions{ProjectDir: dir} | ||
|
|
||
| _, _, err := opts.projectOrName(t.Context(), resolutionCli(t), "typo") | ||
| assert.ErrorContains(t, err, "no such service") | ||
| }) | ||
| } | ||
|
|
||
| // validateServiceNames backs the commands that don't pass their service | ||
| // arguments through the load-time selection (restart, wait): strict when a | ||
| // model is available, no-op in label-based mode. | ||
| func TestValidateServiceNames(t *testing.T) { | ||
| project := &types.Project{ | ||
| Services: types.Services{"web": {Name: "web"}}, | ||
| DisabledServices: types.Services{"gated": {Name: "gated"}}, | ||
| Jobs: types.Jobs{"migrate": {Name: "migrate"}}, | ||
| } | ||
|
|
||
| assert.NilError(t, validateServiceNames(nil, []string{"anything"})) | ||
| assert.NilError(t, validateServiceNames(project, []string{"web"})) | ||
| // profile-disabled services are legitimate targets: restart enables them | ||
| assert.NilError(t, validateServiceNames(project, []string{"gated"})) | ||
| assert.Error(t, validateServiceNames(project, []string{"typo"}), "no such service: typo") | ||
| // restart/wait pass no services to projectOrName, so a job target never | ||
| // reaches jobTargetErr there -- validateServiceNames must recognize it | ||
| // itself instead of falling through to the generic "no such service". | ||
| assert.Error(t, validateServiceNames(project, []string{"migrate"}), `job "migrate" can only be triggered with "docker compose run"`) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| services: | ||
| web: | ||
| image: alpine | ||
| command: sleep infinity | ||
| jobs: | ||
| migrate: | ||
| image: alpine | ||
| command: sh -c 'echo "migration done"' | ||
| triggers: | ||
| manual: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| services: | ||
| web: | ||
| image: alpine | ||
| command: sleep infinity | ||
| jobs: | ||
| migrate: | ||
| image: alpine | ||
| command: sh -c 'echo "migration done"' | ||
| triggers: | ||
| manual: true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.