Skip to content

Commit

Permalink
Merge pull request #32 from shaior/talm_suite
Browse files Browse the repository at this point in the history
Set talm suite and util functions
  • Loading branch information
mcornea authored Oct 24, 2023
2 parents a87cd8a + 42ef709 commit 3646b22
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 0 deletions.
145 changes: 145 additions & 0 deletions tests/gitopsztp/talm/internal/talmhelper/talmhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,48 @@ package talmhelper
import (
"errors"
"fmt"
"os"
"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"
"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"
)

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
Expand Down Expand Up @@ -40,3 +74,114 @@ 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
}

// 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
}
15 changes: 15 additions & 0 deletions tests/gitopsztp/talm/internal/talmparams/talmparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions tests/gitopsztp/talm/talm_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ 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/gitopsztp/talm/tests"
. "github.com/openshift-kni/eco-gosystem/tests/internal/inittools"
)

Expand All @@ -20,3 +27,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)
})
57 changes: 57 additions & 0 deletions tests/gitopsztp/talm/tests/talm_canary.go
Original file line number Diff line number Diff line change
@@ -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())
})

})

0 comments on commit 3646b22

Please sign in to comment.