Fix hacky way to serve fake tf provider schema in tests

main
Elie 2021-11-25 14:57:31 +01:00
parent b02f9e09a9
commit bfc1f5618e
No known key found for this signature in database
GPG Key ID: 399AF69092C727B6
4 changed files with 15 additions and 16 deletions

View File

@ -179,6 +179,6 @@ Then, add a call to this function in the `remote/<provider>/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)!

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}