|
| 1 | +/* |
| 2 | +Copyright 2018 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 common |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "reflect" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/meta" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + volIDZoneFmt = "projects/%s/zones/%s/disks/%s" |
| 29 | + volIDRegionFmt = "projects/%s/regions/%s/disks/%s" |
| 30 | + nodeIDFmt = "%s/%s" |
| 31 | +) |
| 32 | + |
| 33 | +func TestBytesToGb(t *testing.T) { |
| 34 | + testCases := []struct { |
| 35 | + name string |
| 36 | + bytes int64 |
| 37 | + expGB int64 |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "normal 5gb", |
| 41 | + bytes: 5368709120, |
| 42 | + expGB: 5, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "slightly less than 5gb", |
| 46 | + bytes: 5368709119, |
| 47 | + expGB: 4, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "slightly more than 5gb", |
| 51 | + bytes: 5368709121, |
| 52 | + expGB: 5, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "zero", |
| 56 | + bytes: 0, |
| 57 | + expGB: 0, |
| 58 | + }, |
| 59 | + } |
| 60 | + for _, tc := range testCases { |
| 61 | + t.Logf("test case: %s", tc.name) |
| 62 | + gotGB := BytesToGb(tc.bytes) |
| 63 | + |
| 64 | + if gotGB != tc.expGB { |
| 65 | + t.Errorf("got GB %v, expected %v", gotGB, tc.expGB) |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestGbToBytes(t *testing.T) { |
| 72 | + testCases := []struct { |
| 73 | + name string |
| 74 | + gb int64 |
| 75 | + expBytes int64 |
| 76 | + }{ |
| 77 | + { |
| 78 | + name: "5Gb", |
| 79 | + gb: 5, |
| 80 | + expBytes: 5368709120, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "0gb", |
| 84 | + gb: 0, |
| 85 | + expBytes: 0, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "1gb", |
| 89 | + gb: 1, |
| 90 | + expBytes: 1024 * 1024 * 1024, |
| 91 | + }, |
| 92 | + } |
| 93 | + for _, tc := range testCases { |
| 94 | + t.Logf("test case: %s", tc.name) |
| 95 | + gotBytes := GbToBytes(tc.gb) |
| 96 | + if gotBytes != tc.expBytes { |
| 97 | + t.Errorf("got bytes: %v, expected: %v", gotBytes, tc.expBytes) |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestVolumeIDToKey(t *testing.T) { |
| 104 | + testName := "test-name" |
| 105 | + testZone := "test-zone" |
| 106 | + testProject := "test-project" |
| 107 | + testRegion := "test-region" |
| 108 | + |
| 109 | + testCases := []struct { |
| 110 | + name string |
| 111 | + volID string |
| 112 | + expKey *meta.Key |
| 113 | + expErr bool |
| 114 | + }{ |
| 115 | + { |
| 116 | + name: "normal zonal", |
| 117 | + volID: fmt.Sprintf(volIDZoneFmt, testProject, testZone, testName), |
| 118 | + expKey: meta.ZonalKey(testName, testZone), |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "normal regional", |
| 122 | + volID: fmt.Sprintf(volIDRegionFmt, testProject, testRegion, testName), |
| 123 | + expKey: meta.RegionalKey(testName, testRegion), |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "malformed", |
| 127 | + volID: "wrong", |
| 128 | + expErr: true, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "malformed but right length", |
| 132 | + volID: "this/is/wrong/but/right/num", |
| 133 | + expErr: true, |
| 134 | + }, |
| 135 | + } |
| 136 | + for _, tc := range testCases { |
| 137 | + t.Logf("test case: %s", tc.name) |
| 138 | + gotKey, err := VolumeIDToKey(tc.volID) |
| 139 | + if err == nil && tc.expErr { |
| 140 | + t.Errorf("Expected error but got none") |
| 141 | + } |
| 142 | + if err != nil { |
| 143 | + if !tc.expErr { |
| 144 | + t.Errorf("Did not expect error but got: %v", err) |
| 145 | + } |
| 146 | + continue |
| 147 | + } |
| 148 | + |
| 149 | + if !reflect.DeepEqual(gotKey, tc.expKey) { |
| 150 | + t.Errorf("Got key %v, but expected %v, from volume ID %v", gotKey, tc.expKey, tc.volID) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | +} |
| 155 | + |
| 156 | +func TestNodeIDToZoneAndName(t *testing.T) { |
| 157 | + testName := "test-name" |
| 158 | + testZone := "test-zone" |
| 159 | + |
| 160 | + testCases := []struct { |
| 161 | + name string |
| 162 | + nodeID string |
| 163 | + expZone string |
| 164 | + expName string |
| 165 | + expErr bool |
| 166 | + }{ |
| 167 | + { |
| 168 | + name: "normal", |
| 169 | + nodeID: fmt.Sprintf(nodeIDFmt, testZone, testName), |
| 170 | + expZone: testZone, |
| 171 | + expName: testName, |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "malformed", |
| 175 | + nodeID: "wrong", |
| 176 | + expErr: true, |
| 177 | + }, |
| 178 | + } |
| 179 | + for _, tc := range testCases { |
| 180 | + t.Logf("test case: %s", tc.name) |
| 181 | + zone, name, err := NodeIDToZoneAndName(tc.nodeID) |
| 182 | + if err == nil && tc.expErr { |
| 183 | + t.Errorf("Expected error but got none") |
| 184 | + } |
| 185 | + if err != nil { |
| 186 | + if !tc.expErr { |
| 187 | + t.Errorf("Did not expect error but got: %v", err) |
| 188 | + } |
| 189 | + continue |
| 190 | + } |
| 191 | + |
| 192 | + if !(zone == tc.expZone && name == tc.expName) { |
| 193 | + t.Errorf("got wrong zone/name %s/%s, expected %s/%s", zone, name, tc.expZone, tc.expName) |
| 194 | + } |
| 195 | + |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func TestGetRegionFromZones(t *testing.T) { |
| 200 | + testCases := []struct { |
| 201 | + name string |
| 202 | + zones []string |
| 203 | + expRegion string |
| 204 | + expErr bool |
| 205 | + }{ |
| 206 | + { |
| 207 | + name: "single zone success", |
| 208 | + zones: []string{"us-central1-c"}, |
| 209 | + expRegion: "us-central1", |
| 210 | + }, |
| 211 | + { |
| 212 | + name: "multi zone success", |
| 213 | + zones: []string{"us-central1-b", "us-central1-c"}, |
| 214 | + expRegion: "us-central1", |
| 215 | + }, |
| 216 | + { |
| 217 | + name: "multi different zone fail", |
| 218 | + zones: []string{"us-central1-c", "us-asia1-b"}, |
| 219 | + expErr: true, |
| 220 | + }, |
| 221 | + { |
| 222 | + name: "empty zones", |
| 223 | + expErr: true, |
| 224 | + }, |
| 225 | + { |
| 226 | + name: "malformed zone", |
| 227 | + zones: []string{"blah/blooh"}, |
| 228 | + expErr: true, |
| 229 | + }, |
| 230 | + } |
| 231 | + for _, tc := range testCases { |
| 232 | + t.Logf("test case: %s", tc.name) |
| 233 | + region, err := GetRegionFromZones(tc.zones) |
| 234 | + if err == nil && tc.expErr { |
| 235 | + t.Errorf("Expected error but got none") |
| 236 | + } |
| 237 | + if err != nil { |
| 238 | + if !tc.expErr { |
| 239 | + t.Errorf("Did not expect error but got: %v", err) |
| 240 | + } |
| 241 | + continue |
| 242 | + } |
| 243 | + |
| 244 | + if region != tc.expRegion { |
| 245 | + t.Errorf("Got region: %v, expected: %v", region, tc.expRegion) |
| 246 | + } |
| 247 | + |
| 248 | + } |
| 249 | +} |
0 commit comments