Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schema creation helper #910

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions test/schemas/schemas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package schemas

import (
"os"
"testing"

"github.com/cloudskiff/driftctl/pkg/terraform"
terraformtest "github.com/cloudskiff/driftctl/test/terraform"
)

// 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
// 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
func TestCreateNewSchema(t *testing.T) {

if os.Getenv("DCTL_UPDATE_TEST_SCHEMA") != "true" {
t.SkipNow()
}

providerLibrary := terraform.NewProviderLibrary()

// Replace this with provider you want to create schema
realProvider, _ := terraformtest.InitTestAwsProvider(providerLibrary, "3.19.0")

err := realProvider.Init()
if err != nil {
t.Fatal(err)
}
err = writeTestSchema(realProvider.Schema(), realProvider.Name(), realProvider.Version())
if err != nil {
t.Fatal(err)
}
}
17 changes: 17 additions & 0 deletions test/schemas/shemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@ package schemas
import (
gojson "encoding/json"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"

"github.com/hashicorp/terraform/providers"
)

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)
err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
if err != nil {
return err
}
err = ioutil.WriteFile(fileName, content, os.ModePerm)
if err != nil {
return err
}
return nil
}

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"))
Expand Down
18 changes: 1 addition & 17 deletions test/terraform/fake_terraform_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ func (p *FakeTerraformProvider) ShouldUpdate() {
}

func (p *FakeTerraformProvider) Schema() map[string]providers.Schema {
if p.shouldUpdate {
schema := p.realProvider.Schema()
p.writeSchema(schema)
return schema
}
return p.readSchema()
}

Expand All @@ -55,19 +50,8 @@ func (p *FakeTerraformProvider) ReadResource(args terraform.ReadResourceArgs) (*
return p.readResource(args)
}

func (p *FakeTerraformProvider) writeSchema(schema map[string]providers.Schema) {
marshal, err := gojson.Marshal(schema)
if err != nil {
panic(err)
}
err = test.WriteTestFile(fmt.Sprintf("schemas/%s/%s.json", p.realProvider.Name(), p.realProvider.Version()), marshal)
if err != nil {
panic(err)
}
}

func (p *FakeTerraformProvider) readSchema() map[string]providers.Schema {
content, err := test.ReadTestFile(fmt.Sprintf("schemas/%s/%s.json", p.realProvider.Name(), p.realProvider.Version()))
content, err := test.ReadTestFile(fmt.Sprintf("../schemas/%s/%s/schema.json", p.realProvider.Name(), p.realProvider.Version()))
if err != nil {
panic(err)
}
Expand Down