github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/stack/client_test.go (about)

     1  package stack
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/docker/cli/cli/compose/convert"
     8  	"github.com/docker/docker/api"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/filters"
    11  	"github.com/docker/docker/api/types/swarm"
    12  	"github.com/docker/docker/client"
    13  )
    14  
    15  type fakeClient struct {
    16  	client.Client
    17  
    18  	version string
    19  
    20  	services []string
    21  	networks []string
    22  	secrets  []string
    23  	configs  []string
    24  
    25  	removedServices []string
    26  	removedNetworks []string
    27  	removedSecrets  []string
    28  	removedConfigs  []string
    29  
    30  	serviceListFunc    func(options types.ServiceListOptions) ([]swarm.Service, error)
    31  	networkListFunc    func(options types.NetworkListOptions) ([]types.NetworkResource, error)
    32  	secretListFunc     func(options types.SecretListOptions) ([]swarm.Secret, error)
    33  	configListFunc     func(options types.ConfigListOptions) ([]swarm.Config, error)
    34  	nodeListFunc       func(options types.NodeListOptions) ([]swarm.Node, error)
    35  	taskListFunc       func(options types.TaskListOptions) ([]swarm.Task, error)
    36  	nodeInspectWithRaw func(ref string) (swarm.Node, []byte, error)
    37  
    38  	serviceUpdateFunc func(serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error)
    39  
    40  	serviceRemoveFunc func(serviceID string) error
    41  	networkRemoveFunc func(networkID string) error
    42  	secretRemoveFunc  func(secretID string) error
    43  	configRemoveFunc  func(configID string) error
    44  }
    45  
    46  func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) {
    47  	return types.Version{
    48  		Version:    "docker-dev",
    49  		APIVersion: api.DefaultVersion,
    50  	}, nil
    51  }
    52  
    53  func (cli *fakeClient) ClientVersion() string {
    54  	return cli.version
    55  }
    56  
    57  func (cli *fakeClient) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
    58  	if cli.serviceListFunc != nil {
    59  		return cli.serviceListFunc(options)
    60  	}
    61  
    62  	namespace := namespaceFromFilters(options.Filters)
    63  	servicesList := []swarm.Service{}
    64  	for _, name := range cli.services {
    65  		if belongToNamespace(name, namespace) {
    66  			servicesList = append(servicesList, serviceFromName(name))
    67  		}
    68  	}
    69  	return servicesList, nil
    70  }
    71  
    72  func (cli *fakeClient) NetworkList(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
    73  	if cli.networkListFunc != nil {
    74  		return cli.networkListFunc(options)
    75  	}
    76  
    77  	namespace := namespaceFromFilters(options.Filters)
    78  	networksList := []types.NetworkResource{}
    79  	for _, name := range cli.networks {
    80  		if belongToNamespace(name, namespace) {
    81  			networksList = append(networksList, networkFromName(name))
    82  		}
    83  	}
    84  	return networksList, nil
    85  }
    86  
    87  func (cli *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
    88  	if cli.secretListFunc != nil {
    89  		return cli.secretListFunc(options)
    90  	}
    91  
    92  	namespace := namespaceFromFilters(options.Filters)
    93  	secretsList := []swarm.Secret{}
    94  	for _, name := range cli.secrets {
    95  		if belongToNamespace(name, namespace) {
    96  			secretsList = append(secretsList, secretFromName(name))
    97  		}
    98  	}
    99  	return secretsList, nil
   100  }
   101  
   102  func (cli *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
   103  	if cli.configListFunc != nil {
   104  		return cli.configListFunc(options)
   105  	}
   106  
   107  	namespace := namespaceFromFilters(options.Filters)
   108  	configsList := []swarm.Config{}
   109  	for _, name := range cli.configs {
   110  		if belongToNamespace(name, namespace) {
   111  			configsList = append(configsList, configFromName(name))
   112  		}
   113  	}
   114  	return configsList, nil
   115  }
   116  
   117  func (cli *fakeClient) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) {
   118  	if cli.taskListFunc != nil {
   119  		return cli.taskListFunc(options)
   120  	}
   121  	return []swarm.Task{}, nil
   122  }
   123  
   124  func (cli *fakeClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) {
   125  	if cli.nodeListFunc != nil {
   126  		return cli.nodeListFunc(options)
   127  	}
   128  	return []swarm.Node{}, nil
   129  }
   130  
   131  func (cli *fakeClient) NodeInspectWithRaw(ctx context.Context, ref string) (swarm.Node, []byte, error) {
   132  	if cli.nodeInspectWithRaw != nil {
   133  		return cli.nodeInspectWithRaw(ref)
   134  	}
   135  	return swarm.Node{}, nil, nil
   136  }
   137  
   138  func (cli *fakeClient) ServiceUpdate(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error) {
   139  	if cli.serviceUpdateFunc != nil {
   140  		return cli.serviceUpdateFunc(serviceID, version, service, options)
   141  	}
   142  
   143  	return types.ServiceUpdateResponse{}, nil
   144  }
   145  
   146  func (cli *fakeClient) ServiceRemove(ctx context.Context, serviceID string) error {
   147  	if cli.serviceRemoveFunc != nil {
   148  		return cli.serviceRemoveFunc(serviceID)
   149  	}
   150  
   151  	cli.removedServices = append(cli.removedServices, serviceID)
   152  	return nil
   153  }
   154  
   155  func (cli *fakeClient) NetworkRemove(ctx context.Context, networkID string) error {
   156  	if cli.networkRemoveFunc != nil {
   157  		return cli.networkRemoveFunc(networkID)
   158  	}
   159  
   160  	cli.removedNetworks = append(cli.removedNetworks, networkID)
   161  	return nil
   162  }
   163  
   164  func (cli *fakeClient) SecretRemove(ctx context.Context, secretID string) error {
   165  	if cli.secretRemoveFunc != nil {
   166  		return cli.secretRemoveFunc(secretID)
   167  	}
   168  
   169  	cli.removedSecrets = append(cli.removedSecrets, secretID)
   170  	return nil
   171  }
   172  
   173  func (cli *fakeClient) ConfigRemove(ctx context.Context, configID string) error {
   174  	if cli.configRemoveFunc != nil {
   175  		return cli.configRemoveFunc(configID)
   176  	}
   177  
   178  	cli.removedConfigs = append(cli.removedConfigs, configID)
   179  	return nil
   180  }
   181  
   182  func (cli *fakeClient) ServiceInspectWithRaw(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) {
   183  	return swarm.Service{
   184  		ID: serviceID,
   185  		Spec: swarm.ServiceSpec{
   186  			Annotations: swarm.Annotations{
   187  				Name: serviceID,
   188  			},
   189  		},
   190  	}, []byte{}, nil
   191  }
   192  
   193  func serviceFromName(name string) swarm.Service {
   194  	return swarm.Service{
   195  		ID: "ID-" + name,
   196  		Spec: swarm.ServiceSpec{
   197  			Annotations: swarm.Annotations{Name: name},
   198  		},
   199  	}
   200  }
   201  
   202  func networkFromName(name string) types.NetworkResource {
   203  	return types.NetworkResource{
   204  		ID:   "ID-" + name,
   205  		Name: name,
   206  	}
   207  }
   208  
   209  func secretFromName(name string) swarm.Secret {
   210  	return swarm.Secret{
   211  		ID: "ID-" + name,
   212  		Spec: swarm.SecretSpec{
   213  			Annotations: swarm.Annotations{Name: name},
   214  		},
   215  	}
   216  }
   217  
   218  func configFromName(name string) swarm.Config {
   219  	return swarm.Config{
   220  		ID: "ID-" + name,
   221  		Spec: swarm.ConfigSpec{
   222  			Annotations: swarm.Annotations{Name: name},
   223  		},
   224  	}
   225  }
   226  
   227  func namespaceFromFilters(filters filters.Args) string {
   228  	label := filters.Get("label")[0]
   229  	return strings.TrimPrefix(label, convert.LabelNamespace+"=")
   230  }
   231  
   232  func belongToNamespace(id, namespace string) bool {
   233  	return strings.HasPrefix(id, namespace+"_")
   234  }
   235  
   236  func objectName(namespace, name string) string {
   237  	return namespace + "_" + name
   238  }
   239  
   240  func objectID(name string) string {
   241  	return "ID-" + name
   242  }
   243  
   244  func buildObjectIDs(objectNames []string) []string {
   245  	IDs := make([]string, len(objectNames))
   246  	for i, name := range objectNames {
   247  		IDs[i] = objectID(name)
   248  	}
   249  	return IDs
   250  }