/
testing_new_import_state_test.go
82 lines (75 loc) · 2.1 KB
/
testing_new_import_state_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package resource
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestTest_TestStep_ImportStateCheck_SkipDataSourceState(t *testing.T) {
t.Parallel()
UnitTest(t, TestCase{
ProviderFactories: map[string]func() (*schema.Provider, error){
"examplecloud": func() (*schema.Provider, error) { //nolint:unparam // required signature
return &schema.Provider{
DataSourcesMap: map[string]*schema.Resource{
"examplecloud_thing": {
ReadContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
d.SetId("datasource-test")
return nil
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
ResourcesMap: map[string]*schema.Resource{
"examplecloud_thing": {
CreateContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
d.SetId("resource-test")
return nil
},
DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
},
ReadContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
},
Schema: map[string]*schema.Schema{
"id": {
Computed: true,
Type: schema.TypeString,
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
},
},
}, nil
},
},
Steps: []TestStep{
{
Config: `
data "examplecloud_thing" "test" {}
resource "examplecloud_thing" "test" {}
`,
},
{
ResourceName: "examplecloud_thing.test",
ImportState: true,
ImportStateCheck: func(is []*terraform.InstanceState) error {
if len(is) > 1 {
return fmt.Errorf("expected 1 state, got: %d", len(is))
}
return nil
},
},
},
})
}