From bfc1f5618e24c80209aa30837558e69ae6bf7777 Mon Sep 17 00:00:00 2001 From: Elie Date: Thu, 25 Nov 2021 14:57:31 +0100 Subject: [PATCH] Fix hacky way to serve fake tf provider schema in tests --- docs/new-remote-provider.md | 2 +- test/schemas/shemas.go | 9 ++++++--- test/terraform/fake_terraform_provider.go | 8 ++------ test/{schemas => terraform}/schemas_test.go | 12 ++++++------ 4 files changed, 15 insertions(+), 16 deletions(-) rename test/{schemas => terraform}/schemas_test.go (72%) diff --git a/docs/new-remote-provider.md b/docs/new-remote-provider.md index 3e1d7281..7bf68d3e 100644 --- a/docs/new-remote-provider.md +++ b/docs/new-remote-provider.md @@ -179,6 +179,6 @@ Then, add a call to this function in the `remote//init.go` file you cr You also need to create a test schema for upcoming tests. -Please use `TestCreateNewSchema` located in `test/schemas/schemas_test.go` to generate a schema file that will be used for the mocked provider. +Please use `TestCreateNewSchema` located in `test/terraform/schemas_test.go` to generate a schema file that will be used for the mocked provider. Everything is now ready, you should [start adding new resources](new-resource.md)! diff --git a/test/schemas/shemas.go b/test/schemas/shemas.go index 7c4cf956..5f16bccb 100644 --- a/test/schemas/shemas.go +++ b/test/schemas/shemas.go @@ -1,6 +1,7 @@ package schemas import ( + "embed" gojson "encoding/json" "io/ioutil" "os" @@ -11,7 +12,10 @@ import ( "github.com/hashicorp/terraform/providers" ) -func writeTestSchema(schema map[string]providers.Schema, provider, version string) error { +//go:embed */*/schema.json +var fakeSchemaFS embed.FS + +func WriteTestSchema(schema map[string]providers.Schema, provider, version string) error { _, relativeFilePath, _, _ := runtime.Caller(0) fileName := path.Join(path.Dir(relativeFilePath), provider, version, "schema.json") content, _ := gojson.Marshal(schema) @@ -27,8 +31,7 @@ func writeTestSchema(schema map[string]providers.Schema, provider, version strin } func ReadTestSchema(provider, version string) (map[string]providers.Schema, error) { - _, filename, _, _ := runtime.Caller(0) - content, err := ioutil.ReadFile(path.Join(path.Dir(filename), provider, version, "schema.json")) + content, err := fakeSchemaFS.ReadFile(path.Join(provider, version, "schema.json")) if err != nil { return nil, err } diff --git a/test/terraform/fake_terraform_provider.go b/test/terraform/fake_terraform_provider.go index 004ed637..2d0709c1 100644 --- a/test/terraform/fake_terraform_provider.go +++ b/test/terraform/fake_terraform_provider.go @@ -6,9 +6,9 @@ import ( "sort" "github.com/cloudskiff/driftctl/pkg/terraform" - "github.com/cloudskiff/driftctl/test" "github.com/cloudskiff/driftctl/test/goldenfile" "github.com/cloudskiff/driftctl/test/mocks" + "github.com/cloudskiff/driftctl/test/schemas" "github.com/hashicorp/terraform/providers" "github.com/pkg/errors" "github.com/zclconf/go-cty/cty" @@ -51,14 +51,10 @@ func (p *FakeTerraformProvider) ReadResource(args terraform.ReadResourceArgs) (* } func (p *FakeTerraformProvider) readSchema() map[string]providers.Schema { - content, err := test.ReadTestFile(fmt.Sprintf("../schemas/%s/%s/schema.json", p.realProvider.Name(), p.realProvider.Version())) + schema, err := schemas.ReadTestSchema(p.realProvider.Name(), p.realProvider.Version()) if err != nil { panic(err) } - var schema map[string]providers.Schema - if err := gojson.Unmarshal(content, &schema); err != nil { - panic(err) - } return schema } diff --git a/test/schemas/schemas_test.go b/test/terraform/schemas_test.go similarity index 72% rename from test/schemas/schemas_test.go rename to test/terraform/schemas_test.go index 555bb976..f726bf71 100644 --- a/test/schemas/schemas_test.go +++ b/test/terraform/schemas_test.go @@ -1,19 +1,19 @@ -package schemas +package terraform import ( "os" "testing" "github.com/cloudskiff/driftctl/pkg/terraform" - terraformtest "github.com/cloudskiff/driftctl/test/terraform" + "github.com/cloudskiff/driftctl/test/schemas" ) // You can use this test function to create a schema file for a given provider in a given version // You may want to update part of this code to change provider and version to generate desired schema // To use this test you should run this command from the repository root -// DCTL_UPDATE_TEST_SCHEMA=true go test ./test/schemas +// DCTL_UPDATE_TEST_SCHEMA=true go test ./test/terraform // You may need to setup proper environment variable to make the terraform provider work -// DCTL_UPDATE_TEST_SCHEMA=true AWS_PROFILE=myprofile go test ./test/schemas +// DCTL_UPDATE_TEST_SCHEMA=true AWS_PROFILE=myprofile go test ./test/terraform func TestCreateNewSchema(t *testing.T) { if os.Getenv("DCTL_UPDATE_TEST_SCHEMA") != "true" { @@ -23,13 +23,13 @@ func TestCreateNewSchema(t *testing.T) { providerLibrary := terraform.NewProviderLibrary() // Replace this with provider you want to create schema - realProvider, _ := terraformtest.InitTestAwsProvider(providerLibrary, "3.19.0") + realProvider, _ := InitTestAwsProvider(providerLibrary, "3.19.0") err := realProvider.Init() if err != nil { t.Fatal(err) } - err = writeTestSchema(realProvider.Schema(), realProvider.Name(), realProvider.Version()) + err = schemas.WriteTestSchema(realProvider.Schema(), realProvider.Name(), realProvider.Version()) if err != nil { t.Fatal(err) }