sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1alpha4/tags_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1alpha4
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  )
    24  
    25  func TestTags_Merge(t *testing.T) {
    26  	tests := []struct {
    27  		name     string
    28  		other    Tags
    29  		expected Tags
    30  	}{
    31  		{
    32  			name:  "nil other",
    33  			other: nil,
    34  			expected: Tags{
    35  				"a": "b",
    36  				"c": "d",
    37  			},
    38  		},
    39  		{
    40  			name:  "empty other",
    41  			other: Tags{},
    42  			expected: Tags{
    43  				"a": "b",
    44  				"c": "d",
    45  			},
    46  		},
    47  		{
    48  			name: "disjoint",
    49  			other: Tags{
    50  				"1": "2",
    51  				"3": "4",
    52  			},
    53  			expected: Tags{
    54  				"a": "b",
    55  				"c": "d",
    56  				"1": "2",
    57  				"3": "4",
    58  			},
    59  		},
    60  		{
    61  			name: "overlapping, other wins",
    62  			other: Tags{
    63  				"1": "2",
    64  				"3": "4",
    65  				"a": "hello",
    66  			},
    67  			expected: Tags{
    68  				"a": "hello",
    69  				"c": "d",
    70  				"1": "2",
    71  				"3": "4",
    72  			},
    73  		},
    74  	}
    75  	for _, tc := range tests {
    76  		t.Run(tc.name, func(t *testing.T) {
    77  			tags := Tags{
    78  				"a": "b",
    79  				"c": "d",
    80  			}
    81  
    82  			tags.Merge(tc.other)
    83  			if e, a := tc.expected, tags; !cmp.Equal(e, a) {
    84  				t.Errorf("expected %#v, got %#v", e, a)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestTags_Difference(t *testing.T) {
    91  	tests := []struct {
    92  		name     string
    93  		self     Tags
    94  		input    Tags
    95  		expected Tags
    96  	}{
    97  		{
    98  			name:     "self and input are nil",
    99  			self:     nil,
   100  			input:    nil,
   101  			expected: Tags{},
   102  		},
   103  		{
   104  			name: "input is nil",
   105  			self: Tags{
   106  				"a": "b",
   107  				"c": "d",
   108  			},
   109  			input: nil,
   110  			expected: Tags{
   111  				"a": "b",
   112  				"c": "d",
   113  			},
   114  		},
   115  		{
   116  			name: "similar input",
   117  			self: Tags{
   118  				"a": "b",
   119  				"c": "d",
   120  			},
   121  			input: Tags{
   122  				"a": "b",
   123  				"c": "d",
   124  			},
   125  			expected: Tags{},
   126  		},
   127  		{
   128  			name: "input with extra tags",
   129  			self: Tags{
   130  				"a": "b",
   131  				"c": "d",
   132  			},
   133  			input: Tags{
   134  				"a": "b",
   135  				"c": "d",
   136  				"e": "f",
   137  			},
   138  			expected: Tags{},
   139  		},
   140  		{
   141  			name: "same keys, different values",
   142  			self: Tags{
   143  				"a": "b",
   144  				"c": "d",
   145  			},
   146  			input: Tags{
   147  				"a": "b1",
   148  				"c": "d",
   149  				"e": "f",
   150  			},
   151  			expected: Tags{
   152  				"a": "b",
   153  			},
   154  		},
   155  	}
   156  	for _, tc := range tests {
   157  		t.Run(tc.name, func(t *testing.T) {
   158  			out := tc.self.Difference(tc.input)
   159  			if e, a := tc.expected, out; !cmp.Equal(e, a) {
   160  				t.Errorf("expected %#v, got %#v", e, a)
   161  			}
   162  		})
   163  	}
   164  }