github.com/kyma-project/kyma-environment-broker@v0.0.1/testing/e2e/skr/skr-test/btp-manager-secret/index.js (about)

     1  const {expect} = require('chai');
     2  const {
     3    getSecret,
     4    getSecretData,
     5    k8sDelete,
     6    waitForSecret,
     7    k8sApply,
     8    waitForK8sObject,
     9  } = require('../../utils');
    10  const {BTPOperatorCreds} = require('../../smctl/helpers');
    11  
    12  const secretName = 'sap-btp-manager';
    13  const ns = 'kyma-system';
    14  const expectedBtpOperatorCreds = BTPOperatorCreds.dummy();
    15  
    16  const reconciliationTimeout = 1000 * 70;
    17  let secretFromProvisioning;
    18  let modifiedSecret;
    19  
    20  function btpManagerSecretTest() {
    21    describe('BTP Manager Secret Test', function() {
    22      // Check if BTP Manager Secret with BTP Operator credentials is created properly
    23      it('should check if Secret exists and contains the expected data keys', async function() {
    24        secretFromProvisioning = await getSecret(secretName, ns);
    25        checkSecretDataKeys(secretFromProvisioning);
    26        modifiedSecret = JSON.parse(JSON.stringify(secretFromProvisioning));
    27      });
    28      // Check if the Secret contains expected values
    29      it('should check if Secret data values match expected values', async function() {
    30        const actualSecretData = await getSecretData(secretName, ns);
    31        checkSecretDataValues(actualSecretData);
    32      });
    33      // Check if the Secret is properly reconciled after deletion
    34      it('should check if Secret is reconciled after deletion', async function() {
    35        console.log(`Deleting the "sap-btp-manager" Secret`);
    36        await k8sDelete([secretFromProvisioning], ns);
    37        console.log(`Waiting for the reconciliation for ${reconciliationTimeout} ms`);
    38        await waitForSecret(secretName, ns, reconciliationTimeout);
    39        console.log(`Secret has been re-created. Checking Secret's data`);
    40        const actualSecret = await getSecret(secretName, ns);
    41        checkSecretDataKeys(actualSecret);
    42        const actualSecretData = await getSecretData(secretName, ns);
    43        checkSecretDataValues(actualSecretData);
    44        console.log(`Secret has been properly reconciled`);
    45      });
    46      // Check if the Secret is properly reconciled after being edited
    47      it('should check if Secret is reconciled after being edited', async function() {
    48        console.log(`Changing data in the "sap-btp-manager" Secret`);
    49        prepareSecretForApply(modifiedSecret);
    50        changeSecretData(modifiedSecret);
    51        console.log(`Applying edited Secret`);
    52        await k8sApply([modifiedSecret], ns);
    53        console.log(`Waiting ${reconciliationTimeout} ms until edited Secret is created`);
    54        await waitForSecret(secretName, ns, reconciliationTimeout);
    55        let actualSecret = await getSecret(secretName, ns);
    56        console.log(`Waiting for the reconciliation for ${reconciliationTimeout} ms`);
    57        await waitForK8sObject(
    58            `/api/v1/namespaces/${ns}/secrets`,
    59            {},
    60            (_type, _apiObj, watchObj) => {
    61              return (
    62                watchObj.object.metadata.name.includes(secretName) &&
    63                          watchObj.object.metadata.resourceVersion !== actualSecret.metadata.resourceVersion
    64              );
    65            },
    66            reconciliationTimeout,
    67            `Waiting for ${secretName} Secret reconciliation timeout (${reconciliationTimeout} ms)`,
    68        );
    69        console.log(`Secret has been reconciled. Checking Secret's data`);
    70        actualSecret = await getSecret(secretName, ns);
    71        checkSecretDataKeys(actualSecret);
    72        const actualSecretData = await getSecretData(secretName, ns);
    73        checkSecretDataValues(actualSecretData);
    74        console.log(`Secret is correct`);
    75      });
    76    });
    77  }
    78  
    79  function checkSecretDataKeys(secret) {
    80    console.log(`Checking the data keys of the "sap-btp-manager" Secret`);
    81    expect(secret).to.not.be.empty;
    82    expect(secret.metadata.labels['app.kubernetes.io/managed-by']).to.equal('kcp-kyma-environment-broker');
    83    expect(secret.data).to.have.property('clientid');
    84    expect(secret.data).to.have.property('clientsecret');
    85    expect(secret.data).to.have.property('sm_url');
    86    expect(secret.data).to.have.property('tokenurl');
    87    expect(secret.data).to.have.property('cluster_id');
    88  }
    89  
    90  function checkSecretDataValues(secret) {
    91    console.log(`Checking data values of the "sap-btp-manager" Secret`);
    92    expect(secret.clientid).to.equal(expectedBtpOperatorCreds.clientid);
    93    expect(secret.clientsecret).to.equal(expectedBtpOperatorCreds.clientsecret);
    94    expect(secret.sm_url).to.equal(expectedBtpOperatorCreds.smURL);
    95    expect(secret.tokenurl).to.equal(expectedBtpOperatorCreds.url);
    96  }
    97  
    98  function prepareSecretForApply(secret) {
    99    delete secret.metadata.uid;
   100    delete secret.metadata.resourceVersion;
   101    delete secret.metadata.creationTimestamp;
   102    delete secret.metadata.annotations;
   103    delete secret.metadata.managedFields;
   104  }
   105  
   106  function changeSecretData(secret) {
   107    secret.data.clientid = Buffer.from('edited-clientid').toString('base64');
   108    secret.data.clientsecret = '';
   109    secret.data.sm_url = '';
   110    secret.data.tokenurl = Buffer.from('edited-tokenurl').toString('base64');
   111  }
   112  
   113  module.exports = {
   114    btpManagerSecretTest,
   115  };