Skip to content

Commit

Permalink
update terragen tests to use txtar instead of filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarfors committed Apr 10, 2024
1 parent 62449b6 commit 55f7073
Show file tree
Hide file tree
Showing 14 changed files with 6,830 additions and 542 deletions.
153 changes: 0 additions & 153 deletions cmd/tools/filtersb/cli.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/internal/terrajen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ type ProviderGenerator struct {
// E.g. github.com/golingon/lingon/gen/aws
GoProviderPkgPath string
// GeneratedPackageLocation is the directory on the filesystem where the
// generated
// Go files will be created.
// generated Go files will be created.
// The GoProviderPkgPath path must match the location of the generated files
// so that they can be imported correctly.
// E.g. if we are in a Go module called "my-module" and we generate the
Expand Down
24 changes: 3 additions & 21 deletions pkg/terragen/gowrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type GenerateGoArgs struct {
// providers and their schemas.
func GenerateGoCode(
args GenerateGoArgs,
schemas *tfjson.ProviderSchemas,
providerSchema *tfjson.ProviderSchema,
) error {
if args.OutDir == "" {
return errors.New("outDir is empty")
Expand All @@ -69,25 +69,7 @@ func GenerateGoCode(
ProviderVersion: args.ProviderVersion,
}

providerSchema, ok := schemas.Schemas[args.ProviderSource]
if !ok {
// Try adding registry.terraform.io/ prefix if not already added
if !strings.HasPrefix(args.ProviderSource, "registry.terraform.io/") {
providerSchema, ok = schemas.Schemas[fmt.Sprintf(
"registry.terraform.io/%s",
args.ProviderSource,
)]
}
// If still not ok, indicate an error
if !ok {
return fmt.Errorf(
"provider source: %s: %w",
args.ProviderSource,
ErrProviderSchemaNotFound,
)
}
}
arch, err := generateProvider(providerGenerator, providerSchema)
arch, err := generateProviderTxtar(providerGenerator, providerSchema)
if err != nil {
return err
}
Expand Down Expand Up @@ -119,7 +101,7 @@ func writeTxtarArchive(ar *txtar.Archive) error {
return nil
}

func generateProvider(
func generateProviderTxtar(
provider terrajen.ProviderGenerator,
schema *tfjson.ProviderSchema,
) (*txtar.Archive, error) {
Expand Down
146 changes: 146 additions & 0 deletions pkg/terragen/gowrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,157 @@
package terragen

import (
"context"
"encoding/json"
"flag"
"os"
"path/filepath"
"slices"
"testing"

"github.com/golingon/lingon/pkg/internal/terrajen"
tu "github.com/golingon/lingon/pkg/testutil"
tfjson "github.com/hashicorp/terraform-json"
"golang.org/x/tools/txtar"
)

var update = flag.Bool("update", false, "update golden files")

type ProviderTestCase struct {
Name string

ProviderName string
ProviderSource string
ProviderVersion string

FilterResources []string
FilterDataSources []string
}

// TestGenerateProvider tests the generation of Terraform provider schemas into
// Go code.
//
// When using the -update flag, it updates the golden files which are used as a
// baseline for detecting drift in the generated code.
// It is quite challenging to verify the correctness of the generated code,
// and it is out of scope of this pkg and test.
func TestGenerateProvider(t *testing.T) {
goldenTestDir := filepath.Join("testdata", "golden")

tests := []ProviderTestCase{
{
Name: "aws_emr_cluster",
ProviderName: "aws",
ProviderSource: "hashicorp/aws",
ProviderVersion: "5.44.0",

FilterResources: []string{"aws_emr_cluster"},
FilterDataSources: []string{"aws_emr_cluster"},
},
{
Name: "aws_iam_role",
ProviderName: "aws",
ProviderSource: "hashicorp/aws",
ProviderVersion: "5.44.0",

FilterResources: []string{"aws_iam_role"},
FilterDataSources: []string{"aws_iam_role"},
},
}
if *update {
t.Log("running update")
// Generate "golden" files. Start be deleting the directory.
err := os.RemoveAll(goldenTestDir)
tu.AssertNoError(t, err, "removing golden test dir")

for _, test := range tests {
ctx := context.Background()
ps, err := GenerateProviderSchema(
ctx, Provider{
Name: test.ProviderName,
Source: test.ProviderSource,
Version: test.ProviderVersion,
},
)
tu.AssertNoError(t, err, "generating provider schema:", test.Name)

// Filter resources and data sources.
for rName := range ps.ResourceSchemas {
if !slices.Contains(test.FilterResources, rName) {
delete(ps.ResourceSchemas, rName)
}
}
for dName := range ps.DataSourceSchemas {
if !slices.Contains(test.FilterDataSources, dName) {
delete(ps.DataSourceSchemas, dName)
}
}

providerGenerator := terrajen.ProviderGenerator{
GoProviderPkgPath: "test/out",
GeneratedPackageLocation: "out",
ProviderName: test.ProviderName,
ProviderSource: test.ProviderSource,
ProviderVersion: test.ProviderVersion,
}

ar, err := generateProviderTxtar(providerGenerator, ps)
tu.AssertNoError(t, err, "generating provider txtar")

testDir := filepath.Join(goldenTestDir, test.Name)
err = os.MkdirAll(testDir, os.ModePerm)
tu.AssertNoError(t, err, "creating golden test dir: ", testDir)

schemaPath := filepath.Join(testDir, "schema.json")
schemaFile, err := os.Create(schemaPath)
tu.AssertNoError(t, err, "creating schema file")
err = json.NewEncoder(schemaFile).Encode(ps)
tu.AssertNoError(t, err, "writing schema file")

txtarPath := filepath.Join(testDir, "provider.txtar")
err = os.WriteFile(txtarPath, txtar.Format(ar), 0o644)
tu.AssertNoError(t, err, "writing txtar file")
}

t.SkipNow()
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
schemaPath := filepath.Join(goldenTestDir, test.Name, "schema.json")
schemaFile, err := os.Open(schemaPath)
tu.AssertNoError(t, err, "opening schema file")
var ps tfjson.ProviderSchema
err = json.NewDecoder(schemaFile).Decode(&ps)
tu.AssertNoError(t, err, "decoding schema file")

txtarPath := filepath.Join(
goldenTestDir,
test.Name,
"provider.txtar",
)
txtarContents, err := os.ReadFile(txtarPath)
tu.AssertNoError(t, err, "opening txtar file")
expectedAr := txtar.Parse(txtarContents)

providerGenerator := terrajen.ProviderGenerator{
GoProviderPkgPath: "test/out",
GeneratedPackageLocation: "out",
ProviderName: "aws",
ProviderSource: "hashicorp/aws",
ProviderVersion: "5.44.0",
}

actualAr, err := generateProviderTxtar(providerGenerator, &ps)
tu.AssertNoError(t, err, "generating provider txtar")

if diff := tu.DiffTxtar(actualAr, expectedAr); diff != "" {
t.Fatal(tu.Callers(), diff)
}
})
}
}

func TestParseProvider(t *testing.T) {
type test struct {
providerStr string
Expand Down
1 change: 0 additions & 1 deletion pkg/terragen/testdata/.gitignore

This file was deleted.

134 changes: 0 additions & 134 deletions pkg/terragen/testdata/aws_emr_cluster.txtar

This file was deleted.

93 changes: 0 additions & 93 deletions pkg/terragen/testdata/aws_iam_roles.txtar

This file was deleted.

Loading

0 comments on commit 55f7073

Please sign in to comment.