Skip to content

Commit

Permalink
UR tests on to v2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
dasarinaidu committed Dec 19, 2024
1 parent 67c6952 commit 92d0a3e
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 3 deletions.
21 changes: 20 additions & 1 deletion tests/v2/actions/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import (
"strings"

"github.com/rancher/norman/types"
<<<<<<< HEAD
rbacv2 "github.com/rancher/rancher/tests/v2/actions/kubeapi/rbac"
"github.com/rancher/shepherd/clients/rancher"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
=======
"github.com/rancher/shepherd/clients/rancher"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
rbacv2 "github.com/rancher/shepherd/extensions/kubeapi/rbac"
"github.com/rancher/shepherd/extensions/rbac"
>>>>>>> 0bb9b098f (Automation tests for userretention)
"github.com/rancher/shepherd/extensions/users"
"github.com/sirupsen/logrus"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -75,7 +82,11 @@ func SetupUser(client *rancher.Client, globalRole string) (user *management.User
return
}

<<<<<<< HEAD
// GetRoleBindings is a helper function to fetch rolebindings for a user
=======
//GetRoleBindings is a helper function to fetch rolebindings for a user
>>>>>>> 0bb9b098f (Automation tests for userretention)
func GetRoleBindings(rancherClient *rancher.Client, clusterID string, userID string) ([]rbacv1.RoleBinding, error) {
logrus.Infof("Getting role bindings for user %s in cluster %s", userID, clusterID)
listOpt := v1.ListOptions{}
Expand All @@ -97,7 +108,11 @@ func GetRoleBindings(rancherClient *rancher.Client, clusterID string, userID str
return userRoleBindings, nil
}

<<<<<<< HEAD
// GetBindings is a helper function to fetch bindings for a user
=======
//GetBindings is a helper function to fetch bindings for a user
>>>>>>> 0bb9b098f (Automation tests for userretention)
func GetBindings(rancherClient *rancher.Client, userID string) (map[string]interface{}, error) {
logrus.Infof("Getting all bindings for user %s", userID)
bindings := make(map[string]interface{})
Expand Down Expand Up @@ -131,4 +146,8 @@ func GetBindings(rancherClient *rancher.Client, userID string) (map[string]inter

logrus.Info("All bindings retrieved successfully")
return bindings, nil
}
<<<<<<< HEAD
}
=======
}
>>>>>>> 0bb9b098f (Automation tests for userretention)
13 changes: 13 additions & 0 deletions tests/v2/validation/auth/userretention/userretention.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

const (
<<<<<<< HEAD
disableInactiveUserAfter = "disable-inactive-user-after"
deleteInactiveUserAfter = "delete-inactive-user-after"
userRetentionCron = "user-retention-cron"
Expand All @@ -26,6 +27,18 @@ const (
isActive = true
isInActive = false
webhookErrorMessage = "admission webhook \"rancher.cattle.io.settings.management.cattle.io\" denied the request: value: Invalid value:"
=======
disableInactiveUserAfter = "disable-inactive-user-after"
deleteInactiveUserAfter = "delete-inactive-user-after"
userRetentionCron = "user-retention-cron"
userRetentionDryRun = "user-retention-dry-run"
lastloginLabel = "cattle.io/last-login"
defaultWaitDuration = 70 * time.Second
pollInterval = 10 * time.Second
isActive = true
isInActive = false
webhookErrorMessage = "admission webhook \"rancher.cattle.io.settings.management.cattle.io\" denied the request: value: Invalid value:"
>>>>>>> 0bb9b098f (Automation tests for userretention)
)

func setupUserRetentionSettings(client *rancher.Client, disableAfterValue string, deleteAfterValue string, userRetentionCronValue string, dryRunValue string) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,8 @@ func (ur *URDeleteTestSuite) TestUserIsNotGetDeletedWithDryRun() {

func TestURDeleteUserSuite(t *testing.T) {
suite.Run(t, new(URDeleteTestSuite))
}
<<<<<<< HEAD
}
=======
}
>>>>>>> 0bb9b098f (Automation tests for userretention)
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,8 @@ func (ur *URDisableTestSuite) TestUserIsNotDisabledWithDryRun() {

func TestURDisableUserSuite(t *testing.T) {
suite.Run(t, new(URDisableTestSuite))
}
<<<<<<< HEAD
}
=======
}
>>>>>>> 0bb9b098f (Automation tests for userretention)
160 changes: 160 additions & 0 deletions tests/v2/validation/auth/userretention/userretention_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@ package userretention
import (
"testing"

<<<<<<< HEAD
=======
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

>>>>>>> 0bb9b098f (Automation tests for userretention)
"github.com/rancher/shepherd/clients/rancher"
"github.com/rancher/shepherd/pkg/session"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
<<<<<<< HEAD
=======
apierrors "k8s.io/apimachinery/pkg/api/errors"
>>>>>>> 0bb9b098f (Automation tests for userretention)
)

type UserRetentionSettingsTestSuite struct {
suite.Suite
<<<<<<< HEAD
client *rancher.Client
session *session.Session
originalTTL string
settingsReset bool
=======
client *rancher.Client
session *session.Session
>>>>>>> 0bb9b098f (Automation tests for userretention)
}

func (ur *UserRetentionSettingsTestSuite) SetupSuite() {
ur.session = session.NewSession()
client, err := rancher.NewClient("", ur.session)
require.NoError(ur.T(), err)
ur.client = client
<<<<<<< HEAD

settings, err := ur.client.Management.Setting.ByID(authUserSessionTTLMinutes)
require.NoError(ur.T(), err)
Expand Down Expand Up @@ -93,12 +108,25 @@ func (ur *UserRetentionSettingsTestSuite) ensureDefaultTTL() error {

func (ur *UserRetentionSettingsTestSuite) testPositiveInputValues(settingName string, tests []struct {
name string
=======
}

func (ur *UserRetentionSettingsTestSuite) TearDownSuite() {
ur.session.Cleanup()
}

func (ur *UserRetentionSettingsTestSuite) testPositiveInputValues(settingName string, tests []struct {
>>>>>>> 0bb9b098f (Automation tests for userretention)
value string
description string
}) {
logrus.Infof("Updating %s settings with positive values:", settingName)
for _, inputValue := range tests {
<<<<<<< HEAD
ur.T().Run(inputValue.name, func(*testing.T) {
=======
ur.T().Run(inputValue.value, func(*testing.T) {
>>>>>>> 0bb9b098f (Automation tests for userretention)
err := updateUserRetentionSettings(ur.client, settingName, inputValue.value)
assert.NoError(ur.T(), err, "Unexpected error for input '%s'", inputValue.value)

Expand All @@ -115,13 +143,20 @@ func (ur *UserRetentionSettingsTestSuite) testPositiveInputValues(settingName st
}

func (ur *UserRetentionSettingsTestSuite) testNegativeInputValues(settingName string, tests []struct {
<<<<<<< HEAD
name string
=======
>>>>>>> 0bb9b098f (Automation tests for userretention)
value string
description string
}) {
logrus.Infof("Updating %s settings with negative values:", settingName)
for _, inputValue := range tests {
<<<<<<< HEAD
ur.T().Run(inputValue.name, func(*testing.T) {
=======
ur.T().Run(inputValue.value, func(*testing.T) {
>>>>>>> 0bb9b098f (Automation tests for userretention)
err := updateUserRetentionSettings(ur.client, settingName, inputValue.value)
assert.Error(ur.T(), err, "Expected an error for input '%s', but got nil", inputValue.value)

Expand All @@ -135,10 +170,35 @@ func (ur *UserRetentionSettingsTestSuite) testNegativeInputValues(settingName st
}

func (ur *UserRetentionSettingsTestSuite) validateError(err error, expectedDescription string) {
<<<<<<< HEAD
errMsg := err.Error()
if !assert.Contains(ur.T(), errMsg, expectedDescription, "Error message mismatch") {
logrus.Infof("Expected error to contain: %s", expectedDescription)
logrus.Infof("Actual error message: %s", errMsg)
=======
var statusErr *apierrors.StatusError
var found bool

switch e := err.(type) {
case interface{ Unwrap() error }:
if innerErr := e.Unwrap(); innerErr != nil {
if innerWrapper, ok := innerErr.(interface{ Unwrap() error }); ok {
if deepestErr := innerWrapper.Unwrap(); deepestErr != nil {
statusErr, found = deepestErr.(*apierrors.StatusError)
}
}
}
}

if found && statusErr != nil {
assert.Equal(ur.T(), int32(400), statusErr.ErrStatus.Code, "Status code should be 400")
assert.Equal(ur.T(), metav1.StatusReasonBadRequest, statusErr.ErrStatus.Reason, "Reason should be BadRequest")
assert.Contains(ur.T(), statusErr.ErrStatus.Message, expectedDescription, "Error should contain the expected description")
} else {
errMsg := err.Error()
assert.Contains(ur.T(), errMsg, "denied the request", "Error should mention denied request")
assert.Contains(ur.T(), errMsg, expectedDescription, "Error should contain the expected description")
>>>>>>> 0bb9b098f (Automation tests for userretention)
}
}

Expand All @@ -150,6 +210,7 @@ func (ur *UserRetentionSettingsTestSuite) validateSettingsNotUpdated(settingName
}
}

<<<<<<< HEAD
func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForAuthUserSessionTTLWithPositiveInputValues() {
tests := []struct {
name string
Expand Down Expand Up @@ -207,10 +268,28 @@ func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForDisableInactiveUs
{"DisableAfterUpdatedAboveMinimum", minDuration, "Users will be deactivated after minimum required duration"},
{"DisableAfterUpdatedTwoDays", "48h", "Users will be deactivated after 48h"},
{"DisableAfterUpdatedTenThousandHours", "10000h", "Users will be deactivated after 10000h"},
=======
func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForDisableInactiveUserAfterWithPositiveInputValues() {
tests := []struct {
value string
description string
}{
{"", "No action - users will not be deactivated"},
{"0s", "Users will be deactivated after 0s"},
{"0m", "Users will be deactivated after 0m"},
{"0h", "Users will be deactivated after 0h"},
{"10s", "Users will be deactivated after 10s"},
{"10m", "Users will be deactivated after 10m"},
{"20h", "Users will be deactivated after 20h"},
{"10000s", "Users will be deactivated after 10000s"},
{"10000m", "Users will be deactivated after 10000m"},
{"10000h", "Users will be deactivated after 10000h"},
>>>>>>> 0bb9b098f (Automation tests for userretention)
}
ur.testPositiveInputValues(disableInactiveUserAfter, tests)
}

<<<<<<< HEAD
func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForDeleteInactiveUserAfterWithNegativeInputValues() {
tests := []struct {
name string
Expand All @@ -222,12 +301,64 @@ func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForDeleteInactiveUse
{"DeleteErrorBelowTTL", "15h", "Forbidden: must be at least 336h0m0s"},
{"DeleteErrorInvalidUnit", "1d", "time: unknown unit"},
{"DeleteErrorNegative", "-20h", "negative value"},
=======
func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForDisableInactiveUserAfterWithNegativeInputValues() {
tests := []struct {
value string
description string
}{
{"10", "Invalid value: \"10\": time: missing unit in duration \"10\""},
{"10S", "Invalid value: \"10S\": time: unknown unit \"S\" in duration \"10S\""},
{"10M", "Invalid value: \"10M\": time: unknown unit \"M\" in duration \"10M\""},
{"10H", "Invalid value: \"10H\": time: unknown unit \"H\" in duration \"10H\""},
{"10sec", "Invalid value: \"10sec\": time: unknown unit \"sec\" in duration \"10sec\""},
{"10min", "Invalid value: \"10min\": time: unknown unit \"min\" in duration \"10min\""},
{"20hour", "Invalid value: \"20hour\": time: unknown unit \"hour\" in duration \"20hour\""},
{"1d", "Invalid value: \"1d\": time: unknown unit \"d\" in duration \"1d\""},
{"-20m", "Invalid value: \"-20m\": negative duration"},
{"tens", "Invalid value: \"tens\": time: invalid duration \"tens\""},
}
ur.testNegativeInputValues(disableInactiveUserAfter, tests)
}

func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForDeleteInactiveUserAfterWithPositiveInputValues() {
tests := []struct {
value string
description string
}{
{"", "No action - users will not be deleted"},
{"100000000s", "Users will delete after 100000000s"},
{"200000m", "Users will delete after 200000m"},
{"10000h", "Users will delete after 10000h"},
}
ur.testPositiveInputValues(deleteInactiveUserAfter, tests)
}

func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForDeleteInactiveUserAfterWithNegativeInputValues() {
tests := []struct {
value string
description string
}{
{"10", "Invalid value: \"10\": time: missing unit in duration \"10\""},
{"10s", "Invalid value: \"10s\": must be at least 336h0m0s"},
{"10m", "Invalid value: \"10m\": must be at least 336h0m0s"},
{"10h", "Invalid value: \"10h\": must be at least 336h0m0s"},
{"10S", "Invalid value: \"10S\": time: unknown unit \"S\" in duration \"10S\""},
{"10M", "Invalid value: \"10M\": time: unknown unit \"M\" in duration \"10M\""},
{"10H", "Invalid value: \"10H\": time: unknown unit \"H\" in duration \"10H\""},
{"10sec", "Invalid value: \"10sec\": time: unknown unit \"sec\" in duration \"10sec\""},
{"10min", "Invalid value: \"10min\": time: unknown unit \"min\" in duration \"10min\""},
{"20hour", "Invalid value: \"20hour\": time: unknown unit \"hour\" in duration \"20hour\""},
{"1d", "Invalid value: \"1d\": time: unknown unit \"d\" in duration \"1d\""},
{"-20m", "Invalid value: \"-20m\": negative duration"},
>>>>>>> 0bb9b098f (Automation tests for userretention)
}
ur.testNegativeInputValues(deleteInactiveUserAfter, tests)
}

func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForUserRetentionCronWithPositiveInputValues() {
tests := []struct {
<<<<<<< HEAD
name string
value string
description string
Expand All @@ -238,12 +369,27 @@ func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForUserRetentionCron
{"CronRunsEveryMinute", "* * * * *", "every min"},
{"CronRunsEvery2PMTo205PM", "0-5 14 * * *", "every minute 2:00 PM to 2:05 PM"},
{"CronRunsFirstSecondDayMidnight", "0 0 1,2 * *", "midnight of 1st, 2nd day"},
=======
value string
description string
}{
{"0 * * * *", "every 1 hour"},
{"0 0 * * *", "every 1 day"},
{"*/5 * * * *", "every 5 mins"},
{"*/1 * * * *", "every min"},
{"* * * * *", "every min"},
{"30/1 * * * *", "every 30 sec"},
{"0-5 14 * * *", "every minute starting at 2:00 PM and ending at 2:05 PM, every day"},
{"0 0 1,2 * *", "at midnight of 1st, 2nd day of each month"},
{"0 0 1,2 * 3", "at midnight of 1st, 2nd day of each month, and each Wednesday"},
>>>>>>> 0bb9b098f (Automation tests for userretention)
}
ur.testPositiveInputValues(userRetentionCron, tests)
}

func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForUserRetentionCronWithNegativeInputValues() {
tests := []struct {
<<<<<<< HEAD
name string
value string
description string
Expand All @@ -253,6 +399,20 @@ func (ur *UserRetentionSettingsTestSuite) TestUpdateSettingsForUserRetentionCron
{"CronUpdateErrorOutOfRange", "60/1 * * * *", "Invalid value: \"60/1 * * * *\": Beginning of range (60) beyond end of range (59): 60/1"},
{"CronUpdateErrorLessFields", "10min", "Invalid value: \"10min\": Expected exactly 5 fields"},
{"CronUpdateErrorInvalidSyntax", "(*/1) * * * *", "Failed to parse int from"},
=======
value string
description string
}{
{"* * * * * *", "Invalid value: \"* * * * * *\": Expected exactly 5 fields, found 6: * * * * * *"},
{"*/-1 * * * *", "Invalid value: \"*/-1 * * * *\": Negative number (-1) not allowed: -1"},
{"60/1 * * * *", "Invalid value: \"60/1 * * * *\": Beginning of range (60) beyond end of range (59): 60/1"},
{"-30/1 * * * *", "Invalid value: \"-30/1 * * * *\": Failed to parse int from : strconv.Atoi: parsing \"\": invalid syntax"},
{"(*/1) * * * *", "Invalid value: \"(*/1) * * * *\": Failed to parse int from (*: strconv.Atoi: parsing \"(*\": invalid syntax"},
{"10min", "Invalid value: \"10min\": Expected exactly 5 fields, found 1: 10min"},
{"* * * * * */2", "Invalid value: \"* * * * * */2\": Expected exactly 5 fields, found 6: * * * * * */2"},
{"1d", "Invalid value: \"1d\": Expected exactly 5 fields, found 1: 1d"},
{"-20m", "Invalid value: \"-20m\": Expected exactly 5 fields, found 1: -20m"},
>>>>>>> 0bb9b098f (Automation tests for userretention)
}
ur.testNegativeInputValues(userRetentionCron, tests)
}
Expand Down

0 comments on commit 92d0a3e

Please sign in to comment.