Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmimsft committed Feb 27, 2024
1 parent 063bfec commit ea95250
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
27 changes: 27 additions & 0 deletions pkg/recipes/terraform/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,33 @@ func Test_AddProviders(t *testing.T) {
},
expectedConfigFile: "testdata/providers-overridereqproviders.tf.json",
},
{
desc: "recipe providers in env config setup but nil",
expectedUCPConfiguredProviders: nil,
Err: nil,
envConfig: recipes.Configuration{
RecipeConfig: datamodel.RecipeConfigProperties{
Terraform: datamodel.TerraformConfigProperties{
Providers: map[string][]datamodel.ProviderConfigProperties{
"azurerm": {
{
AdditionalProperties: nil,
},
{
AdditionalProperties: map[string]any{
"alias": "az-paymentservice",
"subscriptionid": 45678,
"tenant_id": "gfhf45345-5d73-gh34-wh84",
},
},
},
},
},
},
},
requiredProviders: nil,
expectedConfigFile: "testdata/providers-envrecipedefaultconfig.tf.json",
},
{
desc: "recipe providers not populated",
expectedUCPConfiguredProviders: nil,
Expand Down
2 changes: 1 addition & 1 deletion pkg/recipes/terraform/config/providers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Provider interface {
BuildConfig(ctx context.Context, envConfig *recipes.Configuration) (map[string]any, error)
}

// GetUCPConfiguredTerraformProviders returns a map of Terraform provider names with configuration details stored in UCP, to provider config builder.
// GetUCPConfiguredTerraformProviders returns a map of Terraform provider names to provider config builder.
// These providers represent Terraform providers for which Radius generates custom provider configurations based on credentials stored with UCP
// and providers configured on the Radius environment. For example, the Azure subscription id is added to Azure provider config using Radius Environment's Azure provider scope.
func GetUCPConfiguredTerraformProviders(ucpConn sdk.Connection, secretProvider *ucp_provider.SecretProvider) map[string]Provider {
Expand Down
105 changes: 105 additions & 0 deletions pkg/recipes/terraform/config/providers/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package providers

import (
"context"
"testing"

"github.com/radius-project/radius/pkg/corerp/datamodel"
"github.com/radius-project/radius/pkg/recipes"
"github.com/stretchr/testify/require"
)

func TestGetRecipeProviderConfigs(t *testing.T) {
testCases := []struct {
desc string
envConfig *recipes.Configuration
expected map[string]any
}{
{
desc: "envConfig not set",
envConfig: nil,
expected: map[string]any{},
},
{
desc: "no providers configured",
envConfig: &recipes.Configuration{},
expected: map[string]any{},
},
{
desc: "empty provider config",
envConfig: &recipes.Configuration{
RecipeConfig: datamodel.RecipeConfigProperties{
Terraform: datamodel.TerraformConfigProperties{
Providers: map[string][]datamodel.ProviderConfigProperties{
"aws": {},
},
},
},
},
expected: map[string]any{},
},
{
desc: "Additional Properties set to nil in provider config",
envConfig: &recipes.Configuration{
RecipeConfig: datamodel.RecipeConfigProperties{
Terraform: datamodel.TerraformConfigProperties{
Providers: map[string][]datamodel.ProviderConfigProperties{
"aws": {
{
AdditionalProperties: nil,
},
},
},
},
},
},
expected: map[string]any{"aws": []any{map[string]any(nil)}},
},
{
desc: "provider with config",
envConfig: &recipes.Configuration{
RecipeConfig: datamodel.RecipeConfigProperties{
Terraform: datamodel.TerraformConfigProperties{
Providers: map[string][]datamodel.ProviderConfigProperties{
"azurerm": {
{
AdditionalProperties: map[string]any{
"subscriptionid": 1234,
"tenant_id": "745fg88bf-86f1-41af-43ut",
},
},
{
AdditionalProperties: map[string]any{
"alias": "az-paymentservice",
"subscriptionid": 45678,
"tenant_id": "gfhf45345-5d73-gh34-wh84",
},
},
},
},
},
},
},
expected: map[string]any{
"azurerm": []any{
map[string]any{
"subscriptionid": 1234,
"tenant_id": "745fg88bf-86f1-41af-43ut",
},
map[string]any{
"alias": "az-paymentservice",
"subscriptionid": 45678,
"tenant_id": "gfhf45345-5d73-gh34-wh84",
},
},
},
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result := GetRecipeProviderConfigs(context.Background(), tc.envConfig)
require.Equal(t, tc.expected, result)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"terraform": {
"backend": {
"kubernetes": {
"config_path": "/home/radius/.kube/config",
"namespace": "radius-system",
"secret_suffix": "test-secret-suffix"
}
}
},
"provider": {
"azurerm": [
null,
{
"alias": "az-paymentservice",
"subscriptionid": 45678,
"tenant_id": "gfhf45345-5d73-gh34-wh84"
}
]
},
"module": {
"redis-azure": {
"redis_cache_name": "redis-test",
"resource_group_name": "test-rg",
"sku": "P",
"source": "Azure/redis/azurerm",
"version": "1.1.0"
}
}
}

0 comments on commit ea95250

Please sign in to comment.