...

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

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

     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 merge_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    23  	"sigs.k8s.io/structured-merge-diff/v4/merge"
    24  	"sigs.k8s.io/structured-merge-diff/v4/value"
    25  )
    26  
    27  var (
    28  	// Short names for readable test cases.
    29  	_NS  = fieldpath.NewSet
    30  	_P   = fieldpath.MakePathOrDie
    31  	_KBF = fieldpath.KeyByFields
    32  	_V   = value.NewValueInterface
    33  )
    34  
    35  func TestNewFromSets(t *testing.T) {
    36  	got := merge.ConflictsFromManagers(fieldpath.ManagedFields{
    37  		"Bob": fieldpath.NewVersionedSet(
    38  			_NS(
    39  				_P("key"),
    40  				_P("list", _KBF("key", "a", "id", 2), "id"),
    41  			),
    42  			"v1",
    43  			false,
    44  		),
    45  		"Alice": fieldpath.NewVersionedSet(
    46  			_NS(
    47  				_P("value"),
    48  				_P("list", _KBF("key", "a", "id", 2), "key"),
    49  			),
    50  			"v1",
    51  			false,
    52  		),
    53  	})
    54  	wanted := `conflicts with "Alice":
    55  - .value
    56  - .list[id=2,key="a"].key
    57  conflicts with "Bob":
    58  - .key
    59  - .list[id=2,key="a"].id`
    60  	if got.Error() != wanted {
    61  		t.Errorf("Got %v, wanted %v", got.Error(), wanted)
    62  	}
    63  }
    64  
    65  func TestToSet(t *testing.T) {
    66  	conflicts := merge.ConflictsFromManagers(fieldpath.ManagedFields{
    67  		"Bob": fieldpath.NewVersionedSet(
    68  			_NS(
    69  				_P("key"),
    70  				_P("list", _KBF("key", "a", "id", 2), "id"),
    71  			),
    72  			"v1",
    73  			false,
    74  		),
    75  		"Alice": fieldpath.NewVersionedSet(
    76  			_NS(
    77  				_P("value"),
    78  				_P("list", _KBF("key", "a", "id", 2), "key"),
    79  			),
    80  			"v1",
    81  			false,
    82  		),
    83  	})
    84  	expected := fieldpath.NewSet(
    85  		_P("key"),
    86  		_P("value"),
    87  		_P("list", _KBF("key", "a", "id", 2), "id"),
    88  		_P("list", _KBF("key", "a", "id", 2), "key"),
    89  	)
    90  	actual := conflicts.ToSet()
    91  	if !expected.Equals(actual) {
    92  		t.Fatalf("expected\n%v\n, but got\n%v\n", expected, actual)
    93  	}
    94  }
    95  
    96  func TestConflictsFromManagers(t *testing.T) {
    97  	got := merge.ConflictsFromManagers(fieldpath.ManagedFields{
    98  		"Bob": fieldpath.NewVersionedSet(
    99  			_NS(
   100  				_P("obj", "template", "obj", "list", _KBF("name", "a"), "id"),
   101  				_P("obj", "template", "obj", "list", _KBF("name", "a"), "key"),
   102  			),
   103  			"v1",
   104  			false,
   105  		),
   106  	})
   107  	wanted := `conflicts with "Bob":
   108  - .obj.template.obj.list[name="a"].id
   109  - .obj.template.obj.list[name="a"].key`
   110  	if got.Error() != wanted {
   111  		t.Errorf("Got %v, wanted %v", got.Error(), wanted)
   112  	}
   113  }
   114  

View as plain text