Skip to content

Commit

Permalink
Improve cloud cred e2e tests
Browse files Browse the repository at this point in the history
- don't nuke all cloud creds before tests starts, instead tidy up cloud creds created during tests at end
- use safer method of creating cloud creds and running dependent tests
  - instead of looping over but running tests at end of each chain create them then run test
- most important one, make creds THEN navigate to the page to ensure all creds are correctly in store
  - there's no socket updates for these, so we have to be sure they can be returned before we load the dashboard
  • Loading branch information
richard-cox committed Oct 10, 2024
1 parent 9c7717c commit 01f3d0b
Showing 1 changed file with 97 additions and 90 deletions.
187 changes: 97 additions & 90 deletions cypress/e2e/tests/pages/manager/cloud-credential.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ import ClusterManagerCreateRke2AzurePagePo from '@/cypress/e2e/po/edit/provision

describe('Cloud Credential', { testIsolation: 'off' }, () => {
const clusterList = new ClusterManagerListPagePo();
const doCreatedCloudCredsIds = [];
const azCreatedCloudCredsIds = [];

before(() => {
cy.login();
cy.getRancherResource('v3', 'cloudCredentials').then((resp: Cypress.Response<any>) => {
const credentials = resp.body.data;

credentials.forEach( (credential) => {
cy.deleteRancherResource('v3', 'cloudCredentials', credential.id.trim(), false);
});
});
});

beforeEach(() => {
clusterList.goTo();
});

it('Editing a cluster cloud credential should work with duplicate named cloud credentials', { tags: ['@manager', '@adminUser'] }, () => {
Expand Down Expand Up @@ -69,39 +60,42 @@ describe('Cloud Credential', { testIsolation: 'off' }, () => {
},
];

const createdCloudCredsIds = [];

for (let i = 0; i < cloudCredsToCreate.length; i++) {
cy.createRancherResource('v3', 'cloudcredentials', JSON.stringify(cloudCredentialCreatePayloadDO(cloudCredsToCreate[i].name, cloudCredsToCreate[i].token))).then((resp: Cypress.Response<any>) => {
createdCloudCredsIds.push(resp.body.id);

if (i === 0) {
// intercept GET of list of prov clusters and pass a mock (Digital Ocean)
cy.intercept('GET', '/v1/provisioning.cattle.io.clusters?exclude=metadata.managedFields', (req) => {
req.reply({
statusCode: 200,
body: clusterProvDigitalOceanSingleResponse(clusterName, resp.body.id, machinePoolId),
});
}).as('dummyClusterListLoad');
}
const create = (cloudCredsToCreate) => {
return cy.createRancherResource('v3', 'cloudcredentials', JSON.stringify(cloudCredentialCreatePayloadDO(
cloudCredsToCreate.name,
cloudCredsToCreate.token
))).then((resp: Cypress.Response<any>) => {
doCreatedCloudCredsIds.push(resp.body.id);
});
};

create(cloudCredsToCreate[0])
.then(() => create(cloudCredsToCreate[1]))
.then(() => create(cloudCredsToCreate[2]))
.then(() => {
clusterList.goTo();

cy.intercept('GET', '/v1/provisioning.cattle.io.clusters?exclude=metadata.managedFields', (req) => {
req.reply({
statusCode: 200,
body: clusterProvDigitalOceanSingleResponse(clusterName, doCreatedCloudCredsIds[doCreatedCloudCredsIds.length - 1], machinePoolId),
});
}).as('dummyClusterListLoad');

// code to edit cluster and final checks needs to be after all "then's" otherwise the cloud cred id's aren't stored yet on it's variable...
if (i === cloudCredsToCreate.length - 1) {
clusterList.checkIsCurrentPage();
clusterList.editCluster(clusterName);
clusterList.checkIsCurrentPage();
clusterList.editCluster(clusterName);

const editClusterPage = new ClusterManagerEditGenericPagePo(undefined, clusterName);
const editClusterPage = new ClusterManagerEditGenericPagePo(undefined, clusterName);

editClusterPage.selectOptionForCloudCredentialWithLabel(`${ credsName } (${ createdCloudCredsIds[1] })`);
editClusterPage.save();
editClusterPage.selectOptionForCloudCredentialWithLabel(`${ credsName } (${ doCreatedCloudCredsIds[1] })`);
editClusterPage.save();

cy.wait('@dummyProvClusterSave').then(({ request }) => {
expect(request.body.spec.cloudCredentialSecretName).to.equal(createdCloudCredsIds[1]);
});
}
cy.wait('@dummyProvClusterSave').then(({ request }) => {
expect(request.body.spec.cloudCredentialSecretName).to.equal(doCreatedCloudCredsIds[1]);
});
});
}
});

it('Changing credential environment should change the list of locations when creating an Azure cluster', { tags: ['@manager', '@adminUser'] }, () => {
const clusterName = 'test-cluster-azure';
const machinePoolId = 'dummy-id';
Expand Down Expand Up @@ -170,63 +164,76 @@ describe('Cloud Credential', { testIsolation: 'off' }, () => {
});
}).as('aksVMSizesV2Load');

const createdCloudCredsIds = [];

ClusterManagerListPagePo.navTo();
clusterList.waitForPage();

for (let i = 0; i < cloudCredsToCreate.length; i++) {
cy.createRancherResource('v3', 'cloudcredentials', JSON.stringify(cloudCredentialCreatePayloadAzure(
cloudCredsToCreate[i].name,
cloudCredsToCreate[i].environment,
cloudCredsToCreate[i].subscriptionId,
cloudCredsToCreate[i].clientId,
cloudCredsToCreate[i].clientSecret,
const create = (cloudCredsToCreate) => {
return cy.createRancherResource('v3', 'cloudcredentials', JSON.stringify(cloudCredentialCreatePayloadAzure(
cloudCredsToCreate.name,
cloudCredsToCreate.environment,
cloudCredsToCreate.subscriptionId,
cloudCredsToCreate.clientId,
cloudCredsToCreate.clientSecret,
))).then((resp: Cypress.Response<any>) => {
createdCloudCredsIds.push(resp.body.id);

if (i === cloudCredsToCreate.length - 1) {
cy.intercept('GET', `/meta/aksLocations?cloudCredentialId=${ encodeURIComponent(createdCloudCredsIds[0]) }`, (req) => {
req.reply({
statusCode: 200,
body: cloudCredsToCreate[0].body,
});
}).as('aksLocations0');
cy.intercept('GET', `/meta/aksLocations?cloudCredentialId=${ encodeURIComponent(createdCloudCredsIds[1]) }`, (req) => {
req.reply({
statusCode: 200,
body: cloudCredsToCreate[1].body,
});
}).as('aksLocations1');
cy.intercept('GET', `/meta/aksLocations?cloudCredentialId=${ encodeURIComponent(createdCloudCredsIds[2]) }`, (req) => {
req.reply({
statusCode: 200,
body: cloudCredsToCreate[2].body,
});
}).as('aksLocations2');

clusterList.checkIsCurrentPage();
clusterList.createCluster();
createRKE2AzureClusterPage.rkeToggle().set('RKE2/K3s');
createRKE2AzureClusterPage.selectCreate(1);
createRKE2AzureClusterPage.rke2PageTitle().should('include', 'Create Azure');
createRKE2AzureClusterPage.waitForPage('type=azure&rkeType=rke2');
createRKE2AzureClusterPage.selectOptionForCloudCredentialWithLabel(`${ cloudCredsToCreate[0].name }`);

createRKE2AzureClusterPage.machinePoolTab().location().checkOptionSelected(cloudCredsToCreate[0].body[0].name);
createRKE2AzureClusterPage.machinePoolTab().environment().should('have.text', cloudCredsToCreate[0].environment );

createRKE2AzureClusterPage.selectOptionForCloudCredentialWithLabel(`${ cloudCredsToCreate[1].name }`);

createRKE2AzureClusterPage.machinePoolTab().environment().should('have.text', cloudCredsToCreate[1].environment );
createRKE2AzureClusterPage.machinePoolTab().location().checkOptionSelected(cloudCredsToCreate[1].body[0].name );

createRKE2AzureClusterPage.selectOptionForCloudCredentialWithLabel(`${ cloudCredsToCreate[2].name }`);

createRKE2AzureClusterPage.machinePoolTab().environment().should('have.text', cloudCredsToCreate[2].environment );
createRKE2AzureClusterPage.machinePoolTab().location().checkOptionSelected(cloudCredsToCreate[2].body[0].name );
}
azCreatedCloudCredsIds.push(resp.body.id);
});
};

create(cloudCredsToCreate[0])
.then(() => create(cloudCredsToCreate[1]))
.then(() => create(cloudCredsToCreate[2]))
.then(() => {
clusterList.goTo();

cy.intercept('GET', `/meta/aksLocations?cloudCredentialId=${ encodeURIComponent(azCreatedCloudCredsIds[0]) }`, (req) => {
req.reply({
statusCode: 200,
body: cloudCredsToCreate[0].body,
});
});
cy.intercept('GET', `/meta/aksLocations?cloudCredentialId=${ encodeURIComponent(azCreatedCloudCredsIds[1]) }`, (req) => {
req.reply({
statusCode: 200,
body: cloudCredsToCreate[1].body,
});
});
cy.intercept('GET', `/meta/aksLocations?cloudCredentialId=${ encodeURIComponent(azCreatedCloudCredsIds[2]) }`, (req) => {
req.reply({
statusCode: 200,
body: cloudCredsToCreate[2].body,
});
});

clusterList.checkIsCurrentPage();
clusterList.createCluster();
createRKE2AzureClusterPage.rkeToggle().set('RKE2/K3s');
createRKE2AzureClusterPage.selectCreate(1);
createRKE2AzureClusterPage.rke2PageTitle().should('include', 'Create Azure');
createRKE2AzureClusterPage.waitForPage('type=azure&rkeType=rke2');
createRKE2AzureClusterPage.selectOptionForCloudCredentialWithLabel(`${ cloudCredsToCreate[0].name }`);

createRKE2AzureClusterPage.machinePoolTab().location().checkOptionSelected(cloudCredsToCreate[0].body[0].name);
createRKE2AzureClusterPage.machinePoolTab().environment().should('have.text', cloudCredsToCreate[0].environment );

createRKE2AzureClusterPage.selectOptionForCloudCredentialWithLabel(`${ cloudCredsToCreate[1].name }`);

createRKE2AzureClusterPage.machinePoolTab().environment().should('have.text', cloudCredsToCreate[1].environment );
createRKE2AzureClusterPage.machinePoolTab().location().checkOptionSelected(cloudCredsToCreate[1].body[0].name );

createRKE2AzureClusterPage.selectOptionForCloudCredentialWithLabel(`${ cloudCredsToCreate[2].name }`);

createRKE2AzureClusterPage.machinePoolTab().environment().should('have.text', cloudCredsToCreate[2].environment );
createRKE2AzureClusterPage.machinePoolTab().location().checkOptionSelected(cloudCredsToCreate[2].body[0].name );
});
});

after(() => {
for (let i = 0; i < doCreatedCloudCredsIds.length; i++) {
cy.deleteRancherResource('v3', `cloudcredentials`, doCreatedCloudCredsIds[i]);
}

for (let i = 0; i < azCreatedCloudCredsIds.length; i++) {
cy.deleteRancherResource('v3', `cloudcredentials`, azCreatedCloudCredsIds[i]);
}
});
});

0 comments on commit 01f3d0b

Please sign in to comment.