Skip to content

Commit

Permalink
Terraform integration tests (#2162)
Browse files Browse the repository at this point in the history
This defines a easy to use interface for writing terraform providers and
integration tests in a concise way.

It doesn't have automation API, so not as nice as the pulumi ones but I
think this is useful for checking certain TF/opentofu behaviours.

I've moved/adapted the tf driver from the cross-tests folder into a
separate module.

I used this to explore the TF behaviour around
#2246
  • Loading branch information
VenelinMartinov authored Jul 25, 2024
1 parent 59fe4bf commit a61fbc4
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 178 deletions.
28 changes: 0 additions & 28 deletions pkg/tests/cross-tests/ci.go

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/tests/cross-tests/cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
)

func TestUnchangedBasicObject(t *testing.T) {
skipUnlessLinux(t)
cfg := map[string]any{"f0": []any{map[string]any{"x": "ok"}}}
runDiffCheck(t, diffTestCase{
Resource: &schema.Resource{
Expand All @@ -53,7 +52,6 @@ func TestUnchangedBasicObject(t *testing.T) {
}

func TestSimpleStringNoChange(t *testing.T) {
skipUnlessLinux(t)
config := map[string]any{"name": "A"}
runDiffCheck(t, diffTestCase{
Resource: &schema.Resource{
Expand All @@ -70,7 +68,6 @@ func TestSimpleStringNoChange(t *testing.T) {
}

func TestSimpleStringRename(t *testing.T) {
skipUnlessLinux(t)
runDiffCheck(t, diffTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -90,7 +87,6 @@ func TestSimpleStringRename(t *testing.T) {
}

func TestSetReordering(t *testing.T) {
skipUnlessLinux(t)
resource := &schema.Resource{
Schema: map[string]*schema.Schema{
"set": {
Expand Down Expand Up @@ -121,7 +117,6 @@ func TestSetReordering(t *testing.T) {
// │ 1: resource "crossprovider_testres" "example" {
func TestEmptyRequiredList(t *testing.T) {
t.Skip("TODO - fix panic and make a negative test here")
skipUnlessLinux(t)
resource := &schema.Resource{
Schema: map[string]*schema.Schema{
"f0": {
Expand All @@ -146,7 +141,6 @@ func TestEmptyRequiredList(t *testing.T) {
}

func TestAws2442(t *testing.T) {
skipUnlessLinux(t)
hashes := map[int]string{}

stringHashcode := func(s string) int {
Expand Down Expand Up @@ -404,7 +398,6 @@ func TestAws2442(t *testing.T) {
}

func TestSimpleOptionalComputed(t *testing.T) {
skipUnlessLinux(t)
emptyConfig := tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{})
nonEmptyConfig := tftypes.NewValue(
tftypes.Object{
Expand Down Expand Up @@ -451,7 +444,6 @@ func TestSimpleOptionalComputed(t *testing.T) {
}

func TestOptionalComputedAttrCollection(t *testing.T) {
skipUnlessLinux(t)
emptyConfig := tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{})
t0 := tftypes.List{ElementType: tftypes.String}
t1 := tftypes.Object{
Expand Down Expand Up @@ -522,7 +514,6 @@ func TestOptionalComputedAttrCollection(t *testing.T) {
}

func TestOptionalComputedBlockCollection(t *testing.T) {
skipUnlessLinux(t)
emptyConfig := tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{})
t0 := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
Expand Down Expand Up @@ -602,7 +593,6 @@ func TestOptionalComputedBlockCollection(t *testing.T) {
}

func TestComputedSetFieldsNoDiff(t *testing.T) {
skipUnlessLinux(t)

elemSchema := schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down
2 changes: 1 addition & 1 deletion pkg/tests/cross-tests/diff_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type diffTestCase struct {
func runDiffCheck(t T, tc diffTestCase) {
tfwd := t.TempDir()

tfd := newTfDriver(t, tfwd, defProviderShortName, defRtype, tc.Resource)
tfd := newTFResDriver(t, tfwd, defProviderShortName, defRtype, tc.Resource)
_ = tfd.writePlanApply(t, tc.Resource.Schema, defRtype, "example", tc.Config1)
tfDiffPlan := tfd.writePlanApply(t, tc.Resource.Schema, defRtype, "example", tc.Config2)

Expand Down
2 changes: 1 addition & 1 deletion pkg/tests/cross-tests/input_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func runCreateInputCheck(t T, tc inputTestCase) {

tfwd := t.TempDir()

tfd := newTfDriver(t, tfwd, defProviderShortName, defRtype, tc.Resource)
tfd := newTFResDriver(t, tfwd, defProviderShortName, defRtype, tc.Resource)
tfd.writePlanApply(t, tc.Resource.Schema, defRtype, "example", tc.Config)

resMap := map[string]*schema.Resource{defRtype: tc.Resource}
Expand Down
14 changes: 0 additions & 14 deletions pkg/tests/cross-tests/input_cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func TestInputsEqualStringBasic(t *testing.T) {
skipUnlessLinux(t)
// Test both config representations.
for _, tc := range []struct {
name string
Expand Down Expand Up @@ -46,7 +45,6 @@ func TestInputsEqualStringBasic(t *testing.T) {
}

func TestInputsEqualObjectBasic(t *testing.T) {
skipUnlessLinux(t)
t1 := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"x": tftypes.String,
Expand Down Expand Up @@ -105,7 +103,6 @@ func TestInputsEqualObjectBasic(t *testing.T) {

func TestInputsConfigModeEqual(t *testing.T) {
// Regression test for [pulumi/pulumi-terraform-bridge#1762]
skipUnlessLinux(t)
t2 := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"x": tftypes.String,
}}
Expand Down Expand Up @@ -193,7 +190,6 @@ func TestInputsConfigModeEqual(t *testing.T) {

// Isolated from rapid-generated tests
func TestInputsEmptyString(t *testing.T) {
skipUnlessLinux(t)
runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -218,7 +214,6 @@ func TestInputsEmptyString(t *testing.T) {

func TestInputsUnspecifiedMaxItemsOne(t *testing.T) {
// Regression test for [pulumi/pulumi-terraform-bridge#1767]
skipUnlessLinux(t)
runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -243,7 +238,6 @@ func TestInputsUnspecifiedMaxItemsOne(t *testing.T) {

func TestOptionalSetNotSpecified(t *testing.T) {
// Regression test for [pulumi/pulumi-terraform-bridge#1970] and [pulumi/pulumi-terraform-bridge#1964]
skipUnlessLinux(t)
runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -264,7 +258,6 @@ func TestOptionalSetNotSpecified(t *testing.T) {

func TestInputsEqualEmptyList(t *testing.T) {
// Regression test for [pulumi/pulumi-terraform-bridge#1915]
skipUnlessLinux(t)
for _, maxItems := range []int{0, 1} {
name := fmt.Sprintf("MaxItems: %v", maxItems)
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -301,7 +294,6 @@ func TestInputsEqualEmptyList(t *testing.T) {
}

func TestExplicitNilList(t *testing.T) {
skipUnlessLinux(t)
t0 := tftypes.Map{ElementType: tftypes.Number}
t1 := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"f0": tftypes.List{ElementType: t0},
Expand Down Expand Up @@ -333,7 +325,6 @@ func TestExplicitNilList(t *testing.T) {
}

func TestInputsEmptyCollections(t *testing.T) {
skipUnlessLinux(t)
config := tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{})

// signifies a block
Expand Down Expand Up @@ -394,7 +385,6 @@ func TestInputsEmptyCollections(t *testing.T) {
}

func TestInputsNestedBlocksEmpty(t *testing.T) {
skipUnlessLinux(t)

emptyConfig := tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{})

Expand Down Expand Up @@ -487,7 +477,6 @@ func TestInputsNestedBlocksEmpty(t *testing.T) {
}

func TestEmptySetOfEmptyObjects(t *testing.T) {
skipUnlessLinux(t)
t1 := tftypes.Object{}
t0 := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"d3f0": tftypes.Set{ElementType: t1},
Expand All @@ -511,7 +500,6 @@ func TestEmptySetOfEmptyObjects(t *testing.T) {
}

func TestMap(t *testing.T) {
skipUnlessLinux(t)
t0 := tftypes.Map{ElementType: tftypes.String}
t1 := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"tags": t0,
Expand Down Expand Up @@ -542,7 +530,6 @@ func TestMap(t *testing.T) {
}

func TestTimeouts(t *testing.T) {
skipUnlessLinux(t)
emptyConfig := tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{})
runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Expand All @@ -567,7 +554,6 @@ func TestTimeouts(t *testing.T) {
// TestAccCloudWatch failed with PlanResourceChange to do a simple Create preview because the state upgrade was
// unexpectedly called with nil state. Emulate this here to test it does not fail.
func TestCreateDoesNotPanicWithStateUpgraders(t *testing.T) {
skipUnlessLinux(t)

resourceRuleV0 := func() *schema.Resource {
return &schema.Resource{
Expand Down
Loading

0 comments on commit a61fbc4

Please sign in to comment.