diff --git a/tests/argocd/argocd_suite_test.go b/tests/argocd/argocd_suite_test.go deleted file mode 100644 index 77f54482..00000000 --- a/tests/argocd/argocd_suite_test.go +++ /dev/null @@ -1,167 +0,0 @@ -package argocd - -import ( - "log" - "os" - "runtime" - "testing" - - "github.com/golang/glog" - . "github.com/onsi/ginkgo/v2" - "github.com/openshift-kni/eco-goinfra/pkg/clients" - "github.com/openshift-kni/eco-goinfra/pkg/namespace" - "github.com/openshift-kni/eco-goinfra/pkg/polarion" - "github.com/openshift-kni/eco-goinfra/pkg/reporter" - "github.com/openshift-kni/eco-gosystem/tests/argocd/internal/argocdhelper" - "github.com/openshift-kni/eco-gosystem/tests/argocd/internal/argocdparams" - _ "github.com/openshift-kni/eco-gosystem/tests/argocd/tests" - "github.com/openshift-kni/eco-gosystem/tests/internal/cluster" - . "github.com/openshift-kni/eco-gosystem/tests/internal/inittools" - "github.com/openshift-kni/eco-gosystem/tests/talm/talmparams" - - . "github.com/onsi/gomega" -) - -var _, currentFile, _, _ = runtime.Caller(0) - -func TestArgocd(t *testing.T) { - _, reporterConfig := GinkgoConfiguration() - reporterConfig.JUnitReport = GeneralConfig.GetJunitReportPath(currentFile) - - RegisterFailHandler(Fail) - // Stop ginkgo complaining about slow tests - - RunSpecs(t, "Argocd Suite", reporterConfig) -} - -var _ = BeforeSuite(func() { - // API clients must be initialized first since they will be used later - err := InitializeClients() - Expect(err).ToNot(HaveOccurred()) - - namespace := namespace.NewBuilder(argocdhelper.HubAPIClient, argocdparams.ZtpTestNamespace) - - // Delete and re-create the namespace to start with a clean state - if namespace.Exists() { - err = namespace.DeleteAndWait(argocdparams.DefaultTimeout) - Expect(err).ToNot(HaveOccurred()) - } - - _, err = namespace.Create() - Expect(err).ToNot(HaveOccurred()) - - // create a privileged pod to run commands on nodes - glog.V(100).Infof("Setup initiated: creating a privileged pod") - - _, err = argocdhelper.CreatePrivilegedPods("") - Expect(err).ToNot(HaveOccurred()) - -}) - -var _ = AfterSuite(func() { - // Restore the original Argocd configuration after the tests are completed - err := argocdhelper.ResetArgocdGitDetails() - Expect(err).ToNot(HaveOccurred()) - - // Delete the ztp namespace - err = namespace.NewBuilder(argocdhelper.HubAPIClient, argocdparams.ZtpTestNamespace).Delete() - Expect(err).ToNot(HaveOccurred()) - - // delete the privileged pod - glog.V(100).Infof("Teardown initiated: deleting privileged pod") - err = namespace.NewBuilder(argocdhelper.HubAPIClient, argocdparams.PrivPodNamespace).Delete() - Expect(err).ToNot(HaveOccurred()) -}) - -var _ = JustAfterEach(func() { - reporter.ReportIfFailed( - CurrentSpecReport(), GeneralConfig.GetDumpFailedTestReportLocation(currentFile), - GeneralConfig.ReportsDirAbsPath, argocdhelper.ReporterNamespacesToDump, - argocdhelper.ReporterCRDsToDump, clients.SetScheme) -}) - -var _ = ReportAfterSuite("", func(report Report) { - polarion.CreateReport( - report, GeneralConfig.GetPolarionReportPath(), GeneralConfig.PolarionTCPrefix) -}) - -// InitializeClients initializes hub & spoke clients. -func InitializeClients() error { - if os.Getenv(argocdparams.HubKubeEnvKey) != "" { - var err error - // Define all the hub information - argocdhelper.HubAPIClient, err = argocdhelper.DefineAPIClient(argocdparams.HubKubeEnvKey) - if err != nil { - return err - } - - argocdhelper.HubName, err = cluster.GetClusterName(argocdparams.HubKubeEnvKey) - if err != nil { - return err - } - - ocpVersion, err := cluster.GetClusterVersion(argocdhelper.HubAPIClient) - if err != nil { - return err - } - - log.Printf("cluster '%s' has OCP version '%s'\n", argocdhelper.HubName, ocpVersion) - - argocdhelper.AcmVersion, err = argocdhelper.GetOperatorVersionFromCSV( - argocdhelper.HubAPIClient, - argocdparams.AcmOperatorName, - argocdparams.AcmOperatorNamespace, - ) - if err != nil { - return err - } - - log.Printf("cluster '%s' has ACM version '%s'\n", argocdhelper.HubName, argocdhelper.AcmVersion) - - argocdhelper.ZtpVersion, err = argocdhelper.GetZtpVersionFromArgocd( - argocdhelper.HubAPIClient, - argocdparams.OpenshiftGitopsRepoServer, - argocdparams.OpenshiftGitops, - ) - if err != nil { - return err - } - - log.Printf("cluster '%s' has ZTP version '%s'\n", argocdhelper.HubName, argocdhelper.ZtpVersion) - - argocdhelper.TalmVersion, err = argocdhelper.GetOperatorVersionFromCSV( - argocdhelper.HubAPIClient, - talmparams.OperatorHubTalmNamespace, - talmparams.OpenshiftOperatorNamespace, - ) - if err != nil { - return err - } - - log.Printf("cluster '%s' has TALM version '%s'\n", argocdhelper.HubName, argocdhelper.TalmVersion) - } - - // Spoke is the default kubeconfig - if os.Getenv(argocdparams.SpokeKubeEnvKey) != "" { - var err error - // Define all the spoke information - argocdhelper.SpokeAPIClient, err = argocdhelper.DefineAPIClient(argocdparams.SpokeKubeEnvKey) - if err != nil { - return err - } - - argocdhelper.SpokeName, err = cluster.GetClusterName(argocdparams.SpokeKubeEnvKey) - if err != nil { - return err - } - - ocpVersion, err := cluster.GetClusterVersion(argocdhelper.SpokeAPIClient) - if err != nil { - return err - } - - log.Printf("cluster '%s' has OCP version '%s'\n", argocdhelper.SpokeName, ocpVersion) - } - - return nil -} diff --git a/tests/argocd/internal/argocdparams/argocdparams.go b/tests/argocd/internal/argocdparams/argocdparams.go deleted file mode 100644 index 9a2ea7d2..00000000 --- a/tests/argocd/internal/argocdparams/argocdparams.go +++ /dev/null @@ -1,45 +0,0 @@ -package argocdparams - -import "time" - -// ArgocdGitDetails stores argocd git details. -type ArgocdGitDetails struct { - Repo string - Branch string - Path string -} - -// ZTP and Argocd vars. -const ( - // Namespaces matching '^ztp*' are special - // Argocd will only let us use such namespaces for policy gen templates on the SNO nodes. - ZtpTestNamespace string = "ztp-test" - HubKubeEnvKey string = "KUBECONFIG_HUB" - SpokeKubeEnvKey string = "KUBECONFIG" - OpenshiftGitops string = "openshift-gitops" - OpenshiftGitopsRepoServer string = "openshift-gitops-repo-server" - ArgocdPoliciesAppName string = "policies" - ArgocdClustersAppName string = "clusters" - ArgocdChangeTimeout time.Duration = 10 * time.Minute - ArgocdChangeInterval time.Duration = 10 * time.Second - DefaultTimeout time.Duration = 5 * time.Minute - ZtpSiteGenerateImageName string = "registry-proxy.engineering.redhat.com/rh-osbs/openshift4-ztp-site-generate" - AcmOperatorName string = "advanced-cluster-management" - AcmOperatorNamespace string = "rhacm" - AcmArgocdInitContainer string = "multicluster-operators-subscription" - AcmPolicyGeneratorName string = "acm-policy-generator" - ImageRegistryNamespace string = "openshift-image-registry" - MulticlusterhubOperator string = "multiclusterhub-operator" - PerfProfileName string = "openshift-node-performance-profile" - MachineConfigName string = "02-master-workload-partitioning" - TunedPatchName string = "performance-patch" - TunedNamespace string = "openshift-cluster-node-tuning-operator" - MCPname string = "master" - PrivPodNamespace string = "cnfgotestpriv" -) - -// ArgocdApps is a list of the argocd app names that are defined above. -var ArgocdApps = []string{ - ArgocdClustersAppName, - ArgocdPoliciesAppName, -} diff --git a/tests/gitopsztp/argocd/argocd_suite_test.go b/tests/gitopsztp/argocd/argocd_suite_test.go new file mode 100644 index 00000000..0a3a3552 --- /dev/null +++ b/tests/gitopsztp/argocd/argocd_suite_test.go @@ -0,0 +1,70 @@ +package argocd + +import ( + "runtime" + "testing" + + . "github.com/onsi/ginkgo/v2" + "github.com/openshift-kni/eco-goinfra/pkg/clients" + "github.com/openshift-kni/eco-goinfra/pkg/namespace" + "github.com/openshift-kni/eco-goinfra/pkg/polarion" + "github.com/openshift-kni/eco-goinfra/pkg/reporter" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/argocd/internal/argocdhelper" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/argocd/internal/argocdparams" + _ "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/argocd/tests" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztphelper" + . "github.com/openshift-kni/eco-gosystem/tests/internal/inittools" + + . "github.com/onsi/gomega" +) + +var _, currentFile, _, _ = runtime.Caller(0) + +func TestArgocd(t *testing.T) { + _, reporterConfig := GinkgoConfiguration() + reporterConfig.JUnitReport = GeneralConfig.GetJunitReportPath(currentFile) + + RegisterFailHandler(Fail) + // Stop ginkgo complaining about slow tests + + RunSpecs(t, "Argocd Suite", reporterConfig) +} + +var _ = BeforeSuite(func() { + // API clients must be initialized first since they will be used later + err := gitopsztphelper.InitializeClients() + Expect(err).ToNot(HaveOccurred()) + + namespace := namespace.NewBuilder(gitopsztphelper.HubAPIClient, argocdparams.ZtpTestNamespace) + + // Delete and re-create the namespace to start with a clean state + if namespace.Exists() { + err = namespace.DeleteAndWait(argocdparams.DefaultTimeout) + Expect(err).ToNot(HaveOccurred()) + } + + _, err = namespace.Create() + Expect(err).ToNot(HaveOccurred()) +}) + +var _ = AfterSuite(func() { + // Restore the original Argocd configuration after the tests are completed + err := argocdhelper.ResetArgocdGitDetails() + Expect(err).ToNot(HaveOccurred()) + + // Delete the ztp namespace + err = namespace.NewBuilder(gitopsztphelper.HubAPIClient, argocdparams.ZtpTestNamespace).Delete() + Expect(err).ToNot(HaveOccurred()) +}) + +var _ = JustAfterEach(func() { + reporter.ReportIfFailed( + CurrentSpecReport(), GeneralConfig.GetDumpFailedTestReportLocation(currentFile), + GeneralConfig.ReportsDirAbsPath, argocdhelper.ReporterNamespacesToDump, + argocdhelper.ReporterCRDsToDump, clients.SetScheme) +}) + +var _ = ReportAfterSuite("", func(report Report) { + polarion.CreateReport( + report, GeneralConfig.GetPolarionReportPath(), GeneralConfig.PolarionTCPrefix) +}) diff --git a/tests/argocd/internal/argocdhelper/argocdhelper.go b/tests/gitopsztp/argocd/internal/argocdhelper/argocdhelper.go similarity index 66% rename from tests/argocd/internal/argocdhelper/argocdhelper.go rename to tests/gitopsztp/argocd/internal/argocdhelper/argocdhelper.go index a9d40e9a..e1bbd99b 100644 --- a/tests/argocd/internal/argocdhelper/argocdhelper.go +++ b/tests/gitopsztp/argocd/internal/argocdhelper/argocdhelper.go @@ -12,34 +12,25 @@ import ( "strings" "time" - "github.com/openshift-kni/eco-goinfra/pkg/namespace" - "github.com/openshift-kni/eco-goinfra/pkg/nodes" - "github.com/openshift-kni/eco-goinfra/pkg/olm" - "github.com/openshift-kni/eco-goinfra/pkg/pod" "github.com/openshift-kni/k8sreporter" v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "github.com/openshift-kni/eco-goinfra/pkg/argocd" "github.com/openshift-kni/eco-goinfra/pkg/clients" - "github.com/openshift-kni/eco-goinfra/pkg/deployment" - "github.com/openshift-kni/eco-gosystem/tests/argocd/internal/argocdparams" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/argocd/internal/argocdparams" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztphelper" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztpparams" ) // ztp related vars. var ( // test image. - CnfTestImage = "quay.io/openshift-kni/cnf-tests:4.8" - HubAPIClient *clients.Settings - HubName string - SpokeAPIClient *clients.Settings - SpokeName string - ArgocdApps = map[string]argocdparams.ArgocdGitDetails{} - ZtpVersion string - AcmVersion string - TalmVersion string + CnfTestImage = "quay.io/openshift-kni/cnf-tests:4.8" + + // ArgocdApps represents argocd git details. + ArgocdApps = map[string]argocdparams.ArgocdGitDetails{} // ReporterNamespacesToDump tells to the reporter from where to collect logs. ReporterNamespacesToDump = map[string]string{ argocdparams.ZtpTestNamespace: "", @@ -53,7 +44,7 @@ var ( // SetGitDetailsInArgocd is used to update the git repo, branch, and path in the Argocd app. func SetGitDetailsInArgocd(gitRepo, gitBranch, gitPath, argocdApp string, waitForSync, syncMustBeValid bool) error { - app, err := argocd.PullApplication(HubAPIClient, argocdApp, argocdparams.OpenshiftGitops) + app, err := argocd.PullApplication(gitopsztphelper.HubAPIClient, argocdApp, gitopsztpparams.OpenshiftGitops) if err != nil { return err } @@ -83,46 +74,6 @@ func SetGitDetailsInArgocd(gitRepo, gitBranch, gitPath, argocdApp string, waitFo return nil } -// GetOperatorVersionFromCSV returns operator version from csv. -func GetOperatorVersionFromCSV(client *clients.Settings, operatorName, operatorNamespace string) (string, error) { - // Check if the client is valid - if client == nil { - return "", fmt.Errorf("provided nil client") - } - - // Get the CSV objects - csv, err := olm.ListClusterServiceVersion(client, operatorNamespace, metav1.ListOptions{}) - - // Check for any error getting the CSVs - if err != nil { - return "", err - } - - // Find the CSV that matches the operator - for _, csv := range csv { - if strings.Contains(csv.Object.Name, operatorName) { - return csv.Object.Spec.Version.String(), nil - } - } - - return "", nil -} - -// DefineAPIClient creates new api client instance connected to given cluster. -func DefineAPIClient(kubeconfigEnvVar string) (*clients.Settings, error) { - kubeFilePath, present := os.LookupEnv(kubeconfigEnvVar) - if !present { - return nil, fmt.Errorf("can not load api client. Please check %s env var", kubeconfigEnvVar) - } - - clients := clients.New(kubeFilePath) - if clients == nil { - return nil, fmt.Errorf("client is not set please check %s env variable", kubeconfigEnvVar) - } - - return clients, nil -} - // GetZtpContext is used to get the context for the Ztp test client interactions. func GetZtpContext() context.Context { return context.Background() @@ -131,8 +82,8 @@ func GetZtpContext() context.Context { // GetAllTestClients is used to quickly obtain a list of all the test clients. func GetAllTestClients() []*clients.Settings { return []*clients.Settings{ - HubAPIClient, - SpokeAPIClient, + gitopsztphelper.HubAPIClient, + gitopsztphelper.SpokeAPIClient, } } @@ -282,38 +233,6 @@ func DoesGitPathExist(gitURL, gitBranch, gitPath string) bool { return false } -// GetZtpVersionFromArgocd is used to fetch the version of the ztp-site-generator init container. -func GetZtpVersionFromArgocd(client *clients.Settings, name string, namespace string) (string, error) { - deployment, err := deployment.Pull(client, name, namespace) - if err != nil { - return "", err - } - - for _, container := range deployment.Object.Spec.Template.Spec.InitContainers { - // Legacy 4.11 uses the image name as `ztp-site-generator` - // While 4.12+ uses the image name as `ztp-site-generate` - // So just check for `ztp-site-gen` to cover both - if strings.Contains(container.Image, "ztp-site-gen") { - arr := strings.Split(container.Image, ":") - // Get the image tag which is the last element - ztpVersion := arr[len(arr)-1] - - if ztpVersion == "latest" { - log.Println("Site generator version tag was 'latest', so returning empty version") - - return "", nil - } - - // The format here will be like vX.Y.Z so we need to remove the v at the start - fmt.Println("ztpVersion = ", ztpVersion) - - return ztpVersion[1:], nil - } - } - - return "", fmt.Errorf("unable to identify ztp version") -} - // WaitUntilArgocdChangeIsCompleted is used to wait until Argocd has updated its configuration. func WaitUntilArgocdChangeIsCompleted(appName string, syncMustBeValid bool, timeout time.Duration) error { log.Println("Waiting for Argocd change to finish syncing") @@ -321,7 +240,7 @@ func WaitUntilArgocdChangeIsCompleted(appName string, syncMustBeValid bool, time err := wait.PollImmediate(argocdparams.ArgocdChangeInterval, timeout, func() (bool, error) { log.Println("Checking if argo change is complete...") - app, err := argocd.PullApplication(HubAPIClient, appName, argocdparams.OpenshiftGitops) + app, err := argocd.PullApplication(gitopsztphelper.HubAPIClient, appName, gitopsztpparams.OpenshiftGitops) if err != nil { return false, err } @@ -363,7 +282,7 @@ func WaitUntilArgocdChangeIsCompleted(appName string, syncMustBeValid bool, time // GetGitDetailsFromArgocd is used to get the current git repo, branch, and path in the Argocd app. func GetGitDetailsFromArgocd(appName, namespace string) (string, string, string, error) { - app, err := argocd.PullApplication(HubAPIClient, appName, namespace) + app, err := argocd.PullApplication(gitopsztphelper.HubAPIClient, appName, namespace) if err != nil { return "", "", "", err } @@ -375,10 +294,10 @@ func GetGitDetailsFromArgocd(appName, namespace string) (string, string, string, // If any are undefined then the default values are used instead. func GetArgocdAppGitDetails() error { // Check if the hub is defined - if os.Getenv(argocdparams.HubKubeEnvKey) != "" { + if os.Getenv(gitopsztpparams.HubKubeEnvKey) != "" { // Loop over the apps and save the git details for _, app := range argocdparams.ArgocdApps { - repo, branch, dir, err := GetGitDetailsFromArgocd(app, argocdparams.OpenshiftGitops) + repo, branch, dir, err := GetGitDetailsFromArgocd(app, gitopsztpparams.OpenshiftGitops) if err != nil { return err } @@ -397,7 +316,7 @@ func GetArgocdAppGitDetails() error { // ResetArgocdGitDetails is used to configure Argocd back to the values it had before the tests started. func ResetArgocdGitDetails() error { - if os.Getenv(argocdparams.HubKubeEnvKey) != "" { + if os.Getenv(gitopsztpparams.HubKubeEnvKey) != "" { // Loop over the apps and restore the git details for _, app := range argocdparams.ArgocdApps { // Restore the app's git details @@ -418,46 +337,3 @@ func ResetArgocdGitDetails() error { return nil } - -// CreatePrivilegedPods creates privileged test pods on worker nodes to assist testing -// Returns a map with nodeName as key and pod pointer as value, and error if occurred. -func CreatePrivilegedPods(image string) (map[string]*pod.Builder, error) { - if image == "" { - image = CnfTestImage - } - // Create cnfgotestpriv namespace if not already created - namespace := namespace.NewBuilder(HubAPIClient, argocdparams.PrivPodNamespace) - if !namespace.Exists() { - log.Println("Creating namespace:", argocdparams.PrivPodNamespace) - - _, err := namespace.Create() - if err != nil { - return nil, err - } - } - // Launch priv pods on nodes with worker role so it can be successfully scheduled. - workerNodesList, err := nodes.List(HubAPIClient, metav1.ListOptions{ - LabelSelector: "node-role.kubernetes.io/worker", - }) - if err != nil { - return nil, err - } - - privPods := make(map[string]*pod.Builder) - - for _, workerNode := range workerNodesList { - podName := fmt.Sprintf("%s-%s", argocdparams.PrivPodNamespace, workerNode.Object.Name) - privilegedPod := pod.NewBuilder(HubAPIClient, podName, argocdparams.PrivPodNamespace, image) - - _, err := privilegedPod.WithPrivilegedFlag().DefineOnNode(workerNode.Object.Name).WithLocalVolume( - "rootfs", "/rootfs").WithHostPid(true).CreateAndWaitUntilRunning( - 10 * time.Minute) - if err != nil { - return nil, err - } - - privPods[workerNode.Object.Name] = privilegedPod - } - - return privPods, nil -} diff --git a/tests/gitopsztp/argocd/internal/argocdparams/argocdparams.go b/tests/gitopsztp/argocd/internal/argocdparams/argocdparams.go new file mode 100644 index 00000000..e32f9c01 --- /dev/null +++ b/tests/gitopsztp/argocd/internal/argocdparams/argocdparams.go @@ -0,0 +1,39 @@ +package argocdparams + +import "time" + +// ArgocdGitDetails stores argocd git details. +type ArgocdGitDetails struct { + Repo string + Branch string + Path string +} + +// ZTP and Argocd vars. +const ( + // Namespaces matching '^ztp*' are special + // Argocd will only let us use such namespaces for policy gen templates on the SNO nodes. + ZtpTestNamespace string = "ztp-test" + ArgocdPoliciesAppName string = "policies" + ArgocdClustersAppName string = "clusters" + ArgocdChangeTimeout time.Duration = 10 * time.Minute + ArgocdChangeInterval time.Duration = 10 * time.Second + DefaultTimeout time.Duration = 5 * time.Minute + ZtpSiteGenerateImageName string = "registry-proxy.engineering.redhat.com/rh-osbs/openshift4-ztp-site-generate" + AcmArgocdInitContainer string = "multicluster-operators-subscription" + AcmPolicyGeneratorName string = "acm-policy-generator" + ImageRegistryNamespace string = "openshift-image-registry" + MulticlusterhubOperator string = "multiclusterhub-operator" + PerfProfileName string = "openshift-node-performance-profile" + MachineConfigName string = "02-master-workload-partitioning" + TunedPatchName string = "performance-patch" + TunedNamespace string = "openshift-cluster-node-tuning-operator" + MCPname string = "master" + PrivPodNamespace string = "cnfgotestpriv" +) + +// ArgocdApps is a list of the argocd app names that are defined above. +var ArgocdApps = []string{ + ArgocdClustersAppName, + ArgocdPoliciesAppName, +} diff --git a/tests/argocd/tests/gitopsztp_argocd_clusters_app.go b/tests/gitopsztp/argocd/tests/gitopsztp_argocd_clusters_app.go similarity index 87% rename from tests/argocd/tests/gitopsztp_argocd_clusters_app.go rename to tests/gitopsztp/argocd/tests/gitopsztp_argocd_clusters_app.go index 1fc1dcfd..b4664078 100644 --- a/tests/argocd/tests/gitopsztp_argocd_clusters_app.go +++ b/tests/gitopsztp/argocd/tests/gitopsztp_argocd_clusters_app.go @@ -7,8 +7,9 @@ import ( . "github.com/onsi/gomega" "github.com/openshift-kni/eco-goinfra/pkg/assisted" "github.com/openshift-kni/eco-goinfra/pkg/clients" - "github.com/openshift-kni/eco-gosystem/tests/argocd/internal/argocdhelper" - "github.com/openshift-kni/eco-gosystem/tests/argocd/internal/argocdparams" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/argocd/internal/argocdhelper" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/argocd/internal/argocdparams" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztphelper" "github.com/openshift-kni/eco-gosystem/tests/internal/cluster" ) @@ -31,13 +32,13 @@ var _ = Describe("ZTP Argocd clusters Tests", Ordered, Label("ztp-argocd-cluster // Check for minimum ztp version By("Checking the ZTP version", func() { if !argocdhelper.IsVersionStringInRange( - argocdhelper.ZtpVersion, + gitopsztphelper.ZtpVersion, "4.11", "", ) { Skip(fmt.Sprintf( "unable to run test on ztp version '%s' as it is less than minimum '%s", - argocdhelper.ZtpVersion, + gitopsztphelper.ZtpVersion, "4.11", )) } @@ -64,7 +65,7 @@ var _ = Describe("ZTP Argocd clusters Tests", Ordered, Label("ztp-argocd-cluster }) By("Check nmstateConfig cr exists", func() { - nmStateConfigList, err := assisted.ListNmStateConfigsInAllNamespaces(argocdhelper.HubAPIClient) + nmStateConfigList, err := assisted.ListNmStateConfigsInAllNamespaces(gitopsztphelper.HubAPIClient) Expect(err).ToNot(HaveOccurred()) Expect(nmStateConfigList).ToNot(BeEmpty(), "No NMstateConfig found before test begins") }) @@ -82,7 +83,7 @@ var _ = Describe("ZTP Argocd clusters Tests", Ordered, Label("ztp-argocd-cluster }) By("Check nmstateConfig CR is gone under spoke cluster NS on hub", func() { - nmStateConfigList, err := assisted.ListNmStateConfigsInAllNamespaces(argocdhelper.HubAPIClient) + nmStateConfigList, err := assisted.ListNmStateConfigsInAllNamespaces(gitopsztphelper.HubAPIClient) Expect(err).ToNot(HaveOccurred()) Expect(nmStateConfigList).To(BeEmpty(), "NMstateconfig was found") }) diff --git a/tests/gitopsztp/internal/gitopsztphelper/gitopsztphelper.go b/tests/gitopsztp/internal/gitopsztphelper/gitopsztphelper.go new file mode 100644 index 00000000..d6563289 --- /dev/null +++ b/tests/gitopsztp/internal/gitopsztphelper/gitopsztphelper.go @@ -0,0 +1,185 @@ +package gitopsztphelper + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/openshift-kni/eco-goinfra/pkg/clients" + "github.com/openshift-kni/eco-goinfra/pkg/deployment" + "github.com/openshift-kni/eco-goinfra/pkg/olm" + "github.com/openshift-kni/eco-gosystem/tests/gitopsztp/internal/gitopsztpparams" + "github.com/openshift-kni/eco-gosystem/tests/internal/cluster" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +var ( + // HubAPIClient is the api client to the hub. + HubAPIClient *clients.Settings + // HubName hub cluster name. + HubName string + // SpokeAPIClient is the api client to the spoke. + SpokeAPIClient *clients.Settings + // SpokeName spoke cluster name. + SpokeName string + // ZtpVersion ztp version. + ZtpVersion string + // AcmVersion Advanced cluster management version. + AcmVersion string + // TalmVersion talm version. + TalmVersion string +) + +// InitializeClients initializes hub & spoke clients. +func InitializeClients() error { + if os.Getenv(gitopsztpparams.HubKubeEnvKey) != "" { + var err error + // Define all the hub information + HubAPIClient, err = DefineAPIClient(gitopsztpparams.HubKubeEnvKey) + if err != nil { + return err + } + + HubName, err = cluster.GetClusterName(gitopsztpparams.HubKubeEnvKey) + if err != nil { + return err + } + + ocpVersion, err := cluster.GetClusterVersion(HubAPIClient) + if err != nil { + return err + } + + log.Printf("cluster '%s' has OCP version '%s'\n", HubName, ocpVersion) + + AcmVersion, err = GetOperatorVersionFromCSV( + HubAPIClient, + gitopsztpparams.AcmOperatorName, + gitopsztpparams.AcmOperatorNamespace, + ) + if err != nil { + return err + } + + log.Printf("cluster '%s' has ACM version '%s'\n", HubName, AcmVersion) + + ZtpVersion, err = GetZtpVersionFromArgocd( + HubAPIClient, + gitopsztpparams.OpenshiftGitopsRepoServer, + gitopsztpparams.OpenshiftGitops, + ) + if err != nil { + return err + } + + log.Printf("cluster '%s' has ZTP version '%s'\n", HubName, ZtpVersion) + + TalmVersion, err = GetOperatorVersionFromCSV( + HubAPIClient, + gitopsztpparams.OperatorHubTalmNamespace, + gitopsztpparams.OpenshiftOperatorNamespace, + ) + if err != nil { + return err + } + + log.Printf("cluster '%s' has TALM version '%s'\n", HubName, TalmVersion) + } + + // Spoke is the default kubeconfig + if os.Getenv(gitopsztpparams.SpokeKubeEnvKey) != "" { + var err error + // Define all the spoke information + SpokeAPIClient, err = DefineAPIClient(gitopsztpparams.SpokeKubeEnvKey) + if err != nil { + return err + } + + SpokeName, err = cluster.GetClusterName(gitopsztpparams.SpokeKubeEnvKey) + if err != nil { + return err + } + + ocpVersion, err := cluster.GetClusterVersion(SpokeAPIClient) + if err != nil { + return err + } + + log.Printf("cluster '%s' has OCP version '%s'\n", SpokeName, ocpVersion) + } + + return nil +} + +// DefineAPIClient creates new api client instance connected to given cluster. +func DefineAPIClient(kubeconfigEnvVar string) (*clients.Settings, error) { + kubeFilePath, present := os.LookupEnv(kubeconfigEnvVar) + if !present { + return nil, fmt.Errorf("can not load api client. Please check %s env var", kubeconfigEnvVar) + } + + clients := clients.New(kubeFilePath) + if clients == nil { + return nil, fmt.Errorf("client is not set please check %s env variable", kubeconfigEnvVar) + } + + return clients, nil +} + +// GetOperatorVersionFromCSV returns operator version from csv. +func GetOperatorVersionFromCSV(client *clients.Settings, operatorName, operatorNamespace string) (string, error) { + // Check if the client is valid + if client == nil { + return "", fmt.Errorf("provided nil client") + } + + // Get the CSV objects + csv, err := olm.ListClusterServiceVersion(client, operatorNamespace, metav1.ListOptions{}) + + // Check for any error getting the CSVs + if err != nil { + return "", err + } + + // Find the CSV that matches the operator + for _, csv := range csv { + if strings.Contains(csv.Object.Name, operatorName) { + return csv.Object.Spec.Version.String(), nil + } + } + + return "", nil +} + +// GetZtpVersionFromArgocd is used to fetch the version of the ztp-site-generator init container. +func GetZtpVersionFromArgocd(client *clients.Settings, name string, namespace string) (string, error) { + deployment, err := deployment.Pull(client, name, namespace) + if err != nil { + return "", err + } + + for _, container := range deployment.Object.Spec.Template.Spec.InitContainers { + // Legacy 4.11 uses the image name as `ztp-site-generator` + // While 4.12+ uses the image name as `ztp-site-generate` + // So just check for `ztp-site-gen` to cover both + if strings.Contains(container.Image, "ztp-site-gen") { + arr := strings.Split(container.Image, ":") + // Get the image tag which is the last element + ztpVersion := arr[len(arr)-1] + + if ztpVersion == "latest" { + log.Println("Site generator version tag was 'latest', so returning empty version") + + return "", nil + } + + // The format here will be like vX.Y.Z so we need to remove the v at the start + fmt.Println("ztpVersion = ", ztpVersion) + + return ztpVersion[1:], nil + } + } + + return "", fmt.Errorf("unable to identify ztp version") +} diff --git a/tests/gitopsztp/internal/gitopsztpparams/gitopsztpparams.go b/tests/gitopsztp/internal/gitopsztpparams/gitopsztpparams.go new file mode 100644 index 00000000..65885e33 --- /dev/null +++ b/tests/gitopsztp/internal/gitopsztpparams/gitopsztpparams.go @@ -0,0 +1,20 @@ +package gitopsztpparams + +const ( + // HubKubeEnvKey is the hub's kubeconfig env var. + HubKubeEnvKey string = "KUBECONFIG_HUB" + // SpokeKubeEnvKey is the spoke's kubeconfig env var. + SpokeKubeEnvKey string = "KUBECONFIG" + // AcmOperatorName operator name of ACM. + AcmOperatorName string = "advanced-cluster-management" + // AcmOperatorNamespace ACM's namespace. + AcmOperatorNamespace string = "rhacm" + // OpenshiftGitops name. + OpenshiftGitops string = "openshift-gitops" + // OpenshiftGitopsRepoServer ocp git repo server. + OpenshiftGitopsRepoServer string = "openshift-gitops-repo-server" + // OperatorHubTalmNamespace talm namespace. + OperatorHubTalmNamespace string = "topology-aware-lifecycle-manager" + // OpenshiftOperatorNamespace ocp namespaces. + OpenshiftOperatorNamespace string = "openshift-operators" +) diff --git a/tests/talm/talmhelper/talmhelper.go b/tests/gitopsztp/talm/internal/talmhelper/talmhelper.go similarity index 100% rename from tests/talm/talmhelper/talmhelper.go rename to tests/gitopsztp/talm/internal/talmhelper/talmhelper.go diff --git a/tests/talm/talmparams/talmparameters.go b/tests/gitopsztp/talm/internal/talmparams/talmparams.go similarity index 96% rename from tests/talm/talmparams/talmparameters.go rename to tests/gitopsztp/talm/internal/talmparams/talmparams.go index cc84655f..cc69f157 100644 --- a/tests/talm/talmparams/talmparameters.go +++ b/tests/gitopsztp/talm/internal/talmparams/talmparams.go @@ -6,6 +6,7 @@ import ( // talm related vars. const ( + HubKubeEnvKey = "KUBECONFIG_HUB" TalmUpdatedConditionsVersion = "4.12" OpenshiftOperatorNamespace = "openshift-operators" OperatorHubTalmNamespace = "topology-aware-lifecycle-manager" diff --git a/tests/talm/talmparams/talmparams.go b/tests/talm/talmparams/talmparams.go deleted file mode 100644 index 63ed1e5d..00000000 --- a/tests/talm/talmparams/talmparams.go +++ /dev/null @@ -1 +0,0 @@ -package talmparams