Skip to content

Commit

Permalink
Merge pull request rancher#47674 from Priyashetty17/main_rbac-P0proje…
Browse files Browse the repository at this point in the history
…cts-1251

[main] P0 Project RBAC Tests
  • Loading branch information
Priyashetty17 authored Oct 24, 2024
2 parents 21c339d + 5b8000d commit 695357a
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 122 deletions.
2 changes: 1 addition & 1 deletion tests/v2/actions/workloads/daemonset/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var DaemonsetGroupVersionResource = schema.GroupVersionResource{

// CreateDaemonset is a helper to create a daemonset
func CreateDaemonset(client *rancher.Client, clusterID, namespaceName string, replicaCount int, secretName, configMapName string, useEnvVars, useVolumes bool) (*appv1.DaemonSet, error) {
deploymentTemplate, err := deployment.CreateDeployment(client, clusterID, namespaceName, replicaCount, secretName, configMapName, useEnvVars, useVolumes)
deploymentTemplate, err := deployment.CreateDeployment(client, clusterID, namespaceName, replicaCount, secretName, configMapName, useEnvVars, useVolumes, true)
if err != nil {
return nil, err
}
Expand Down
17 changes: 15 additions & 2 deletions tests/v2/actions/workloads/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/rancher/rancher/tests/v2/actions/kubeapi/workloads/deployments"
"github.com/rancher/rancher/tests/v2/actions/workloads/pods"
"github.com/rancher/shepherd/clients/rancher"
"github.com/rancher/shepherd/extensions/charts"
"github.com/rancher/shepherd/extensions/workloads"
namegen "github.com/rancher/shepherd/pkg/namegenerator"
"github.com/rancher/shepherd/pkg/wrangler"
Expand All @@ -21,7 +22,7 @@ const (
)

// CreateDeployment is a helper to create a deployment with or without a secret/configmap
func CreateDeployment(client *rancher.Client, clusterID, namespaceName string, replicaCount int, secretName, configMapName string, useEnvVars, useVolumes bool) (*appv1.Deployment, error) {
func CreateDeployment(client *rancher.Client, clusterID, namespaceName string, replicaCount int, secretName, configMapName string, useEnvVars, useVolumes, watchDeployment bool) (*appv1.Deployment, error) {
deploymentName := namegen.AppendRandomString("testdeployment")
containerName := namegen.AppendRandomString("testcontainer")
pullPolicy := corev1.PullAlways
Expand Down Expand Up @@ -56,11 +57,17 @@ func CreateDeployment(client *rancher.Client, clusterID, namespaceName string, r
return nil, err
}

if watchDeployment {
err = charts.WatchAndWaitDeployments(client, clusterID, namespaceName, metav1.ListOptions{
FieldSelector: "metadata.name=" + createdDeployment.Name,
})
}

return createdDeployment, err
}

// UpdateDeployment is a helper to update deployments
func UpdateDeployment(client *rancher.Client, clusterID, namespaceName string, deployment *appv1.Deployment) (*appv1.Deployment, error) {
func UpdateDeployment(client *rancher.Client, clusterID, namespaceName string, deployment *appv1.Deployment, watchDeployment bool) (*appv1.Deployment, error) {
var wranglerContext *wrangler.Context
var err error

Expand All @@ -84,6 +91,12 @@ func UpdateDeployment(client *rancher.Client, clusterID, namespaceName string, d
return nil, err
}

if watchDeployment {
err = charts.WatchAndWaitDeployments(client, clusterID, namespaceName, metav1.ListOptions{
FieldSelector: "metadata.name=" + updatedDeployment.Name,
})
}

return updatedDeployment, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (

"github.com/rancher/rancher/tests/v2/actions/kubeapi/namespaces"
"github.com/rancher/rancher/tests/v2/actions/kubeapi/projects"
project "github.com/rancher/rancher/tests/v2/actions/projects"
"github.com/rancher/rancher/tests/v2/actions/rbac"
deployment "github.com/rancher/rancher/tests/v2/actions/workloads/deployment"
"github.com/rancher/shepherd/clients/rancher"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/charts"
"github.com/rancher/shepherd/extensions/clusters"
"github.com/rancher/shepherd/extensions/users"
"github.com/rancher/shepherd/pkg/session"
Expand Down Expand Up @@ -163,8 +161,6 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestCpuAndMemoryLimitEqualT
require.Equal(pcrl.T(), memoryReservation, projectSpec.RequestsMemory, "Memory reservation mismatch")

log.Info("Verify that the namespace has the label and annotation referencing the project.")
err = project.WaitForProjectIDAnnotationUpdate(standardUserClient, pcrl.cluster.ID, createdProject.Name, createdNamespace.Name)
require.NoError(pcrl.T(), err)
updatedNamespace, err := namespaces.GetNamespaceByName(standardUserClient, pcrl.cluster.ID, createdNamespace.Name)
require.NoError(pcrl.T(), err)
err = checkNamespaceLabelsAndAnnotations(pcrl.cluster.ID, createdProject.Name, updatedNamespace)
Expand All @@ -175,12 +171,8 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestCpuAndMemoryLimitEqualT
require.NoError(pcrl.T(), err)

log.Info("Create a deployment in the namespace with one replica and verify that a pod is created.")
createdDeployment, err := deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false)
createdDeployment, err := deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false, true)
require.NoError(pcrl.T(), err, "Failed to create deployment in the namespace")
err = charts.WatchAndWaitDeployments(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, metav1.ListOptions{
FieldSelector: "metadata.name=" + createdDeployment.Name,
})
require.NoError(pcrl.T(), err)

log.Info("Verify that the resource limits and requests for the container in the pod spec is accurate.")
err = checkContainerResources(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, createdDeployment.Name, cpuLimit, cpuReservation, memoryLimit, memoryReservation)
Expand Down Expand Up @@ -210,8 +202,6 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestCpuAndMemoryLimitGreate
require.Equal(pcrl.T(), memoryReservation, projectSpec.RequestsMemory, "Memory reservation mismatch")

log.Info("Verify that the namespace has the label and annotation referencing the project.")
err = project.WaitForProjectIDAnnotationUpdate(standardUserClient, pcrl.cluster.ID, createdProject.Name, createdNamespace.Name)
require.NoError(pcrl.T(), err)
updatedNamespace, err := namespaces.GetNamespaceByName(standardUserClient, pcrl.cluster.ID, createdNamespace.Name)
require.NoError(pcrl.T(), err)
err = checkNamespaceLabelsAndAnnotations(pcrl.cluster.ID, createdProject.Name, updatedNamespace)
Expand All @@ -222,12 +212,8 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestCpuAndMemoryLimitGreate
require.NoError(pcrl.T(), err)

log.Info("Create a deployment in the namespace with one replica and verify that a pod is created.")
createdDeployment, err := deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false)
createdDeployment, err := deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false, true)
require.NoError(pcrl.T(), err, "Failed to create deployment in the namespace")
err = charts.WatchAndWaitDeployments(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, metav1.ListOptions{
FieldSelector: "metadata.name=" + createdDeployment.Name,
})
require.NoError(pcrl.T(), err)

log.Info("Verify that the resource limits and requests for the container in the pod spec is accurate.")
err = checkContainerResources(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, createdDeployment.Name, cpuLimit, cpuReservation, memoryLimit, memoryReservation)
Expand Down Expand Up @@ -368,8 +354,6 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestLimitDeletionPropagatio
require.Equal(pcrl.T(), memoryReservation, projectSpec.RequestsMemory, "Memory reservation mismatch")

log.Info("Verify that the namespace has the label and annotation referencing the project.")
err = project.WaitForProjectIDAnnotationUpdate(standardUserClient, pcrl.cluster.ID, createdProject.Name, createdNamespace.Name)
require.NoError(pcrl.T(), err)
updatedNamespace, err := namespaces.GetNamespaceByName(standardUserClient, pcrl.cluster.ID, createdNamespace.Name)
require.NoError(pcrl.T(), err)
err = checkNamespaceLabelsAndAnnotations(pcrl.cluster.ID, createdProject.Name, updatedNamespace)
Expand Down Expand Up @@ -401,12 +385,8 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestLimitDeletionPropagatio
require.Equal(pcrl.T(), 0, len(limitRanges.Items))

log.Info("Create a deployment in the namespace with one replica and verify that a pod is created.")
createdDeployment, err := deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false)
createdDeployment, err := deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false, true)
require.NoError(pcrl.T(), err, "Failed to create deployment in the namespace")
err = charts.WatchAndWaitDeployments(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, metav1.ListOptions{
FieldSelector: "metadata.name=" + createdDeployment.Name,
})
require.NoError(pcrl.T(), err)

log.Info("Verify that the resource limits and requests for the container in the pod spec is accurate.")
err = checkContainerResources(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, createdDeployment.Name, cpuLimit, cpuReservation, memoryLimit, memoryReservation)
Expand Down Expand Up @@ -436,8 +416,6 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestOverrideDefaultLimitInN
require.Equal(pcrl.T(), memoryReservation, projectSpec.RequestsMemory, "Memory reservation mismatch")

log.Info("Verify that the namespace has the label and annotation referencing the project.")
err = project.WaitForProjectIDAnnotationUpdate(standardUserClient, pcrl.cluster.ID, createdProject.Name, createdNamespace.Name)
require.NoError(pcrl.T(), err)
updatedNamespace, err := namespaces.GetNamespaceByName(standardUserClient, pcrl.cluster.ID, createdNamespace.Name)
require.NoError(pcrl.T(), err)
err = checkNamespaceLabelsAndAnnotations(pcrl.cluster.ID, createdProject.Name, updatedNamespace)
Expand All @@ -448,12 +426,8 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestOverrideDefaultLimitInN
require.NoError(pcrl.T(), err)

log.Info("Create a deployment in the namespace with one replica and verify that a pod is created.")
createdDeployment, err := deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false)
createdDeployment, err := deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false, true)
require.NoError(pcrl.T(), err, "Failed to create deployment in the namespace")
err = charts.WatchAndWaitDeployments(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, metav1.ListOptions{
FieldSelector: "metadata.name=" + createdDeployment.Name,
})
require.NoError(pcrl.T(), err)

log.Info("Verify that the resource limits and requests for the container in the pod spec is accurate.")
err = checkContainerResources(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, createdDeployment.Name, cpuLimit, cpuReservation, memoryLimit, memoryReservation)
Expand All @@ -479,12 +453,8 @@ func (pcrl *ProjectsContainerResourceLimitTestSuite) TestOverrideDefaultLimitInN
require.NoError(pcrl.T(), err)

log.Info("Create a deployment in the namespace with one replica and verify that a pod is created.")
createdDeployment, err = deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false)
createdDeployment, err = deployment.CreateDeployment(standardUserClient, pcrl.cluster.ID, updatedNamespace.Name, 1, "", "", false, false, true)
require.NoError(pcrl.T(), err, "Failed to create deployment in the namespace")
err = charts.WatchAndWaitDeployments(standardUserClient, pcrl.cluster.ID, namespace.Name, metav1.ListOptions{
FieldSelector: "metadata.name=" + createdDeployment.Name,
})
require.NoError(pcrl.T(), err)

log.Info("Verify that the resource limits and requests for the container in the pod spec is accurate.")
err = checkContainerResources(standardUserClient, pcrl.cluster.ID, namespace.Name, createdDeployment.Name, cpuLimit, cpuReservation, memoryLimit, memoryReservation)
Expand Down
Loading

0 comments on commit 695357a

Please sign in to comment.