|
| 1 | +/* |
| 2 | +Copyright 2020 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 | + "reflect" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestExtractAndDefaultParameters(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + parameters map[string]string |
| 28 | + expectParams DiskParameters |
| 29 | + expectErr bool |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "defaults", |
| 33 | + parameters: map[string]string{}, |
| 34 | + expectParams: DiskParameters{ |
| 35 | + DiskType: "pd-standard", |
| 36 | + ReplicationType: "none", |
| 37 | + DiskEncryptionKMSKey: "", |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "specified empties", |
| 42 | + parameters: map[string]string{ParameterKeyType: "", ParameterKeyReplicationType: "", ParameterKeyDiskEncryptionKmsKey: ""}, |
| 43 | + expectParams: DiskParameters{ |
| 44 | + DiskType: "pd-standard", |
| 45 | + ReplicationType: "none", |
| 46 | + DiskEncryptionKMSKey: "", |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "random keys", |
| 51 | + parameters: map[string]string{ParameterKeyType: "", "foo": "", ParameterKeyDiskEncryptionKmsKey: ""}, |
| 52 | + expectErr: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "real values", |
| 56 | + parameters: map[string]string{ParameterKeyType: "pd-ssd", ParameterKeyReplicationType: "regional-pd", ParameterKeyDiskEncryptionKmsKey: "foo/key"}, |
| 57 | + expectParams: DiskParameters{ |
| 58 | + DiskType: "pd-ssd", |
| 59 | + ReplicationType: "regional-pd", |
| 60 | + DiskEncryptionKMSKey: "foo/key", |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "partial spec", |
| 65 | + parameters: map[string]string{ParameterKeyDiskEncryptionKmsKey: "foo/key"}, |
| 66 | + expectParams: DiskParameters{ |
| 67 | + DiskType: "pd-standard", |
| 68 | + ReplicationType: "none", |
| 69 | + DiskEncryptionKMSKey: "foo/key", |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tc := range tests { |
| 75 | + t.Run(tc.name, func(t *testing.T) { |
| 76 | + p, err := ExtractAndDefaultParameters(tc.parameters) |
| 77 | + if gotErr := err != nil; gotErr != tc.expectErr { |
| 78 | + t.Fatalf("ExtractAndDefaultParameters(%+v) = %v; expectedErr: %v", tc.parameters, err, tc.expectErr) |
| 79 | + } |
| 80 | + if err != nil { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if !reflect.DeepEqual(p, tc.expectParams) { |
| 85 | + t.Errorf("ExtractAndDefaultParameters(%+v) = %v; expected params: %v", tc.parameters, p, tc.expectParams) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments