Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
Signed-off-by: vinayada1 <[email protected]>
  • Loading branch information
vinayada1 committed Feb 12, 2024
1 parent 3fd989f commit 3f5cd64
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 65 deletions.
34 changes: 0 additions & 34 deletions pkg/recipes/driver/bicep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,40 +378,6 @@ func Test_Bicep_PrepareRecipeResponse_EmptyResult(t *testing.T) {
require.Equal(t, expectedResponse, actualResponse)
}

func Test_Bicep_Execute_SimulatedEnvironment(t *testing.T) {
ts := registrytest.NewFakeRegistryServer(t)
t.Cleanup(ts.CloseServer)

opts := ExecuteOptions{
BaseOptions: BaseOptions{
Configuration: recipes.Configuration{
Runtime: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "test-namespace",
},
},
Simulated: true,
},
Recipe: recipes.ResourceMetadata{
EnvironmentID: "/subscriptions/test-sub/resourceGroups/test-group/providers/Applications.Core/environments/test-env",
Name: "test-recipe",
ResourceID: "/subscriptions/test-sub/resourceGroups/test-group/providers/Applications.Datastores/mongoDatabases/test-db",
},
Definition: recipes.EnvironmentDefinition{
Name: "test-recipe",
Driver: recipes.TemplateKindBicep,
TemplatePath: ts.TestImageURL,
ResourceType: "Applications.Datastores/mongoDatabases",
},
},
}
ctx := testcontext.New(t)
d := &bicepDriver{RegistryClient: ts.TestServer.Client()}
recipesOutput, err := d.Execute(ctx, opts)
require.NoError(t, err)
require.Nil(t, recipesOutput)
}

func setupDeleteInputs(t *testing.T) (bicepDriver, *processors.MockResourceClient) {
ctrl := gomock.NewController(t)
client := processors.NewMockResourceClient(ctrl)
Expand Down
22 changes: 0 additions & 22 deletions pkg/recipes/driver/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,28 +307,6 @@ func Test_Terraform_Execute_MissingARMRequestContext_Panics(t *testing.T) {
})
}

func Test_Terraform_Execute_SimulatedEnvironment(t *testing.T) {
ctx := testcontext.New(t)
armCtx := &v1.ARMRequestContext{
OperationID: uuid.New(),
}
ctx = v1.WithARMRequestContext(ctx, armCtx)

_, driver := setup(t)
envConfig, recipeMetadata, envRecipe := buildTestInputs()
envConfig.Simulated = true

recipeOutput, err := driver.Execute(ctx, ExecuteOptions{
BaseOptions: BaseOptions{
Configuration: envConfig,
Recipe: recipeMetadata,
Definition: envRecipe,
},
})
require.NoError(t, err)
require.Nil(t, recipeOutput)
}

func TestTerraformDriver_GetRecipeMetadata_Success(t *testing.T) {
ctx := testcontext.New(t)
armCtx := &v1.ARMRequestContext{
Expand Down
2 changes: 2 additions & 0 deletions pkg/recipes/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ func (e *engine) Delete(ctx context.Context, opts DeleteOptions) error {
// deleteCore function is the core logic of the Delete function.
// Any changes to the core logic of the Delete function should be made here.
func (e *engine) deleteCore(ctx context.Context, recipe recipes.ResourceMetadata, outputResources []rpv1.OutputResource) (*recipes.EnvironmentDefinition, error) {
logger := ucplog.FromContextOrDiscard(ctx)
configuration, err := e.options.ConfigurationLoader.LoadConfiguration(ctx, recipe)
if err != nil {
return nil, err
}

if configuration.Simulated {
logger.Info("simulated environment enabled, skipping deleting resources")
return nil, nil
}

Expand Down
169 changes: 160 additions & 9 deletions pkg/recipes/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,55 @@ func Test_Engine_Execute_Success(t *testing.T) {
require.Equal(t, result, recipeResult)
}

func Test_Engine_Execute_SimulatedEnv_Success(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",
},
}

prevState := []string{
"/subscriptions/test-sub/resourceGroups/test-rg/providers/System.Test/testResources/test1",
}

envConfig := &recipes.Configuration{
Runtime: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "default",
},
},
Providers: datamodel.Providers{
Azure: datamodel.ProvidersAzure{
Scope: "scope",
},
},
Simulated: true,
}

ctx := testcontext.New(t)
engine, configLoader, _ := setup(t)

configLoader.EXPECT().
LoadConfiguration(ctx, recipeMetadata).
Times(1).
Return(envConfig, nil)

// Note: LoadRecipe is not called as the environment is simulated

result, err := engine.Execute(ctx, ExecuteOptions{
BaseOptions: BaseOptions{
Recipe: recipeMetadata,
},
PreviousState: prevState,
})
require.NoError(t, err)
require.Nil(t, result)
}

func Test_Engine_Execute_Failure(t *testing.T) {
recipeMetadata := recipes.ResourceMetadata{
Name: "mongo-azure",
Expand Down Expand Up @@ -264,6 +313,19 @@ func Test_Engine_InvalidDriver(t *testing.T) {
ctx := testcontext.New(t)
engine, configLoader, _ := setup(t)

envConfig := &recipes.Configuration{
Runtime: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "default",
},
},
Providers: datamodel.Providers{
Azure: datamodel.ProvidersAzure{
Scope: "scope",
},
},
}

recipeDefinition := &recipes.EnvironmentDefinition{
Driver: "invalid",
TemplatePath: "ghcr.io/radius-project/dev/recipes/functionaltest/basic/mongodatabases/azure:1.0",
Expand All @@ -282,6 +344,12 @@ func Test_Engine_InvalidDriver(t *testing.T) {
prevState := []string{
"/subscriptions/test-sub/resourceGroups/test-rg/providers/System.Test/testResources/test1",
}

configLoader.EXPECT().
LoadConfiguration(ctx, recipeMetadata).
Times(1).
Return(envConfig, nil)

configLoader.EXPECT().
LoadRecipe(ctx, &recipeMetadata).
Times(1).
Expand All @@ -299,6 +367,20 @@ func Test_Engine_InvalidDriver(t *testing.T) {
func Test_Engine_Lookup_Error(t *testing.T) {
ctx := testcontext.New(t)
engine, configLoader, _ := setup(t)

envConfig := &recipes.Configuration{
Runtime: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "default",
},
},
Providers: datamodel.Providers{
Azure: datamodel.ProvidersAzure{
Scope: "scope",
},
},
}

recipeMetadata := recipes.ResourceMetadata{
Name: "mongo-azure",
ApplicationID: "/planes/radius/local/resourcegroups/test-rg/providers/applications.core/applications/app1",
Expand All @@ -311,6 +393,12 @@ func Test_Engine_Lookup_Error(t *testing.T) {
prevState := []string{
"/subscriptions/test-sub/resourceGroups/test-rg/providers/System.Test/testResources/test1",
}

configLoader.EXPECT().
LoadConfiguration(ctx, recipeMetadata).
Times(1).
Return(envConfig, nil)

configLoader.EXPECT().
LoadRecipe(ctx, &recipeMetadata).
Times(1).
Expand All @@ -328,6 +416,7 @@ func Test_Engine_Lookup_Error(t *testing.T) {
func Test_Engine_Load_Error(t *testing.T) {
ctx := testcontext.New(t)
engine, configLoader, _ := setup(t)

recipeMetadata := recipes.ResourceMetadata{
Name: "mongo-azure",
ApplicationID: "/planes/radius/local/resourcegroups/test-rg/providers/applications.core/applications/app1",
Expand All @@ -340,15 +429,7 @@ func Test_Engine_Load_Error(t *testing.T) {
prevState := []string{
"/subscriptions/test-sub/resourceGroups/test-rg/providers/System.Test/testResources/test1",
}
recipeDefinition := &recipes.EnvironmentDefinition{
Driver: recipes.TemplateKindBicep,
TemplatePath: "ghcr.io/radius-project/dev/recipes/functionaltest/basic/mongodatabases/azure:1.0",
ResourceType: "Applications.Datastores/mongoDatabases",
}
configLoader.EXPECT().
LoadRecipe(ctx, &recipeMetadata).
Times(1).
Return(recipeDefinition, nil)

configLoader.EXPECT().
LoadConfiguration(ctx, recipeMetadata).
Times(1).
Expand Down Expand Up @@ -413,6 +494,40 @@ func Test_Engine_Delete_Success(t *testing.T) {
require.NoError(t, err)
}

func Test_Engine_Delete_SimulatedEnv_Success(t *testing.T) {
recipeMetadata, _, outputResources := getRecipeInputs()

envConfig := &recipes.Configuration{
Runtime: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "default",
},
},
Providers: datamodel.Providers{
Azure: datamodel.ProvidersAzure{
Scope: "scope",
},
},
Simulated: true,
}

ctx := testcontext.New(t)
engine, configLoader, _ := setup(t)

configLoader.EXPECT().
LoadConfiguration(ctx, recipeMetadata).
Times(1).
Return(envConfig, nil)

err := engine.Delete(ctx, DeleteOptions{
BaseOptions: BaseOptions{
Recipe: recipeMetadata,
},
OutputResources: outputResources,
})
require.NoError(t, err)
}

func Test_Engine_Delete_Error(t *testing.T) {
recipeMetadata, recipeDefinition, outputResources := getRecipeInputs()

Expand Down Expand Up @@ -471,6 +586,24 @@ func Test_Delete_InvalidDriver(t *testing.T) {
ctx := testcontext.New(t)
engine, configLoader, _ := setup(t)

envConfig := &recipes.Configuration{
Runtime: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "default",
},
},
Providers: datamodel.Providers{
Azure: datamodel.ProvidersAzure{
Scope: "scope",
},
},
}

configLoader.EXPECT().
LoadConfiguration(ctx, recipeMetadata).
Times(1).
Return(envConfig, nil)

configLoader.EXPECT().
LoadRecipe(ctx, &recipeMetadata).
Times(1).
Expand All @@ -490,6 +623,24 @@ func Test_Delete_Lookup_Error(t *testing.T) {
engine, configLoader, _ := setup(t)
recipeMetadata, _, outputResources := getRecipeInputs()

envConfig := &recipes.Configuration{
Runtime: recipes.RuntimeConfiguration{
Kubernetes: &recipes.KubernetesRuntime{
Namespace: "default",
},
},
Providers: datamodel.Providers{
Azure: datamodel.ProvidersAzure{
Scope: "scope",
},
},
}

configLoader.EXPECT().
LoadConfiguration(ctx, recipeMetadata).
Times(1).
Return(envConfig, nil)

configLoader.EXPECT().
LoadRecipe(ctx, &recipeMetadata).
Times(1).
Expand Down

0 comments on commit 3f5cd64

Please sign in to comment.