From 598fae476e9700f29ddb6b6f7405df678cb1506b Mon Sep 17 00:00:00 2001 From: Shai Oren Date: Sun, 22 Oct 2023 14:55:53 +0300 Subject: [PATCH 1/2] Set talm suite and util functions --- .../talm/internal/talmhelper/talmhelper.go | 68 +++++++++++++++++++ .../talm/internal/talmparams/talmparams.go | 15 ++++ tests/gitopsztp/talm/talm_suite_test.go | 39 +++++++++++ 3 files changed, 122 insertions(+) diff --git a/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go b/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go index 86cfa043..31c5ae38 100644 --- a/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go +++ b/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go @@ -3,10 +3,16 @@ package talmhelper import ( "errors" "fmt" + "os" + "time" + "github.com/golang/glog" + "github.com/openshift-kni/eco-goinfra/pkg/namespace" "github.com/openshift-kni/eco-goinfra/pkg/pod" "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztphelper" "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztpinittools" + k8sErr "k8s.io/apimachinery/pkg/api/errors" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/talm/internal/talmparams" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -40,3 +46,65 @@ func VerifyTalmIsInstalled() error { return nil } + +// DeleteTalmTestNamespace deletes the TALM test namespace. +func DeleteTalmTestNamespace(allowNotFound bool) error { + glog.V(100).Info("Deleting talm namespace") + + // Hub may be optional depending on what tests are running + if os.Getenv(talmparams.HubKubeEnvKey) != "" { + glog.V(100).Info("Deleting talm namespace on hub") + + err := namespace.NewBuilder(gitopsztpinittools.HubAPIClient, + talmparams.TalmTestNamespace).DeleteAndWait(5 * time.Minute) + if err != nil { + if !allowNotFound || (allowNotFound && !k8sErr.IsNotFound(err)) { + return err + } + } + } + + // Spoke1 is the default kubeconfig + if os.Getenv(talmparams.Spoke1KubeEnvKey) != "" { + glog.V(100).Info("Deleting talm namespace on spoke1") + + err := namespace.NewBuilder(gitopsztpinittools.SpokeAPIClient, + talmparams.TalmTestNamespace).DeleteAndWait(5 * time.Minute) + if err != nil { + if !allowNotFound || (allowNotFound && !k8sErr.IsNotFound(err)) { + return err + } + } + } + + return nil +} + +// CreateTalmTestNamespace deletes the TALM test namespace. +func CreateTalmTestNamespace() error { + glog.V(100).Info("Creating talm namespace") + + // Hub may be optional depending on what tests are running + if os.Getenv(talmparams.HubKubeEnvKey) != "" { + glog.V(100).Info("Creating talm namespace on hub") + + _, err := namespace.NewBuilder(gitopsztpinittools.HubAPIClient, + talmparams.TalmTestNamespace).Create() + if err != nil { + return err + } + } + + // Spoke1 is the default kubeconfig + if os.Getenv(talmparams.Spoke1KubeEnvKey) != "" { + glog.V(100).Info("Creating talm namespace on spoke1") + + _, err := namespace.NewBuilder(gitopsztpinittools.SpokeAPIClient, + talmparams.TalmTestNamespace).Create() + if err != nil { + return err + } + } + + return nil +} diff --git a/tests/gitopsztp/talm/internal/talmparams/talmparams.go b/tests/gitopsztp/talm/internal/talmparams/talmparams.go index cc69f157..f9c9792b 100644 --- a/tests/gitopsztp/talm/internal/talmparams/talmparams.go +++ b/tests/gitopsztp/talm/internal/talmparams/talmparams.go @@ -2,6 +2,21 @@ package talmparams import ( "time" + + "github.com/openshift-kni/k8sreporter" + v1 "k8s.io/api/core/v1" +) + +var ( + // ReporterNamespacesToDump tells to the reporter from where to collect logs. + ReporterNamespacesToDump = map[string]string{ + TalmTestNamespace: "", + } + + // ReporterCRDsToDump tells to the reporter what CRs to dump. + ReporterCRDsToDump = []k8sreporter.CRData{ + {Cr: &v1.PodList{}}, + } ) // talm related vars. diff --git a/tests/gitopsztp/talm/talm_suite_test.go b/tests/gitopsztp/talm/talm_suite_test.go index 5fb9e087..5a5a1746 100644 --- a/tests/gitopsztp/talm/talm_suite_test.go +++ b/tests/gitopsztp/talm/talm_suite_test.go @@ -6,6 +6,12 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/openshift-kni/eco-goinfra/pkg/clients" + "github.com/openshift-kni/eco-goinfra/pkg/polarion" + "github.com/openshift-kni/eco-goinfra/pkg/reporter" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztphelper" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/talm/internal/talmhelper" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/talm/internal/talmparams" . "github.com/openshift-kni/eco-gosystem/tests/internal/inittools" ) @@ -20,3 +26,36 @@ func TestTalm(t *testing.T) { RunSpecs(t, "TALM Suite", reporterConfig) } + +var _ = BeforeSuite(func() { + err := gitopsztphelper.InitializeClients() + Expect(err).ToNot(HaveOccurred()) + + // Make sure TALM is present + err = talmhelper.VerifyTalmIsInstalled() + Expect(err).ToNot(HaveOccurred()) + + // Delete the namespace before creating it to ensure it is in a consistent blank state + err = talmhelper.DeleteTalmTestNamespace(true) + Expect(err).ToNot(HaveOccurred()) + err = talmhelper.CreateTalmTestNamespace() + Expect(err).ToNot(HaveOccurred()) +}) + +var _ = AfterSuite(func() { + // Deleting the namespace after the suite finishes ensures all the CGUs created are deleted + err := talmhelper.DeleteTalmTestNamespace(false) + Expect(err).ToNot(HaveOccurred()) +}) + +var _ = JustAfterEach(func() { + reporter.ReportIfFailed( + CurrentSpecReport(), GeneralConfig.GetDumpFailedTestReportLocation(currentFile), + GeneralConfig.ReportsDirAbsPath, talmparams.ReporterNamespacesToDump, + talmparams.ReporterCRDsToDump, clients.SetScheme) +}) + +var _ = ReportAfterSuite("", func(report Report) { + polarion.CreateReport( + report, GeneralConfig.GetPolarionReportPath(), GeneralConfig.PolarionTCPrefix) +}) From 42ef7093b4a7403aea5677d4cff7021eca0dcad5 Mon Sep 17 00:00:00 2001 From: Shai Oren Date: Mon, 23 Oct 2023 11:57:20 +0300 Subject: [PATCH 2/2] Add talm canary tc infra --- .../talm/internal/talmhelper/talmhelper.go | 77 +++++++++++++++++++ tests/gitopsztp/talm/talm_suite_test.go | 1 + tests/gitopsztp/talm/tests/talm_canary.go | 57 ++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 tests/gitopsztp/talm/tests/talm_canary.go diff --git a/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go b/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go index 31c5ae38..1db84182 100644 --- a/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go +++ b/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go @@ -7,6 +7,7 @@ import ( "time" "github.com/golang/glog" + "github.com/openshift-kni/eco-goinfra/pkg/clients" "github.com/openshift-kni/eco-goinfra/pkg/namespace" "github.com/openshift-kni/eco-goinfra/pkg/pod" "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztphelper" @@ -17,6 +18,33 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +var ( + // Spoke2APIClient api client of the second spoke. + Spoke2APIClient *clients.Settings + // Spoke2Name name of the second spoke. + Spoke2Name string +) + +// talm consts. +const ( + CguName string = "talm-cgu" + Namespace string = "talm-namespace" + PlacementBindingName string = "talm-placement-binding" + PlacementRule string = "talm-placement-rule" + PolicyName string = "talm-policy" + PolicySetName string = "talm-policyset" + CatalogSourceName string = "talm-catsrc" + Talm411TimeoutMessage string = "The ClusterGroupUpgrade CR policies are taking too long to complete" + Talm412TimeoutMessage string = "Policy remediation took too long" + TemporaryNamespaceName string = Namespace + "-temp" + ProgressingType string = "Progressing" + ReadyType string = "Ready" + SucceededType string = "Succeeded" + ValidatedType string = "Validated" + ConditionReasonCompleted string = "Completed" + ConditionReasonUpgradeCompleted string = "UpgradeCompleted" +) + // VerifyTalmIsInstalled checks that talm pod+container is present and that CGUs can be fetched. func VerifyTalmIsInstalled() error { // Check for talm pods @@ -108,3 +136,52 @@ func CreateTalmTestNamespace() error { return nil } + +// GetAllTestClients is used to quickly obtain a list of all the test clients. +func GetAllTestClients() []*clients.Settings { + return []*clients.Settings{ + gitopsztpinittools.HubAPIClient, + gitopsztpinittools.SpokeAPIClient, + Spoke2APIClient, + } +} + +// CleanupTestResourcesOnClients is used to delete all references to specified cgu, +// policy, and namespace on all clusters specified in the client list. +func CleanupTestResourcesOnClients( + clients []*clients.Settings, + cguName string, + policyName string, + createdNamespace string, + placementBinding string, + placementRule string, + policySet string, + catsrcName string) []error { + // Create a list of cleanupErrors + var cleanupErrors []error + + // Loop over all clients for cleanup + + // complete function + for _, client := range clients { + // Perform the deletes + glog.V(100).Infof("cleaning up resources on client '%s'", client.Config.Host) + } + + return cleanupErrors +} + +// CleanupNamespace is used to cleanup a namespace on multiple clients. +func CleanupNamespace(clients []*clients.Settings, ns string) error { + for _, client := range clients { + namespace := namespace.NewBuilder(client, ns) + if namespace.Exists() { + err := namespace.DeleteAndWait(5 * time.Minute) + if err != nil { + return err + } + } + } + + return nil +} diff --git a/tests/gitopsztp/talm/talm_suite_test.go b/tests/gitopsztp/talm/talm_suite_test.go index 5a5a1746..aeb7e0d5 100644 --- a/tests/gitopsztp/talm/talm_suite_test.go +++ b/tests/gitopsztp/talm/talm_suite_test.go @@ -12,6 +12,7 @@ import ( "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztphelper" "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/talm/internal/talmhelper" "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/talm/internal/talmparams" + _ "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/talm/tests" . "github.com/openshift-kni/eco-gosystem/tests/internal/inittools" ) diff --git a/tests/gitopsztp/talm/tests/talm_canary.go b/tests/gitopsztp/talm/tests/talm_canary.go new file mode 100644 index 00000000..b59a3788 --- /dev/null +++ b/tests/gitopsztp/talm/tests/talm_canary.go @@ -0,0 +1,57 @@ +package tests + +import ( + "fmt" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/openshift-kni/eco-goinfra/pkg/clients" + "github.com/openshift-kni/eco-goinfra/pkg/namespace" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/talm/internal/talmhelper" + "github.com/openshift-kni/eco-gosystem/tests/internal/cluster" +) + +var _ = Describe("Talm Canary Tests", Ordered, Label("talmcanary"), func() { + + var clusterList []*clients.Settings + + BeforeAll(func() { + // Initialize cluster list + clusterList = talmhelper.GetAllTestClients() + }) + + BeforeEach(func() { + // Check that the required clusters are present + err := cluster.CheckClustersPresent(clusterList) + if err != nil { + Skip(fmt.Sprintf("error occurred validating required clusters are present: %s", err.Error())) + } + + // Cleanup state to make it consistent + for _, client := range clusterList { + + // Cleanup everything + errList := talmhelper.CleanupTestResourcesOnClients( + []*clients.Settings{ + client, + }, + talmhelper.CguName, + talmhelper.PolicyName, + talmhelper.Namespace, + talmhelper.PlacementBindingName, + talmhelper.PlacementRule, + talmhelper.PolicySetName, + talmhelper.CatalogSourceName) + Expect(errList).To(BeEmpty()) + + // Create namespace + _, err := namespace.NewBuilder(client, talmhelper.Namespace).Create() + Expect(err).ToNot(HaveOccurred()) + } + + // Cleanup the temporary namespace + err = talmhelper.CleanupNamespace(clusterList, talmhelper.TemporaryNamespaceName) + Expect(err).ToNot(HaveOccurred()) + }) + +})