Skip to content

Commit

Permalink
Ingress RBAC tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dasarinaidu committed Jan 6, 2025
1 parent 32d38e8 commit 37aa7db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 70 deletions.
36 changes: 0 additions & 36 deletions tests/v2/validation/rbac/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,39 +392,3 @@ func updateDeploymentWorkload(client *rancher.Client, clusterID, namespace, name

return updatedDeployObj, nil
}

// Helper method to create ingress template
func createIngressTemplate(workload *v1.SteveAPIObject, namespace string) networkingv1.Ingress {
ingressName := namegen.AppendRandomString("test-ingress")
pathType := networkingv1.PathTypePrefix

return networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: ingressName,
Namespace: namespace,
},
Spec: networkingv1.IngressSpec{
Rules: []networkingv1.IngressRule{
{
Host: fmt.Sprintf("%s.foo.com", namegen.AppendRandomString("test")),
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
PathType: &pathType,
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: workload.Name,
Port: networkingv1.ServiceBackendPort{Number: 80},
},
},
},
},
},
},
},
},
},
}
}
66 changes: 32 additions & 34 deletions tests/v2/validation/rbac/ingress/ingress_rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

networkingv1 "k8s.io/api/networking/v1"

"github.com/rancher/rancher/tests/v2/actions/ingresses"
"github.com/rancher/rancher/tests/v2/actions/projects"
"github.com/rancher/rancher/tests/v2/actions/rbac"
Expand All @@ -34,52 +36,28 @@ type IngressRBACTestSuite struct {
}

func (i *IngressRBACTestSuite) SetupSuite() {
testSession := session.NewSession()
i.session = testSession
i.session = session.NewSession()

client, err := rancher.NewClient("", testSession)
client, err := rancher.NewClient("", i.session)
require.NoError(i.T(), err)
i.client = client

log.Info("Getting cluster name from the config file and append cluster details in i")
clusterName := client.RancherConfig.ClusterName
require.NotEmptyf(i.T(), clusterName, "Cluster name should be set")

clusterID, err := clusters.GetClusterIDByName(i.client, clusterName)
require.NoError(i.T(), err)

i.cluster, err = i.client.Management.Cluster.ByID(clusterID)
require.NoError(i.T(), err)
require.NoError(i.T(), err, "Error getting cluster ID")
}

func (i *IngressRBACTestSuite) TearDownSuite() {
log.Info("Starting test suite cleanup")
i.session.Cleanup()
log.Info("Cleanup completed")

if i.session != nil {
log.Info("Cleaning up session resources")
i.session.Cleanup()
}

if i.client != nil && i.cluster != nil {
log.Info("Cleaning up any remaining ingresses")
steveClient, err := i.client.Steve.ProxyDownstream(i.cluster.ID)
if err != nil {
log.Errorf("Error getting steve client during cleanup: %v", err)
return
}

ingressList, err := steveClient.SteveType("networking.k8s.io.ingress").List(nil)
if err != nil {
log.Errorf("Error listing ingresses during cleanup: %v", err)
return
}

for _, ingress := range ingressList.Data {
if err := steveClient.SteveType(ingress.Type).Delete(&ingress); err != nil {
log.Errorf("Error deleting ingress %s during cleanup: %v", ingress.Name, err)
}
}
log.Info("Cleanup completed")
}
}

func (i *IngressRBACTestSuite) TestCreateIngress() {
Expand Down Expand Up @@ -117,7 +95,12 @@ func (i *IngressRBACTestSuite) TestCreateIngress() {
err = v1.ConvertToK8sType(workload, steveWorkload)
require.NoError(i.T(), err)

ingressTemplate := createIngressTemplate(steveWorkload, namespace.Name)
pathType := networkingv1.PathTypePrefix
ingressPath := extensionsingress.NewIngressPathTemplate(pathType, "/", steveWorkload.Name, 80)

ingressName := namegen.AppendRandomString("test-ingress")
hostName := fmt.Sprintf("%s.foo.com", namegen.AppendRandomString("test"))
ingressTemplate := extensionsingress.NewIngressTemplate(ingressName, namespace.Name, hostName, []networkingv1.HTTPIngressPath{ingressPath})

steveClient, err := standardUserClient.Steve.ProxyDownstream(i.cluster.ID)
require.NoError(i.T(), err)
Expand Down Expand Up @@ -214,7 +197,12 @@ func (i *IngressRBACTestSuite) TestListIngress() {
err = v1.ConvertToK8sType(workload, steveWorkload)
require.NoError(i.T(), err)

ingressTemplate := createIngressTemplate(steveWorkload, namespace.Name)
pathType := networkingv1.PathTypePrefix
ingressPath := extensionsingress.NewIngressPathTemplate(pathType, "/", steveWorkload.Name, 80)

ingressName := namegen.AppendRandomString("test-ingress")
hostName := fmt.Sprintf("%s.foo.com", namegen.AppendRandomString("test"))
ingressTemplate := extensionsingress.NewIngressTemplate(ingressName, namespace.Name, hostName, []networkingv1.HTTPIngressPath{ingressPath})

log.Info("Creating test ingress")
ingress, err := extensionsingress.CreateIngress(adminSteveClient, ingressTemplate.Name, ingressTemplate)
Expand Down Expand Up @@ -306,7 +294,12 @@ func (i *IngressRBACTestSuite) TestUpdateIngress() {
err = v1.ConvertToK8sType(workload, steveWorkload)
require.NoError(i.T(), err)

ingressTemplate := createIngressTemplate(steveWorkload, namespace.Name)
pathType := networkingv1.PathTypePrefix
ingressPath := extensionsingress.NewIngressPathTemplate(pathType, "/", steveWorkload.Name, 80)

ingressName := namegen.AppendRandomString("test-ingress")
hostName := fmt.Sprintf("%s.foo.com", namegen.AppendRandomString("test"))
ingressTemplate := extensionsingress.NewIngressTemplate(ingressName, namespace.Name, hostName, []networkingv1.HTTPIngressPath{ingressPath})

log.Info("Setting up admin steve client")
adminSteveClient, err := i.client.Steve.ProxyDownstream(i.cluster.ID)
Expand Down Expand Up @@ -388,7 +381,12 @@ func (i *IngressRBACTestSuite) TestDeleteIngress() {
err = v1.ConvertToK8sType(workload, steveWorkload)
require.NoError(i.T(), err)

ingressTemplate := createIngressTemplate(steveWorkload, namespace.Name)
pathType := networkingv1.PathTypePrefix
ingressPath := extensionsingress.NewIngressPathTemplate(pathType, "/", steveWorkload.Name, 80)

ingressName := namegen.AppendRandomString("test-ingress")
hostName := fmt.Sprintf("%s.foo.com", namegen.AppendRandomString("test"))
ingressTemplate := extensionsingress.NewIngressTemplate(ingressName, namespace.Name, hostName, []networkingv1.HTTPIngressPath{ingressPath})

log.Info("Setting up admin steve client")
adminSteveClient, err := i.client.Steve.ProxyDownstream(i.cluster.ID)
Expand Down

0 comments on commit 37aa7db

Please sign in to comment.