Skip to content

Commit

Permalink
[CY] Add RBAC
Browse files Browse the repository at this point in the history
  • Loading branch information
n313893254 committed Sep 1, 2023
1 parent 2436425 commit b344e51
Show file tree
Hide file tree
Showing 16 changed files with 466 additions and 36 deletions.
14 changes: 10 additions & 4 deletions cypress/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export class Constants {
public password = Cypress.env("password");
public mockPassword = Cypress.env("mockPassword");
public rancherUrl = Cypress.env("rancherUrl");
public rancher_user = Cypress.env("rancherUser");
public rancher_password = Cypress.env("rancherPassword");
public rancherUser = Cypress.env("rancherUser");
public rancherPassword = Cypress.env("rancherPassword");
public rancher_vm_user = Cypress.env("rancher_vm_user");
public rancher_vm_password = Cypress.env("rancher_vm_password");
public vagrant_pxe_path = Cypress.env("vagrant_pxe_path");
Expand All @@ -17,7 +17,7 @@ export class Constants {
public uiSourceUrl = '/harvester/c/local/harvesterhci.io.setting/ui-source?mode=edit';
public hostsPage = '/harvester/c/local/harvesterhci.io.host';
public supportPage = '/harvester/c/local/support';
public vmPage = '/harvester/c/local/kubevirt.io.virtualmachine';
public vmPage = `/harvester/c/${Cypress.config('clusterId')}/kubevirt.io.virtualmachine`;
public settingUrl = '/harvester/c/local/harvesterhci.io.setting';
public volumePage = '/harvester/c/local/harvesterhci.io.volume';
public imagePage = '/harvester/c/local/harvesterhci.io.virtualmachineimage';
Expand All @@ -36,7 +36,13 @@ export const PageUrl = {
virtualMachine: '/harvester/c/local/kubevirt.io.virtualmachine',
vmNetwork: '/harvester/c/local/harvesterhci.io.networkattachmentdefinition',
namespace: '/harvester/c/local/namespace',
volumeSnapshot: '/harvester/c/local/harvesterhci.io.volumesnapshot'
volumeSnapshot: '/harvester/c/local/harvesterhci.io.volumesnapshot',
clusterMember: `/harvester/c/${Cypress.config('clusterId')}/management.cattle.io.clusterroletemplatebinding`,
project: `/harvester/c/${Cypress.config('clusterId')}/projectsnamespaces`,
}

export const RancherPageUrl = {
user: `/c/local/auth/management.cattle.io.user`,
}

export const MenuNav = {
Expand Down
11 changes: 11 additions & 0 deletions cypress/constants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export const HCI = {
volumeSnapshot: 'snapshot.storage.k8s.io.volumesnapshot'
};


export const CAPI = {
RANCHER_CLUSTER: 'provisioning.cattle.io.cluster',
};

export const MANAGEMENT = {
USER: 'management.cattle.io.user',
CLUSTER_ROLE_TEMPLATE_BINDING: 'management.cattle.io.clusterroletemplatebinding',
PROJECT: 'management.cattle.io.project',
}

export const HCI_URL = {
volumeSnapshot: 'harvesterhci.io.volumesnapshot'
}
Expand Down
4 changes: 2 additions & 2 deletions cypress/cypress.env.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"mockPassword": "mockPassword",
"baseUrl": "https://online-server",
"rancherUrl": "https://rancher-server",
"rancher_user": "admin",
"rancher_password": "admin",
"rancherUser": "admin",
"rancherPassword": "admin",
"rancher_vm_user": "vagrant",
"rancher_vm_password": "vagrant",
"vagrant_pxe_path": "../ipxe-examples/vagrant-pxe-harvester",
Expand Down
50 changes: 50 additions & 0 deletions cypress/pageobjects/clusterMember.po.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import CruResourcePo from '@/utils/components/cru-resource.po';
import { MANAGEMENT } from '@/constants/types'
import LabeledSelectPo from '@/utils/components/labeled-select.po';
import RadioButtonPo from '@/utils/components/radio-button.po';

export default class UserPo extends CruResourcePo {
constructor() {
super({
type: MANAGEMENT.CLUSTER_ROLE_TEMPLATE_BINDING
});
}

public selectMember() {
return new LabeledSelectPo('.labeled-select.hoverable', `:contains("Select Member")`)
}

public clusterPermissions() {
return new RadioButtonPo('.radio-group')
}

public setValue(value: any) {
this.selectMember().search(value?.selectMember)
this.selectMember().select({ option: value?.selectMember })

this.clusterPermissions().input(value?.clusterPermissions)
}

public create(value: any, urlWithNamespace?: boolean) {
cy.visit(`/harvester/c/${Cypress.config('clusterId')}/${this.type}/create`)

this.setValue(value)

this.save()
}

public save({
buttonText = 'save',
edit,
} : {
buttonText?:string,
edit?: boolean;
} = {}) {
cy.intercept('POST', `/v3/clusterroletemplatebindings`).as('createClusterMember');

this.clickFooterBtn(buttonText)
cy.wait('@createClusterMember').then(res => {
expect(res.response?.statusCode, `Create ${this.type} success`).to.equal(201);
})
}
}
24 changes: 24 additions & 0 deletions cypress/pageobjects/namespace.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,28 @@ export default class NamespacePage extends CruResourcePo {
this.description().input(description)
}
}

public create(value: any) {
if (value.projectName) {
cy.window().then(async (window: any) => {
const store = window.$nuxt.$store

await store.dispatch('management/findAll', {
type: 'management.cattle.io.project'
})

const projects = store.getters['management/all']('management.cattle.io.project')
const project = projects.find(p => p.spec.displayName === value.projectName)
const projectId = project?.metadata?.name

cy.visit(`/harvester/c/${Cypress.config('clusterId')}/${this.type}/create?projectId=${projectId}`)
})
} else {
cy.visit(`/harvester/c/${Cypress.config('clusterId')}/${this.type}/create`)
}

this.setValue(value)

this.save()
}
}
61 changes: 61 additions & 0 deletions cypress/pageobjects/project.po.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import CruResourcePo from '@/utils/components/cru-resource.po';
import { MANAGEMENT } from '@/constants/types'
import LabeledSelectPo from '@/utils/components/labeled-select.po';
import RadioButtonPo from '@/utils/components/radio-button.po';

export default class ProjectPo extends CruResourcePo {
constructor() {
super({
type: MANAGEMENT.PROJECT
});
}

public setValue(value: any) {
this.name().input(value?.name)
}

public create(value: any, urlWithNamespace?: boolean) {
cy.visit(`/harvester/c/${Cypress.config('clusterId')}/${this.type}/create`)

this.setValue(value)
this.addMember(value?.members)

this.save()
}

public selectMember() {
return new LabeledSelectPo('.labeled-select.hoverable', `:contains("Select Member")`)
}

public clusterPermissions() {
return new RadioButtonPo('.radio-group')
}

public addMember(members: object[] = []) {
members.map(member => {
cy.contains('Add').click()

this.selectMember().search(member.name)
this.selectMember().select({ option: member.name })

this.clusterPermissions().input(member?.permissions)

cy.get('[data-testid="card-actions-slot"]').contains('Add').click()
})
}

public save({
buttonText = 'save',
edit,
} : {
buttonText?:string,
edit?: boolean;
} = {}) {
cy.intercept('POST', `/v3/projects`).as('createProject');

this.clickFooterBtn(buttonText)
cy.wait('@createProject').then(res => {
expect(res.response?.statusCode, `Create ${this.type} success`).to.equal(201);
})
}
}
12 changes: 6 additions & 6 deletions cypress/pageobjects/rancher.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ export class rancherPage {
// cy.log('Select a specific password to use')
cy.get(this.boostrap_page_radioSelectPW).click().log('Select a specific password to use');

// cy.log('Input new password')
cy.get(this.boostrap_page_newPWInput).type(constants.rancher_password).log('Input new password');
// cy.log('Confirm password again')
cy.get(this.boostrap_page_newPWRepeat).type(constants.rancher_password).log('Confirm password again');
// cy.log('Input new password')
cy.get(this.boostrap_page_newPWInput).type(constants.rancherPassword).log('Input new password');
// cy.log('Confirm password again')
cy.get(this.boostrap_page_newPWRepeat).type(constants.rancherPassword).log('Confirm password again');

// cy.log('Agree EULA')
cy.get(this.boostrap_page_checkAgreeEULA).click().log('Agree EULA');
Expand All @@ -144,8 +144,8 @@ export class rancherPage {
* Rancher login page: Input username and password -> submit
*/
public login() {
cy.get(this.login_page_usernameInput).type(constants.rancher_user).log('Input username');
cy.get(this.login_page_passwordInput).type(constants.rancher_password).log('Input password');
cy.get(this.login_page_usernameInput).type(constants.rancherUser).log('Input username');
cy.get(this.login_page_passwordInput).type(constants.rancherPassword).log('Input password');
cy.get(this.login_page_loginButton).click().log('Login with local user');
}

Expand Down
66 changes: 66 additions & 0 deletions cypress/pageobjects/rancher/user.po.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import CruResourcePo from '@/utils/components/cru-resource.po';
import { MANAGEMENT } from '@/constants/types'
import LabeledInputPo from '@/utils/components/labeled-input.po';
import CheckboxPo from '@/utils/components/checkbox.po';

export default class UserPo extends CruResourcePo {
constructor() {
super({
type: MANAGEMENT.USER
});
}

public username() {
return new LabeledInputPo('.labeled-input', `:contains("Username")`)
}

public newPassword() {
return new LabeledInputPo('.labeled-input', `:contains("New Password")`)
}

public confirmPassword() {
return new LabeledInputPo('.labeled-input', `:contains("Confirm Password")`)
}

public globalPermissions(string) {
return cy.find('.card-body').contains(string).click()
}

administrator() {
return new CheckboxPo(':nth-child(1) > [data-testid="card"] > .card-wrap > [data-testid="card-body-slot"] > .checkbox-section > :nth-child(1) > .checkbox-outer-container > .checkbox-container', `:contains("Administrator")`)
}

public setValue(value: any) {
this.username().input(value?.username)
this.newPassword().input(value?.newPassword)
this.confirmPassword().input(value?.confirmPassword)

// TODO: Can not get the value of globalPermissions
if (value?.administrator) {
this.administrator().click()
}
}

public create(value: any, urlWithNamespace?: boolean) {
cy.visit(`/c/local/auth/${this.type}/create`)

this.setValue(value)

this.save()
}

public save({
buttonText = 'save',
edit,
} : {
buttonText?:string,
edit?: boolean;
} = {}) {
cy.intercept('POST', `/v3/users`).as('create');

this.clickFooterBtn(buttonText)
cy.wait('@create').then(res => {
expect(res.response?.statusCode, `Create ${this.type} success`).to.equal(201);
})
}
}
8 changes: 4 additions & 4 deletions cypress/pageobjects/virtualmachine.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ export class VmsPage extends CruResourcePo {
edit = false,
} = {}) {
if (edit) {
cy.intercept('PUT', '/v1/harvester/kubevirt.io.virtualmachines/*/*').as('createVM');
cy.intercept('PUT', '**/v1/harvester/kubevirt.io.virtualmachines/*/*').as('createVM');
cy.get('.cru-resource-footer').contains('Save').click()
cy.get('.card-actions').contains('Save & Restart').click()
} else {
cy.intercept('POST', '/v1/harvester/kubevirt.io.virtualmachines/*').as('createVM');
cy.intercept('POST', '**/v1/harvester/kubevirt.io.virtualmachines/*').as('createVM');
cy.get('.cru-resource-footer').contains('Create').click()
}

Expand Down Expand Up @@ -365,15 +365,15 @@ export class VmsPage extends CruResourcePo {
}

public delete(namespace:string, name: string, displayName?: string, { removeRootDisk, id }: { removeRootDisk?: boolean, id?: string } = { removeRootDisk: true }) {
cy.visit(`/harvester/c/local/${this.type}`)
cy.visit(`/harvester/c/${Cypress.config('clusterId')}/${this.type}`)

this.clickAction(name, 'Delete').then((_) => {
if (!removeRootDisk) {
this.rootDisk().click();
}
})

cy.intercept('DELETE', `/v1/harvester/${this.realType}s/${namespace}/${name}*`).as('delete');
cy.intercept('DELETE', `**/v1/harvester/${this.realType}s/${namespace}/${name}*`).as('delete');
cy.get(this.confirmRemove).contains('Delete').click();
cy.wait('@delete').then(res => {
cy.window().then((win) => {
Expand Down
13 changes: 8 additions & 5 deletions cypress/scripts/e2e
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ TESTS_FAILED=false
MAX_RETRIES=3
RETRY_DELAY=5

current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "Current Time: $current_time"

xvfb-run --server-args='-screen 0 1024x768x16 -ac' yarn run e2e

if [ $? -ne 0 ]; then
echo "Cypress tests failed."
TESTS_FAILED=true
fi

if (REPORT_NAME) {
echo "Custom report name provided: ${REPORT_NAME}"
} else {
if [ -n "$REPORT_NAME" ]; then
echo "Custom report name provided: $REPORT_NAME"
else
timestamp=$(date +%Y%m%d-%H%M%S)
commit_id=$(git rev-parse --short HEAD)

REPORT_NAME = "${timestamp}-${commit_id}"
}
REPORT_NAME="${timestamp}-${commit_id}"
fi

report_dir="cypress/results/${REPORT_NAME}"

Expand Down
Loading

0 comments on commit b344e51

Please sign in to comment.