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

     1  package kubernetes
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/api/types/filters"
     7  	"github.com/docker/docker/api/types/swarm"
     8  	"gotest.tools/v3/assert"
     9  	"gotest.tools/v3/assert/cmp"
    10  )
    11  
    12  func TestServiceFiltersLabelSelectorGen(t *testing.T) {
    13  	cases := []struct {
    14  		name                  string
    15  		stackName             string
    16  		filters               filters.Args
    17  		expectedSelectorParts []string
    18  	}{
    19  		{
    20  			name:      "no-filter",
    21  			stackName: "test",
    22  			filters:   filters.NewArgs(),
    23  			expectedSelectorParts: []string{
    24  				"com.docker.stack.namespace=test",
    25  			},
    26  		},
    27  		{
    28  			name:      "label present filter",
    29  			stackName: "test",
    30  			filters: filters.NewArgs(
    31  				filters.KeyValuePair{Key: "label", Value: "label-is-present"},
    32  			),
    33  			expectedSelectorParts: []string{
    34  				"com.docker.stack.namespace=test",
    35  				"label-is-present",
    36  			},
    37  		},
    38  		{
    39  			name:      "single value label filter",
    40  			stackName: "test",
    41  			filters: filters.NewArgs(
    42  				filters.KeyValuePair{Key: "label", Value: "label1=test"},
    43  			),
    44  			expectedSelectorParts: []string{
    45  				"com.docker.stack.namespace=test",
    46  				"label1=test",
    47  			},
    48  		},
    49  		{
    50  			name:      "multi value label filter",
    51  			stackName: "test",
    52  			filters: filters.NewArgs(
    53  				filters.KeyValuePair{Key: "label", Value: "label1=test"},
    54  				filters.KeyValuePair{Key: "label", Value: "label1=test2"},
    55  			),
    56  			expectedSelectorParts: []string{
    57  				"com.docker.stack.namespace=test",
    58  				"label1=test",
    59  				"label1=test2",
    60  			},
    61  		},
    62  		{
    63  			name:      "2 different labels filter",
    64  			stackName: "test",
    65  			filters: filters.NewArgs(
    66  				filters.KeyValuePair{Key: "label", Value: "label1=test"},
    67  				filters.KeyValuePair{Key: "label", Value: "label2=test2"},
    68  			),
    69  			expectedSelectorParts: []string{
    70  				"com.docker.stack.namespace=test",
    71  				"label1=test",
    72  				"label2=test2",
    73  			},
    74  		},
    75  	}
    76  
    77  	for _, c := range cases {
    78  		t.Run(c.name, func(t *testing.T) {
    79  			result := generateLabelSelector(c.filters, c.stackName)
    80  			for _, toFind := range c.expectedSelectorParts {
    81  				assert.Assert(t, cmp.Contains(result, toFind))
    82  			}
    83  		})
    84  	}
    85  }
    86  func TestServiceFiltersServiceByName(t *testing.T) {
    87  	cases := []struct {
    88  		name             string
    89  		filters          []string
    90  		services         []swarm.Service
    91  		expectedServices []swarm.Service
    92  	}{
    93  		{
    94  			name:             "no filter",
    95  			filters:          []string{},
    96  			services:         makeServices("s1", "s2"),
    97  			expectedServices: makeServices("s1", "s2"),
    98  		},
    99  		{
   100  			name:             "single-name filter",
   101  			filters:          []string{"s1"},
   102  			services:         makeServices("s1", "s2"),
   103  			expectedServices: makeServices("s1"),
   104  		},
   105  		{
   106  			name:             "filter by prefix",
   107  			filters:          []string{"prefix"},
   108  			services:         makeServices("prefix-s1", "prefix-s2", "s2"),
   109  			expectedServices: makeServices("prefix-s1", "prefix-s2"),
   110  		},
   111  		{
   112  			name:             "multi-name filter",
   113  			filters:          []string{"s1", "s2"},
   114  			services:         makeServices("s1", "s2", "s3"),
   115  			expectedServices: makeServices("s1", "s2"),
   116  		},
   117  		{
   118  			name:             "stack name prefix is valid",
   119  			filters:          []string{"stack_s1"},
   120  			services:         makeServices("s1", "s11", "s2"),
   121  			expectedServices: makeServices("s1", "s11"),
   122  		},
   123  	}
   124  	for _, c := range cases {
   125  		t.Run(c.name, func(t *testing.T) {
   126  			result := filterServicesByName(c.services, c.filters, "stack")
   127  			assert.DeepEqual(t, c.expectedServices, result)
   128  		})
   129  	}
   130  }
   131  
   132  func makeServices(names ...string) []swarm.Service {
   133  	result := make([]swarm.Service, len(names))
   134  	for i, n := range names {
   135  		result[i] = swarm.Service{Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "stack_" + n}}}
   136  	}
   137  	return result
   138  }