Skip to content

Commit

Permalink
RDS, egressIP test added
Browse files Browse the repository at this point in the history
  • Loading branch information
elenagerman committed Dec 6, 2024
1 parent b0c647e commit 1705ef4
Show file tree
Hide file tree
Showing 6 changed files with 758 additions and 296 deletions.
260 changes: 260 additions & 0 deletions tests/system-tests/internal/apiobjectshelper/apiobjectshelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"fmt"
"time"

"github.com/openshift-kni/eco-goinfra/pkg/deployment"
"github.com/openshift-kni/eco-goinfra/pkg/pod"
"github.com/openshift-kni/eco-goinfra/pkg/rbac"
"github.com/openshift-kni/eco-goinfra/pkg/service"
"github.com/openshift-kni/eco-goinfra/pkg/serviceaccount"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/namespace"
Expand Down Expand Up @@ -81,3 +89,255 @@ func VerifyOperatorDeployment(apiClient *clients.Settings,

return nil
}

// CreateServiceAccount creates the service account and verifies it was created.
func CreateServiceAccount(apiClient *clients.Settings, saName, nsName string) error {
glog.V(100).Infof(fmt.Sprintf("Creating ServiceAccount %q in %q namespace",
saName, nsName))
glog.V(100).Infof("Creating SA %q in %q namespace", saName, nsName)

deploySa := serviceaccount.NewBuilder(apiClient, saName, nsName)

err := wait.PollUntilContextTimeout(
context.TODO(),
time.Second*15,
time.Minute,
true,
func(ctx context.Context) (bool, error) {
deploySa, err := deploySa.Create()

if err != nil {
glog.V(100).Infof("Error creating SA %q in %q namespace: %v", saName, nsName, err)

return false, nil
}

glog.V(100).Infof("Created SA %q in %q namespace",
deploySa.Definition.Name, deploySa.Definition.Namespace)

return true, nil
})

if err != nil {
return fmt.Errorf("failed to create ServiceAccount %q in %q namespace", saName, nsName)
}

return nil
}

// CreateClusterRBAC creates the RBAC and verifies it was created.
func CreateClusterRBAC(
apiClient *clients.Settings,
rbacName, clusterRole, saName, nsName string) error {
glog.V(100).Infof("Creating RBAC for SA %s", saName)

glog.V(100).Infof("Creating ClusterRoleBinding %q", rbacName)
crbSa := rbac.NewClusterRoleBindingBuilder(
apiClient,
rbacName,
clusterRole,
rbacv1.Subject{
Name: saName,
Kind: "ServiceAccount",
Namespace: nsName,
})

err := wait.PollUntilContextTimeout(
context.TODO(),
time.Second*15,
time.Minute,
true,
func(ctx context.Context) (bool, error) {
crbSa, err := crbSa.Create()
if err != nil {
glog.V(100).Infof(
"Error Creating ClusterRoleBinding %q : %v", crbSa.Definition.Name, err)

return false, nil
}

glog.V(100).Infof("ClusterRoleBinding %q created:\n\t%v",
crbSa.Definition.Name, crbSa)

return true, nil
})

if err != nil {
return fmt.Errorf("failed to create ClusterRoleBinding '%s' during timeout %v; %w",
rbacName, time.Minute, err)
}

return nil
}

// DeleteService deletes the service and verifies it was removed.
func DeleteService(apiClient *clients.Settings, svcName, nsName string) error {
glog.V(100).Infof("Delete service %q from namespace %s", svcName, nsName)

if svcObj, err := service.Pull(
apiClient, svcName, nsName); err == nil {
glog.V(100).Infof("Service %q found in %q nsname", svcName, nsName)
glog.V(100).Infof("Deleting service %q in %q nsname", svcName, nsName)

err = wait.PollUntilContextTimeout(
context.TODO(),
time.Second*15,
time.Minute,
true,
func(ctx context.Context) (bool, error) {
err := svcObj.Delete()

if err != nil {
glog.V(100).Infof("Error deleting service %q in %q nsname: %v",
svcName, nsName, err)

return false, nil
}

glog.V(100).Infof("Deleted service %q in %q nsname", svcName, nsName)

return true, nil
})

if err != nil {
return fmt.Errorf("failed to delete service %q from %q ns", svcName, nsName)
}
} else {
glog.V(100).Infof("service %q not found in %q nsname", svcName, nsName)
}

return nil
}

// DeleteClusterRBAC deletes the RBAC and verifies it was removed.
func DeleteClusterRBAC(apiClient *clients.Settings, rbacName string) error {
glog.V(100).Infof("Deleting Cluster RBAC")

glog.V(100).Infof("Assert ClusterRoleBinding %q exists", rbacName)

crbSa, err := rbac.PullClusterRoleBinding(apiClient, rbacName)

if err != nil {
glog.V(100).Infof("ClusterRoleBinding %q not found; %v", rbacName, err)

return nil
}

glog.V(100).Infof("ClusterRoleBinding %q found. Deleting...", rbacName)

err = wait.PollUntilContextTimeout(
context.TODO(),
time.Second*15,
time.Minute,
true,
func(ctx context.Context) (bool, error) {
err = crbSa.Delete()

if err != nil {
glog.V(100).Infof("Error deleting ClusterRoleBinding %q : %v", rbacName, err)

return false, nil
}

glog.V(100).Infof("Deleted ClusterRoleBinding %q", rbacName)

return true, nil
})

if err != nil {
return fmt.Errorf("failed to delete Cluster RBAC %q", rbacName)
}

return nil
}

// DeleteServiceAccount deletes the service account and verifies it was removed.
func DeleteServiceAccount(apiClient *clients.Settings, saName, nsName string) error {
glog.V(100).Infof("Removing Service Account")
glog.V(100).Infof("Assert SA %q exists in %q namespace", saName, nsName)

if deploySa, err := serviceaccount.Pull(
apiClient, saName, nsName); err == nil {
glog.V(100).Infof("ServiceAccount %q found in %q namespace", saName, nsName)
glog.V(100).Infof("Deleting ServiceAccount %q in %q namespace", saName, nsName)

err = wait.PollUntilContextTimeout(
context.TODO(),
time.Second*15,
time.Minute,
true,
func(ctx context.Context) (bool, error) {
err := deploySa.Delete()

if err != nil {
glog.V(100).Infof("Error deleting ServiceAccount %q in %q namespace: %v",
saName, nsName, err)

return false, nil
}

glog.V(100).Infof("Deleted ServiceAccount %q in %q namespace", saName, nsName)

return true, nil
})

if err != nil {
return fmt.Errorf("failed to delete ServiceAccount %q from %q ns", saName, nsName)
}
} else {
glog.V(100).Infof("ServiceAccount %q not found in %q namespace", saName, nsName)
}

return nil
}

// DeleteDeployment deletes the deployment and verifies it and all related pods were removed.
func DeleteDeployment(
apiClient *clients.Settings,
deploymentName, nsName string) error {
glog.V(100).Infof("Removing test deployment %q from %q ns", deploymentName, nsName)

if deploymentObj, err := deployment.Pull(apiClient, deploymentName, nsName); err == nil {
glog.V(100).Infof("Deleting deployment %q from %q namespace", deploymentName, nsName)

err = deploymentObj.DeleteAndWait(300 * time.Second)

if err != nil {
glog.V(100).Infof("Error deleting deployment %q from %q namespace: %v",
deploymentName, nsName, err)

return fmt.Errorf("failed to delete deployment %q from %q namespace: %w",
deploymentName, nsName, err)
}
} else {
glog.V(100).Infof("deployment %q not found in %q namespace", deploymentName, nsName)
}

return nil
}

// EnsureAllPodsRemoved Ensure all deployment pods in namespace with the specific pod label were removed.
func EnsureAllPodsRemoved(
apiClient *clients.Settings,
nsName, podLabel string) error {
glog.V(100).Infof("Ensuring pods in %q namespace with label %q are gone", nsName, podLabel)

err := wait.PollUntilContextTimeout(
context.TODO(),
time.Second*3,
time.Minute*6,
true,
func(ctx context.Context) (bool, error) {
oldPods, _ := pod.List(apiClient, nsName,
metav1.ListOptions{LabelSelector: podLabel})

return len(oldPods) == 0, nil
})

if err != nil {
return fmt.Errorf("pods matching label(%q) still present in namespace %q",
podLabel, nsName)
}

return nil
}
Loading

0 comments on commit 1705ef4

Please sign in to comment.