|
| 1 | +package spectrocloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 5 | + "github.com/spectrocloud/gomi/pkg/ptr" |
| 6 | + "github.com/spectrocloud/hapi/models" |
| 7 | + "github.com/spectrocloud/terraform-provider-spectrocloud/types" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestToMachinePoolGcp(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + input map[string]interface{} |
| 16 | + expectedOutput *models.V1GcpMachinePoolConfigEntity |
| 17 | + expectError bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Control Plane", |
| 21 | + input: map[string]interface{}{ |
| 22 | + "control_plane": true, |
| 23 | + "control_plane_as_worker": true, |
| 24 | + "azs": schema.NewSet(schema.HashString, []interface{}{"us-central1-a"}), |
| 25 | + "instance_type": "n1-standard-1", |
| 26 | + "disk_size_gb": 50, |
| 27 | + "name": "example-name", |
| 28 | + "count": 3, |
| 29 | + "node_repave_interval": 0, |
| 30 | + }, |
| 31 | + expectedOutput: &models.V1GcpMachinePoolConfigEntity{ |
| 32 | + CloudConfig: &models.V1GcpMachinePoolCloudConfigEntity{ |
| 33 | + Azs: []string{"us-central1-a"}, |
| 34 | + InstanceType: types.Ptr("n1-standard-1"), |
| 35 | + RootDeviceSize: int64(50), |
| 36 | + }, |
| 37 | + PoolConfig: &models.V1MachinePoolConfigEntity{ |
| 38 | + AdditionalLabels: map[string]string{}, |
| 39 | + Taints: nil, |
| 40 | + IsControlPlane: true, |
| 41 | + Labels: []string{"master"}, |
| 42 | + Name: types.Ptr("example-name"), |
| 43 | + Size: types.Ptr(int32(3)), |
| 44 | + UpdateStrategy: &models.V1UpdateStrategy{ |
| 45 | + Type: "RollingUpdateScaleOut", |
| 46 | + }, |
| 47 | + UseControlPlaneAsWorker: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + expectError: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Node Repave Interval Error", |
| 54 | + input: map[string]interface{}{ |
| 55 | + "control_plane": true, |
| 56 | + "control_plane_as_worker": false, |
| 57 | + "azs": schema.NewSet(schema.HashString, []interface{}{"us-central1-a"}), |
| 58 | + "instance_type": "n1-standard-2", |
| 59 | + "disk_size_gb": 100, |
| 60 | + "name": "example-name-2", |
| 61 | + "count": 2, |
| 62 | + "node_repave_interval": -1, |
| 63 | + }, |
| 64 | + expectedOutput: &models.V1GcpMachinePoolConfigEntity{ |
| 65 | + CloudConfig: &models.V1GcpMachinePoolCloudConfigEntity{ |
| 66 | + Azs: []string{"us-central1-a"}, |
| 67 | + InstanceType: types.Ptr("n1-standard-2"), |
| 68 | + RootDeviceSize: int64(100), |
| 69 | + }, |
| 70 | + PoolConfig: &models.V1MachinePoolConfigEntity{ |
| 71 | + AdditionalLabels: map[string]string{"example": "label"}, |
| 72 | + Taints: []*models.V1Taint{}, |
| 73 | + IsControlPlane: true, |
| 74 | + Labels: []string{"master"}, |
| 75 | + Name: types.Ptr("example-name-2"), |
| 76 | + Size: types.Ptr(int32(2)), |
| 77 | + UpdateStrategy: &models.V1UpdateStrategy{ |
| 78 | + Type: "RollingUpdate", |
| 79 | + }, |
| 80 | + UseControlPlaneAsWorker: false, |
| 81 | + }, |
| 82 | + }, |
| 83 | + expectError: true, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + output, err := toMachinePoolGcp(tt.input) |
| 90 | + |
| 91 | + if tt.expectError { |
| 92 | + assert.Error(t, err) |
| 93 | + } else { |
| 94 | + assert.NoError(t, err) |
| 95 | + assert.Equal(t, tt.expectedOutput, output) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestFlattenMachinePoolConfigsGcp(t *testing.T) { |
| 102 | + tests := []struct { |
| 103 | + name string |
| 104 | + input []*models.V1GcpMachinePoolConfig |
| 105 | + expectedOutput []interface{} |
| 106 | + }{ |
| 107 | + { |
| 108 | + name: "Single Machine Pool", |
| 109 | + input: []*models.V1GcpMachinePoolConfig{ |
| 110 | + { |
| 111 | + AdditionalLabels: map[string]string{"label1": "value1", "label2": "value2"}, |
| 112 | + Taints: []*models.V1Taint{{Key: "taint1", Value: "value1", Effect: "NoSchedule"}}, |
| 113 | + IsControlPlane: ptr.BoolPtr(true), |
| 114 | + UseControlPlaneAsWorker: true, |
| 115 | + Name: "machine-pool-1", |
| 116 | + Size: int32(3), |
| 117 | + UpdateStrategy: &models.V1UpdateStrategy{Type: "RollingUpdate"}, |
| 118 | + InstanceType: types.Ptr("n1-standard-4"), |
| 119 | + RootDeviceSize: int64(100), |
| 120 | + Azs: []string{"us-west1-a", "us-west1-b"}, |
| 121 | + NodeRepaveInterval: 0, |
| 122 | + }, |
| 123 | + }, |
| 124 | + expectedOutput: []interface{}{ |
| 125 | + map[string]interface{}{ |
| 126 | + "additional_labels": map[string]string{ |
| 127 | + "label1": "value1", |
| 128 | + "label2": "value2", |
| 129 | + }, |
| 130 | + "taints": []interface{}{ |
| 131 | + map[string]interface{}{ |
| 132 | + "key": "taint1", |
| 133 | + "value": "value1", |
| 134 | + "effect": "NoSchedule", |
| 135 | + }, |
| 136 | + }, |
| 137 | + "control_plane": true, |
| 138 | + "control_plane_as_worker": true, |
| 139 | + "name": "machine-pool-1", |
| 140 | + "count": 3, |
| 141 | + "update_strategy": "RollingUpdate", |
| 142 | + "instance_type": "n1-standard-4", |
| 143 | + "disk_size_gb": 100, |
| 144 | + "azs": []string{"us-west1-a", "us-west1-b"}, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + output := flattenMachinePoolConfigsGcp(tt.input) |
| 153 | + assert.Equal(t, tt.expectedOutput, output) |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestFlattenClusterConfigsGcp(t *testing.T) { |
| 159 | + tests := []struct { |
| 160 | + name string |
| 161 | + input *models.V1GcpCloudConfig |
| 162 | + expectedOutput []interface{} |
| 163 | + }{ |
| 164 | + { |
| 165 | + name: "Valid Cloud Config", |
| 166 | + input: &models.V1GcpCloudConfig{ |
| 167 | + Spec: &models.V1GcpCloudConfigSpec{ |
| 168 | + ClusterConfig: &models.V1GcpClusterConfig{ |
| 169 | + Project: ptr.StringPtr("my-project"), |
| 170 | + Network: "my-network", |
| 171 | + Region: ptr.StringPtr("us-west1"), |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + expectedOutput: []interface{}{ |
| 176 | + map[string]interface{}{ |
| 177 | + "project": ptr.StringPtr("my-project"), |
| 178 | + "network": "my-network", |
| 179 | + "region": "us-west1", |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "Nil Cloud Config", |
| 185 | + input: nil, |
| 186 | + expectedOutput: []interface{}{}, |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "Empty Cluster Config", |
| 190 | + input: &models.V1GcpCloudConfig{}, |
| 191 | + expectedOutput: []interface{}{}, |
| 192 | + }, |
| 193 | + { |
| 194 | + name: "Empty Cluster Config Spec", |
| 195 | + input: &models.V1GcpCloudConfig{Spec: &models.V1GcpCloudConfigSpec{}}, |
| 196 | + expectedOutput: []interface{}{}, |
| 197 | + }, |
| 198 | + { |
| 199 | + name: "Missing Fields in Cluster Config", |
| 200 | + input: &models.V1GcpCloudConfig{ |
| 201 | + Spec: &models.V1GcpCloudConfigSpec{ |
| 202 | + ClusterConfig: &models.V1GcpClusterConfig{}, |
| 203 | + }, |
| 204 | + }, |
| 205 | + expectedOutput: []interface{}{ |
| 206 | + map[string]interface{}{}, |
| 207 | + }, |
| 208 | + }, |
| 209 | + } |
| 210 | + |
| 211 | + for _, tt := range tests { |
| 212 | + t.Run(tt.name, func(t *testing.T) { |
| 213 | + output := flattenClusterConfigsGcp(tt.input) |
| 214 | + assert.Equal(t, tt.expectedOutput, output) |
| 215 | + }) |
| 216 | + } |
| 217 | +} |
0 commit comments