|
| 1 | +/* |
| 2 | +Copyright 2022 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 v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/utils/pointer" |
| 24 | +) |
| 25 | + |
| 26 | +func TestIBMPowerVSCluster_create(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + powervsCluster *IBMPowerVSCluster |
| 30 | + wantErr bool |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "Should allow if either Network ID or name is set", |
| 34 | + powervsCluster: &IBMPowerVSCluster{ |
| 35 | + Spec: IBMPowerVSClusterSpec{ |
| 36 | + ServiceInstanceID: "capi-si-id", |
| 37 | + Network: IBMPowerVSResourceReference{ |
| 38 | + ID: pointer.String("capi-net-id"), |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + wantErr: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Should error if both Network ID and name are set", |
| 46 | + powervsCluster: &IBMPowerVSCluster{ |
| 47 | + Spec: IBMPowerVSClusterSpec{ |
| 48 | + ServiceInstanceID: "capi-si-id", |
| 49 | + Network: IBMPowerVSResourceReference{ |
| 50 | + ID: pointer.String("capi-net-id"), |
| 51 | + Name: pointer.String("capi-net"), |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + wantErr: true, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tc := range tests { |
| 60 | + t.Run(tc.name, func(t *testing.T) { |
| 61 | + cluster := tc.powervsCluster.DeepCopy() |
| 62 | + cluster.ObjectMeta = metav1.ObjectMeta{ |
| 63 | + GenerateName: "capi-cluster-", |
| 64 | + Namespace: "default", |
| 65 | + } |
| 66 | + |
| 67 | + if err := testEnv.Create(ctx, cluster); (err != nil) != tc.wantErr { |
| 68 | + t.Errorf("ValidateCreate() error = %v, wantErr %v", err, tc.wantErr) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestIBMPowerVSCluster_update(t *testing.T) { |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + oldPowervsCluster *IBMPowerVSCluster |
| 78 | + newPowervsCluster *IBMPowerVSCluster |
| 79 | + wantErr bool |
| 80 | + }{ |
| 81 | + { |
| 82 | + name: "Should allow if either Network ID or name is set", |
| 83 | + oldPowervsCluster: &IBMPowerVSCluster{ |
| 84 | + Spec: IBMPowerVSClusterSpec{ |
| 85 | + ServiceInstanceID: "capi-si-id", |
| 86 | + Network: IBMPowerVSResourceReference{ |
| 87 | + ID: pointer.String("capi-net-id"), |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + newPowervsCluster: &IBMPowerVSCluster{ |
| 92 | + Spec: IBMPowerVSClusterSpec{ |
| 93 | + ServiceInstanceID: "capi-si-id", |
| 94 | + Network: IBMPowerVSResourceReference{ |
| 95 | + ID: pointer.String("capi-net-id"), |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + wantErr: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "Should error if both Network ID and name are set", |
| 103 | + oldPowervsCluster: &IBMPowerVSCluster{ |
| 104 | + Spec: IBMPowerVSClusterSpec{ |
| 105 | + ServiceInstanceID: "capi-si-id", |
| 106 | + Network: IBMPowerVSResourceReference{ |
| 107 | + ID: pointer.String("capi-net-id"), |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + newPowervsCluster: &IBMPowerVSCluster{ |
| 112 | + Spec: IBMPowerVSClusterSpec{ |
| 113 | + ServiceInstanceID: "capi-si-id", |
| 114 | + Network: IBMPowerVSResourceReference{ |
| 115 | + ID: pointer.String("capi-net-id"), |
| 116 | + Name: pointer.String("capi-net-name"), |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + wantErr: true, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tc := range tests { |
| 125 | + t.Run(tc.name, func(t *testing.T) { |
| 126 | + cluster := tc.oldPowervsCluster.DeepCopy() |
| 127 | + cluster.ObjectMeta = metav1.ObjectMeta{ |
| 128 | + GenerateName: "capi-cluster-", |
| 129 | + Namespace: "default", |
| 130 | + } |
| 131 | + |
| 132 | + if err := testEnv.Create(ctx, cluster); err != nil { |
| 133 | + t.Errorf("failed to create cluster: %v", err) |
| 134 | + } |
| 135 | + |
| 136 | + cluster.Spec = tc.newPowervsCluster.Spec |
| 137 | + if err := testEnv.Update(ctx, cluster); (err != nil) != tc.wantErr { |
| 138 | + t.Errorf("ValidateUpdate() error = %v, wantErr %v", err, tc.wantErr) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
0 commit comments