forked from radius-project/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding functional test for private terraform repo support and fixing …
…delete issue with private repo (radius-project#7436) # Description - Added functional test for terraform recipe using module stored in private git repository. - Adding support for delete functionality for private repos. - Adding required unit tests ## Type of change <!-- Please select **one** of the following options that describes your change and delete the others. Clearly identifying the type of change you are making will help us review your PR faster, and is used in authoring release notes. If you are making a bug fix or functionality change to Radius and do not have an associated issue link please create one now. --> - This pull request is a minor refactor, code cleanup, test improvement, or other maintenance task and doesn't change the functionality of Radius (issue link optional). <!-- Please update the following to link the associated issue. This is required for some kinds of changes (see above). --> Fixes: #issue_number --------- Signed-off-by: Vishwanath Hiremath <[email protected]>
- Loading branch information
1 parent
de78e1e
commit f3e6673
Showing
6 changed files
with
470 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,6 +316,155 @@ func Test_Engine_Terraform_Success(t *testing.T) { | |
require.NoError(t, err) | ||
require.Equal(t, result, recipeResult) | ||
} | ||
func Test_Engine_Terraform_Failure(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
errFindSecretRefs error | ||
errLoadSecrets error | ||
errExecute error | ||
}{ | ||
{ | ||
name: "find secret references failed", | ||
errFindSecretRefs: fmt.Errorf("failed to parse git url %s", "git://https://dev.azure.com/mongo-recipe/recipe"), | ||
}, | ||
{ | ||
name: "failed loading secrets", | ||
errLoadSecrets: fmt.Errorf("%q is a valid resource id but does not refer to a resource", ""), | ||
}, | ||
{ | ||
name: "find secret references failed", | ||
errExecute: errors.New("failed to add git config"), | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
recipeMetadata := recipes.ResourceMetadata{ | ||
Name: "mongo-azure", | ||
ApplicationID: "/planes/radius/local/resourcegroups/test-rg/providers/applications.core/applications/app1", | ||
EnvironmentID: "/planes/radius/local/resourcegroups/test-rg/providers/applications.core/environments/env1", | ||
ResourceID: "/planes/radius/local/resourceGroups/test-rg/providers/Microsoft.Resources/deployments/recipe", | ||
Parameters: map[string]any{ | ||
"resourceName": "resource1", | ||
}, | ||
} | ||
envConfig := &recipes.Configuration{ | ||
Runtime: recipes.RuntimeConfiguration{ | ||
Kubernetes: &recipes.KubernetesRuntime{ | ||
Namespace: "default", | ||
}, | ||
}, | ||
Providers: datamodel.Providers{ | ||
Azure: datamodel.ProvidersAzure{ | ||
Scope: "scope", | ||
}, | ||
}, | ||
RecipeConfig: datamodel.RecipeConfigProperties{ | ||
Terraform: datamodel.TerraformConfigProperties{ | ||
Authentication: datamodel.AuthConfig{ | ||
Git: datamodel.GitAuthConfig{ | ||
PAT: map[string]datamodel.SecretConfig{ | ||
"dev.azure.com": { | ||
Secret: "/planes/radius/local/resourcegroups/default/providers/Applications.Core/secretStores/azdevopsgit", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
recipeResult := &recipes.RecipeOutput{ | ||
Resources: []string{"mongoStorageAccount", "mongoDatabase"}, | ||
Secrets: map[string]any{ | ||
"connectionString": "mongodb://testUser:[email protected]:10255", | ||
}, | ||
Values: map[string]any{ | ||
"host": "testAccount1.mongo.cosmos.azure.com", | ||
"port": 10255, | ||
}, | ||
} | ||
recipeDefinition := &recipes.EnvironmentDefinition{ | ||
Driver: recipes.TemplateKindTerraform, | ||
TemplatePath: "git://https://dev.azure.com/mongo-recipe/recipe", | ||
ResourceType: "Applications.Datastores/mongoDatabases", | ||
} | ||
prevState := []string{ | ||
"/subscriptions/test-sub/resourceGroups/test-rg/providers/System.Test/testResources/test1", | ||
} | ||
ctx := testcontext.New(t) | ||
engine, configLoader, _, driverWithSecrets, secretsLoader := setup(t) | ||
configLoader.EXPECT(). | ||
LoadConfiguration(ctx, recipeMetadata). | ||
Times(1). | ||
Return(envConfig, nil) | ||
configLoader.EXPECT(). | ||
LoadRecipe(ctx, &recipeMetadata). | ||
Times(1). | ||
Return(recipeDefinition, nil) | ||
|
||
if tc.errFindSecretRefs != nil { | ||
driverWithSecrets.EXPECT(). | ||
FindSecretIDs(ctx, *envConfig, *recipeDefinition). | ||
Times(1). | ||
Return("", tc.errFindSecretRefs) | ||
} else { | ||
driverWithSecrets.EXPECT(). | ||
FindSecretIDs(ctx, *envConfig, *recipeDefinition). | ||
Times(1). | ||
Return("/planes/radius/local/resourcegroups/default/providers/Applications.Core/secretStores/azdevopsgit", nil) | ||
|
||
if tc.errLoadSecrets != nil { | ||
secretsLoader.EXPECT(). | ||
LoadSecrets(ctx, gomock.Any()). | ||
Times(1). | ||
Return(v20231001preview.SecretStoresClientListSecretsResponse{}, tc.errLoadSecrets) | ||
} else { | ||
secretsLoader.EXPECT(). | ||
LoadSecrets(ctx, gomock.Any()). | ||
Times(1). | ||
Return(v20231001preview.SecretStoresClientListSecretsResponse{}, nil) | ||
if tc.errExecute != nil { | ||
driverWithSecrets.EXPECT(). | ||
Execute(ctx, recipedriver.ExecuteOptions{ | ||
BaseOptions: recipedriver.BaseOptions{ | ||
Configuration: *envConfig, | ||
Recipe: recipeMetadata, | ||
Definition: *recipeDefinition, | ||
}, | ||
PrevState: prevState, | ||
}). | ||
Times(1). | ||
Return(nil, tc.errExecute) | ||
} else { | ||
driverWithSecrets.EXPECT(). | ||
Execute(ctx, recipedriver.ExecuteOptions{ | ||
BaseOptions: recipedriver.BaseOptions{ | ||
Configuration: *envConfig, | ||
Recipe: recipeMetadata, | ||
Definition: *recipeDefinition, | ||
}, | ||
PrevState: prevState, | ||
}). | ||
Times(1). | ||
Return(recipeResult, nil) | ||
} | ||
} | ||
} | ||
|
||
result, err := engine.Execute(ctx, ExecuteOptions{ | ||
BaseOptions: BaseOptions{ | ||
Recipe: recipeMetadata, | ||
}, | ||
PreviousState: prevState, | ||
}) | ||
if tc.errFindSecretRefs != nil || tc.errLoadSecrets != nil || tc.errExecute != nil { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, result, recipeResult) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_Engine_InvalidDriver(t *testing.T) { | ||
ctx := testcontext.New(t) | ||
|
@@ -824,3 +973,85 @@ func Test_Engine_Execute_With_Secrets_Success(t *testing.T) { | |
require.NoError(t, err) | ||
require.Equal(t, result, recipeResult) | ||
} | ||
|
||
func Test_Engine_Delete_With_Secrets_Success(t *testing.T) { | ||
_, _, outputResources := getRecipeInputs() | ||
recipeMetadata := recipes.ResourceMetadata{ | ||
Name: "mongo-azure", | ||
ApplicationID: "/planes/radius/local/resourcegroups/test-rg/providers/applications.core/applications/app1", | ||
EnvironmentID: "/planes/radius/local/resourcegroups/test-rg/providers/applications.core/environments/env1", | ||
ResourceID: "/planes/radius/local/resourceGroups/test-rg/providers/Microsoft.Resources/deployments/recipe", | ||
Parameters: map[string]any{ | ||
"resourceName": "resource1", | ||
}, | ||
} | ||
envConfig := &recipes.Configuration{ | ||
Runtime: recipes.RuntimeConfiguration{ | ||
Kubernetes: &recipes.KubernetesRuntime{ | ||
Namespace: "default", | ||
}, | ||
}, | ||
Providers: datamodel.Providers{ | ||
Azure: datamodel.ProvidersAzure{ | ||
Scope: "scope", | ||
}, | ||
}, | ||
RecipeConfig: datamodel.RecipeConfigProperties{ | ||
Terraform: datamodel.TerraformConfigProperties{ | ||
Authentication: datamodel.AuthConfig{ | ||
Git: datamodel.GitAuthConfig{ | ||
PAT: map[string]datamodel.SecretConfig{ | ||
"dev.azure.com": { | ||
Secret: "/planes/radius/local/resourcegroups/default/providers/Applications.Core/secretStores/azdevopsgit", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
recipeDefinition := &recipes.EnvironmentDefinition{ | ||
Driver: recipes.TemplateKindTerraform, | ||
TemplatePath: "git://https://dev.azure.com/mongo-recipe/recipe", | ||
ResourceType: "Applications.Datastores/mongoDatabases", | ||
} | ||
ctx := testcontext.New(t) | ||
engine, configLoader, _, driverWithSecrets, secretsLoader := setup(t) | ||
|
||
configLoader.EXPECT(). | ||
LoadRecipe(ctx, &recipeMetadata). | ||
Times(1). | ||
Return(recipeDefinition, nil) | ||
|
||
configLoader.EXPECT(). | ||
LoadConfiguration(ctx, recipeMetadata). | ||
Times(1). | ||
Return(envConfig, nil) | ||
driverWithSecrets.EXPECT(). | ||
FindSecretIDs(ctx, *envConfig, *recipeDefinition). | ||
Times(1). | ||
Return("/planes/radius/local/resourcegroups/default/providers/Applications.Core/secretStores/azdevopsgit", nil) | ||
secretsLoader.EXPECT(). | ||
LoadSecrets(ctx, "/planes/radius/local/resourcegroups/default/providers/Applications.Core/secretStores/azdevopsgit"). | ||
Times(1). | ||
Return(v20231001preview.SecretStoresClientListSecretsResponse{}, nil) | ||
driverWithSecrets.EXPECT(). | ||
Delete(ctx, recipedriver.DeleteOptions{ | ||
BaseOptions: recipedriver.BaseOptions{ | ||
Configuration: *envConfig, | ||
Recipe: recipeMetadata, | ||
Definition: *recipeDefinition, | ||
}, | ||
OutputResources: outputResources, | ||
}). | ||
Times(1). | ||
Return(nil) | ||
|
||
err := engine.Delete(ctx, DeleteOptions{ | ||
BaseOptions: BaseOptions{ | ||
Recipe: recipeMetadata, | ||
}, | ||
OutputResources: outputResources, | ||
}) | ||
require.NoError(t, err) | ||
} |
Oops, something went wrong.