Skip to content

Commit

Permalink
add func test
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmimsft committed Mar 25, 2024
1 parent 060afba commit b3f0891
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 16 deletions.
89 changes: 89 additions & 0 deletions test/functional/shared/resources/recipe_terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,95 @@ func Test_TerraformRecipe_KubernetesRedis(t *testing.T) {
test.Test(t)
}

// Test_TerraformRecipe_KubernetesPostgres covers the following terraform recipe scenario:
//
// - Create an extender resource using a Terraform recipe that deploys Postgres on Kubernetes.
// - The recipe deployment creates a Kubernetes deployment and a Kubernetes service and a postgres db.
func Test_TerraformRecipe_KubernetesPostgres(t *testing.T) {
template := "testdata/corerp-resources-terraform-postgres.bicep"
name := "corerp-resources-terraform-pg"
appName := "corerp-resources-terraform-pg-app"
envName := "corerp-resources-terraform-pg-env"
userName := "postgres"
password := "abc-123-hgd-@#$'"

secretSuffix, err := getSecretSuffix("/planes/radius/local/resourcegroups/kind-radius/providers/Applications.Core/extenders/"+name, envName, appName)
require.NoError(t, err)

test := shared.NewRPTest(t, name, []shared.TestStep{
{
Executor: step.NewDeployExecutor(template, testutil.GetTerraformRecipeModuleServerURL(), "userName="+userName, "password="+password),
RPResources: &validation.RPResourceSet{
Resources: []validation.RPResource{
{
Name: envName,
Type: validation.EnvironmentsResource,
},
{
Name: appName,
Type: validation.ApplicationsResource,
},
{
Name: name,
Type: validation.ExtendersResource,
App: appName,
OutputResources: []validation.OutputResourceResponse{
{ID: "/planes/kubernetes/local/namespaces/corerp-resources-terraform-pg-app/providers/apps/Deployment/postgres"},
{ID: "/planes/kubernetes/local/namespaces/corerp-resources-terraform-pg-app/providers/core/Service/postgres"},
},
},
},
},
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appName: {
validation.NewK8sServiceForResource(appName, "postgres").
ValidateLabels(false),
validation.NewK8sPodForResource(name, "postgres").
ValidateLabels(false),
},
secretNamespace: {
validation.NewK8sSecretForResourceWithResourceName(secretPrefix + secretSuffix).
ValidateLabels(false),
},
},
},
PostStepVerify: func(ctx context.Context, t *testing.T, test shared.RPTest) {
secret, err := test.Options.K8sClient.CoreV1().Secrets(secretNamespace).
Get(ctx, secretPrefix+secretSuffix, metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t, secretNamespace, secret.Namespace)
require.Equal(t, secretPrefix+secretSuffix, secret.Name)

pg, err := test.Options.ManagementClient.ShowResource(ctx, "Applications.Core/extenders", name)
require.NoError(t, err)
require.NotNil(t, pg)
status := pg.Properties["status"].(map[string]any)
recipe := status["recipe"].(map[string]interface{})
require.Equal(t, "terraform", recipe["templateKind"].(string))
expectedTemplatePath := strings.Replace(testutil.GetTerraformRecipeModuleServerURL()+"/postgres.zip", "moduleServer=", "", 1)
require.Equal(t, expectedTemplatePath, recipe["templatePath"].(string))

// At present, it is not possible to verify the template version in functional tests
// This is verified by UTs though

// Manually delete Kubernetes the secret that stores the Terraform state file now. The next step in the test will be the deletion
// of the portable resource that uses this secret for Terraform recipe. This is to verify that the test and portable resource
// deletion will not fail even though the secret is already deleted.
err = test.Options.K8sClient.CoreV1().Secrets(secretNamespace).Delete(ctx, secretPrefix+secretSuffix, metav1.DeleteOptions{})
require.NoError(t, err)
},
},
})

test.PostDeleteVerify = func(ctx context.Context, t *testing.T, test shared.RPTest) {
resourceID := "/planes/radius/local/resourcegroups/kind-radius/providers/Applications.Core/extenders/" + name
testSecretDeletion(t, ctx, test, appName, envName, resourceID)
}

test.Test(t)
}

func Test_TerraformRecipe_Context(t *testing.T) {
template := "testdata/corerp-resources-terraform-context.bicep"
name := "corerp-resources-terraform-context"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import radius as radius

@description('The URL of the server hosting test Terraform modules.')
param moduleServer string

@description('Username for Postgres db.')
param userName string

@description('Password for Postgres db.')
@secure()
param password string

resource env 'Applications.Core/environments@2023-10-01-preview' = {
name: 'corerp-resources-terraform-pg-env'
location: 'global'
properties: {
compute: {
kind: 'kubernetes'
resourceId: 'self'
namespace: 'corerp-resources-terraform-pg-env'
}
recipeConfig: {
terraform:{
providers:{
postgresql:[{
username: userName
port: 5432
password: password
sslmode: 'disable'
}]
}
}
env: {
PGHOST: 'postgres.corerp-resources-terraform-pg-app.svc.cluster.local'
}
}
recipes: {
'Applications.Core/extenders': {
defaultpostgres: {
templateKind: 'terraform'
templatePath:'${moduleServer}/postgres.zip'
}
}
}
}
}

resource app 'Applications.Core/applications@2023-10-01-preview' = {
name: 'corerp-resources-terraform-pg-app'
location: 'global'
properties: {
environment: env.id
extensions: [
{
kind: 'kubernetesNamespace'
namespace: 'corerp-resources-terraform-pg-app'
}
]
}
}

resource pgsapp 'Applications.Core/extenders@2023-10-01-preview' = {
name: 'pgs-resources-terraform-pgsapp'
properties: {
application: app.id
environment: env.id
recipe: {
name: 'defaultpostgres'
parameters: {
password: password
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ terraform {
resource "kubernetes_deployment" "postgres" {
metadata {
name = "postgres"
namespace = "postgresns"
namespace = var.context.runtime.kubernetes.namespace
}

spec {
Expand Down Expand Up @@ -53,7 +53,7 @@ resource "kubernetes_deployment" "postgres" {
resource "kubernetes_service" "postgres" {
metadata {
name = "postgres"
namespace = "postgresns"
namespace = var.context.runtime.kubernetes.namespace
}

spec {
Expand All @@ -68,17 +68,12 @@ resource "kubernetes_service" "postgres" {
}
}

variable "port" {
default = 5432
}

provider "postgresql" {
host = var.host
port = var.port
password = var.password
sslmode = "disable"
resource "time_sleep" "wait_10_seconds" {
depends_on = [kubernetes_service.postgres]
create_duration = "10s"
}

resource postgresql_database "pg_db_test" {
depends_on = [time_sleep.wait_10_seconds]
name = "pg_db_test"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

variable "context" {
description = "This variable contains Radius recipe context."
type = any
}

variable "password" {
description = "The password for the PostgreSQL database"
type = string
}

variable "host" {
default = "localhost"
}

0 comments on commit b3f0891

Please sign in to comment.