Fix down command without any resource to delete

A command down without any resource to delete was
trying to remove a default network that doesn't exist

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2022-03-31 18:14:09 +02:00
parent e700f0a5d7
commit 56e48f8360
2 changed files with 8 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import (
"time"
"github.com/compose-spec/compose-go/types"
"github.com/docker/cli/cli/registry/client"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/errdefs"
"golang.org/x/sync/errgroup"
@ -131,6 +132,11 @@ func (s *composeService) ensureNetworksDown(ctx context.Context, project *types.
continue
}
networkName := n.Name
_, err := s.apiClient().NetworkInspect(ctx, networkName, moby.NetworkInspectOptions{})
if client.IsNotFound(err) {
return nil
}
ops = append(ops, func() error {
return s.removeNetwork(ctx, networkName, w)
})

View File

@ -23,7 +23,6 @@ import (
compose "github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/mocks"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
@ -60,6 +59,7 @@ func TestDown(t *testing.T) {
api.EXPECT().ContainerRemove(gomock.Any(), "456", moby.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().ContainerRemove(gomock.Any(), "789", moby.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().NetworkInspect(gomock.Any(), "myProject_default", moby.NetworkInspectOptions{}).Return(moby.NetworkResource{Name: "myProject_default"}, nil)
api.EXPECT().NetworkRemove(gomock.Any(), "myProject_default").Return(nil)
err := tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{})
@ -94,6 +94,7 @@ func TestDownRemoveOrphans(t *testing.T) {
api.EXPECT().ContainerRemove(gomock.Any(), "789", moby.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().ContainerRemove(gomock.Any(), "321", moby.ContainerRemoveOptions{Force: true}).Return(nil)
api.EXPECT().NetworkInspect(gomock.Any(), "myProject_default", moby.NetworkInspectOptions{}).Return(moby.NetworkResource{Name: "myProject_default"}, nil)
api.EXPECT().NetworkRemove(gomock.Any(), "myProject_default").Return(nil)
err := tested.Down(context.Background(), strings.ToLower(testProject), compose.DownOptions{RemoveOrphans: true})