Skip to content

Commit

Permalink
Move upgrade tests to the new framework (#3772)
Browse files Browse the repository at this point in the history
This change makes tests consistent with the latest API exposed in
github.com/pulumi/providertest/pulumitest and unblocks the upstream
upgrade and release.
  • Loading branch information
t0yv0 authored Apr 3, 2024
1 parent 97eea72 commit 2d5ad20
Show file tree
Hide file tree
Showing 70 changed files with 8,739 additions and 8,779 deletions.
79 changes: 38 additions & 41 deletions provider/provider_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,60 @@
package provider

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/pulumi/providertest"
"github.com/stretchr/testify/assert"
"github.com/pulumi/providertest/pulumitest"
"github.com/pulumi/providertest/pulumitest/opttest"
"github.com/stretchr/testify/require"
)

func TestLambdaLayerNew(t *testing.T) {
nodeTest(t, filepath.Join("..", "examples", "lambda-layer-new"))
func TestLambdaLayerNewUpgrade(t *testing.T) {
testProviderUpgrade(t, filepath.Join("..", "examples", "lambda-layer-new"), nodeProviderUpgradeOpts())
}

func TestCloudWatch(t *testing.T) {
nodeTest(t, filepath.Join("..", "examples", "cloudwatch"))
func TestCloudWatchUpgrade(t *testing.T) {
testProviderUpgrade(t, filepath.Join("..", "examples", "cloudwatch"), nodeProviderUpgradeOpts())
}

func TestLogGroup(t *testing.T) {
nodeTest(t, filepath.Join("..", "examples", "logGroup"))
func TestLogGroupUpgrade(t *testing.T) {
testProviderUpgrade(t, filepath.Join("..", "examples", "logGroup"), nodeProviderUpgradeOpts())
}

func TestQueue(t *testing.T) {
nodeTest(t, filepath.Join("..", "examples", "queue"),
providertest.WithSkippedUpgradeTestMode(providertest.UpgradeTestMode_Quick, "Prefer PreviewOnly"),
providertest.WithDiffValidation(providertest.NoReplacements()))
func TestQueueUpgrade(t *testing.T) {
testProviderUpgrade(t, filepath.Join("..", "examples", "queue"), nodeProviderUpgradeOpts())
}

func TestRoute53(t *testing.T) {
nodeTest(t, filepath.Join("..", "examples", "route53"))
func TestRoute53Upgrade(t *testing.T) {
testProviderUpgrade(t, filepath.Join("..", "examples", "route53"), nodeProviderUpgradeOpts())
}

func TestJobQueue(t *testing.T) {
simpleNodeTest(t, filepath.Join("test-programs", "job-queue"),
providertest.WithSkippedUpgradeTestMode(providertest.UpgradeTestMode_Quick, "Prefer PreviewOnly"),
providertest.WithDiffValidation(func(t *testing.T, d providertest.Diffs) {
for _, diff := range d {
if diff.URN.Name() != "testQueue" {
continue
}
assert.Emptyf(t, diff.Replaces, "Unexpected replace plan for testQueue")
for _, changedProp := range diff.Diffs {
// Ignoring benign update from nil to empty-map tags.
if changedProp == "tags" {
continue
}
if changedProp == "tagsAll" {
continue
}
assert.Fail(t, fmt.Sprintf("Unexpected update for testQueue: %s", changedProp))
}
}
}))
func TestJobQueueUpgrade(t *testing.T) {
opts := nodeProviderUpgradeOpts()
opts.setEnvRegion = false
testProviderUpgrade(t, filepath.Join("test-programs", "job-queue"), opts)
}

func nodeProviderUpgradeOpts() *testProviderUpgradeOptions {
return &testProviderUpgradeOptions{
linkNodeSDK: true,
installDeps: true,
setEnvRegion: true,
}
}

func TestRegress3094(t *testing.T) {
simpleNodeTest(t,
filepath.Join("test-programs", "regress-3094"),
providertest.WithSkippedUpgradeTestMode(providertest.UpgradeTestMode_Quick, "Not testing upgrades"),
providertest.WithSkippedUpgradeTestMode(providertest.UpgradeTestMode_PreviewOnly, "Not testing upgrades"),
)
skipIfShort(t)
dir := filepath.Join("test-programs", "regress-3094")
cwd, err := os.Getwd()
require.NoError(t, err)
providerName := "aws"
options := []opttest.Option{
opttest.LocalProviderPath(providerName, filepath.Join(cwd, "..", "bin")),
opttest.YarnLink("@pulumi/aws"),
}
test := pulumitest.NewPulumiTest(t, dir, options...)
upResult := test.Up()
t.Logf("#%v", upResult.Summary)
}
135 changes: 50 additions & 85 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,108 +2,73 @@
package provider

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

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/pulumi/providertest"
pfbridge "github.com/pulumi/pulumi-terraform-bridge/pf/tfbridge"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"

"github.com/pulumi/pulumi-aws/provider/v6/pkg/version"
"github.com/pulumi/providertest/optproviderupgrade"
"github.com/pulumi/providertest/pulumitest"
"github.com/pulumi/providertest/pulumitest/assertpreview"
"github.com/pulumi/providertest/pulumitest/opttest"
"github.com/stretchr/testify/require"
)

func providerServer(t *testing.T) pulumirpc.ResourceProviderServer {
ctx := context.Background()
version.Version = "0.0.1"
info := Provider()
p, err := pfbridge.MakeMuxedServer(ctx, info.Name, *info,
/*
* We leave the schema blank. This will result in incorrect calls to
* GetSchema, but otherwise does not effect the provider. It reduces the
* time to test start by minutes.
*/
[]byte("{}"),
)(nil)
require.NoError(t, err)
return p
func TestUpgradeCoverage(t *testing.T) {
providertest.ReportUpgradeCoverage(t)
}

func skipIfShort(t *testing.T) {
if testing.Short() {
t.Skipf("Skipping in testing.Short() mode, assuming this is a CI run without AWS creds")
return
func getEnvRegion(t *testing.T) string {
envRegion := os.Getenv("AWS_REGION")
if envRegion == "" {
envRegion = "us-west-2"
}
return envRegion
}

func withNoChangesInResourcesAndNoReplacementsInProvider() providertest.Option {
return providertest.WithDiffValidation(func(t *testing.T, diffs providertest.Diffs) {
for _, d := range diffs {
// For the explicit provider, prohibit replacements.
isProvider := strings.HasSuffix(string(d.URN), "aws::provider") ||
strings.Contains(string(d.URN), "pulumi:providers:aws")
if isProvider {
assert.Emptyf(t, d.Replaces, "Unexpected replacement plan for %#v", d)
} else { // For normal resources, prohibit any changes.
assert.Falsef(t, d.HasChanges, "Expected no change for %#v", d)
}
}
})
type testProviderUpgradeOptions struct {
baselineVersion string
linkNodeSDK bool
installDeps bool
setEnvRegion bool
}

func test(t *testing.T, dir, baselineVersion string, opts ...providertest.Option) {
if len(opts) == 0 {
opts = []providertest.Option{withNoChangesInResourcesAndNoReplacementsInProvider()}
func skipIfShort(t *testing.T) {
if testing.Short() {
t.Skipf("Skipping in testing.Short() mode, assuming this is a CI run without credentials")
}

}
func testProviderUpgrade(t *testing.T, dir string, opts *testProviderUpgradeOptions) {
skipIfShort(t)
if baselineVersion == "" {
baselineVersion = "5.42.0"
}
opts = append(opts,
providertest.WithProviderName("aws"),
providertest.WithBaselineVersion(baselineVersion),
providertest.WithResourceProviderServer(providerServer(t)),
t.Parallel()
t.Helper()
var (
providerName string = "aws"
baselineVersion string = "5.42.0"
)
ptest := providertest.NewProviderTest(dir, opts...)
ptest.Run(t)
}

func nodeTest(t *testing.T, dir string, opts ...providertest.Option) {
if len(opts) == 0 {
opts = []providertest.Option{withNoChangesInResourcesAndNoReplacementsInProvider()}
if opts != nil && opts.baselineVersion != "" {
baselineVersion = opts.baselineVersion
}
envRegion := getEnvRegion(t)
opts = append(opts,
providertest.WithConfig("aws:region", "INVALID_REGION"),
providertest.WithConfig("aws:envRegion", envRegion),
)
test(t, dir, "", opts...)
}

// This version of nodeTest does not aws:region INVALID_REGION manipulation.
func simpleNodeTest(t *testing.T, dir string, opts ...providertest.Option) {
if len(opts) == 0 {
opts = []providertest.Option{withNoChangesInResourcesAndNoReplacementsInProvider()}
cwd, err := os.Getwd()
require.NoError(t, err)
options := []opttest.Option{
opttest.DownloadProviderVersion(providerName, baselineVersion),
opttest.LocalProviderPath(providerName, filepath.Join(cwd, "..", "bin")),
}
envRegion := getEnvRegion(t)
opts = append(opts,
providertest.WithConfig("aws:region", envRegion),
)
test(t, dir, "", opts...)
}

func TestUpgradeCoverage(t *testing.T) {
providertest.ReportUpgradeCoverage(t)
}

func getEnvRegion(t *testing.T) string {
envRegion := os.Getenv("AWS_REGION")
if envRegion == "" {
envRegion = "us-west-2"
if opts == nil || !opts.installDeps {
options = append(options, opttest.SkipInstall())
}
return envRegion
if opts != nil && opts.linkNodeSDK {
options = append(options, opttest.YarnLink("@pulumi/aws"))
}
test := pulumitest.NewPulumiTest(t, dir, options...)
if opts != nil && opts.setEnvRegion {
test.SetConfig("aws:region", "INVALID_REGION")
test.SetConfig("aws:envRegion", getEnvRegion(t))
}
result := providertest.PreviewProviderUpgrade(
test, providerName, baselineVersion, optproviderupgrade.DisableAttach(),
)
assertpreview.HasNoReplacements(t, result)
}
Loading

0 comments on commit 2d5ad20

Please sign in to comment.