github.com/kubewharf/katalyst-core@v0.5.3/pkg/client/control/node_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 "context" 21 "fmt" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 "k8s.io/api/core/v1" 26 "k8s.io/apimachinery/pkg/api/resource" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/apimachinery/pkg/runtime" 29 30 katalystbase "github.com/kubewharf/katalyst-core/cmd/base" 31 ) 32 33 func TestRealNodeUpdater_PatchNodeStatus(t *testing.T) { 34 t.Parallel() 35 36 type fields struct { 37 oldNode *v1.Node 38 } 39 type args struct { 40 newNode *v1.Node 41 } 42 tests := []struct { 43 name string 44 fields fields 45 args args 46 wantNode *v1.Node 47 wantErr assert.ErrorAssertionFunc 48 }{ 49 { 50 name: "patch resources", 51 fields: fields{ 52 oldNode: &v1.Node{ 53 ObjectMeta: metav1.ObjectMeta{ 54 Name: "node-1", 55 }, 56 }, 57 }, 58 args: args{ 59 newNode: &v1.Node{ 60 ObjectMeta: metav1.ObjectMeta{ 61 Name: "node-1", 62 }, 63 Status: v1.NodeStatus{ 64 Capacity: v1.ResourceList{ 65 "resource-1": resource.MustParse("10"), 66 }, 67 }, 68 }, 69 }, 70 wantNode: &v1.Node{ 71 ObjectMeta: metav1.ObjectMeta{ 72 Name: "node-1", 73 }, 74 Status: v1.NodeStatus{ 75 Capacity: v1.ResourceList{ 76 "resource-1": resource.MustParse("10"), 77 }, 78 }, 79 }, 80 wantErr: assert.NoError, 81 }, 82 } 83 for _, tt := range tests { 84 tt := tt 85 t.Run(tt.name, func(t *testing.T) { 86 t.Parallel() 87 88 baseCtx, err := katalystbase.GenerateFakeGenericContext([]runtime.Object{tt.fields.oldNode}) 89 assert.NoError(t, err) 90 r := NewRealNodeUpdater(baseCtx.Client.KubeClient) 91 tt.wantErr(t, r.PatchNodeStatus(context.Background(), tt.fields.oldNode, tt.args.newNode), fmt.Sprintf("PatchNodeStatus(%v, %v)", tt.fields.oldNode, tt.args.newNode)) 92 93 getNode, err := baseCtx.Client.KubeClient.CoreV1().Nodes().Get(context.Background(), tt.fields.oldNode.Name, metav1.GetOptions{ResourceVersion: "0"}) 94 assert.NoError(t, err) 95 assert.Equal(t, tt.wantNode, getNode) 96 }) 97 } 98 } 99 100 func TestRealNodeUpdater_PatchNode(t *testing.T) { 101 t.Parallel() 102 103 type fields struct { 104 oldNode *v1.Node 105 } 106 type args struct { 107 newNode *v1.Node 108 } 109 tests := []struct { 110 name string 111 fields fields 112 args args 113 wantNode *v1.Node 114 wantErr assert.ErrorAssertionFunc 115 }{ 116 { 117 name: "patch taint", 118 fields: fields{ 119 oldNode: &v1.Node{ 120 ObjectMeta: metav1.ObjectMeta{ 121 Name: "node-1", 122 }, 123 }, 124 }, 125 args: args{ 126 newNode: &v1.Node{ 127 ObjectMeta: metav1.ObjectMeta{ 128 Name: "node-1", 129 }, 130 Spec: v1.NodeSpec{ 131 Taints: []v1.Taint{ 132 { 133 Key: "key-1", 134 Value: "value-1", 135 Effect: v1.TaintEffectNoSchedule, 136 }, 137 }, 138 }, 139 }, 140 }, 141 wantNode: &v1.Node{ 142 ObjectMeta: metav1.ObjectMeta{ 143 Name: "node-1", 144 }, 145 Spec: v1.NodeSpec{ 146 Taints: []v1.Taint{ 147 { 148 Key: "key-1", 149 Value: "value-1", 150 Effect: v1.TaintEffectNoSchedule, 151 }, 152 }, 153 }, 154 }, 155 wantErr: assert.NoError, 156 }, 157 } 158 for _, tt := range tests { 159 tt := tt 160 t.Run(tt.name, func(t *testing.T) { 161 t.Parallel() 162 163 baseCtx, err := katalystbase.GenerateFakeGenericContext([]runtime.Object{tt.fields.oldNode}) 164 assert.NoError(t, err) 165 r := NewRealNodeUpdater(baseCtx.Client.KubeClient) 166 tt.wantErr(t, r.PatchNode(context.Background(), tt.fields.oldNode, tt.args.newNode), fmt.Sprintf("PatchNodeStatus(%v, %v)", tt.fields.oldNode, tt.args.newNode)) 167 168 getNode, err := baseCtx.Client.KubeClient.CoreV1().Nodes().Get(context.Background(), tt.fields.oldNode.Name, metav1.GetOptions{ResourceVersion: "0"}) 169 assert.NoError(t, err) 170 assert.Equal(t, tt.wantNode, getNode) 171 }) 172 } 173 } 174 175 func TestRealNodeUpdater_UpdateNode(t *testing.T) { 176 t.Parallel() 177 178 type fields struct { 179 oldNode *v1.Node 180 } 181 type args struct { 182 newNode *v1.Node 183 opt metav1.UpdateOptions 184 } 185 tests := []struct { 186 name string 187 fields fields 188 args args 189 wantNode *v1.Node 190 wantErr assert.ErrorAssertionFunc 191 }{ 192 { 193 name: "update taint", 194 fields: fields{ 195 oldNode: &v1.Node{ 196 ObjectMeta: metav1.ObjectMeta{ 197 Name: "node-1", 198 }, 199 }, 200 }, 201 args: args{ 202 newNode: &v1.Node{ 203 ObjectMeta: metav1.ObjectMeta{ 204 Name: "node-1", 205 }, 206 Spec: v1.NodeSpec{ 207 Taints: []v1.Taint{ 208 { 209 Key: "key-1", 210 Value: "value-1", 211 Effect: v1.TaintEffectNoSchedule, 212 }, 213 }, 214 }, 215 }, 216 }, 217 wantNode: &v1.Node{ 218 ObjectMeta: metav1.ObjectMeta{ 219 Name: "node-1", 220 }, 221 Spec: v1.NodeSpec{ 222 Taints: []v1.Taint{ 223 { 224 Key: "key-1", 225 Value: "value-1", 226 Effect: v1.TaintEffectNoSchedule, 227 }, 228 }, 229 }, 230 }, 231 wantErr: assert.NoError, 232 }, 233 } 234 for _, tt := range tests { 235 tt := tt 236 t.Run(tt.name, func(t *testing.T) { 237 t.Parallel() 238 239 baseCtx, err := katalystbase.GenerateFakeGenericContext([]runtime.Object{tt.fields.oldNode}) 240 assert.NoError(t, err) 241 r := NewRealNodeUpdater(baseCtx.Client.KubeClient) 242 getNode, err := r.UpdateNode(context.Background(), tt.args.newNode, tt.args.opt) 243 assert.NoError(t, err) 244 assert.Equal(t, tt.wantNode, getNode) 245 }) 246 } 247 }