Skip to content

Commit

Permalink
Adding provider.Configure tests to validate autoNaming unmarshalling
Browse files Browse the repository at this point in the history
Adding `startsWith` comparison to better assert autoTrim
  • Loading branch information
corymhall committed Nov 18, 2024
1 parent 40a8d56 commit 7fad16d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 11 deletions.
28 changes: 17 additions & 11 deletions provider/pkg/autonaming/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_getDefaultName(t *testing.T) {
olds resource.PropertyMap
news resource.PropertyMap
err error
comparison func(actual resource.PropertyValue) bool
comparison func(t *testing.T, actual resource.PropertyValue) bool
}{
{
name: "Name specified explicitly",
Expand Down Expand Up @@ -81,7 +81,7 @@ func Test_getDefaultName(t *testing.T) {
} else {
require.NoError(t, err)
}
if !tt.comparison(got) {
if !tt.comparison(t, got) {
t.Errorf("getDefaultName() = %v for spec: %+v", got, autoNamingSpec)
}
t.Logf("getDefaultName() = %v for spec: %+v", got, autoNamingSpec)
Expand All @@ -100,7 +100,7 @@ func Test_getDefaultName_withAutoNameConfig(t *testing.T) {
olds resource.PropertyMap
news resource.PropertyMap
err error
comparison func(actual resource.PropertyValue) bool
comparison func(t *testing.T, actual resource.PropertyValue) bool
}{
{
name: "Name specified explicitly",
Expand Down Expand Up @@ -135,7 +135,7 @@ func Test_getDefaultName_withAutoNameConfig(t *testing.T) {
autoNameConfig: AutoNamingConfig{
AutoTrim: true,
},
comparison: within(12, 12),
comparison: startsWith("myReagName", 1),
},
{
resourceName: "myReallyLongName",
Expand All @@ -144,7 +144,7 @@ func Test_getDefaultName_withAutoNameConfig(t *testing.T) {
autoNameConfig: AutoNamingConfig{
AutoTrim: true,
},
comparison: within(13, 13),
comparison: startsWith("myRealgName", 1),
},
{
name: "Autoname with max length too small and no auto trim",
Expand All @@ -165,7 +165,7 @@ func Test_getDefaultName_withAutoNameConfig(t *testing.T) {
AutoTrim: true,
},
maxLength: 13,
comparison: within(13, 13),
comparison: startsWith("myReagName", 2),
},
{
name: "Autoname with long random suffix",
Expand Down Expand Up @@ -197,22 +197,28 @@ func Test_getDefaultName_withAutoNameConfig(t *testing.T) {
} else {
require.NoError(t, err)
}
if !tt.comparison(got) {
if !tt.comparison(t, got) {
t.Errorf("getDefaultName() = %v for spec: %+v", got, autoNamingSpec)
}
t.Logf("getDefaultName() = %v for spec: %+v", got, autoNamingSpec)
})
}
}

func equals(expected resource.PropertyValue) func(resource.PropertyValue) bool {
return func(actual resource.PropertyValue) bool {
func equals(expected resource.PropertyValue) func(t *testing.T, actual resource.PropertyValue) bool {
return func(t *testing.T, actual resource.PropertyValue) bool {
return expected == actual
}
}

func within(min, max int) func(value resource.PropertyValue) bool {
return func(actual resource.PropertyValue) bool {
func startsWith(prefix string, randomLen int) func(t *testing.T, actual resource.PropertyValue) bool {
return func(t *testing.T, actual resource.PropertyValue) bool {
return assert.Regexp(t, fmt.Sprintf("^%s-[a-z0-9]{%d}", prefix, randomLen), actual.StringValue())
}
}

func within(min, max int) func(t *testing.T, value resource.PropertyValue) bool {
return func(t *testing.T, actual resource.PropertyValue) bool {
l := len(actual.V.(string))
return min <= l && l <= max
}
Expand Down
83 changes: 83 additions & 0 deletions provider/pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/pulumi/pulumi-aws-native/provider/pkg/autonaming"
"github.com/pulumi/pulumi-aws-native/provider/pkg/client"
"github.com/pulumi/pulumi-aws-native/provider/pkg/metadata"
"github.com/pulumi/pulumi-aws-native/provider/pkg/resources"
Expand All @@ -19,6 +20,88 @@ import (
"google.golang.org/protobuf/types/known/structpb"
)

func TestConfigure(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockCCC := client.NewMockCloudControlClient(ctrl)
mockCustomResource := resources.NewMockCustomResource(ctrl)

ctx, cancel := context.WithCancel(context.Background())
provider := &cfnProvider{
name: "test-provider",
resourceMap: &metadata.CloudAPIMetadata{Resources: map[string]metadata.CloudAPIResource{}},
customResources: map[string]resources.CustomResource{"custom:resource": mockCustomResource},
ccc: mockCCC,
canceler: &cancellationContext{
context: ctx,
cancel: cancel,
},
}

t.Run("No AutoNaming Config", func(t *testing.T) {
req := &pulumirpc.ConfigureRequest{
Variables: map[string]string{
"aws-native:config:skipCredentialsValidation": "true",
"aws-native:config:region": "us-west-2",
},
}

_, err := provider.Configure(ctx, req)
assert.NoError(t, err)

assert.Nil(t, provider.autoNamingConfig)
})

t.Run("AutoNaming config", func(t *testing.T) {
req := &pulumirpc.ConfigureRequest{
Variables: map[string]string{
"aws-native:config:skipCredentialsValidation": "true",
"aws-native:config:autoNaming": "{\"autoTrim\": true, \"randomSuffixMinLength\": 5}",
"aws-native:config:region": "us-west-2",
},
}

_, err := provider.Configure(ctx, req)
assert.NoError(t, err)

assert.NotNil(t, provider.autoNamingConfig)
assert.Equal(t, autonaming.AutoNamingConfig{
AutoTrim: true,
RandomSuffixMinLength: 5,
}, *provider.autoNamingConfig)
})

t.Run("AutoNaming empty", func(t *testing.T) {
req := &pulumirpc.ConfigureRequest{
Variables: map[string]string{
"aws-native:config:skipCredentialsValidation": "true",
"aws-native:config:autoNaming": "{}",
"aws-native:config:region": "us-west-2",
},
}

_, err := provider.Configure(ctx, req)
assert.NoError(t, err)

assert.NotNil(t, provider.autoNamingConfig)
assert.Equal(t, autonaming.AutoNamingConfig{}, *provider.autoNamingConfig)
})

t.Run("AutoNaming config invalid", func(t *testing.T) {
req := &pulumirpc.ConfigureRequest{
Variables: map[string]string{
"aws-native:config:skipCredentialsValidation": "true",
"aws-native:config:autoNaming": "autoTrim: true",
"aws-native:config:region": "us-west-2",
},
}

_, err := provider.Configure(ctx, req)
assert.ErrorContains(t, err, "failed to unmarshal 'autoNaming' config")
})
}

func TestCreate(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down

0 comments on commit 7fad16d

Please sign in to comment.