| 1 |
/* |
| 2 |
Copyright 2019 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 storageversion |
| 18 |
|
| 19 |
import ( |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/google/go-cmp/cmp" |
| 24 |
"k8s.io/api/apiserverinternal/v1alpha1" |
| 25 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
v1 = "v1" |
| 30 |
v2 = "v2" |
| 31 |
ssv1 = v1alpha1.ServerStorageVersion{ |
| 32 |
APIServerID: "1", |
| 33 |
EncodingVersion: "v1", |
| 34 |
DecodableVersions: []string{"v1", "v2"}, |
| 35 |
ServedVersions: []string{"v1"}, |
| 36 |
} |
| 37 |
ssv2 = v1alpha1.ServerStorageVersion{ |
| 38 |
APIServerID: "2", |
| 39 |
EncodingVersion: "v1", |
| 40 |
DecodableVersions: []string{"v1", "v2"}, |
| 41 |
ServedVersions: []string{"v1", "v2"}, |
| 42 |
} |
| 43 |
// ssv3 has a different encoding version |
| 44 |
ssv3 = v1alpha1.ServerStorageVersion{ |
| 45 |
APIServerID: "3", |
| 46 |
EncodingVersion: "v2", |
| 47 |
DecodableVersions: []string{"v1", "v2"}, |
| 48 |
ServedVersions: []string{"v1", "v2"}, |
| 49 |
} |
| 50 |
ssv4 = v1alpha1.ServerStorageVersion{ |
| 51 |
APIServerID: "4", |
| 52 |
EncodingVersion: "v1", |
| 53 |
DecodableVersions: []string{"v1", "v2", "v4"}, |
| 54 |
ServedVersions: []string{"v1", "v2"}, |
| 55 |
} |
| 56 |
ssv5 = v1alpha1.ServerStorageVersion{ |
| 57 |
APIServerID: "5", |
| 58 |
EncodingVersion: "v2", |
| 59 |
DecodableVersions: []string{"v1", "v2", "v4"}, |
| 60 |
ServedVersions: []string{"v1", "v2"}, |
| 61 |
} |
| 62 |
) |
| 63 |
|
| 64 |
func TestLocalUpdateStorageVersion(t *testing.T) { |
| 65 |
tests := []struct { |
| 66 |
old v1alpha1.StorageVersionStatus |
| 67 |
newSSV v1alpha1.ServerStorageVersion |
| 68 |
expected v1alpha1.StorageVersionStatus |
| 69 |
expectLastTransitionTimeUpdate bool |
| 70 |
}{ |
| 71 |
{ |
| 72 |
old: v1alpha1.StorageVersionStatus{}, |
| 73 |
newSSV: ssv1, |
| 74 |
expected: v1alpha1.StorageVersionStatus{ |
| 75 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1}, |
| 76 |
CommonEncodingVersion: &v1, |
| 77 |
Conditions: commonVersionTrueCondition(), |
| 78 |
}, |
| 79 |
expectLastTransitionTimeUpdate: true, |
| 80 |
}, |
| 81 |
{ |
| 82 |
old: v1alpha1.StorageVersionStatus{ |
| 83 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2}, |
| 84 |
CommonEncodingVersion: &v1, |
| 85 |
Conditions: commonVersionTrueCondition(), |
| 86 |
}, |
| 87 |
newSSV: ssv3, |
| 88 |
expected: v1alpha1.StorageVersionStatus{ |
| 89 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3}, |
| 90 |
Conditions: commonVersionFalseCondition(), |
| 91 |
}, |
| 92 |
expectLastTransitionTimeUpdate: true, |
| 93 |
}, |
| 94 |
{ |
| 95 |
old: v1alpha1.StorageVersionStatus{ |
| 96 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2}, |
| 97 |
CommonEncodingVersion: &v1, |
| 98 |
Conditions: commonVersionTrueCondition(), |
| 99 |
}, |
| 100 |
newSSV: ssv4, |
| 101 |
expected: v1alpha1.StorageVersionStatus{ |
| 102 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv4}, |
| 103 |
CommonEncodingVersion: &v1, |
| 104 |
Conditions: commonVersionTrueCondition(), |
| 105 |
}, |
| 106 |
}, |
| 107 |
{ |
| 108 |
old: v1alpha1.StorageVersionStatus{ |
| 109 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3}, |
| 110 |
Conditions: commonVersionFalseCondition(), |
| 111 |
}, |
| 112 |
newSSV: ssv4, |
| 113 |
expected: v1alpha1.StorageVersionStatus{ |
| 114 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3, ssv4}, |
| 115 |
Conditions: commonVersionFalseCondition(), |
| 116 |
}, |
| 117 |
}, |
| 118 |
} |
| 119 |
|
| 120 |
for _, tc := range tests { |
| 121 |
sv := &v1alpha1.StorageVersion{Status: tc.old} |
| 122 |
updated := localUpdateStorageVersion(sv, tc.newSSV.APIServerID, tc.newSSV.EncodingVersion, tc.newSSV.DecodableVersions, tc.newSSV.ServedVersions) |
| 123 |
if tc.expectLastTransitionTimeUpdate == updated.Status.Conditions[0].LastTransitionTime.IsZero() { |
| 124 |
t.Errorf("unexpected LastTransitionTime, expected update: %v, got: %v", |
| 125 |
tc.expectLastTransitionTimeUpdate, updated.Status.Conditions[0].LastTransitionTime) |
| 126 |
} |
| 127 |
updated.Status.Conditions[0].LastTransitionTime = metav1.Time{} |
| 128 |
if e, a := tc.expected, updated.Status; !reflect.DeepEqual(e, a) { |
| 129 |
t.Errorf("unexpected: %v", cmp.Diff(e, a)) |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func commonVersionTrueCondition() []v1alpha1.StorageVersionCondition { |
| 135 |
return []v1alpha1.StorageVersionCondition{{ |
| 136 |
Type: v1alpha1.AllEncodingVersionsEqual, |
| 137 |
Status: v1alpha1.ConditionTrue, |
| 138 |
Reason: "CommonEncodingVersionSet", |
| 139 |
Message: "Common encoding version set", |
| 140 |
}} |
| 141 |
} |
| 142 |
func commonVersionFalseCondition() []v1alpha1.StorageVersionCondition { |
| 143 |
return []v1alpha1.StorageVersionCondition{{ |
| 144 |
Type: v1alpha1.AllEncodingVersionsEqual, |
| 145 |
Status: v1alpha1.ConditionFalse, |
| 146 |
Reason: "CommonEncodingVersionUnset", |
| 147 |
Message: "Common encoding version unset", |
| 148 |
}} |
| 149 |
} |
| 150 |
|
| 151 |
func TestSetCommonEncodingVersion(t *testing.T) { |
| 152 |
tests := []struct { |
| 153 |
name string |
| 154 |
old v1alpha1.StorageVersionStatus |
| 155 |
expected v1alpha1.StorageVersionStatus |
| 156 |
expectLastTransitionTimeUpdate bool |
| 157 |
}{ |
| 158 |
{ |
| 159 |
name: "no-common_init", |
| 160 |
old: v1alpha1.StorageVersionStatus{ |
| 161 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3}, |
| 162 |
}, |
| 163 |
expected: v1alpha1.StorageVersionStatus{ |
| 164 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3}, |
| 165 |
Conditions: commonVersionFalseCondition(), |
| 166 |
}, |
| 167 |
expectLastTransitionTimeUpdate: true, |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: "no-common_transition", |
| 171 |
old: v1alpha1.StorageVersionStatus{ |
| 172 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3}, |
| 173 |
CommonEncodingVersion: &v1, |
| 174 |
Conditions: commonVersionTrueCondition(), |
| 175 |
}, |
| 176 |
expected: v1alpha1.StorageVersionStatus{ |
| 177 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3}, |
| 178 |
Conditions: commonVersionFalseCondition(), |
| 179 |
}, |
| 180 |
expectLastTransitionTimeUpdate: true, |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "no-common_no-transition", |
| 184 |
old: v1alpha1.StorageVersionStatus{ |
| 185 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3}, |
| 186 |
Conditions: commonVersionFalseCondition(), |
| 187 |
}, |
| 188 |
expected: v1alpha1.StorageVersionStatus{ |
| 189 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2, ssv3}, |
| 190 |
Conditions: commonVersionFalseCondition(), |
| 191 |
}, |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "common_init", |
| 195 |
old: v1alpha1.StorageVersionStatus{ |
| 196 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2}, |
| 197 |
}, |
| 198 |
expected: v1alpha1.StorageVersionStatus{ |
| 199 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2}, |
| 200 |
CommonEncodingVersion: &v1, |
| 201 |
Conditions: commonVersionTrueCondition(), |
| 202 |
}, |
| 203 |
expectLastTransitionTimeUpdate: true, |
| 204 |
}, |
| 205 |
{ |
| 206 |
name: "common_no-transition", |
| 207 |
old: v1alpha1.StorageVersionStatus{ |
| 208 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2}, |
| 209 |
CommonEncodingVersion: &v1, |
| 210 |
Conditions: commonVersionTrueCondition(), |
| 211 |
}, |
| 212 |
expected: v1alpha1.StorageVersionStatus{ |
| 213 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2}, |
| 214 |
CommonEncodingVersion: &v1, |
| 215 |
Conditions: commonVersionTrueCondition(), |
| 216 |
}, |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "common_transition", |
| 220 |
old: v1alpha1.StorageVersionStatus{ |
| 221 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2}, |
| 222 |
Conditions: commonVersionFalseCondition(), |
| 223 |
}, |
| 224 |
expected: v1alpha1.StorageVersionStatus{ |
| 225 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv1, ssv2}, |
| 226 |
CommonEncodingVersion: &v1, |
| 227 |
Conditions: commonVersionTrueCondition(), |
| 228 |
}, |
| 229 |
expectLastTransitionTimeUpdate: true, |
| 230 |
}, |
| 231 |
{ |
| 232 |
name: "common_version-changed", |
| 233 |
old: v1alpha1.StorageVersionStatus{ |
| 234 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv3, ssv5}, |
| 235 |
CommonEncodingVersion: &v1, |
| 236 |
Conditions: commonVersionTrueCondition(), |
| 237 |
}, |
| 238 |
expected: v1alpha1.StorageVersionStatus{ |
| 239 |
StorageVersions: []v1alpha1.ServerStorageVersion{ssv3, ssv5}, |
| 240 |
CommonEncodingVersion: &v2, |
| 241 |
Conditions: commonVersionTrueCondition(), |
| 242 |
}, |
| 243 |
expectLastTransitionTimeUpdate: true, |
| 244 |
}, |
| 245 |
} |
| 246 |
|
| 247 |
for _, tc := range tests { |
| 248 |
t.Run(tc.name, func(t *testing.T) { |
| 249 |
sv := &v1alpha1.StorageVersion{Status: tc.old} |
| 250 |
SetCommonEncodingVersion(sv) |
| 251 |
if (tc.expectLastTransitionTimeUpdate && sv.Status.Conditions[0].LastTransitionTime.IsZero()) || |
| 252 |
(!tc.expectLastTransitionTimeUpdate && !sv.Status.Conditions[0].LastTransitionTime.IsZero()) { |
| 253 |
t.Errorf("unexpected LastTransitionTime, expected update: %v, got: %v", |
| 254 |
tc.expectLastTransitionTimeUpdate, sv.Status.Conditions[0].LastTransitionTime) |
| 255 |
} |
| 256 |
sv.Status.Conditions[0].LastTransitionTime = metav1.Time{} |
| 257 |
if e, a := tc.expected, sv.Status; !reflect.DeepEqual(e, a) { |
| 258 |
t.Errorf("unexpected: %v", cmp.Diff(e, a)) |
| 259 |
} |
| 260 |
}) |
| 261 |
} |
| 262 |
} |