github.com/kubewharf/katalyst-core@v0.5.3/pkg/client/control/unstructured_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 control
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/api/resource"
    26  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    28  
    29  	"github.com/kubewharf/katalyst-core/pkg/util/native"
    30  )
    31  
    32  func toTestUnstructured(t *testing.T, obj interface{}) *unstructured.Unstructured {
    33  	ret, err := native.ToUnstructured(obj)
    34  	assert.NoError(t, err)
    35  	return ret
    36  }
    37  
    38  func Test_prepareUnstructuredPatchBytes(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	type args struct {
    42  		oldObj *unstructured.Unstructured
    43  		newObj *unstructured.Unstructured
    44  	}
    45  	tests := []struct {
    46  		name    string
    47  		args    args
    48  		want    string
    49  		wantErr assert.ErrorAssertionFunc
    50  	}{
    51  		{
    52  			name: "label patch bytes",
    53  			args: args{
    54  				oldObj: toTestUnstructured(t, &corev1.Node{
    55  					ObjectMeta: v1.ObjectMeta{
    56  						Labels: map[string]string{
    57  							"aa": "bb",
    58  						},
    59  					},
    60  				}),
    61  				newObj: toTestUnstructured(t, &corev1.Node{
    62  					ObjectMeta: v1.ObjectMeta{
    63  						Labels: map[string]string{
    64  							"aa": "bb",
    65  							"cc": "dd",
    66  						},
    67  					},
    68  				}),
    69  			},
    70  			want: "{\"metadata\":{\"labels\":{\"cc\":\"dd\"}}}",
    71  			wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
    72  				return true
    73  			},
    74  		},
    75  		{
    76  			name: "spec patch bytes",
    77  			args: args{
    78  				oldObj: toTestUnstructured(t, &corev1.Node{
    79  					Spec: corev1.NodeSpec{
    80  						PodCIDR: "aa",
    81  					},
    82  				}),
    83  				newObj: toTestUnstructured(t, &corev1.Node{
    84  					Spec: corev1.NodeSpec{
    85  						PodCIDR: "bb",
    86  					},
    87  				}),
    88  			},
    89  			want: "{\"spec\":{\"podCIDR\":\"bb\"}}",
    90  			wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
    91  				return true
    92  			},
    93  		},
    94  	}
    95  	for _, tt := range tests {
    96  		tt := tt
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			t.Parallel()
    99  
   100  			got, err := prepareUnstructuredPatchBytes(tt.args.oldObj, tt.args.newObj)
   101  			if !tt.wantErr(t, err, fmt.Sprintf("prepareUnstructuredPatchBytes(%v, %v)", tt.args.oldObj, tt.args.newObj)) {
   102  				return
   103  			}
   104  			assert.Equalf(t, tt.want, string(got), "prepareUnstructuredPatchBytes(%v, %v)", tt.args.oldObj, tt.args.newObj)
   105  		})
   106  	}
   107  }
   108  
   109  func Test_prepareUnstructuredStatusPatchBytes(t *testing.T) {
   110  	t.Parallel()
   111  
   112  	type args struct {
   113  		oldObj *unstructured.Unstructured
   114  		newObj *unstructured.Unstructured
   115  	}
   116  	tests := []struct {
   117  		name    string
   118  		args    args
   119  		want    string
   120  		wantErr assert.ErrorAssertionFunc
   121  	}{
   122  		{
   123  			name: "label patch bytes",
   124  			args: args{
   125  				oldObj: toTestUnstructured(t, &corev1.Node{
   126  					Status: corev1.NodeStatus{
   127  						Allocatable: corev1.ResourceList{
   128  							corev1.ResourceCPU: resource.MustParse("10"),
   129  						},
   130  					},
   131  				}),
   132  				newObj: toTestUnstructured(t, &corev1.Node{
   133  					Status: corev1.NodeStatus{
   134  						Allocatable: corev1.ResourceList{
   135  							corev1.ResourceCPU: resource.MustParse("20"),
   136  						},
   137  					},
   138  				}),
   139  			},
   140  			want: "{\"status\":{\"allocatable\":{\"cpu\":\"20\"}}}",
   141  			wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
   142  				return true
   143  			},
   144  		},
   145  	}
   146  	for _, tt := range tests {
   147  		tt := tt
   148  		t.Run(tt.name, func(t *testing.T) {
   149  			t.Parallel()
   150  
   151  			got, err := prepareUnstructuredStatusPatchBytes(tt.args.oldObj, tt.args.newObj)
   152  			if !tt.wantErr(t, err, fmt.Sprintf("prepareUnstructuredStatusPatchBytes(%v, %v)", tt.args.oldObj, tt.args.newObj)) {
   153  				return
   154  			}
   155  			assert.Equalf(t, tt.want, string(got), "prepareUnstructuredStatusPatchBytes(%v, %v)", tt.args.oldObj, tt.args.newObj)
   156  		})
   157  	}
   158  }