|
| 1 | +package importstate_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/tfversion" |
| 13 | + |
| 14 | + r "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_TestStep_Import_AsFirstStep(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + r.UnitTest(t, r.TestCase{ |
| 21 | + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ |
| 22 | + tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithId requires Terraform 1.5.0 or later |
| 23 | + }, |
| 24 | + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ |
| 25 | + "examplecloud": providerserver.NewProviderServer(testprovider.Provider{ |
| 26 | + Resources: map[string]testprovider.Resource{ |
| 27 | + "examplecloud_container": examplecloudResource(), |
| 28 | + }, |
| 29 | + }), |
| 30 | + }, |
| 31 | + Steps: []r.TestStep{ |
| 32 | + { |
| 33 | + ResourceName: "examplecloud_container.test", |
| 34 | + ImportStateId: "examplecloud_container.test", |
| 35 | + ImportState: true, |
| 36 | + ImportStateKind: r.ImportCommandWithId, |
| 37 | + // ImportStateVerify: true, |
| 38 | + Config: `resource "examplecloud_container" "test" { |
| 39 | + name = "somevalue" |
| 40 | + location = "westeurope" |
| 41 | + }`, |
| 42 | + ImportStatePersist: true, |
| 43 | + ImportStateCheck: func(states []*terraform.InstanceState) error { |
| 44 | + if len(states) != 1 { |
| 45 | + return fmt.Errorf("expected 1 state; got %d", len(states)) |
| 46 | + } |
| 47 | + if states[0].ID != "westeurope/somevalue" { |
| 48 | + return fmt.Errorf("unexpected ID: %s", states[0].ID) |
| 49 | + } |
| 50 | + if states[0].Attributes["name"] != "somevalue" { |
| 51 | + return fmt.Errorf("unexpected name: %s", states[0].Attributes["name"]) |
| 52 | + } |
| 53 | + if states[0].Attributes["location"] != "westeurope" { |
| 54 | + return fmt.Errorf("unexpected location: %s", states[0].Attributes["location"]) |
| 55 | + } |
| 56 | + return nil |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + RefreshState: true, |
| 61 | + Check: r.TestCheckResourceAttr("examplecloud_container.test", "name", "somevalue"), |
| 62 | + }, |
| 63 | + }, |
| 64 | + }) |
| 65 | +} |
0 commit comments