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 <glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours 2026-10-13 11:10:17 +02:00 • committed by Guillaume Lours
parent 1c88d072b6
commit 2c38ead3eb
2 changed files with 66 additions and 7 deletions

View file

@ -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

View file

@ -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")
})
}
}