Skip to content

Commit

Permalink
Adds a labels test matrix (#1309)
Browse files Browse the repository at this point in the history
On top of #1238

The new release of GCP adds a DefaultLabels feature.

This change ports a test matrix from a similar feature of AWS provider.
The matrix tests that for
all interesting label combinations (empty, non-empty, 0, 1, or two
labels, at provider DefaultLabels
or resource Labels level), Pulumi provider can transition a sample
resource from one combination to
another, and the actual effective set of labels is what you would
expect.

What is currently not tested here but may need scrutiny based on similar
issues in AWS:

- passing unknowns into Labels or DefaultLabels
- passing secrets into Labels and making sure this avoids secrets leaks
- EffectiveLabels feature of the GCP provider that detects drift and
pulls labels from the cloud
  into management under GCP; this is not tested here
- working with labels in Plugin Framework based resources
  
The tests are currently failing likely due to a bridge issue:
pulumi/pulumi-terraform-bridge#1501
  • Loading branch information
guineveresaenger authored Nov 2, 2023
2 parents ff06c4d + c5f9cb6 commit 788bf91
Show file tree
Hide file tree
Showing 8 changed files with 752 additions and 6 deletions.
200 changes: 200 additions & 0 deletions examples/examples_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package examples
import (
"context"
cryptoRand "crypto/rand"
"encoding/json"
"fmt"
"math/big"
"math/rand"
"os"
"path/filepath"
"testing"

Expand All @@ -19,6 +21,7 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAccBucketGo(t *testing.T) {
Expand Down Expand Up @@ -179,3 +182,200 @@ func GenerateRandomString(n int) (string, error) {

return string(ret), nil
}

func TestLabelsCombinationsGo(t *testing.T) {
type testCase struct {
name string
s1 labelsState
s2 labelsState
}

testCases := []testCase{
{
"can add an empty label",
labelsState{
DefaultLabels: map[string]string{},
Labels: map[string]string{},
},
labelsState{
DefaultLabels: map[string]string{},
Labels: map[string]string{"x": ""},
},
},
{
"convoluted test case found by random-sampling",
labelsState{
DefaultLabels: map[string]string{"x": "", "y": "s"},
Labels: map[string]string{"x": ""},
},
labelsState{
DefaultLabels: map[string]string{"x": ""},
Labels: map[string]string{"x": "", "y": ""},
},
},
{
"can add a new default label on Update of existing stack",
labelsState{
DefaultLabels: map[string]string{},
Labels: map[string]string{},
},
labelsState{
DefaultLabels: map[string]string{"hello": "goodbye"},
Labels: map[string]string{},
},
},
{
"no changes means no changes",
labelsState{
DefaultLabels: map[string]string{},
Labels: map[string]string{},
},
labelsState{
DefaultLabels: map[string]string{},
Labels: map[string]string{},
},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tc.s1.validateTransitionTo(t, tc.s2)
})
}
}

func TestRandomLabelsCombinationsGo(t *testing.T) {
labelValues := []string{"", "s"} // empty values are conflated with unknowns in TF internals, must test

labelsValues := []map[string]string{
nil,
{},
}

for _, label := range labelValues {
m := map[string]string{"x": label}
labelsValues = append(labelsValues, m)
}

for _, label1 := range labelValues {
for _, label2 := range labelValues {
m := map[string]string{
"x": label1,
"y": label2,
}
labelsValues = append(labelsValues, m)
}
}

states := []labelsState{}

for _, label1 := range labelsValues {
for _, label2 := range labelsValues {
states = append(states, labelsState{
DefaultLabels: label1,
Labels: label2,
})
}
}

t.Logf("total state space: %v states", len(states))
t.Logf("random-sampling 100 state transitions")

for i := 0; i < 100; i++ {
t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
t.Parallel()
i := rand.Intn(len(states))
j := rand.Intn(len(states))
state1, state2 := states[i], states[j]
state1.validateTransitionTo(t, state2)
})
}
}

type labelsState struct {
DefaultLabels map[string]string `json:"defaultLabels"`
Labels map[string]string `json:"labels"`
}

func (st labelsState) serialize(t *testing.T) string {
bytes, err := json.Marshal(st)
require.NoError(t, err)
return string(bytes)
}

func (st labelsState) validateTransitionTo(t *testing.T, st2 labelsState) {
t.Logf("state1 = %v", st.serialize(t))
t.Logf("state2 = %v", st2.serialize(t))

goSdkFolder, err := filepath.Abs(filepath.Join("..", "sdk"))
require.NoError(t, err)

baseOpts := integration.ProgramTestOptions{
Dependencies: []string{
fmt.Sprintf("github.com/pulumi/pulumi-gcp/sdk/v7=%s", goSdkFolder),
},
}
if _, envConfigSet := os.LookupEnv("GOOGLE_ZONE"); envConfigSet {
baseOpts = getGoBaseOptions(t)
}

opts := baseOpts.With(integration.ProgramTestOptions{
Dir: "labels-combinations-go",
ExtraRuntimeValidation: validateStateResult(1, st, st2),
EditDirs: []integration.EditDir{{
Dir: filepath.Join("labels-combinations-go", "step1"),
Additive: true,
ExtraRuntimeValidation: validateStateResult(2, st, st2),
}},
Config: map[string]string{
"state1": st.serialize(t),
"state2": st2.serialize(t),
},
Quick: true,
DestroyOnCleanup: true,
})

integration.ProgramTest(t, &opts)
}

func (st labelsState) expectedLabels() map[string]string {
r := map[string]string{}
for k, v := range st.DefaultLabels {
r[k] = v
}
for k, v := range st.Labels {
r[k] = v
}
return r
}

func validateStateResult(phase int, st1, st2 labelsState) func(
t *testing.T,
stack integration.RuntimeValidationStackInfo,
) {
var st labelsState
switch phase {
case 1:
st = st1
default:
st = st2
}

return func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
for k, v := range stack.Outputs {
actualLabelsJSON := v.(string)
var actualLabels map[string]string
err := json.Unmarshal([]byte(actualLabelsJSON), &actualLabels)
require.NoError(t, err)
t.Logf("phase: %d", phase)
t.Logf("state1: %v", st1.serialize(t))
if phase == 2 {
t.Logf("state2: %v", st2.serialize(t))
}
require.Equalf(t, st.expectedLabels(), actualLabels, "key=%s", k)
t.Logf("key=%s labels are as expected: %v", k, actualLabelsJSON)
}
}
}
3 changes: 3 additions & 0 deletions examples/labels-combinations-go/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: labels-combinations-go
description: Testing provider capability to label resources correctly
runtime: go
88 changes: 88 additions & 0 deletions examples/labels-combinations-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module github.com/pulumi/pulumi-aws/examples/tags-combinations-go

go 1.21

require (
github.com/pulumi/pulumi-gcp/sdk/v7 v7.0.0-alpha.0
github.com/pulumi/pulumi/sdk/v3 v3.91.1
)

require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20221026131551-cf6655e29de4 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/charmbracelet/bubbles v0.16.1 // indirect
github.com/charmbracelet/bubbletea v0.24.2 // indirect
github.com/charmbracelet/lipgloss v0.7.1 // indirect
github.com/cheggaaa/pb v1.0.29 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/djherbis/times v1.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.0 // indirect
github.com/go-git/go-git/v5 v5.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl/v2 v2.17.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/opentracing/basictracer-go v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/term v1.1.0 // indirect
github.com/pulumi/esc v0.5.6 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
github.com/tweekmonster/luser v0.0.0-20161003172636-3fa38070dbd7 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/zclconf/go-cty v1.13.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/frand v1.4.2 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20211028080628-e2786a622600 // indirect
)
Loading

0 comments on commit 788bf91

Please sign in to comment.