knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/kmp/diff_test.go (about)

     1  /*
     2  Copyright 2018 The Knative 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      https://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 kmp
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	corev1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/api/resource"
    25  )
    26  
    27  func TestCompareKcmpDefault(t *testing.T) {
    28  	a := resource.MustParse("50m")
    29  	b := resource.MustParse("100m")
    30  
    31  	want := cmp.Diff(a, b, defaultOpts...)
    32  
    33  	if got, err := SafeDiff(a, b); err != nil {
    34  		t.Error("unexpected SafeDiff err:", err)
    35  	} else if diff := cmp.Diff(want, got); diff != "" {
    36  		t.Error("SafeDiff (-want, +got):", diff)
    37  	}
    38  
    39  	if got, err := SafeEqual(a, b); err != nil {
    40  		t.Fatal("unexpected SafeEqual err:", err)
    41  	} else if diff := cmp.Diff(false, got); diff != "" {
    42  		t.Error("SafeEqual(-want, +got):", diff)
    43  	}
    44  }
    45  
    46  func TestRecovery(t *testing.T) {
    47  	type foo struct {
    48  		bar string
    49  	}
    50  
    51  	a := foo{"a"}
    52  	b := foo{"b"}
    53  
    54  	if _, err := SafeDiff(a, b); err == nil {
    55  		t.Error("expected err, got nil")
    56  	}
    57  
    58  	if _, err := SafeEqual(a, b); err == nil {
    59  		t.Error("expected err, got nil")
    60  	}
    61  
    62  	if _, err := ShortDiff(a, b); err == nil {
    63  		t.Error("expected err, got nil")
    64  	}
    65  
    66  	if _, err := CompareSetFields(a, b); err == nil {
    67  		t.Error("expected err, got nil")
    68  	}
    69  }
    70  
    71  func TestFieldDiff(t *testing.T) {
    72  	type foo struct {
    73  		Bar string `json:"stringField"`
    74  		Baz int    `json:"intField"`
    75  	}
    76  
    77  	a := foo{
    78  		Bar: "a",
    79  		Baz: 1,
    80  	}
    81  	b := foo{
    82  		Bar: "b",
    83  		Baz: 1,
    84  	}
    85  
    86  	want := []string{"stringField"}
    87  
    88  	got, err := CompareSetFields(a, b)
    89  
    90  	if err != nil {
    91  		t.Error("unexpected FieldDiff err:", err)
    92  	} else if !cmp.Equal(got, want) {
    93  		t.Errorf("FieldDiff() = %v, want: %s", got, want)
    94  	}
    95  }
    96  
    97  func TestImmutableDiff(t *testing.T) {
    98  	tests := []struct {
    99  		name      string
   100  		x         interface{}
   101  		y         interface{}
   102  		want      string
   103  		expectErr bool
   104  	}{{
   105  		name: "Resource diff",
   106  		x:    resource.MustParse("50m"),
   107  		y:    resource.MustParse("100m"),
   108  		want: `{resource.Quantity}:
   109  	-: resource.Quantity: "{i:{value:50 scale:-3} d:{Dec:<nil>} s:50m Format:DecimalSI}"
   110  	+: resource.Quantity: "{i:{value:100 scale:-3} d:{Dec:<nil>} s:100m Format:DecimalSI}"
   111  `,
   112  	}, {
   113  		name:      "Both Nil objects",
   114  		expectErr: true,
   115  	}, {
   116  		name: "Nil first object",
   117  		y: corev1.ResourceList{
   118  			corev1.ResourceName("cpu"): resource.MustParse("100m"),
   119  		},
   120  		want: `{any}:
   121  	+: "map[cpu:{i:{value:100 scale:-3} d:{Dec:<nil>} s:100m Format:DecimalSI}]"
   122  `,
   123  	}, {
   124  		name: "Nil second object",
   125  		x: corev1.ResourceList{
   126  			corev1.ResourceName("cpu"): resource.MustParse("100m"),
   127  		},
   128  		want: `{any}:
   129  	-: "map[cpu:{i:{value:100 scale:-3} d:{Dec:<nil>} s:100m Format:DecimalSI}]"
   130  `,
   131  	}, {
   132  		name: "Resource list compare",
   133  		x: corev1.ResourceList{
   134  			corev1.ResourceName("cpu"):     resource.MustParse("100m"),
   135  			corev1.ResourceName("storage"): resource.MustParse("1Mi"),
   136  		},
   137  		y: corev1.ResourceList{
   138  			corev1.ResourceName("cpu"):    resource.MustParse("50m"),
   139  			corev1.ResourceName("memory"): resource.MustParse("200Mi"),
   140  		},
   141  		want: `{v1.ResourceList}["cpu"]:
   142  	-: resource.Quantity: "{i:{value:100 scale:-3} d:{Dec:<nil>} s:100m Format:DecimalSI}"
   143  	+: resource.Quantity: "{i:{value:50 scale:-3} d:{Dec:<nil>} s:50m Format:DecimalSI}"
   144  {v1.ResourceList}["memory"]:
   145  	+: resource.Quantity: "{i:{value:209715200 scale:0} d:{Dec:<nil>} s: Format:BinarySI}"
   146  {v1.ResourceList}["storage"]:
   147  	-: resource.Quantity: "{i:{value:1048576 scale:0} d:{Dec:<nil>} s:1Mi Format:BinarySI}"
   148  `,
   149  	}}
   150  
   151  	for _, test := range tests {
   152  		t.Run(test.name, func(t *testing.T) {
   153  			if got, err := ShortDiff(test.x, test.y); err != nil {
   154  				t.Error("unexpected ShortDiff err:", err)
   155  			} else if diff := cmp.Diff(test.want, got); diff != "" {
   156  				t.Error("SafeDiff (-want, +got):", diff)
   157  			}
   158  		})
   159  	}
   160  }