...

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

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

     1  /*
     2  Copyright 2019 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/internal/fixture"
    24  	"sigs.k8s.io/structured-merge-diff/v4/typed"
    25  )
    26  
    27  var associativeListParser = func() Parser {
    28  	parser, err := typed.NewParser(`types:
    29  - name: type
    30    map:
    31      fields:
    32        - name: list
    33          type:
    34            namedType: associativeList
    35  - name: associativeList
    36    list:
    37      elementType:
    38        namedType: myElement
    39      elementRelationship: associative
    40      keys:
    41      - name
    42  - name: myElement
    43    map:
    44      fields:
    45      - name: name
    46        type:
    47          scalar: string
    48      - name: value
    49        type:
    50          scalar: numeric
    51  `)
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	return SameVersionParser{T: parser.Type("type")}
    56  }()
    57  
    58  func TestUpdateAssociativeLists(t *testing.T) {
    59  	tests := map[string]TestCase{
    60  		"removing_obsolete_applied_structs": {
    61  			Ops: []Operation{
    62  				Apply{
    63  					Manager: "default",
    64  					Object: `
    65  						list:
    66  						- name: a
    67  						  value: 1
    68  					`,
    69  					APIVersion: "v1",
    70  				},
    71  				Apply{
    72  					Manager: "default",
    73  					Object: `
    74  						list:
    75  						- name: b
    76  						  value: 2
    77  					`,
    78  					APIVersion: "v1",
    79  				},
    80  			},
    81  			Object: `
    82  				list:
    83  				- name: b
    84  				  value: 2
    85  			`,
    86  			APIVersion: "v1",
    87  			Managed: fieldpath.ManagedFields{
    88  				"default": fieldpath.NewVersionedSet(
    89  					_NS(
    90  						_P("list", _KBF("name", "b")),
    91  						_P("list", _KBF("name", "b"), "name"),
    92  						_P("list", _KBF("name", "b"), "value"),
    93  					),
    94  					"v1",
    95  					false,
    96  				),
    97  			},
    98  		},
    99  	}
   100  
   101  	for name, test := range tests {
   102  		t.Run(name, func(t *testing.T) {
   103  			if err := test.Test(associativeListParser); err != nil {
   104  				t.Fatal(err)
   105  			}
   106  		})
   107  	}
   108  }
   109  

View as plain text