|
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package gcecloudprovider |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + computev1 "google.golang.org/api/compute/v1" |
| 21 | + "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common" |
| 22 | +) |
| 23 | + |
| 24 | +func TestValidateDiskParameters(t *testing.T) { |
| 25 | + testCases := []struct { |
| 26 | + fetchedKMSKey string |
| 27 | + storageClassKMSKey string |
| 28 | + expectErr bool |
| 29 | + }{ |
| 30 | + { |
| 31 | + fetchedKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/test-key/cryptoKeyVersions/8", |
| 32 | + storageClassKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/test-key", |
| 33 | + expectErr: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + fetchedKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/test-key", |
| 37 | + storageClassKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/test-key", |
| 38 | + expectErr: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + fetchedKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/garbage/cryptoKeyVersions/8", |
| 42 | + storageClassKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/test-key", |
| 43 | + expectErr: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + fetchedKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/garbage", |
| 47 | + storageClassKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/test-key", |
| 48 | + expectErr: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + fetchedKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/test-key", |
| 52 | + storageClassKMSKey: "projects/my-project/locations/us-west1/keyRings/TestKeyRing/cryptoKeys/test-key", |
| 53 | + expectErr: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + fetchedKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/foobar/cryptoKeyVersions/8", |
| 57 | + storageClassKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/foo", |
| 58 | + expectErr: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + fetchedKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/foobar", |
| 62 | + storageClassKMSKey: "projects/my-project/locations/us-central1/keyRings/TestKeyRing/cryptoKeys/foo", |
| 63 | + expectErr: true, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for i, tc := range testCases { |
| 68 | + // Arrange |
| 69 | + existingDisk := &CloudDisk{ |
| 70 | + ZonalDisk: &computev1.Disk{ |
| 71 | + Id: 546559531467326555, |
| 72 | + CreationTimestamp: "2020-07-24T17:20:06.292-07:00", |
| 73 | + Name: "test-disk", |
| 74 | + SizeGb: 500, |
| 75 | + Zone: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c", |
| 76 | + Status: "READY", |
| 77 | + SelfLink: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/disks/test-disk", |
| 78 | + Type: "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/diskTypes/pd-standard", |
| 79 | + DiskEncryptionKey: &computev1.CustomerEncryptionKey{ |
| 80 | + KmsKeyName: tc.fetchedKMSKey, |
| 81 | + }, |
| 82 | + LabelFingerprint: "42WmSpB8rSM=", |
| 83 | + PhysicalBlockSizeBytes: 4096, |
| 84 | + Kind: "compute#disk", |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + storageClassParams := common.DiskParameters{ |
| 89 | + DiskType: "pd-standard", |
| 90 | + ReplicationType: "none", |
| 91 | + DiskEncryptionKMSKey: tc.storageClassKMSKey, |
| 92 | + } |
| 93 | + |
| 94 | + // Act |
| 95 | + err := ValidateDiskParameters(existingDisk, storageClassParams) |
| 96 | + |
| 97 | + // Assert |
| 98 | + if !tc.expectErr && err != nil { |
| 99 | + t.Fatalf("Test case #%v: ValidateDiskParameters did not expect error, but got %v", i, err) |
| 100 | + } |
| 101 | + if tc.expectErr && err == nil { |
| 102 | + t.Fatalf("Test case #%v: ValidateDiskParameters expected error, but got no error", i) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments