|
4 | 4 | package resource |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "context" |
8 | 7 | "fmt" |
9 | 8 | "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/datasource" |
10 | 9 | "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 10 | + "regexp" |
11 | 11 | "testing" |
12 | 12 |
|
13 | 13 | "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
@@ -46,6 +46,126 @@ func TestTest_TestStep_ImportBlockId(t *testing.T) { |
46 | 46 | }, |
47 | 47 | ), |
48 | 48 | }, |
| 49 | + ReadResponse: &resource.ReadResponse{ |
| 50 | + NewState: tftypes.NewValue( |
| 51 | + tftypes.Object{ |
| 52 | + AttributeTypes: map[string]tftypes.Type{ |
| 53 | + "id": tftypes.String, |
| 54 | + "location": tftypes.String, |
| 55 | + "name": tftypes.String, |
| 56 | + }, |
| 57 | + }, |
| 58 | + map[string]tftypes.Value{ |
| 59 | + "id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), |
| 60 | + "location": tftypes.NewValue(tftypes.String, "westeurope"), |
| 61 | + "name": tftypes.NewValue(tftypes.String, "somevalue"), |
| 62 | + }, |
| 63 | + ), |
| 64 | + }, |
| 65 | + ImportStateResponse: &resource.ImportStateResponse{ |
| 66 | + State: tftypes.NewValue( |
| 67 | + tftypes.Object{ |
| 68 | + AttributeTypes: map[string]tftypes.Type{ |
| 69 | + "id": tftypes.String, |
| 70 | + "location": tftypes.String, |
| 71 | + "name": tftypes.String, |
| 72 | + }, |
| 73 | + }, |
| 74 | + map[string]tftypes.Value{ |
| 75 | + "id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), |
| 76 | + "location": tftypes.NewValue(tftypes.String, "westeurope"), |
| 77 | + "name": tftypes.NewValue(tftypes.String, "somevalue"), |
| 78 | + }, |
| 79 | + ), |
| 80 | + }, |
| 81 | + SchemaResponse: &resource.SchemaResponse{ |
| 82 | + Schema: &tfprotov6.Schema{ |
| 83 | + Block: &tfprotov6.SchemaBlock{ |
| 84 | + Attributes: []*tfprotov6.SchemaAttribute{ |
| 85 | + { |
| 86 | + Name: "id", |
| 87 | + Type: tftypes.String, |
| 88 | + Computed: true, |
| 89 | + }, |
| 90 | + { |
| 91 | + Name: "location", |
| 92 | + Type: tftypes.String, |
| 93 | + Required: true, |
| 94 | + }, |
| 95 | + { |
| 96 | + Name: "name", |
| 97 | + Type: tftypes.String, |
| 98 | + Required: true, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }), |
| 107 | + }, |
| 108 | + Steps: []TestStep{ |
| 109 | + { |
| 110 | + Config: ` |
| 111 | + resource "examplecloud_container" "test" { |
| 112 | + location = "westeurope" |
| 113 | + name = "somevalue" |
| 114 | + }`, |
| 115 | + }, |
| 116 | + { |
| 117 | + ResourceName: "examplecloud_container.test", |
| 118 | + ImportState: true, |
| 119 | + ImportStateKind: ImportBlockWithId, |
| 120 | + ImportStateVerify: true, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +func TestTest_TestStep_ImportBlockId_ExpectError(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + UnitTest(t, TestCase{ |
| 130 | + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ |
| 131 | + tfversion.SkipBelow(tfversion.Version1_5_0), // ProtoV6ProviderFactories |
| 132 | + }, |
| 133 | + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ |
| 134 | + "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ |
| 135 | + Resources: map[string]testprovider.Resource{ |
| 136 | + "examplecloud_container": { |
| 137 | + CreateResponse: &resource.CreateResponse{ |
| 138 | + NewState: tftypes.NewValue( |
| 139 | + tftypes.Object{ |
| 140 | + AttributeTypes: map[string]tftypes.Type{ |
| 141 | + "id": tftypes.String, |
| 142 | + "location": tftypes.String, |
| 143 | + "name": tftypes.String, |
| 144 | + }, |
| 145 | + }, |
| 146 | + map[string]tftypes.Value{ |
| 147 | + "id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), |
| 148 | + "location": tftypes.NewValue(tftypes.String, "westeurope"), |
| 149 | + "name": tftypes.NewValue(tftypes.String, "somevalue"), |
| 150 | + }, |
| 151 | + ), |
| 152 | + }, |
| 153 | + ReadResponse: &resource.ReadResponse{ |
| 154 | + NewState: tftypes.NewValue( |
| 155 | + tftypes.Object{ |
| 156 | + AttributeTypes: map[string]tftypes.Type{ |
| 157 | + "id": tftypes.String, |
| 158 | + "location": tftypes.String, |
| 159 | + "name": tftypes.String, |
| 160 | + }, |
| 161 | + }, |
| 162 | + map[string]tftypes.Value{ |
| 163 | + "id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), |
| 164 | + "location": tftypes.NewValue(tftypes.String, "westeurope"), |
| 165 | + "name": tftypes.NewValue(tftypes.String, "somevalue"), |
| 166 | + }, |
| 167 | + ), |
| 168 | + }, |
49 | 169 | ImportStateResponse: &resource.ImportStateResponse{ |
50 | 170 | State: tftypes.NewValue( |
51 | 171 | tftypes.Object{ |
@@ -98,10 +218,16 @@ func TestTest_TestStep_ImportBlockId(t *testing.T) { |
98 | 218 | }`, |
99 | 219 | }, |
100 | 220 | { |
| 221 | + Config: ` |
| 222 | + resource "examplecloud_container" "test" { |
| 223 | + location = "eastus" |
| 224 | + name = "somevalue" |
| 225 | + }`, |
101 | 226 | ResourceName: "examplecloud_container.test", |
102 | 227 | ImportState: true, |
103 | 228 | ImportStateKind: ImportBlockWithId, |
104 | 229 | ImportStateVerify: true, |
| 230 | + ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test should be a no-op, but got action update with plan(.?)`), |
105 | 231 | }, |
106 | 232 | }, |
107 | 233 | }) |
@@ -211,6 +337,8 @@ func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) { |
211 | 337 | }) |
212 | 338 | } |
213 | 339 |
|
| 340 | +// These tests currently pass but only because the `getState` function which is used on the imported resource |
| 341 | +// to do the state comparison doesn't return an error if there is no state in the working directory |
214 | 342 | func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *testing.T) { |
215 | 343 | /* |
216 | 344 | This test tries to imitate a real world example of behaviour we often see in the AzureRM provider which requires |
@@ -283,33 +411,6 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes |
283 | 411 | }, |
284 | 412 | ), |
285 | 413 | }, |
286 | | - PlanChangeFunc: func(ctx context.Context, request resource.PlanChangeRequest, response *resource.PlanChangeResponse) { |
287 | | - /* |
288 | | - Returning a nil for another attribute to simulate a situation where `ImportStateVerifyIgnore` |
289 | | - should be used results in the error below from Terraform |
290 | | -
|
291 | | - Error: Provider returned invalid result object after apply |
292 | | -
|
293 | | - After the apply operation, the provider still indicated an unknown value for |
294 | | - examplecloud_container.test.id. All values must be known after apply, so this |
295 | | - is always a bug in the provider and should be reported in the provider's own |
296 | | - repository. Terraform will still save the other known object values in the |
297 | | - state. |
298 | | -
|
299 | | - Modifying the plan to set the id to a known value appears to be the only way to |
300 | | - circumvent this behaviour, the cause of which I don't fully understand. |
301 | | -
|
302 | | - This doesn't seem great, because this gets applied to all Plans that happen in this |
303 | | - test - so we're modifying plans in steps that we might not want to. |
304 | | - */ |
305 | | - |
306 | | - objVal := map[string]tftypes.Value{} |
307 | | - |
308 | | - if !response.PlannedState.IsNull() { |
309 | | - _ = response.PlannedState.As(&objVal) |
310 | | - objVal["id"] = tftypes.NewValue(tftypes.String, "sometestid") |
311 | | - } |
312 | | - }, |
313 | 414 | SchemaResponse: &resource.SchemaResponse{ |
314 | 415 | Schema: &tfprotov6.Schema{ |
315 | 416 | Block: &tfprotov6.SchemaBlock{ |
|
0 commit comments