Skip to content

Commit

Permalink
Validating labels of k8s resources in functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yetkin Timocin authored and Yetkin Timocin committed Sep 22, 2023
1 parent 5457752 commit 338e1ed
Show file tree
Hide file tree
Showing 30 changed files with 443 additions and 217 deletions.
3 changes: 2 additions & 1 deletion pkg/kubernetes/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ func MakeSelectorLabels(application string, resource string) map[string]string {
LabelRadiusResource: NormalizeResourceName(resource),
}
}

return map[string]string{
LabelRadiusApplication: application,
LabelRadiusApplication: NormalizeResourceName(application),
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/recipes/terraform/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (cfg *TerraformConfig) Save(ctx context.Context, workingDir string) error {
if err = os.WriteFile(getMainConfigFilePath(workingDir), jsonData, modeConfigFile); err != nil {
return fmt.Errorf("error creating file: %w", err)
}

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/ucp/store/apiserverstore/apiserverclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,15 @@ func resourceName(id resources.ID) string {
prefix = store.UCPScopePrefix
}

noramlizedName := normalizeName(id.Name())
normalizedName := normalizeName(id.Name())
// 211 = 253 (max length of Kubernetes Object name) - 40 (hex hash length) - 2 (dot separators)
maxResourceNameLen := 211 - len(prefix)
if len(noramlizedName) >= maxResourceNameLen {
noramlizedName = noramlizedName[:maxResourceNameLen]
if len(normalizedName) >= maxResourceNameLen {
normalizedName = normalizedName[:maxResourceNameLen]
}

// example: resource.resource1.ec291e26078b7ea8a74abfac82530005a0ecbf15
return fmt.Sprintf("%s.%s.%x", prefix, noramlizedName, hash)
return fmt.Sprintf("%s.%s.%x", prefix, normalizedName, hash)
}

func assignLabels(resource *ucpv1alpha1.Resource) labels.Set {
Expand Down
19 changes: 6 additions & 13 deletions test/functional/daprrp/resources/dapr_pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,10 @@ func Test_DaprPubSubBroker_Manual(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "dpsb-manual-app-ctnr"),
validation.NewK8sPodForResource(name, "dpsb-manual-redis").
ValidateLabels(false),
validation.NewK8sServiceForResource(name, "dpsb-manual-redis").
ValidateLabels(false),

validation.NewDaprComponent(name, "dpsb-manual").
ValidateLabels(false),
validation.NewK8sPodForResource("dpsb-manual-app-ctnr", name, true),
validation.NewK8sPodForResource("dpsb-manual-redis", name, false),
validation.NewK8sServiceForResource("dpsb-manual-redis", name, false),
validation.NewK8sDaprComponent("dpsb-manual", name, false),
},
},
},
Expand Down Expand Up @@ -113,11 +109,8 @@ func Test_DaprPubSubBroker_Recipe(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "dpsb-recipe-ctnr").
ValidateLabels(false),

validation.NewDaprComponent(name, "dpsb-recipe").
ValidateLabels(false),
validation.NewK8sPodForResource("dpsb-recipe-ctnr", name, false),
validation.NewK8sDaprComponent("dpsb-recipe", name, false),
},
},
},
Expand Down
14 changes: 4 additions & 10 deletions test/functional/daprrp/resources/dapr_secretstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ func Test_DaprSecretStore_Manual(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "gnrc-scs-ctnr"),

// Not sure why we skip validating the labels
validation.NewDaprComponent(name, "gnrc-scs-manual").
ValidateLabels(false),
validation.NewK8sPodForResource("gnrc-scs-ctnr", name, true),
validation.NewK8sDaprComponent("gnrc-scs-manual", name, false),
},
},
},
Expand Down Expand Up @@ -104,11 +101,8 @@ func Test_DaprSecretStore_Recipe(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "gnrc-scs-ctnr-recipe").
ValidateLabels(false),

validation.NewDaprComponent(name, "gnrc-scs-recipe").
ValidateLabels(false),
validation.NewK8sPodForResource("gnrc-scs-ctnr-recipe", name, false),
validation.NewK8sDaprComponent("gnrc-scs-recipe", name, false),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func Test_DaprServiceInvocation(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "dapr-frontend"),
validation.NewK8sPodForResource(name, "dapr-backend"),
validation.NewK8sPodForResource("dapr-frontend", name, true),
validation.NewK8sPodForResource("dapr-backend", name, true),
},
},
},
Expand Down
18 changes: 6 additions & 12 deletions test/functional/daprrp/resources/dapr_statestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,13 @@ func Test_DaprStateStore_Manual(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "dapr-sts-manual-ctnr"),
validation.NewK8sPodForResource("dapr-sts-manual-ctnr", name, true),

// Deployed as supporting resources using Kubernetes Bicep extensibility.
validation.NewK8sPodForResource(name, "dapr-sts-manual-redis").
ValidateLabels(false),
validation.NewK8sServiceForResource(name, "dapr-sts-manual-redis").
ValidateLabels(false),
validation.NewK8sPodForResource("dapr-sts-manual-redis", name, false),
validation.NewK8sServiceForResource("dapr-sts-manual-redis", name, false),

validation.NewDaprComponent(name, "dapr-sts-manual").
ValidateLabels(false),
validation.NewK8sDaprComponent("dapr-sts-manual", name, false),
},
},
},
Expand Down Expand Up @@ -115,11 +112,8 @@ func Test_DaprStateStore_Recipe(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "dapr-sts-recipe-ctnr").
ValidateLabels(false),

validation.NewDaprComponent(name, "dapr-sts-recipe").
ValidateLabels(false),
validation.NewK8sPodForResource("dapr-sts-recipe-ctnr", name, false),
validation.NewK8sDaprComponent("dapr-sts-recipe", name, false),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Test_MicrosoftSQL_Manual(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mssql-app-ctnr"),
validation.NewK8sPodForResource("mssql-app-ctnr", name, true),
},
},
},
Expand Down
12 changes: 6 additions & 6 deletions test/functional/datastoresrp/resources/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func Test_MongoDB_Manual(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mdb-us-app-ctnr").ValidateLabels(false),
validation.NewK8sPodForResource(name, "mdb-us-ctnr").ValidateLabels(false),
validation.NewK8sServiceForResource(name, "mdb-us-rte").ValidateLabels(false),
validation.NewK8sPodForResource("mdb-us-app-ctnr", name, false),
validation.NewK8sPodForResource("mdb-us-ctnr", name, false),
validation.NewK8sServiceForResource("mdb-us-rte", name, false),
},
},
},
Expand Down Expand Up @@ -114,7 +114,7 @@ func Test_MongoDB_Recipe(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mongodb-app-ctnr").ValidateLabels(false),
validation.NewK8sPodForResource("mongodb-app-ctnr", name, false),
},
},
},
Expand Down Expand Up @@ -170,7 +170,7 @@ func Test_MongoDB_RecipeParameters(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mdb-param-ctnr").ValidateLabels(false),
validation.NewK8sPodForResource("mdb-param-ctnr", name, false),
},
},
},
Expand Down Expand Up @@ -225,7 +225,7 @@ func Test_MongoDB_Recipe_ContextParameter(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mdb-ctx-ctnr").ValidateLabels(false),
validation.NewK8sPodForResource("mdb-ctx-ctnr", name, false),
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions test/functional/datastoresrp/resources/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func Test_Redis_Manual(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "rds-app-ctnr"),
validation.NewK8sPodForResource(name, "rds-ctnr"),
validation.NewK8sServiceForResource(name, "rds-rte"),
validation.NewK8sPodForResource("rds-app-ctnr", name, true),
validation.NewK8sPodForResource("rds-ctnr", name, true),
validation.NewK8sServiceForResource("rds-rte", name, true),
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions test/functional/datastoresrp/resources/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func Test_SQLDatabase_Manual(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "sql-app-ctnr"),
validation.NewK8sPodForResource(name, "sql-ctnr"),
validation.NewK8sServiceForResource(name, "sql-rte"),
validation.NewK8sPodForResource("sql-app-ctnr", name, true),
validation.NewK8sPodForResource("sql-ctnr", name, true),
validation.NewK8sServiceForResource("sql-rte", name, true),
},
},
},
Expand Down Expand Up @@ -112,8 +112,8 @@ func Test_SQLDatabase_Recipe(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "sql-recipe-app-ctnr").ValidateLabels(false),
validation.NewK8sPodForResource(name, "sql-recipe-resource").ValidateLabels(false),
validation.NewK8sPodForResource("sql-recipe-app-ctnr", name, false),
validation.NewK8sPodForResource("sql-recipe-resource", name, false),
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions test/functional/messagingrp/resources/rabbitmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func Test_RabbitMQ_Manual(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "rmq-app-ctnr"),
validation.NewK8sPodForResource(name, "rmq-ctnr"),
validation.NewK8sServiceForResource(name, "rmq-rte"),
validation.NewK8sPodForResource("rmq-app-ctnr", name, true),
validation.NewK8sPodForResource("rmq-ctnr", name, true),
validation.NewK8sServiceForResource("rmq-rte", name, true),
},
},
},
Expand Down Expand Up @@ -104,8 +104,8 @@ func Test_RabbitMQ_Recipe(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "rmq-recipe-app-ctnr").ValidateLabels(false),
validation.NewK8sPodForResource(name, "rmq-recipe-resource").ValidateLabels(false),
validation.NewK8sPodForResource("rmq-recipe-app-ctnr", name, false),
validation.NewK8sPodForResource("rmq-recipe-resource", name, false),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion test/functional/samples/tutorial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func Test_FirstApplicationSample(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(appName, "demo"),
validation.NewK8sPodForResource("demo", appName, true),
},
},
},
Expand Down
20 changes: 10 additions & 10 deletions test/functional/shared/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ func Test_CLI(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
"default-kubernetes-cli": {
validation.NewK8sPodForResource(name, "containera"),
validation.NewK8sPodForResource(name, "containerb"),
validation.NewK8sPodForResource("containera", name, true),
validation.NewK8sPodForResource("containerb", name, true),
},
},
},
Expand Down Expand Up @@ -481,8 +481,8 @@ func Test_CLI_JSON(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
"default-kubernetes-cli-json": {
validation.NewK8sPodForResource(name, "containera-json"),
validation.NewK8sPodForResource(name, "containerb-json"),
validation.NewK8sPodForResource("containera-json", name, true),
validation.NewK8sPodForResource("containerb-json", name, true),
},
},
},
Expand Down Expand Up @@ -521,8 +521,8 @@ func Test_CLI_Delete(t *testing.T) {
validation.ValidateObjectsRunning(ctx, t, options.K8sClient, options.DynamicClient, validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
"default-kubernetes-cli-with-resources": {
validation.NewK8sPodForResource(appName, "containera-app-with-resources"),
validation.NewK8sPodForResource(appName, "containerb-app-with-resources"),
validation.NewK8sPodForResource("containera-app-with-resources", appName, true),
validation.NewK8sPodForResource("containerb-app-with-resources", appName, true),
},
},
})
Expand Down Expand Up @@ -555,8 +555,8 @@ func Test_CLI_Delete(t *testing.T) {
validation.ValidateObjectsRunning(ctx, t, options.K8sClient, options.DynamicClient, validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
"default-kubernetes-cli-with-resources": {
validation.NewK8sPodForResource(appName, "containera-app-with-resources"),
validation.NewK8sPodForResource(appName, "containerb-app-with-resources"),
validation.NewK8sPodForResource("containera-app-with-resources", appName, true),
validation.NewK8sPodForResource("containerb-app-with-resources", appName, true),
},
},
})
Expand Down Expand Up @@ -615,8 +615,8 @@ func Test_CLI_DeploymentParameters(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
"default-kubernetes-cli-params": {
validation.NewK8sPodForResource(name, "containerc"),
validation.NewK8sPodForResource(name, "containerd"),
validation.NewK8sPodForResource("containerc", name, true),
validation.NewK8sPodForResource("containerd", name, true),
},
},
},
Expand Down
20 changes: 10 additions & 10 deletions test/functional/shared/mechanics/mechanics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func Test_RedeployWithAnotherResource(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mechanicsa"),
validation.NewK8sPodForResource("mechanicsa", name, true),
},
},
},
Expand Down Expand Up @@ -110,8 +110,8 @@ func Test_RedeployWithAnotherResource(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mechanicsb"),
validation.NewK8sPodForResource(name, "mechanicsc"),
validation.NewK8sPodForResource("mechanicsb", name, true),
validation.NewK8sPodForResource("mechanicsc", name, true),
},
},
},
Expand Down Expand Up @@ -145,7 +145,7 @@ func Test_RedeployWithUpdatedResourceUpdatesResource(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mechanicsd"),
validation.NewK8sPodForResource("mechanicsd", name, true),
},
},
},
Expand All @@ -168,7 +168,7 @@ func Test_RedeployWithUpdatedResourceUpdatesResource(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mechanicsd"),
validation.NewK8sPodForResource("mechanicsd", name, true),
},
},
},
Expand Down Expand Up @@ -215,7 +215,7 @@ func Test_RedeployWithTwoSeparateResourcesKeepsResource(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mechanicse"),
validation.NewK8sPodForResource("mechanicse", name, true),
},
},
},
Expand Down Expand Up @@ -243,8 +243,8 @@ func Test_RedeployWithTwoSeparateResourcesKeepsResource(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mechanicse"),
validation.NewK8sPodForResource(name, "mechanicsf"),
validation.NewK8sPodForResource("mechanicse", name, true),
validation.NewK8sPodForResource("mechanicsf", name, true),
},
},
},
Expand Down Expand Up @@ -293,8 +293,8 @@ func Test_CommunicationCycle(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, "mechanicsg"),
validation.NewK8sPodForResource(name, "cyclea"),
validation.NewK8sPodForResource("mechanicsg", name, true),
validation.NewK8sPodForResource("cyclea", name, true),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion test/functional/shared/resources/azure_connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Test_AzureConnections(t *testing.T) {
K8sObjects: &validation.K8sObjectSet{
Namespaces: map[string][]validation.K8sObject{
appNamespace: {
validation.NewK8sPodForResource(name, containerResourceName),
validation.NewK8sPodForResource(containerResourceName, name, true),
},
},
},
Expand Down
Loading

0 comments on commit 338e1ed

Please sign in to comment.