From f8f37dc8f05757a4956de39d30adb6647fb80769 Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath Date: Fri, 5 May 2023 09:59:46 -0700 Subject: [PATCH 1/6] adding skiplabelsvalidation flag to test step --- test/functional/corerp/cli/cli_test.go | 4 ++-- test/functional/corerp/corerptest.go | 3 ++- test/validation/k8s.go | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/test/functional/corerp/cli/cli_test.go b/test/functional/corerp/cli/cli_test.go index 52ba8f3db7..a7a27fa59a 100644 --- a/test/functional/corerp/cli/cli_test.go +++ b/test/functional/corerp/cli/cli_test.go @@ -476,7 +476,7 @@ func Test_CLI_Delete(t *testing.T) { validation.NewK8sPodForResource(appName, "containerb-app-with-resources"), }, }, - }) + }, false) err = cli.ApplicationDelete(ctx, appName) require.NoErrorf(t, err, "failed to delete %s", appName) @@ -510,7 +510,7 @@ func Test_CLI_Delete(t *testing.T) { validation.NewK8sPodForResource(appName, "containerb-app-with-resources"), }, }, - }) + }, false) //ignore response for tests _, err = options.ManagementClient.DeleteResource(ctx, "Applications.Core/containers", "containerb-app-with-resources") diff --git a/test/functional/corerp/corerptest.go b/test/functional/corerp/corerptest.go index 481951a010..8fdcbbc82a 100644 --- a/test/functional/corerp/corerptest.go +++ b/test/functional/corerp/corerptest.go @@ -67,6 +67,7 @@ type TestStep struct { SkipKubernetesOutputResourceValidation bool SkipObjectValidation bool SkipResourceDeletion bool + SkipLabelValidation bool } type CoreRPTest struct { @@ -280,7 +281,7 @@ func (ct CoreRPTest) Test(t *testing.T) { } else { if step.K8sObjects != nil { t.Logf("validating creation of objects for %s", step.Executor.GetDescription()) - validation.ValidateObjectsRunning(ctx, t, ct.Options.K8sClient, ct.Options.DynamicClient, *step.K8sObjects) + validation.ValidateObjectsRunning(ctx, t, ct.Options.K8sClient, ct.Options.DynamicClient, *step.K8sObjects, step.SkipLabelValidation) t.Logf("finished validating creation of objects for %s", step.Executor.GetDescription()) } } diff --git a/test/validation/k8s.go b/test/validation/k8s.go index 80c6699ad2..60fd67e74a 100644 --- a/test/validation/k8s.go +++ b/test/validation/k8s.go @@ -267,7 +267,7 @@ func streamLogFile(ctx context.Context, podClient v1.PodInterface, pod corev1.Po } // ValidateObjectsRunning validates the namespaces and objects specified in each namespace are running -func ValidateObjectsRunning(ctx context.Context, t *testing.T, k8s *kubernetes.Clientset, dynamic dynamic.Interface, expected K8sObjectSet) { +func ValidateObjectsRunning(ctx context.Context, t *testing.T, k8s *kubernetes.Clientset, dynamic dynamic.Interface, expected K8sObjectSet, skipLabelValidation bool) { restMapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(k8s.DiscoveryClient)) for namespace, expectedObjects := range expected.Namespaces { log.Printf("validating objects in namespace %v", namespace) @@ -299,7 +299,7 @@ func ValidateObjectsRunning(ctx context.Context, t *testing.T, k8s *kubernetes.C } assert.NoErrorf(t, err, "could not list deployed resources of type %s in namespace %s", resourceGVR.GroupResource(), namespace) - validated = validated && matchesActualLabels(expectedInNamespace, deployedResources.Items) + validated = skipLabelValidation || (validated && matchesActualLabels(expectedInNamespace, deployedResources.Items)) } case <-ctx.Done(): assert.Fail(t, "timed out after waiting for services to be created") From 72bda7c17e1cc9214a36e8f0f29009e13b260f63 Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath Date: Sun, 7 May 2023 23:40:49 -0700 Subject: [PATCH 2/6] adding kubernetes recipe for mongodb --- .../corerp/resources/mongodb_test.go | 19 +--- .../corerp-resources-mongodb-recipe.bicep | 2 +- .../mongodb-recipe-kubernetes.bicep | 99 +++++++++++++++++++ 3 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 test/functional/corerp/resources/testdata/recipes/test-recipes/mongodb-recipe-kubernetes.bicep diff --git a/test/functional/corerp/resources/mongodb_test.go b/test/functional/corerp/resources/mongodb_test.go index 7d20ab1b07..85f7336e56 100644 --- a/test/functional/corerp/resources/mongodb_test.go +++ b/test/functional/corerp/resources/mongodb_test.go @@ -116,7 +116,7 @@ func Test_MongoDBUserSecrets(t *testing.T) { // the creation of a mongoDB from recipe // container using the mongoDB link to connect to the mongoDB resource func Test_MongoDB_Recipe(t *testing.T) { - t.Skipf("Enable this test once test flakiness is fixed. - https://github.com/project-radius/radius/issues/5053") + // template using recipe testdata/recipes/test-recipes/mongodb-recipe-kubernetes.bicep template := "testdata/corerp-resources-mongodb-recipe.bicep" name := "corerp-resources-mongodb-recipe" appNamespace := "corerp-resources-mongodb-recipe-app" @@ -140,30 +140,17 @@ func Test_MongoDB_Recipe(t *testing.T) { Type: validation.ContainersResource, App: name, }, - { - Name: "mongo-recipe-db", - Type: validation.MongoDatabasesResource, - App: name, - OutputResources: []validation.OutputResourceResponse{ - { - Provider: resourcemodel.ProviderAzure, - LocalID: rpv1.LocalIDAzureCosmosAccount, - }, - { - Provider: resourcemodel.ProviderAzure, - LocalID: rpv1.LocalIDAzureCosmosDBMongo, - }, - }, - }, }, }, K8sObjects: &validation.K8sObjectSet{ Namespaces: map[string][]validation.K8sObject{ appNamespace: { validation.NewK8sPodForResource(name, "mongodb-recipe-app-ctnr"), + validation.NewK8sPodForResource(name, "mongo-recipe-resource"), }, }, }, + SkipLabelValidation: true, }, }) diff --git a/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep b/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep index 39b41d854d..2e70df8f4a 100644 --- a/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep +++ b/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep @@ -23,7 +23,7 @@ resource env 'Applications.Core/environments@2022-03-15-privatepreview' = { recipes: { 'Applications.Link/mongoDatabases':{ mongodb: { - templatePath: 'radiusdev.azurecr.io/recipes/functionaltest/basic/mongodatabases/azure:1.0' + templatePath: 'radiusdev.azurecr.io/recipes/functionaltest/valuebacked/mongodatabases/kubernetes:1.0' } } } diff --git a/test/functional/corerp/resources/testdata/recipes/test-recipes/mongodb-recipe-kubernetes.bicep b/test/functional/corerp/resources/testdata/recipes/test-recipes/mongodb-recipe-kubernetes.bicep new file mode 100644 index 0000000000..f2d22f32e6 --- /dev/null +++ b/test/functional/corerp/resources/testdata/recipes/test-recipes/mongodb-recipe-kubernetes.bicep @@ -0,0 +1,99 @@ +import kubernetes as kubernetes { + kubeConfig: '' + namespace: context.runtime.kubernetes.namespace +} + +param context object + +@description('Admin username for the Mongo database. Default is "admin"') +param username string = 'admin' + +@description('Admin password for the Mongo database') +@secure() +param password string = newGuid() + +resource mongo 'apps/Deployment@v1' = { + metadata: { + name: 'mongo-recipe-resource' + } + spec: { + selector: { + matchLabels: { + app: 'mongo' + resource: context.resource.name + } + } + template: { + metadata: { + labels: { + app: 'mongo' + resource: context.resource.name + } + } + spec: { + containers: [ + { + name: 'mongo' + image: 'mongo:4.2' + ports: [ + { + containerPort: 27017 + } + ] + env: [ + { + name: 'MONGO_INITDB_ROOT_USERNAME' + value: username + } + { + name: 'MONGO_INITDB_ROOT_PASSWORD' + value: password + } + ] + } + ] + } + } + } +} + +resource svc 'core/Service@v1' = { + metadata: { + name: 'mongo-recipe-svc' + labels: { + name: 'mongo-recipe-svc' + } + } + spec: { + type: 'ClusterIP' + selector: { + app: 'mongo' + resource: context.resource.name + } + ports: [ + { + port: 27017 + } + ] + } +} + +output result object = { + // This workaround is needed because the deployment engine omits Kubernetes resources from its output. + // + // Once this gap is addressed, users won't need to do this. + resources: [ + '/planes/kubernetes/local/namespaces/${svc.metadata.namespace}/providers/core/Service/${svc.metadata.name}' + '/planes/kubernetes/local/namespaces/${mongo.metadata.namespace}/providers/apps/Deployment/${mongo.metadata.name}' + ] + values: { + host: '${svc.metadata.name}.${svc.metadata.namespace}.svc.cluster.local' + port: 27017 + + } + secrets: { + connectionString: 'mongodb://${username}:${password}@${svc.metadata.name}.${svc.metadata.namespace}.svc.cluster.local:27017' + username: username + password: password + } +} From dd787aed8a1c567d3f65095975cc53092f44df3f Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath Date: Mon, 8 May 2023 13:14:42 -0700 Subject: [PATCH 3/6] fixing nit --- test/validation/k8s.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/validation/k8s.go b/test/validation/k8s.go index 60fd67e74a..bd99262ba0 100644 --- a/test/validation/k8s.go +++ b/test/validation/k8s.go @@ -298,8 +298,9 @@ func ValidateObjectsRunning(ctx context.Context, t *testing.T, k8s *kubernetes.C deployedResources, err = dynamic.Resource(mapping.Resource).List(ctx, metav1.ListOptions{}) } assert.NoErrorf(t, err, "could not list deployed resources of type %s in namespace %s", resourceGVR.GroupResource(), namespace) - - validated = skipLabelValidation || (validated && matchesActualLabels(expectedInNamespace, deployedResources.Items)) + if !skipLabelValidation { + validated = validated && matchesActualLabels(expectedInNamespace, deployedResources.Items) + } } case <-ctx.Done(): assert.Fail(t, "timed out after waiting for services to be created") From 47db19173db150018cd4cb884e807393d0157498 Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath Date: Mon, 8 May 2023 14:47:25 -0700 Subject: [PATCH 4/6] Trigger Build From b9788dc343af5dcd01488229b6131aad436d6942 Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath Date: Tue, 9 May 2023 22:39:47 -0700 Subject: [PATCH 5/6] move skip_label_validation to pod level --- test/functional/corerp/cli/cli_test.go | 4 ++-- test/functional/corerp/corerptest.go | 3 +-- .../corerp/resources/mongodb_test.go | 5 ++--- .../corerp-resources-mongodb-recipe.bicep | 2 +- test/validation/k8s.go | 18 ++++++++++++++---- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/test/functional/corerp/cli/cli_test.go b/test/functional/corerp/cli/cli_test.go index a7a27fa59a..52ba8f3db7 100644 --- a/test/functional/corerp/cli/cli_test.go +++ b/test/functional/corerp/cli/cli_test.go @@ -476,7 +476,7 @@ func Test_CLI_Delete(t *testing.T) { validation.NewK8sPodForResource(appName, "containerb-app-with-resources"), }, }, - }, false) + }) err = cli.ApplicationDelete(ctx, appName) require.NoErrorf(t, err, "failed to delete %s", appName) @@ -510,7 +510,7 @@ func Test_CLI_Delete(t *testing.T) { validation.NewK8sPodForResource(appName, "containerb-app-with-resources"), }, }, - }, false) + }) //ignore response for tests _, err = options.ManagementClient.DeleteResource(ctx, "Applications.Core/containers", "containerb-app-with-resources") diff --git a/test/functional/corerp/corerptest.go b/test/functional/corerp/corerptest.go index 8fdcbbc82a..481951a010 100644 --- a/test/functional/corerp/corerptest.go +++ b/test/functional/corerp/corerptest.go @@ -67,7 +67,6 @@ type TestStep struct { SkipKubernetesOutputResourceValidation bool SkipObjectValidation bool SkipResourceDeletion bool - SkipLabelValidation bool } type CoreRPTest struct { @@ -281,7 +280,7 @@ func (ct CoreRPTest) Test(t *testing.T) { } else { if step.K8sObjects != nil { t.Logf("validating creation of objects for %s", step.Executor.GetDescription()) - validation.ValidateObjectsRunning(ctx, t, ct.Options.K8sClient, ct.Options.DynamicClient, *step.K8sObjects, step.SkipLabelValidation) + validation.ValidateObjectsRunning(ctx, t, ct.Options.K8sClient, ct.Options.DynamicClient, *step.K8sObjects) t.Logf("finished validating creation of objects for %s", step.Executor.GetDescription()) } } diff --git a/test/functional/corerp/resources/mongodb_test.go b/test/functional/corerp/resources/mongodb_test.go index 85f7336e56..d9b1dab31f 100644 --- a/test/functional/corerp/resources/mongodb_test.go +++ b/test/functional/corerp/resources/mongodb_test.go @@ -145,12 +145,11 @@ func Test_MongoDB_Recipe(t *testing.T) { K8sObjects: &validation.K8sObjectSet{ Namespaces: map[string][]validation.K8sObject{ appNamespace: { - validation.NewK8sPodForResource(name, "mongodb-recipe-app-ctnr"), - validation.NewK8sPodForResource(name, "mongo-recipe-resource"), + validation.NewK8sPodForResource(name, "mongodb-recipe-app-ctnr").ValidateLabels(false), + validation.NewK8sPodForResource(name, "mongo-recipe-resource").ValidateLabels(false), }, }, }, - SkipLabelValidation: true, }, }) diff --git a/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep b/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep index 2e70df8f4a..bcec3beea3 100644 --- a/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep +++ b/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep @@ -23,7 +23,7 @@ resource env 'Applications.Core/environments@2022-03-15-privatepreview' = { recipes: { 'Applications.Link/mongoDatabases':{ mongodb: { - templatePath: 'radiusdev.azurecr.io/recipes/functionaltest/valuebacked/mongodatabases/kubernetes:1.0' + templatePath: 'vishwaradius.azurecr.io/recipes/mongo-test:1.0' } } } diff --git a/test/validation/k8s.go b/test/validation/k8s.go index bd99262ba0..0e2a6b7ff2 100644 --- a/test/validation/k8s.go +++ b/test/validation/k8s.go @@ -48,6 +48,7 @@ type K8sObject struct { GroupVersionResource schema.GroupVersionResource Labels map[string]string Kind string + SkipLabelValidation bool } func NewK8sPodForResource(application string, name string) K8sObject { @@ -64,6 +65,12 @@ func NewK8sPodForResource(application string, name string) K8sObject { } } +func (k K8sObject) ValidateLabels(validate bool) K8sObject { + copy := k + copy.SkipLabelValidation = !validate + return copy +} + func NewK8sHTTPProxyForResource(application string, name string) K8sObject { return K8sObject{ GroupVersionResource: schema.GroupVersionResource{ @@ -267,7 +274,7 @@ func streamLogFile(ctx context.Context, podClient v1.PodInterface, pod corev1.Po } // ValidateObjectsRunning validates the namespaces and objects specified in each namespace are running -func ValidateObjectsRunning(ctx context.Context, t *testing.T, k8s *kubernetes.Clientset, dynamic dynamic.Interface, expected K8sObjectSet, skipLabelValidation bool) { +func ValidateObjectsRunning(ctx context.Context, t *testing.T, k8s *kubernetes.Clientset, dynamic dynamic.Interface, expected K8sObjectSet) { restMapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(k8s.DiscoveryClient)) for namespace, expectedObjects := range expected.Namespaces { log.Printf("validating objects in namespace %v", namespace) @@ -298,9 +305,9 @@ func ValidateObjectsRunning(ctx context.Context, t *testing.T, k8s *kubernetes.C deployedResources, err = dynamic.Resource(mapping.Resource).List(ctx, metav1.ListOptions{}) } assert.NoErrorf(t, err, "could not list deployed resources of type %s in namespace %s", resourceGVR.GroupResource(), namespace) - if !skipLabelValidation { - validated = validated && matchesActualLabels(expectedInNamespace, deployedResources.Items) - } + + validated = validated && matchesActualLabels(expectedInNamespace, deployedResources.Items) + } case <-ctx.Done(): assert.Fail(t, "timed out after waiting for services to be created") @@ -490,6 +497,9 @@ func matchesActualLabels(expectedResources []K8sObject, actualResources []unstru remaining := []K8sObject{} for _, expectedResource := range expectedResources { + if expectedResource.SkipLabelValidation { + continue + } resourceExists := false for idx, actualResource := range actualResources { if labelsEqual(expectedResource.Labels, actualResource.GetLabels()) { From 1681ece52e417d88156a34c87b4d08a81ba8c05b Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath Date: Tue, 9 May 2023 22:43:49 -0700 Subject: [PATCH 6/6] update acr to radiusacr --- .../resources/testdata/corerp-resources-mongodb-recipe.bicep | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep b/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep index bcec3beea3..2e70df8f4a 100644 --- a/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep +++ b/test/functional/corerp/resources/testdata/corerp-resources-mongodb-recipe.bicep @@ -23,7 +23,7 @@ resource env 'Applications.Core/environments@2022-03-15-privatepreview' = { recipes: { 'Applications.Link/mongoDatabases':{ mongodb: { - templatePath: 'vishwaradius.azurecr.io/recipes/mongo-test:1.0' + templatePath: 'radiusdev.azurecr.io/recipes/functionaltest/valuebacked/mongodatabases/kubernetes:1.0' } } }