github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/mergepatch/util_test.go (about)

     1  /*
     2  Copyright 2017 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 mergepatch
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  )
    23  
    24  func TestHasConflicts(t *testing.T) {
    25  	testCases := []struct {
    26  		A   interface{}
    27  		B   interface{}
    28  		Ret bool
    29  	}{
    30  		{A: "hello", B: "hello", Ret: false},
    31  		{A: "hello", B: "hell", Ret: true},
    32  		{A: "hello", B: nil, Ret: true},
    33  		{A: "hello", B: int64(1), Ret: true},
    34  		{A: "hello", B: float64(1.0), Ret: true},
    35  		{A: "hello", B: false, Ret: true},
    36  		{A: int64(1), B: int64(1), Ret: false},
    37  		{A: nil, B: nil, Ret: false},
    38  		{A: false, B: false, Ret: false},
    39  		{A: float64(3), B: float64(3), Ret: false},
    40  
    41  		{A: "hello", B: []interface{}{}, Ret: true},
    42  		{A: []interface{}{int64(1)}, B: []interface{}{}, Ret: true},
    43  		{A: []interface{}{}, B: []interface{}{}, Ret: false},
    44  		{A: []interface{}{int64(1)}, B: []interface{}{int64(1)}, Ret: false},
    45  		{A: map[string]interface{}{}, B: []interface{}{int64(1)}, Ret: true},
    46  
    47  		{A: map[string]interface{}{}, B: map[string]interface{}{"a": int64(1)}, Ret: false},
    48  		{A: map[string]interface{}{"a": int64(1)}, B: map[string]interface{}{"a": int64(1)}, Ret: false},
    49  		{A: map[string]interface{}{"a": int64(1)}, B: map[string]interface{}{"a": int64(2)}, Ret: true},
    50  		{A: map[string]interface{}{"a": int64(1)}, B: map[string]interface{}{"b": int64(2)}, Ret: false},
    51  
    52  		{
    53  			A:   map[string]interface{}{"a": []interface{}{int64(1)}},
    54  			B:   map[string]interface{}{"a": []interface{}{int64(1)}},
    55  			Ret: false,
    56  		},
    57  		{
    58  			A:   map[string]interface{}{"a": []interface{}{int64(1)}},
    59  			B:   map[string]interface{}{"a": []interface{}{}},
    60  			Ret: true,
    61  		},
    62  		{
    63  			A:   map[string]interface{}{"a": []interface{}{int64(1)}},
    64  			B:   map[string]interface{}{"a": int64(1)},
    65  			Ret: true,
    66  		},
    67  
    68  		// Maps and lists with multiple entries.
    69  		{
    70  			A:   map[string]interface{}{"a": int64(1), "b": int64(2)},
    71  			B:   map[string]interface{}{"a": int64(1), "b": int64(0)},
    72  			Ret: true,
    73  		},
    74  		{
    75  			A:   map[string]interface{}{"a": int64(1), "b": int64(2)},
    76  			B:   map[string]interface{}{"a": int64(1), "b": int64(2)},
    77  			Ret: false,
    78  		},
    79  		{
    80  			A:   map[string]interface{}{"a": int64(1), "b": int64(2)},
    81  			B:   map[string]interface{}{"a": int64(1), "b": int64(0), "c": int64(3)},
    82  			Ret: true,
    83  		},
    84  		{
    85  			A:   map[string]interface{}{"a": int64(1), "b": int64(2)},
    86  			B:   map[string]interface{}{"a": int64(1), "b": int64(2), "c": int64(3)},
    87  			Ret: false,
    88  		},
    89  		{
    90  			A:   map[string]interface{}{"a": []interface{}{int64(1), int64(2)}},
    91  			B:   map[string]interface{}{"a": []interface{}{int64(1), int64(0)}},
    92  			Ret: true,
    93  		},
    94  		{
    95  			A:   map[string]interface{}{"a": []interface{}{int64(1), int64(2)}},
    96  			B:   map[string]interface{}{"a": []interface{}{int64(1), int64(2)}},
    97  			Ret: false,
    98  		},
    99  
   100  		// Numeric types are not interchangeable.
   101  		// Callers are expected to ensure numeric types are consistent in 'left' and 'right'.
   102  		{A: int64(0), B: float64(0), Ret: true},
   103  		// Other types are not interchangeable.
   104  		{A: int64(0), B: "0", Ret: true},
   105  		{A: int64(0), B: nil, Ret: true},
   106  		{A: int64(0), B: false, Ret: true},
   107  		{A: "true", B: true, Ret: true},
   108  		{A: "null", B: nil, Ret: true},
   109  	}
   110  
   111  	for _, testCase := range testCases {
   112  		testStr := fmt.Sprintf("A = %#v, B = %#v", testCase.A, testCase.B)
   113  		// Run each test case multiple times if it passes because HasConflicts()
   114  		// uses map iteration, which returns keys in nondeterministic order.
   115  		for try := 0; try < 10; try++ {
   116  			out, err := HasConflicts(testCase.A, testCase.B)
   117  			if err != nil {
   118  				t.Errorf("%v: unexpected error: %v", testStr, err)
   119  				break
   120  			}
   121  			if out != testCase.Ret {
   122  				t.Errorf("%v: expected %t got %t", testStr, testCase.Ret, out)
   123  				break
   124  			}
   125  			out, err = HasConflicts(testCase.B, testCase.A)
   126  			if err != nil {
   127  				t.Errorf("%v: unexpected error: %v", testStr, err)
   128  				break
   129  			}
   130  			if out != testCase.Ret {
   131  				t.Errorf("%v: expected reversed %t got %t", testStr, testCase.Ret, out)
   132  				break
   133  			}
   134  		}
   135  	}
   136  }