...

Source file src/sigs.k8s.io/kustomize/api/types/patch_test.go

Documentation: sigs.k8s.io/kustomize/api/types

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package types_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	. "sigs.k8s.io/kustomize/api/types"
    10  	"sigs.k8s.io/kustomize/kyaml/resid"
    11  )
    12  
    13  func TestPatchEquals(t *testing.T) {
    14  	selector := Selector{
    15  		ResId: resid.ResId{
    16  			Gvk: resid.Gvk{
    17  				Group:   "group",
    18  				Version: "version",
    19  				Kind:    "kind",
    20  			},
    21  			Name:      "name",
    22  			Namespace: "namespace",
    23  		},
    24  		LabelSelector:      "selector",
    25  		AnnotationSelector: "selector",
    26  	}
    27  	type testcase struct {
    28  		patch1 Patch
    29  		patch2 Patch
    30  		expect bool
    31  		name   string
    32  	}
    33  	testcases := []testcase{
    34  		{
    35  			name:   "empty patches",
    36  			patch1: Patch{},
    37  			patch2: Patch{},
    38  			expect: true,
    39  		},
    40  		{
    41  			name: "full patches",
    42  			patch1: Patch{
    43  				Path:  "foo",
    44  				Patch: "bar",
    45  				Target: &Selector{
    46  					ResId: resid.ResId{
    47  						Gvk: resid.Gvk{
    48  							Group:   "group",
    49  							Version: "version",
    50  							Kind:    "kind",
    51  						},
    52  						Name:      "name",
    53  						Namespace: "namespace",
    54  					},
    55  					LabelSelector:      "selector",
    56  					AnnotationSelector: "selector",
    57  				},
    58  			},
    59  			patch2: Patch{
    60  				Path:  "foo",
    61  				Patch: "bar",
    62  				Target: &Selector{
    63  					ResId: resid.ResId{
    64  						Gvk: resid.Gvk{
    65  							Group:   "group",
    66  							Version: "version",
    67  							Kind:    "kind",
    68  						},
    69  						Name:      "name",
    70  						Namespace: "namespace",
    71  					},
    72  					LabelSelector:      "selector",
    73  					AnnotationSelector: "selector",
    74  				},
    75  			},
    76  			expect: true,
    77  		},
    78  		{
    79  			name: "same target",
    80  			patch1: Patch{
    81  				Path:   "foo",
    82  				Patch:  "bar",
    83  				Target: &selector,
    84  			},
    85  			patch2: Patch{
    86  				Path:   "foo",
    87  				Patch:  "bar",
    88  				Target: &selector,
    89  			},
    90  			expect: true,
    91  		},
    92  		{
    93  			name: "omit target",
    94  			patch1: Patch{
    95  				Path:  "foo",
    96  				Patch: "bar",
    97  			},
    98  			patch2: Patch{
    99  				Path:  "foo",
   100  				Patch: "bar",
   101  			},
   102  			expect: true,
   103  		},
   104  		{
   105  			name: "one nil target",
   106  			patch1: Patch{
   107  				Path:   "foo",
   108  				Patch:  "bar",
   109  				Target: &selector,
   110  			},
   111  			patch2: Patch{
   112  				Path:  "foo",
   113  				Patch: "bar",
   114  			},
   115  			expect: false,
   116  		},
   117  		{
   118  			name: "different path",
   119  			patch1: Patch{
   120  				Path: "foo",
   121  			},
   122  			patch2: Patch{
   123  				Path: "bar",
   124  			},
   125  			expect: false,
   126  		},
   127  	}
   128  
   129  	for _, tc := range testcases {
   130  		if tc.expect != tc.patch1.Equals(tc.patch2) {
   131  			t.Fatalf("%s: unexpected result %v", tc.name, !tc.expect)
   132  		}
   133  	}
   134  }
   135  

View as plain text