|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making TKEStack |
| 3 | + * available. |
| 4 | + * |
| 5 | + * Copyright (C) 2012-2021 Tencent. All Rights Reserved. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 8 | + * this file except in compliance with the License. You may obtain a copy of the |
| 9 | + * License at |
| 10 | + * |
| 11 | + * https://opensource.org/licenses/Apache-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OF ANY KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package cluster |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + platformv1 "tkestack.io/tke/api/platform/v1" |
| 27 | +) |
| 28 | + |
| 29 | +func TestController_needsUpdate(t *testing.T) { |
| 30 | + // type fields struct { |
| 31 | + // queue workqueue.RateLimitingInterface |
| 32 | + // lister platformv1lister.ClusterLister |
| 33 | + // listerSynced cache.InformerSynced |
| 34 | + // log log.Logger |
| 35 | + // platformClient platformversionedclient.PlatformV1Interface |
| 36 | + // deleter deletion.ClusterDeleterInterface |
| 37 | + // healthCheckPeriod time.Duration |
| 38 | + // } |
| 39 | + type args struct { |
| 40 | + old *platformv1.Cluster |
| 41 | + new *platformv1.Cluster |
| 42 | + } |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + // fields fields |
| 46 | + args args |
| 47 | + want bool |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "change spec", |
| 51 | + args: args{ |
| 52 | + old: &platformv1.Cluster{ |
| 53 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 54 | + Spec: platformv1.ClusterSpec{DisplayName: "old"}, |
| 55 | + }, |
| 56 | + new: &platformv1.Cluster{ |
| 57 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 58 | + Spec: platformv1.ClusterSpec{DisplayName: "nes"}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + want: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Initializing to Running", |
| 65 | + args: args{ |
| 66 | + old: &platformv1.Cluster{ |
| 67 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 68 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing}, |
| 69 | + }, |
| 70 | + new: &platformv1.Cluster{ |
| 71 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 72 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterRunning}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + want: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "Initializing to Failed", |
| 79 | + args: args{ |
| 80 | + old: &platformv1.Cluster{ |
| 81 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 82 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing}, |
| 83 | + }, |
| 84 | + new: &platformv1.Cluster{ |
| 85 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 86 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterFailed}, |
| 87 | + }, |
| 88 | + }, |
| 89 | + want: true, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "Running to Failed", |
| 93 | + args: args{ |
| 94 | + old: &platformv1.Cluster{ |
| 95 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 96 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterRunning}, |
| 97 | + }, |
| 98 | + new: &platformv1.Cluster{ |
| 99 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 100 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterFailed}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + want: true, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "Running to Terminating", |
| 107 | + args: args{ |
| 108 | + old: &platformv1.Cluster{ |
| 109 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 110 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterRunning}, |
| 111 | + }, |
| 112 | + new: &platformv1.Cluster{ |
| 113 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 114 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterTerminating}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + want: true, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "Failed to Terminating", |
| 121 | + args: args{ |
| 122 | + old: &platformv1.Cluster{ |
| 123 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 124 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterFailed}, |
| 125 | + }, |
| 126 | + new: &platformv1.Cluster{ |
| 127 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 128 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterTerminating}, |
| 129 | + }, |
| 130 | + }, |
| 131 | + want: true, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "Failed to Running", |
| 135 | + args: args{ |
| 136 | + old: &platformv1.Cluster{ |
| 137 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 138 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterFailed}, |
| 139 | + }, |
| 140 | + new: &platformv1.Cluster{ |
| 141 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 142 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterRunning}, |
| 143 | + }, |
| 144 | + }, |
| 145 | + want: true, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "Failed to Initializing", |
| 149 | + args: args{ |
| 150 | + old: &platformv1.Cluster{ |
| 151 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 152 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterFailed}, |
| 153 | + }, |
| 154 | + new: &platformv1.Cluster{ |
| 155 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 156 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing}, |
| 157 | + }, |
| 158 | + }, |
| 159 | + want: true, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "last conditon unkonwn to false", |
| 163 | + args: args{ |
| 164 | + old: &platformv1.Cluster{ |
| 165 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 166 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 167 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionUnknown}}}, |
| 168 | + }, |
| 169 | + new: &platformv1.Cluster{ |
| 170 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 171 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 172 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionFalse}}}, |
| 173 | + }, |
| 174 | + }, |
| 175 | + want: false, |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "last conditon unkonwn to true", |
| 179 | + args: args{ |
| 180 | + old: &platformv1.Cluster{ |
| 181 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 182 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 183 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionUnknown}}}, |
| 184 | + }, |
| 185 | + new: &platformv1.Cluster{ |
| 186 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 187 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 188 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionTrue}}}, |
| 189 | + }, |
| 190 | + }, |
| 191 | + want: true, |
| 192 | + }, |
| 193 | + { |
| 194 | + name: "last conditon unkonwn to true resync", |
| 195 | + args: args{ |
| 196 | + old: &platformv1.Cluster{ |
| 197 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 198 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 199 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionUnknown}}}, |
| 200 | + }, |
| 201 | + new: &platformv1.Cluster{ |
| 202 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 203 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 204 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionUnknown}}}, |
| 205 | + }, |
| 206 | + }, |
| 207 | + want: true, |
| 208 | + }, |
| 209 | + { |
| 210 | + name: "last conditon true to unknown", |
| 211 | + args: args{ |
| 212 | + old: &platformv1.Cluster{ |
| 213 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 214 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 215 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionTrue}}}, |
| 216 | + }, |
| 217 | + new: &platformv1.Cluster{ |
| 218 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 219 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 220 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionUnknown}}}, |
| 221 | + }, |
| 222 | + }, |
| 223 | + want: true, |
| 224 | + }, |
| 225 | + { |
| 226 | + name: "last conditon false to unknown", |
| 227 | + args: args{ |
| 228 | + old: &platformv1.Cluster{ |
| 229 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "old"}, |
| 230 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 231 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionFalse}}}, |
| 232 | + }, |
| 233 | + new: &platformv1.Cluster{ |
| 234 | + ObjectMeta: v1.ObjectMeta{ResourceVersion: "new"}, |
| 235 | + Status: platformv1.ClusterStatus{Phase: platformv1.ClusterInitializing, |
| 236 | + Conditions: []platformv1.ClusterCondition{{Status: platformv1.ConditionUnknown}}}, |
| 237 | + }, |
| 238 | + }, |
| 239 | + want: true, |
| 240 | + }, |
| 241 | + // TODO: Add test cases. |
| 242 | + } |
| 243 | + for _, tt := range tests { |
| 244 | + t.Run(tt.name, func(t *testing.T) { |
| 245 | + c := &Controller{ |
| 246 | + // queue: tt.fields.queue, |
| 247 | + // lister: tt.fields.lister, |
| 248 | + // listerSynced: tt.fields.listerSynced, |
| 249 | + // log: tt.fields.log, |
| 250 | + // platformClient: tt.fields.platformClient, |
| 251 | + // deleter: tt.fields.deleter, |
| 252 | + healthCheckPeriod: time.Second, |
| 253 | + } |
| 254 | + if got := c.needsUpdate(tt.args.old, tt.args.new); got != tt.want { |
| 255 | + t.Errorf("Controller.needsUpdate() = %v, want %v", got, tt.want) |
| 256 | + } |
| 257 | + }) |
| 258 | + } |
| 259 | +} |
0 commit comments