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 install tf GHA step #2067

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
uses: pulumi/actions@v5
with:
pulumi-version: ^3.0.0
- uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
- name: Check out source code
uses: actions/checkout@master
- name: Install Go
Expand Down
30 changes: 30 additions & 0 deletions pkg/tests/cross-tests/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package crosstests

import (
"github.com/hashicorp/go-cty/cty"
"github.com/stretchr/testify/require"
)

func FailNotEqual(t T, name string, tfVal, pulVal any) {
t.Logf(name + " not equal!")
t.Logf("TF value %s", tfVal)
t.Logf("PU value %s", pulVal)
t.Fail()
}

func assertCtyValEqual(t T, name string, tfVal, pulVal cty.Value) {
if !tfVal.RawEquals(pulVal) {
FailNotEqual(t, name, tfVal.GoString(), pulVal.GoString())
}
}

func assertValEqual(t T, name string, tfVal, pulVal any) {
// usually plugin-sdk schema types
if hasEqualTfVal, ok := tfVal.(interface{ Equal(interface{}) bool }); ok {
if !hasEqualTfVal.Equal(pulVal) {
FailNotEqual(t, name, tfVal, pulVal)
}
} else {
require.Equal(t, tfVal, pulVal, "Values for key %s do not match", name)
}
}
28 changes: 0 additions & 28 deletions pkg/tests/cross-tests/ci.go

This file was deleted.

12 changes: 1 addition & 11 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,15 +87,13 @@ func TestSimpleStringRename(t *testing.T) {
}

func TestSetReordering(t *testing.T) {
skipUnlessLinux(t)
resource := &schema.Resource{
Schema: map[string]*schema.Schema{
"set": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
},
},
},
Expand All @@ -122,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 @@ -147,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 @@ -405,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 @@ -452,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 @@ -523,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
9 changes: 9 additions & 0 deletions pkg/tests/cross-tests/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package crosstests

var (
defProviderShortName = "crossprovider"
defRtype = "crossprovider_test_res"
defRtok = "TestRes"
defRtoken = defProviderShortName + ":index:" + defRtok
defProviderVer = "0.0.1"
)
64 changes: 17 additions & 47 deletions pkg/tests/cross-tests/diff_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
package crosstests

import (
"context"
"os"
"path/filepath"

"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pulumi/providertest/providers"
"github.com/pulumi/providertest/pulumitest"
"github.com/pulumi/providertest/pulumitest/opttest"
shimv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/pulcheck"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/stretchr/testify/assert"
Expand All @@ -47,58 +45,30 @@ type diffTestCase struct {
}

func runDiffCheck(t T, tc diffTestCase) {
var (
providerShortName = "crossprovider"
rtype = "crossprovider_testres"
rtok = "TestRes"
rtoken = providerShortName + ":index:" + rtok
providerVer = "0.0.1"
)

tfwd := t.TempDir()

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

tfp := &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
rtype: tc.Resource,
},
}
tfd := newTfDriver(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)

shimProvider := shimv2.NewProvider(tfp, shimv2.WithPlanResourceChange(
func(tfResourceType string) bool { return true },
))
resMap := map[string]*schema.Resource{defRtype: tc.Resource}
bridgedProvider := pulcheck.BridgedProvider(t, defProviderShortName, resMap)

pd := &pulumiDriver{
name: providerShortName,
version: providerVer,
shimProvider: shimProvider,
pulumiResourceToken: rtoken,
tfResourceName: rtype,
name: defProviderShortName,
pulumiResourceToken: defRtoken,
tfResourceName: defRtype,
objectType: nil,
}

puwd := t.TempDir()
pd.writeYAML(t, puwd, tc.Config1)

pt := pulumitest.NewPulumiTest(t, puwd,
opttest.TestInPlace(),
opttest.SkipInstall(),
opttest.AttachProvider(
providerShortName,
func(ctx context.Context, pt providers.PulumiTest) (providers.Port, error) {
handle, err := pd.startPulumiProvider(ctx)
require.NoError(t, err)
return providers.Port(handle.Port), nil
},
),
)
yamlProgram := pd.generateYAML(t, bridgedProvider.P.ResourcesMap(), tc.Config1)
pt := pulcheck.PulCheck(t, bridgedProvider, string(yamlProgram))

pt.Up()

pd.writeYAML(t, puwd, tc.Config2)
yamlProgram = pd.generateYAML(t, bridgedProvider.P.ResourcesMap(), tc.Config2)
p := filepath.Join(pt.CurrentStack().Workspace().WorkDir(), "Pulumi.yaml")
err := os.WriteFile(p, yamlProgram, 0o600)
require.NoErrorf(t, err, "writing Pulumi.yaml")
x := pt.Up()

tfAction := tfd.parseChangesFromTFPlan(*tfDiffPlan)
Expand Down
96 changes: 11 additions & 85 deletions pkg/tests/cross-tests/input_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ package crosstests
import (
"context"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pulumi/providertest/providers"
"github.com/pulumi/providertest/pulumitest"
"github.com/pulumi/providertest/pulumitest/opttest"
shimv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/pulcheck"
)

// Adapted from diff_check.go
Expand All @@ -35,46 +30,9 @@ type inputTestCase struct {
ObjectType *tftypes.Object
}

func FailNotEqual(t T, name string, tfVal, pulVal any) {
t.Logf(name + " not equal!")
t.Logf("TF value %s", tfVal)
t.Logf("PU value %s", pulVal)
t.Fail()
}

func assertCtyValEqual(t T, name string, tfVal, pulVal cty.Value) {
if !tfVal.RawEquals(pulVal) {
FailNotEqual(t, name, tfVal.GoString(), pulVal.GoString())
}
}

func assertValEqual(t T, name string, tfVal, pulVal any) {
// usually plugin-sdk schema types
if hasEqualTfVal, ok := tfVal.(interface{ Equal(interface{}) bool }); ok {
if !hasEqualTfVal.Equal(pulVal) {
FailNotEqual(t, name, tfVal, pulVal)
}
} else {
require.Equal(t, tfVal, pulVal, "Values for key %s do not match", name)
}
}

func ensureProviderValid(t T, tfp *schema.Provider) {
for _, r := range tfp.ResourcesMap {
//nolint:staticcheck
if r.Read == nil && r.ReadContext == nil {
r.ReadContext = func(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}
}
}
require.NoError(t, tfp.InternalValidate())
}

// Adapted from diff_check.go
func runCreateInputCheck(t T, tc inputTestCase) {
//nolint:staticcheck
if tc.Resource.CreateContext != nil || tc.Resource.Create != nil {
if tc.Resource.CreateContext != nil {
t.Errorf("Create methods should not be set for these tests!")
}

Expand All @@ -89,55 +47,23 @@ func runCreateInputCheck(t T, tc inputTestCase) {
rd.SetId("someid") // CreateContext must pick an ID
return make(diag.Diagnostics, 0)
}
var (
providerShortName = "crossprovider"
rtype = "crossprovider_testres"
rtok = "TestRes"
rtoken = providerShortName + ":index:" + rtok
providerVer = "0.0.1"
)

tfwd := t.TempDir()

tfd := newTfDriver(t, tfwd, providerShortName, rtype, tc.Resource)
tfd.writePlanApply(t, tc.Resource.Schema, rtype, "example", tc.Config)

tfp := &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
rtype: tc.Resource,
},
}
ensureProviderValid(t, tfp)

shimProvider := shimv2.NewProvider(tfp, shimv2.WithPlanResourceChange(
func(tfResourceType string) bool { return true },
))
tfd := newTfDriver(t, tfwd, defProviderShortName, defRtype, tc.Resource)
tfd.writePlanApply(t, tc.Resource.Schema, defRtype, "example", tc.Config)

resMap := map[string]*schema.Resource{defRtype: tc.Resource}
bridgedProvider := pulcheck.BridgedProvider(t, defProviderShortName, resMap)
pd := &pulumiDriver{
name: providerShortName,
version: providerVer,
shimProvider: shimProvider,
pulumiResourceToken: rtoken,
tfResourceName: rtype,
name: defProviderShortName,
pulumiResourceToken: defRtoken,
tfResourceName: defRtype,
objectType: tc.ObjectType,
}
yamlProgram := pd.generateYAML(t, bridgedProvider.P.ResourcesMap(), tc.Config)

puwd := t.TempDir()
pd.writeYAML(t, puwd, tc.Config)

pt := pulumitest.NewPulumiTest(t, puwd,
opttest.TestInPlace(),
opttest.SkipInstall(),
opttest.AttachProvider(
providerShortName,
func(ctx context.Context, pt providers.PulumiTest) (providers.Port, error) {
handle, err := pd.startPulumiProvider(ctx)
require.NoError(t, err)
return providers.Port(handle.Port), nil
},
),
opttest.Env("DISABLE_AUTOMATIC_PLUGIN_ACQUISITION", "true"),
)
pt := pulcheck.PulCheck(t, bridgedProvider, string(yamlProgram))

pt.Up()

Expand Down
Loading
Loading