From 52cd0727c874221aaf791d76d77ffeddaa944974 Mon Sep 17 00:00:00 2001 From: Brandon Palm Date: Tue, 3 Dec 2024 10:59:54 -0600 Subject: [PATCH] Test grpcurl --- pkg/autodiscover/autodiscover.go | 5 +++++ pkg/provider/catalogsources.go | 13 +++++++++++-- pkg/provider/provider.go | 2 ++ tests/operator/helper.go | 5 ----- tests/operator/suite.go | 15 ++++++++++----- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/pkg/autodiscover/autodiscover.go b/pkg/autodiscover/autodiscover.go index 3da30e72a..948020fa3 100644 --- a/pkg/autodiscover/autodiscover.go +++ b/pkg/autodiscover/autodiscover.go @@ -92,6 +92,7 @@ type DiscoveredTestData struct { RoleBindings []rbacv1.RoleBinding // Contains all rolebindings from all namespaces Roles []rbacv1.Role // Contains all roles from all namespaces Services []*corev1.Service + AllServices []*corev1.Service ServiceAccounts []*corev1.ServiceAccount AllServiceAccounts []*corev1.ServiceAccount Hpas []*scalingv1.HorizontalPodAutoscaler @@ -279,6 +280,10 @@ func DoAutoDiscover(config *configuration.TestConfiguration) DiscoveredTestData if err != nil { log.Fatal("Cannot get list of services, err: %v", err) } + data.AllServices, err = getServices(oc.K8sClient.CoreV1(), data.AllNamespaces, data.ServicesIgnoreList) + if err != nil { + log.Fatal("Cannot get list of all services, err: %v", err) + } data.ServiceAccounts, err = getServiceAccounts(oc.K8sClient.CoreV1(), data.Namespaces) if err != nil { log.Fatal("Cannot get list of service accounts under test, err: %v", err) diff --git a/pkg/provider/catalogsources.go b/pkg/provider/catalogsources.go index 6d006169b..fbf48c0f8 100644 --- a/pkg/provider/catalogsources.go +++ b/pkg/provider/catalogsources.go @@ -2,6 +2,7 @@ package provider import ( "strconv" + "strings" "github.com/Masterminds/semver" olmv1Alpha "github.com/operator-framework/api/pkg/operators/v1alpha1" @@ -48,12 +49,14 @@ func getCatalogSourceBundleCountFromProbeContainer(env *TestEnvironment, cs *olm o := clientsholder.GetClientsHolder() // Find the kubernetes service associated with the catalog source - for _, svc := range env.Services { + for _, svc := range env.AllServices { // Skip if the service is not associated with the catalog source if svc.Spec.Selector["olm.catalogSource"] != cs.Name { continue } + log.Info("Found service %q associated with catalog source %q.", svc.Name, cs.Name) + // Use a probe pod to get the bundle count for _, probePod := range env.ProbePods { ctx := clientsholder.NewContext(probePod.Namespace, probePod.Name, probePod.Spec.Containers[0].Name) @@ -64,6 +67,10 @@ func getCatalogSourceBundleCountFromProbeContainer(env *TestEnvironment, cs *olm continue } + // Sanitize the command output + cmdValue = strings.TrimSpace(cmdValue) + cmdValue = strings.Trim(cmdValue, "\"") + // Parse the command output bundleCount, err := strconv.Atoi(cmdValue) if err != nil { @@ -72,11 +79,13 @@ func getCatalogSourceBundleCountFromProbeContainer(env *TestEnvironment, cs *olm } // Try each probe pod until we get a valid bundle count (which should only be 1 probe pod) + log.Info("Found bundle count via grpcurl %d for catalog source %q.", bundleCount, cs.Name) return bundleCount } } - return 0 + log.Warn("Warning: No services found associated with catalog source %q.", cs.Name) + return -1 } func getCatalogSourceBundleCountFromPackageManifests(env *TestEnvironment, cs *olmv1Alpha.CatalogSource) int { diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index e7f997332..c01d57975 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -104,6 +104,7 @@ type TestEnvironment struct { // rename this with testTarget HorizontalScaler []*scalingv1.HorizontalPodAutoscaler `json:"testHorizontalScaler"` Services []*corev1.Service `json:"testServices"` + AllServices []*corev1.Service `json:"testAllServices"` ServiceAccounts []*corev1.ServiceAccount `json:"testServiceAccounts"` AllServiceAccounts []*corev1.ServiceAccount `json:"AllServiceAccounts"` AllServiceAccountsMap map[string]*corev1.ServiceAccount @@ -335,6 +336,7 @@ func buildTestEnvironment() { //nolint:funlen,gocyclo env.RoleBindings = data.RoleBindings env.Roles = data.Roles env.Services = data.Services + env.AllServices = data.AllServices env.NetworkPolicies = data.NetworkPolicies for _, nsHelmChartReleases := range data.HelmChartReleases { for _, helmChartRelease := range nsHelmChartReleases { diff --git a/tests/operator/helper.go b/tests/operator/helper.go index b89bf92e4..793d81511 100644 --- a/tests/operator/helper.go +++ b/tests/operator/helper.go @@ -28,11 +28,6 @@ type CsvResult struct { Namespace string } -type CsvNameVersion struct { - Name string - Version string -} - // splitCsv splits the input string to extract namecsv and namespace. func SplitCsv(csv string) CsvResult { // Split by comma to separate components diff --git a/tests/operator/suite.go b/tests/operator/suite.go index b6f87934b..fa7659896 100644 --- a/tests/operator/suite.go +++ b/tests/operator/suite.go @@ -492,12 +492,17 @@ func testOperatorCatalogSourceBundleCount(check *checksdb.Check, env *provider.T // The name and namespace match. Lookup the bundle count. bundleCount := provider.GetCatalogSourceBundleCount(env, catalogSource) - if bundleCount > bundleCountLimit { - check.LogError("CatalogSource %q has more than "+bundleCountLimitStr+" ("+strconv.Itoa(bundleCount)+") referenced images", catalogSource.Name) - nonCompliantObjects = append(nonCompliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "CatalogSource has more than "+bundleCountLimitStr+" referenced images", false)) + if bundleCount == -1 { + check.LogError("Failed to get bundle count for CatalogSource %q", catalogSource.Name) + nonCompliantObjects = append(nonCompliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "Failed to get bundle count", false)) } else { - check.LogInfo("CatalogSource %q has less than "+bundleCountLimitStr+" ("+strconv.Itoa(bundleCount)+") referenced images", catalogSource.Name) - compliantObjects = append(compliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "CatalogSource has less than "+bundleCountLimitStr+" referenced images", true)) + if bundleCount > bundleCountLimit { + check.LogError("CatalogSource %q has more than "+bundleCountLimitStr+" ("+strconv.Itoa(bundleCount)+") referenced images", catalogSource.Name) + nonCompliantObjects = append(nonCompliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "CatalogSource has more than "+bundleCountLimitStr+" referenced images", false)) + } else { + check.LogInfo("CatalogSource %q has less than "+bundleCountLimitStr+" ("+strconv.Itoa(bundleCount)+") referenced images", catalogSource.Name) + compliantObjects = append(compliantObjects, testhelper.NewCatalogSourceReportObject(catalogSource.Namespace, catalogSource.Name, "CatalogSource has less than "+bundleCountLimitStr+" referenced images", true)) + } } log.Debug("Adding catalog source %q to list of already reported", catalogSource.Name)