Skip to content

Commit

Permalink
move skip_label_validation to pod level
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwahiremat committed May 10, 2023
1 parent 47db191 commit b9788dc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions test/functional/corerp/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions test/functional/corerp/corerptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type TestStep struct {
SkipKubernetesOutputResourceValidation bool
SkipObjectValidation bool
SkipResourceDeletion bool
SkipLabelValidation bool
}

type CoreRPTest struct {
Expand Down Expand Up @@ -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())
}
}
Expand Down
5 changes: 2 additions & 3 deletions test/functional/corerp/resources/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})

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/valuebacked/mongodatabases/kubernetes:1.0'
templatePath: 'vishwaradius.azurecr.io/recipes/mongo-test:1.0'
}
}
}
Expand Down
18 changes: 14 additions & 4 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 @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit b9788dc

Please sign in to comment.