Skip to content

Commit

Permalink
Make label validation optional for recipe functional tests (#5530)
Browse files Browse the repository at this point in the history
# 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

<!--
We strive to have all PR being opened based on an issue, where the
problem or feature have been discussed prior to implementation.
-->

#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
  • Loading branch information
vishwahiremat authored May 11, 2023
1 parent bb335a1 commit d1e2d4a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 18 deletions.
20 changes: 3 additions & 17 deletions test/functional/corerp/resources/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
11 changes: 11 additions & 0 deletions test/validation/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit d1e2d4a

Please sign in to comment.