deploy_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package stack
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/swarm"
  7. "github.com/docker/docker/cli/compose/convert"
  8. "github.com/docker/docker/cli/internal/test"
  9. "github.com/docker/docker/client"
  10. "github.com/docker/docker/pkg/testutil/assert"
  11. "golang.org/x/net/context"
  12. )
  13. type fakeClient struct {
  14. client.Client
  15. serviceList []string
  16. removedIDs []string
  17. }
  18. func (cli *fakeClient) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
  19. services := []swarm.Service{}
  20. for _, name := range cli.serviceList {
  21. services = append(services, swarm.Service{
  22. ID: name,
  23. Spec: swarm.ServiceSpec{
  24. Annotations: swarm.Annotations{Name: name},
  25. },
  26. })
  27. }
  28. return services, nil
  29. }
  30. func (cli *fakeClient) ServiceRemove(ctx context.Context, serviceID string) error {
  31. cli.removedIDs = append(cli.removedIDs, serviceID)
  32. return nil
  33. }
  34. func TestPruneServices(t *testing.T) {
  35. ctx := context.Background()
  36. namespace := convert.NewNamespace("foo")
  37. services := map[string]struct{}{
  38. "new": {},
  39. "keep": {},
  40. }
  41. client := &fakeClient{serviceList: []string{"foo_keep", "foo_remove"}}
  42. dockerCli := test.NewFakeCli(client, &bytes.Buffer{})
  43. dockerCli.SetErr(&bytes.Buffer{})
  44. pruneServices(ctx, dockerCli, namespace, services)
  45. assert.DeepEqual(t, client.removedIDs, []string{"foo_remove"})
  46. }