From 2c38ead3eb586733385eec88b4ccc4aea9ffd6e5 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Tue, 13 Oct 2026 11:10:17 +0200 Subject: [PATCH] fix(provider): do not execute provider services in dry-run mode Dry-run only wraps the Docker API client, so provider services, which are external processes, were still executed: up and create ran the provider's up command, down and stop ran theirs. Skip the process in executePlugin when dry-run is set while still emitting the progress events, and return early from runPlugin so no relay is deployed or removed for a provider that never ran. The metadata command is still invoked to keep validating the binary and its required parameters. Signed-off-by: Guillaume Lours --- pkg/compose/plugins.go | 25 ++++++++++----- pkg/compose/plugins_control_test.go | 48 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/pkg/compose/plugins.go b/pkg/compose/plugins.go index 595eaa13c..d614393df 100644 --- a/pkg/compose/plugins.go +++ b/pkg/compose/plugins.go @@ -152,7 +152,7 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project, return err } - if command == "stop" || command == "pull" { + if command == "stop" || command == "pull" || s.dryRun { return nil } @@ -236,6 +236,13 @@ func (s *composeService) executePlugin(ctx context.Context, project *types.Proje return pluginVariables{}, fmt.Errorf("unsupported plugin command: %s", command) } + if s.dryRun { + // the provider is an external process the dry-run API client cannot + // intercept: running it would apply the change for real + s.events.On(pluginDoneEvent(command, service.Name)) + return pluginVariables{}, nil + } + stdout, err := cmd.StdoutPipe() if err != nil { return pluginVariables{}, err @@ -315,17 +322,21 @@ func (s *composeService) executePlugin(ctx context.Context, project *types.Proje s.events.On(errorEvent(service.Name, err.Error())) return pluginVariables{}, fmt.Errorf("failed to %s service provider: %s", action, err.Error()) } + s.events.On(pluginDoneEvent(command, service.Name)) + return variables, nil +} + +func pluginDoneEvent(command, service string) api.Resource { switch command { case "up": - s.events.On(createdEvent(service.Name)) + return createdEvent(service) case "down": - s.events.On(removedEvent(service.Name)) + return removedEvent(service) case "stop": - s.events.On(stoppedEvent(service.Name)) - case "pull": - s.events.On(newEvent(service.Name, api.Done, "Pulled")) + return stoppedEvent(service) + default: // pull + return newEvent(service, api.Done, api.StatusPulled) } - return variables, nil } // imageStreamChunkSize is the fixed buffer compose fills from the image diff --git a/pkg/compose/plugins_control_test.go b/pkg/compose/plugins_control_test.go index d35dcedf1..67e234c52 100644 --- a/pkg/compose/plugins_control_test.go +++ b/pkg/compose/plugins_control_test.go @@ -22,9 +22,13 @@ import ( "net/netip" "os" "os/exec" + "path/filepath" + "runtime" "testing" "github.com/compose-spec/compose-go/v2/types" + dockerconfig "github.com/docker/cli/cli/config" + "github.com/docker/cli/cli/config/configfile" "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "go.uber.org/mock/gomock" @@ -385,3 +389,47 @@ func TestExecutePlugin_GetRelayInfoDesktop(t *testing.T) { assert.Equal(t, variables.prefixed["RELAY_NETWORK"], "desktop") assert.Equal(t, variables.prefixed["RELAY_GATEWAY"], "127.0.0.1") } + +// TestRunPlugin_DryRunDoesNotExecuteProvider: a provider is an external +// process the dry-run API client cannot intercept; runPlugin must not run it +// in dry-run mode (only the read-only metadata command may be invoked). +func TestRunPlugin_DryRunDoesNotExecuteProvider(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("the fake provider is a shell script") + } + // getPluginMetadata caches the provider metadata under config.Dir() + old := dockerconfig.Dir() + dockerconfig.SetDir(t.TempDir()) + t.Cleanup(func() { dockerconfig.SetDir(old) }) + + dir := t.TempDir() + calls := filepath.Join(dir, "calls.log") + script := `#!/bin/sh +if [ "$2" = "metadata" ]; then + echo '{"description":"fake","up":{"parameters":[]},"down":{"parameters":[]},"stop":{"parameters":[]},"pull":{"parameters":[]}}' + exit 0 +fi +echo "$*" >> "` + calls + `" +` + assert.NilError(t, os.WriteFile(filepath.Join(dir, "dryrunprov"), []byte(script), 0o700)) + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + + mockCtrl := gomock.NewController(t) + cli := mocks.NewMockCli(mockCtrl) + cli.EXPECT().Client().Return(mocks.NewMockAPIClient(mockCtrl)).AnyTimes() + cli.EXPECT().ConfigFile().Return(configfile.New("")).AnyTimes() + svc, err := NewComposeService(cli, WithEventProcessor(noopEventProcessor{})) + assert.NilError(t, err) + s := svc.(*composeService) + s.dryRun = true + + service := types.ServiceConfig{Name: "db", Provider: &types.ServiceProviderConfig{Type: "dryrunprov"}} + project := &types.Project{Name: "proj", Services: types.Services{"db": service}} + for _, command := range []string{"up", "down", "stop", "pull"} { + t.Run(command, func(t *testing.T) { + assert.NilError(t, s.runPlugin(t.Context(), project, service, command)) + _, err := os.Stat(calls) + assert.Assert(t, os.IsNotExist(err), "provider must not be executed in dry-run mode") + }) + } +}