...

Source file src/sigs.k8s.io/structured-merge-diff/v4/fieldpath/managers_test.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/fieldpath

     1  /*
     2  Copyright 2018 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 fieldpath_test
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    25  )
    26  
    27  var (
    28  	// Short names for readable test cases.
    29  	_NS = fieldpath.NewSet
    30  	_P  = fieldpath.MakePathOrDie
    31  )
    32  
    33  func TestManagersEquals(t *testing.T) {
    34  	tests := []struct {
    35  		name string
    36  		lhs  fieldpath.ManagedFields
    37  		rhs  fieldpath.ManagedFields
    38  		out  fieldpath.ManagedFields
    39  	}{
    40  		{
    41  			name: "Empty sets",
    42  			out:  fieldpath.ManagedFields{},
    43  		},
    44  		{
    45  			name: "Empty RHS",
    46  			lhs: fieldpath.ManagedFields{
    47  				"default": fieldpath.NewVersionedSet(
    48  					_NS(_P("numeric"), _P("string"), _P("bool")),
    49  					"v1",
    50  					false,
    51  				),
    52  			},
    53  			out: fieldpath.ManagedFields{
    54  				"default": fieldpath.NewVersionedSet(
    55  					_NS(_P("numeric"), _P("string"), _P("bool")),
    56  					"v1",
    57  					false,
    58  				),
    59  			},
    60  		},
    61  		{
    62  			name: "Empty LHS",
    63  			rhs: fieldpath.ManagedFields{
    64  				"default": fieldpath.NewVersionedSet(
    65  					_NS(_P("numeric"), _P("string"), _P("bool")),
    66  					"v1",
    67  					false,
    68  				),
    69  			},
    70  			out: fieldpath.ManagedFields{
    71  				"default": fieldpath.NewVersionedSet(
    72  					_NS(_P("numeric"), _P("string"), _P("bool")),
    73  					"v1",
    74  					false,
    75  				),
    76  			},
    77  		},
    78  		{
    79  			name: "Different managers",
    80  			lhs: fieldpath.ManagedFields{
    81  				"one": fieldpath.NewVersionedSet(
    82  					_NS(_P("numeric"), _P("string"), _P("bool")),
    83  					"v1",
    84  					false,
    85  				),
    86  			},
    87  			rhs: fieldpath.ManagedFields{
    88  				"two": fieldpath.NewVersionedSet(
    89  					_NS(_P("numeric"), _P("string"), _P("bool")),
    90  					"v1",
    91  					false,
    92  				),
    93  			},
    94  			out: fieldpath.ManagedFields{
    95  				"one": fieldpath.NewVersionedSet(
    96  					_NS(_P("numeric"), _P("string"), _P("bool")),
    97  					"v1",
    98  					false,
    99  				),
   100  				"two": fieldpath.NewVersionedSet(
   101  					_NS(_P("numeric"), _P("string"), _P("bool")),
   102  					"v1",
   103  					false,
   104  				),
   105  			},
   106  		},
   107  		{
   108  			name: "Same manager, different version",
   109  			lhs: fieldpath.ManagedFields{
   110  				"one": fieldpath.NewVersionedSet(
   111  					_NS(_P("numeric"), _P("string"), _P("integer")),
   112  					"v1",
   113  					false,
   114  				),
   115  			},
   116  			rhs: fieldpath.ManagedFields{
   117  				"one": fieldpath.NewVersionedSet(
   118  					_NS(_P("numeric"), _P("string"), _P("bool")),
   119  					"v2",
   120  					false,
   121  				),
   122  			},
   123  			out: fieldpath.ManagedFields{
   124  				"one": fieldpath.NewVersionedSet(
   125  					_NS(_P("numeric"), _P("string"), _P("bool")),
   126  					"v2",
   127  					false,
   128  				),
   129  			},
   130  		},
   131  		{
   132  			name: "Set difference",
   133  			lhs: fieldpath.ManagedFields{
   134  				"one": fieldpath.NewVersionedSet(
   135  					_NS(_P("numeric"), _P("string")),
   136  					"v1",
   137  					false,
   138  				),
   139  			},
   140  			rhs: fieldpath.ManagedFields{
   141  				"one": fieldpath.NewVersionedSet(
   142  					_NS(_P("string"), _P("bool")),
   143  					"v1",
   144  					false,
   145  				),
   146  			},
   147  			out: fieldpath.ManagedFields{
   148  				"one": fieldpath.NewVersionedSet(
   149  					_NS(_P("numeric"), _P("bool")),
   150  					"v1",
   151  					false,
   152  				),
   153  			},
   154  		},
   155  	}
   156  
   157  	for _, test := range tests {
   158  		t.Run(fmt.Sprintf(test.name), func(t *testing.T) {
   159  			want := test.out
   160  			got := test.lhs.Difference(test.rhs)
   161  			if !reflect.DeepEqual(want, got) {
   162  				t.Errorf("want %v, got %v", want, got)
   163  			}
   164  		})
   165  	}
   166  }
   167  
   168  func TestManagersDifference(t *testing.T) {
   169  	tests := []struct {
   170  		name  string
   171  		lhs   fieldpath.ManagedFields
   172  		rhs   fieldpath.ManagedFields
   173  		equal bool
   174  	}{
   175  		{
   176  			name:  "Empty sets",
   177  			equal: true,
   178  		},
   179  		{
   180  			name: "Same everything",
   181  			lhs: fieldpath.ManagedFields{
   182  				"one": fieldpath.NewVersionedSet(
   183  					_NS(_P("numeric"), _P("string"), _P("bool")),
   184  					"v1",
   185  					false,
   186  				),
   187  			},
   188  			rhs: fieldpath.ManagedFields{
   189  				"one": fieldpath.NewVersionedSet(
   190  					_NS(_P("numeric"), _P("string"), _P("bool")),
   191  					"v1",
   192  					false,
   193  				),
   194  			},
   195  			equal: true,
   196  		},
   197  		{
   198  			name: "Empty RHS",
   199  			lhs: fieldpath.ManagedFields{
   200  				"default": fieldpath.NewVersionedSet(
   201  					_NS(_P("numeric"), _P("string"), _P("bool")),
   202  					"v1",
   203  					false,
   204  				),
   205  			},
   206  			equal: false,
   207  		},
   208  		{
   209  			name: "Empty LHS",
   210  			rhs: fieldpath.ManagedFields{
   211  				"default": fieldpath.NewVersionedSet(
   212  					_NS(_P("numeric"), _P("string"), _P("bool")),
   213  					"v1",
   214  					false,
   215  				),
   216  			},
   217  			equal: false,
   218  		},
   219  		{
   220  			name: "Different managers",
   221  			lhs: fieldpath.ManagedFields{
   222  				"one": fieldpath.NewVersionedSet(
   223  					_NS(_P("numeric"), _P("string"), _P("bool")),
   224  					"v1",
   225  					false,
   226  				),
   227  			},
   228  			rhs: fieldpath.ManagedFields{
   229  				"two": fieldpath.NewVersionedSet(
   230  					_NS(_P("numeric"), _P("string"), _P("bool")),
   231  					"v1",
   232  					false,
   233  				),
   234  			},
   235  			equal: false,
   236  		},
   237  		{
   238  			name: "Same manager, different version",
   239  			lhs: fieldpath.ManagedFields{
   240  				"one": fieldpath.NewVersionedSet(
   241  					_NS(_P("numeric"), _P("string"), _P("integer")),
   242  					"v1",
   243  					false,
   244  				),
   245  			},
   246  			rhs: fieldpath.ManagedFields{
   247  				"one": fieldpath.NewVersionedSet(
   248  					_NS(_P("numeric"), _P("string"), _P("bool")),
   249  					"v2",
   250  					false,
   251  				),
   252  			},
   253  			equal: false,
   254  		},
   255  		{
   256  			name: "Set difference",
   257  			lhs: fieldpath.ManagedFields{
   258  				"one": fieldpath.NewVersionedSet(
   259  					_NS(_P("numeric"), _P("string")),
   260  					"v1",
   261  					false,
   262  				),
   263  			},
   264  			rhs: fieldpath.ManagedFields{
   265  				"one": fieldpath.NewVersionedSet(
   266  					_NS(_P("string"), _P("bool")),
   267  					"v1",
   268  					false,
   269  				),
   270  			},
   271  			equal: false,
   272  		},
   273  	}
   274  
   275  	for _, test := range tests {
   276  		t.Run(fmt.Sprintf(test.name), func(t *testing.T) {
   277  			equal := test.lhs.Equals(test.rhs)
   278  			if test.equal && !equal {
   279  				difference := test.lhs.Difference(test.rhs)
   280  				t.Errorf("should be equal, but are different: %v", difference)
   281  			} else if !test.equal && equal {
   282  				t.Errorf("should not be equal, but they are")
   283  			}
   284  		})
   285  	}
   286  }
   287  

View as plain text