From 8a88ddc6ebcd1ac701e68f86156f44ae44c40572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Sm=C3=A4do?= Date: Wed, 6 Nov 2024 00:53:39 +0100 Subject: [PATCH] Add additional checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomáš Smädo --- .../scs_0212_v1_registry_standard_test.go | 110 ++++++++++++------ 1 file changed, 72 insertions(+), 38 deletions(-) diff --git a/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0212_v1_registry_standard_test.go b/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0212_v1_registry_standard_test.go index 6c39f414b..a38f480c3 100644 --- a/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0212_v1_registry_standard_test.go +++ b/Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0212_v1_registry_standard_test.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "log" + "os" + "strings" "testing" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -11,7 +13,7 @@ import ( "k8s.io/client-go/rest" ) -// list of common Harbor components. +// list of common harbor components. var HarborComponentNames = []string{ "harbor-core", "harbor-db", @@ -21,8 +23,20 @@ var HarborComponentNames = []string{ "nginx", } +// other registries +var OtherRegistries = []string{ + "docker-registry", + "quay", + "jfrog", + "artifacthub", + "dragonfly", + "keppel", + "nexus", + "kraken", +} + func Test_scs_0212_registry_standard_test(t *testing.T) { - // Set up the Kubernetes client + // set up the kubernetes client config, err := rest.InClusterConfig() if err != nil { log.Fatalf("Failed to create rest config: %v", err) @@ -33,68 +47,88 @@ func Test_scs_0212_registry_standard_test(t *testing.T) { log.Fatalf("Failed to create Kubernetes client: %v", err) } + harborComponentsFound := make(map[string]bool) + otherRegistryFound := false + fmt.Println("Checking for Harbor components in Deployments...") - if err := checkDeployments(clientset); err != nil { - log.Fatalf("Error checking deployments: %v", err) - } + otherRegistryFound = checkHarborDeployments(clientset, harborComponentsFound) || otherRegistryFound fmt.Println("Checking for Harbor components in Services...") - if err := checkServices(clientset); err != nil { - log.Fatalf("Error checking services: %v", err) + otherRegistryFound = checkHarborServices(clientset, harborComponentsFound) || otherRegistryFound + + if otherRegistryFound { + log.Fatalf("Non-Harbor registry detected in the cluster. Failing test.") } - fmt.Println("Harbor check completed.") + // ensure all harbor components are found + for _, component := range HarborComponentNames { + if !harborComponentsFound[component] { + fmt.Printf("Harbor component missing: %s\n", component) + log.Fatalf("Harbor registry not fully deployed. Failing test.") + } + } + + fmt.Println("All Harbor components are deployed, and no other registries are found. Test passed.") + os.Exit(0) } -// check deployments for the registry -func checkDeployments(clientset *kubernetes.Clientset) error { + +// checkHarborDeployments checks if required harbor components are present in deployments +// and ensures no other registries are found. +func checkHarborDeployments(clientset *kubernetes.Clientset, harborComponentsFound map[string]bool) bool { + otherRegistryFound := false deployments, err := clientset.AppsV1().Deployments("").List(context.TODO(), v1.ListOptions{}) - harborDeployments := 0 if err != nil { - return fmt.Errorf("failed to list deployments: %v", err) + log.Fatalf("Failed to list deployments: %v", err) } for _, deployment := range deployments.Items { - for _, componentName := range HarborComponentNames { - if containsString(deployment.Name, componentName) { + // check for Harbor components + for _, component := range HarborComponentNames { + if strings.Contains(deployment.Name, component) { + harborComponentsFound[component] = true fmt.Printf("Found Harbor deployment: %s in namespace: %s\n", deployment.Name, deployment.Namespace) - harborDeployments++ + } + } + + // check for other registries + for _, registry := range OtherRegistries { + if strings.Contains(deployment.Name, registry) { + otherRegistryFound = true + fmt.Printf("Found non-Harbor registry deployment: %s in namespace: %s\n", deployment.Name, deployment.Namespace) } } } - if harborDeployments > 0 { - fmt.Printf("Harbor deployments found\n") - } else { - fmt.Printf("Harbor was not found in deployments\n") - } - return nil + + return otherRegistryFound } -// check services for the registry components -func checkServices(clientset *kubernetes.Clientset) error { +// checks if required harbor components are present in services +// and ensures no other registries are found. +func checkHarborServices(clientset *kubernetes.Clientset, harborComponentsFound map[string]bool) bool { + otherRegistryFound := false services, err := clientset.CoreV1().Services("").List(context.TODO(), v1.ListOptions{}) - harborServices := 0 if err != nil { - return fmt.Errorf("failed to list services: %v", err) + log.Fatalf("Failed to list services: %v", err) } for _, service := range services.Items { - for _, componentName := range HarborComponentNames { - if containsString(service.Name, componentName) { + // check for harbor components + for _, component := range HarborComponentNames { + if strings.Contains(service.Name, component) { + harborComponentsFound[component] = true fmt.Printf("Found Harbor service: %s in namespace: %s\n", service.Name, service.Namespace) - harborServices++ + } + } + + // check for other registries + for _, registry := range OtherRegistries { + if strings.Contains(service.Name, registry) { + otherRegistryFound = true + fmt.Printf("Found non-Harbor registry service: %s in namespace: %s\n", service.Name, service.Namespace) } } } - if harborServices > 0 { - fmt.Printf("Harbor services found\n") - } else { - fmt.Printf("Harbor was not found services\n") - } - return nil -} -// containsString checks if a string is contained in another string (case-insensitive). -func containsString(str, substr string) bool { - return len(str) > 0 && len(substr) > 0 && (str == substr || (len(str) > len(substr) && str[:len(substr)] == substr)) + return otherRegistryFound }