|
3 | 3 | package compute
|
4 | 4 |
|
5 | 5 | import (
|
| 6 | + "reflect" |
6 | 7 | "testing"
|
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 10 | + |
| 11 | + "google.golang.org/api/compute/v1" |
7 | 12 | )
|
8 | 13 |
|
9 | 14 | func TestInstanceGroupManager_parseUniqueId(t *testing.T) {
|
@@ -78,3 +83,234 @@ func TestInstanceGroupManager_convertUniqueId(t *testing.T) {
|
78 | 83 | }
|
79 | 84 | }
|
80 | 85 | }
|
| 86 | + |
| 87 | +func TestFlattenStatefulPolicyStatefulIps(t *testing.T) { |
| 88 | + cases := map[string]struct { |
| 89 | + ConfigValues []interface{} |
| 90 | + Ips map[string]compute.StatefulPolicyPreservedStateNetworkIp |
| 91 | + Expected []map[string]interface{} |
| 92 | + }{ |
| 93 | + "No IPs in config nor API data": { |
| 94 | + ConfigValues: []interface{}{}, |
| 95 | + Ips: map[string]compute.StatefulPolicyPreservedStateNetworkIp{}, |
| 96 | + Expected: []map[string]interface{}{}, |
| 97 | + }, |
| 98 | + "Single IP (nic0) in config and API data": { |
| 99 | + ConfigValues: []interface{}{ |
| 100 | + map[string]interface{}{ |
| 101 | + "interface_name": "nic0", |
| 102 | + "delete_rule": "NEVER", |
| 103 | + }, |
| 104 | + }, |
| 105 | + Ips: map[string]compute.StatefulPolicyPreservedStateNetworkIp{ |
| 106 | + "nic0": { |
| 107 | + AutoDelete: "NEVER", |
| 108 | + }, |
| 109 | + }, |
| 110 | + Expected: []map[string]interface{}{ |
| 111 | + { |
| 112 | + "interface_name": "nic0", |
| 113 | + "delete_rule": "NEVER", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + "Two IPs (nic0, nic1). Unordered in config and sorted in API data": { |
| 118 | + ConfigValues: []interface{}{ |
| 119 | + map[string]interface{}{ |
| 120 | + "interface_name": "nic1", |
| 121 | + "delete_rule": "NEVER", |
| 122 | + }, |
| 123 | + map[string]interface{}{ |
| 124 | + "interface_name": "nic0", |
| 125 | + "delete_rule": "NEVER", |
| 126 | + }, |
| 127 | + }, |
| 128 | + Ips: map[string]compute.StatefulPolicyPreservedStateNetworkIp{ |
| 129 | + "nic0": { |
| 130 | + AutoDelete: "NEVER", |
| 131 | + }, |
| 132 | + "nic1": { |
| 133 | + AutoDelete: "NEVER", |
| 134 | + }, |
| 135 | + }, |
| 136 | + Expected: []map[string]interface{}{ |
| 137 | + { |
| 138 | + "interface_name": "nic1", |
| 139 | + "delete_rule": "NEVER", |
| 140 | + }, |
| 141 | + { |
| 142 | + "interface_name": "nic0", |
| 143 | + "delete_rule": "NEVER", |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + "Two IPs (nic0, nic1). Only nic0 in config and both stored in API data": { |
| 148 | + ConfigValues: []interface{}{ |
| 149 | + map[string]interface{}{ |
| 150 | + "interface_name": "nic0", |
| 151 | + "delete_rule": "NEVER", |
| 152 | + }, |
| 153 | + }, |
| 154 | + Ips: map[string]compute.StatefulPolicyPreservedStateNetworkIp{ |
| 155 | + "nic0": { |
| 156 | + AutoDelete: "NEVER", |
| 157 | + }, |
| 158 | + "nic1": { |
| 159 | + AutoDelete: "NEVER", |
| 160 | + }, |
| 161 | + }, |
| 162 | + Expected: []map[string]interface{}{ |
| 163 | + { |
| 164 | + "interface_name": "nic0", |
| 165 | + "delete_rule": "NEVER", |
| 166 | + }, |
| 167 | + { |
| 168 | + "interface_name": "nic1", |
| 169 | + "delete_rule": "NEVER", |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + "Five IPs (nic0 - nic4). None stored in config and all stored in API data": { |
| 174 | + ConfigValues: []interface{}{}, |
| 175 | + Ips: map[string]compute.StatefulPolicyPreservedStateNetworkIp{ |
| 176 | + // Out of order here to encourage randomness |
| 177 | + "nic3": { |
| 178 | + AutoDelete: "NEVER", |
| 179 | + }, |
| 180 | + "nic0": { |
| 181 | + AutoDelete: "NEVER", |
| 182 | + }, |
| 183 | + "nic1": { |
| 184 | + AutoDelete: "NEVER", |
| 185 | + }, |
| 186 | + "nic4": { |
| 187 | + AutoDelete: "NEVER", |
| 188 | + }, |
| 189 | + "nic2": { |
| 190 | + AutoDelete: "NEVER", |
| 191 | + }, |
| 192 | + }, |
| 193 | + Expected: []map[string]interface{}{ |
| 194 | + { |
| 195 | + "interface_name": "nic0", |
| 196 | + "delete_rule": "NEVER", |
| 197 | + }, |
| 198 | + { |
| 199 | + "interface_name": "nic1", |
| 200 | + "delete_rule": "NEVER", |
| 201 | + }, |
| 202 | + { |
| 203 | + "interface_name": "nic2", |
| 204 | + "delete_rule": "NEVER", |
| 205 | + }, |
| 206 | + { |
| 207 | + "interface_name": "nic3", |
| 208 | + "delete_rule": "NEVER", |
| 209 | + }, |
| 210 | + { |
| 211 | + "interface_name": "nic4", |
| 212 | + "delete_rule": "NEVER", |
| 213 | + }, |
| 214 | + }, |
| 215 | + }, |
| 216 | + "Three IPs (nic0, nic1, nic2). Only nic1, nic2 in config and all 3 stored in API data": { |
| 217 | + ConfigValues: []interface{}{ |
| 218 | + map[string]interface{}{ |
| 219 | + "interface_name": "nic1", |
| 220 | + "delete_rule": "NEVER", |
| 221 | + }, |
| 222 | + map[string]interface{}{ |
| 223 | + "interface_name": "nic2", |
| 224 | + "delete_rule": "NEVER", |
| 225 | + }, |
| 226 | + }, |
| 227 | + Ips: map[string]compute.StatefulPolicyPreservedStateNetworkIp{ |
| 228 | + "nic0": { |
| 229 | + AutoDelete: "NEVER", |
| 230 | + }, |
| 231 | + "nic1": { |
| 232 | + AutoDelete: "NEVER", |
| 233 | + }, |
| 234 | + "nic2": { |
| 235 | + AutoDelete: "NEVER", |
| 236 | + }, |
| 237 | + }, |
| 238 | + Expected: []map[string]interface{}{ |
| 239 | + { |
| 240 | + "interface_name": "nic1", |
| 241 | + "delete_rule": "NEVER", |
| 242 | + }, |
| 243 | + { |
| 244 | + "interface_name": "nic2", |
| 245 | + "delete_rule": "NEVER", |
| 246 | + }, |
| 247 | + { |
| 248 | + "interface_name": "nic0", |
| 249 | + "delete_rule": "NEVER", |
| 250 | + }, |
| 251 | + }, |
| 252 | + }, |
| 253 | + "Three IPs (nic0, nic1, nic2). Only nic0, nic2 in config and only nic1, nic2 stored in API data": { |
| 254 | + ConfigValues: []interface{}{ |
| 255 | + map[string]interface{}{ |
| 256 | + "interface_name": "nic2", |
| 257 | + "delete_rule": "NEVER", |
| 258 | + }, |
| 259 | + map[string]interface{}{ |
| 260 | + "interface_name": "nic0", |
| 261 | + "delete_rule": "NEVER", |
| 262 | + }, |
| 263 | + }, |
| 264 | + Ips: map[string]compute.StatefulPolicyPreservedStateNetworkIp{ |
| 265 | + "nic1": { |
| 266 | + AutoDelete: "NEVER", |
| 267 | + }, |
| 268 | + "nic2": { |
| 269 | + AutoDelete: "NEVER", |
| 270 | + }, |
| 271 | + }, |
| 272 | + Expected: []map[string]interface{}{ |
| 273 | + { |
| 274 | + "interface_name": "nic2", |
| 275 | + "delete_rule": "NEVER", |
| 276 | + }, |
| 277 | + { |
| 278 | + "interface_name": "nic1", |
| 279 | + "delete_rule": "NEVER", |
| 280 | + }, |
| 281 | + }, |
| 282 | + }, |
| 283 | + } |
| 284 | + |
| 285 | + for tn, tc := range cases { |
| 286 | + t.Run(tn, func(t *testing.T) { |
| 287 | + |
| 288 | + // Terraform config |
| 289 | + schema := ResourceComputeRegionInstanceGroupManager().Schema |
| 290 | + config := map[string]interface{}{ |
| 291 | + "stateful_external_ip": tc.ConfigValues, |
| 292 | + "stateful_internal_ip": tc.ConfigValues, |
| 293 | + } |
| 294 | + d := tpgresource.SetupTestResourceDataFromConfigMap(t, schema, config) |
| 295 | + |
| 296 | + // API response |
| 297 | + statefulPolicyPreservedState := compute.StatefulPolicyPreservedState{ |
| 298 | + ExternalIPs: tc.Ips, |
| 299 | + InternalIPs: tc.Ips, |
| 300 | + } |
| 301 | + statefulPolicy := compute.StatefulPolicy{ |
| 302 | + PreservedState: &statefulPolicyPreservedState, |
| 303 | + } |
| 304 | + |
| 305 | + outputExternal := flattenStatefulPolicyStatefulExternalIps(d, &statefulPolicy) |
| 306 | + if !reflect.DeepEqual(tc.Expected, outputExternal) { |
| 307 | + t.Fatalf("expected external IPs output to be %#v, but got %#v", tc.Expected, outputExternal) |
| 308 | + } |
| 309 | + |
| 310 | + outputInternal := flattenStatefulPolicyStatefulInternalIps(d, &statefulPolicy) |
| 311 | + if !reflect.DeepEqual(tc.Expected, outputInternal) { |
| 312 | + t.Fatalf("expected internal IPs output to be %#v, but got %#v", tc.Expected, outputInternal) |
| 313 | + } |
| 314 | + }) |
| 315 | + } |
| 316 | +} |
0 commit comments