Skip to content

Commit

Permalink
Fix overfitting label tests (#2072)
Browse files Browse the repository at this point in the history
During PlanResourceChange these tests was discovered to fail. Looks like
this is actually an upstream bug - the provider takes empty string for a
label to mean "keep previous value". This was reproed in TF.

I've adjusted the tests to take this into account - they should now work
for both PRC and non-PRC. We can remove the non-PRC bit after enabling
PRC by default.

fixes #2078
  • Loading branch information
VenelinMartinov authored Jun 13, 2024
1 parent 63d645c commit faff475
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion examples/examples_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"math/rand"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/sql"
Expand Down Expand Up @@ -232,6 +233,28 @@ func TestLabelsCombinationsGo(t *testing.T) {
Labels: map[string]string{"x": ""},
},
},
{
"make label empty",
labelsState{
DefaultLabels: map[string]string{},
Labels: map[string]string{"x": "s"},
},
labelsState{
DefaultLabels: map[string]string{},
Labels: map[string]string{"x": ""},
},
},
{
"make default label empty",
labelsState{
DefaultLabels: map[string]string{"x": "s"},
Labels: map[string]string{},
},
labelsState{
DefaultLabels: map[string]string{"x": ""},
Labels: map[string]string{},
},
},
{
"convoluted test case found by random-sampling",
labelsState{
Expand Down Expand Up @@ -370,6 +393,35 @@ func (st labelsState) validateTransitionTo(t *testing.T, st2 labelsState) {
integration.ProgramTest(t, &opts)
}

func (st labelsState) expectedLabelsPRC(prev labelsState) map[string]string {
// Note that the upstream provider actually takes a "" value for a label to mean "keep the previous value".
// This behaviour is exposed under PlanResourceChange
r := map[string]string{}
for k, v := range st.DefaultLabels {
if v != "" {
r[k] = v
} else {
if prev.DefaultLabels[k] != "" {
r[k] = prev.DefaultLabels[k]
} else if prev.Labels[k] != "" {
r[k] = prev.Labels[k]
}
}
}
for k, v := range st.Labels {
if v != "" {
r[k] = v
} else {
if prev.DefaultLabels[k] != "" {
r[k] = prev.DefaultLabels[k]
} else if prev.Labels[k] != "" {
r[k] = prev.Labels[k]
}
}
}
return r
}

func (st labelsState) expectedLabels() map[string]string {
r := map[string]string{}
for k, v := range st.DefaultLabels {
Expand Down Expand Up @@ -401,10 +453,14 @@ func validateStateResult(phase int, st1, st2 labelsState) func(
require.NoError(t, err)
t.Logf("phase: %d", phase)
t.Logf("state1: %v", st1.serialize(t))
prev := labelsState{}
if phase == 2 {
prev = st1
t.Logf("state2: %v", st2.serialize(t))
}
require.Equalf(t, st.expectedLabels(), actualLabels, "key=%s", k)
if !reflect.DeepEqual(actualLabels, st.expectedLabelsPRC(prev)) {
require.Equalf(t, st.expectedLabels(), actualLabels, "key=%s", k)
}
t.Logf("key=%s labels are as expected: %v", k, actualLabelsJSON)
}
}
Expand Down

0 comments on commit faff475

Please sign in to comment.