From d1e2d4a9b0371976f26cafccab2530cb7e4ce127 Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath <100623239+vishwahiremat@users.noreply.github.com> Date: Thu, 11 May 2023 10:12:53 -0700 Subject: [PATCH] Make label validation optional for recipe functional tests (#5530) # Description Core RP adds "radius.dev/application", "radius.dev/resource" labels on Radius containers. These labels aren't required in customer recipes but test framework pod validation currently expects these to present. - Added SkipLabelsValidation flag to the test step, using that we can disable labels validation. ## Issue reference https://github.com/project-radius/radius/issues/5095 ## Checklist Please make sure you've completed the relevant tasks for this PR, out of the following list: * [x] Code compiles correctly * [ ] Adds necessary unit tests for change * [ ] Adds necessary E2E tests for change * [ ] Unit tests passing * [x] Extended the documentation / Created issue for it --- .../corerp/resources/mongodb_test.go | 20 +--- .../corerp-resources-mongodb-recipe.bicep | 2 +- .../mongodb-recipe-kubernetes.bicep | 99 +++++++++++++++++++ test/validation/k8s.go | 11 +++ 4 files changed, 114 insertions(+), 18 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..d9b1dab31f 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,27 +140,13 @@ 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, "mongodb-recipe-app-ctnr").ValidateLabels(false), + validation.NewK8sPodForResource(name, "mongo-recipe-resource").ValidateLabels(false), }, }, }, 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 + } +} diff --git a/test/validation/k8s.go b/test/validation/k8s.go index 80c6699ad2..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{ @@ -300,6 +307,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) + } case <-ctx.Done(): assert.Fail(t, "timed out after waiting for services to be created") @@ -489,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()) {