Skip to content

Commit

Permalink
refactor: use execSync for tests (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and callmehiphop committed Apr 4, 2019
1 parent 37ec3a3 commit 95ac5bd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
1 change: 0 additions & 1 deletion kms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
},
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^6.0.0",
"uuid": "^3.2.1",
"yargs": "^13.0.0"
Expand Down
61 changes: 26 additions & 35 deletions kms/system-test/kms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
const fs = require(`fs`);
const path = require(`path`);
const {assert} = require('chai');
const execa = require('execa');
const {execSync} = require('child_process');
const uuid = require(`uuid`);
const {promisify} = require('util');
const unlink = promisify(fs.unlink);

const cwd = path.join(__dirname, `..`);
const keyRingName = `test-ring-${uuid.v4()}`;
const keyNameOne = `test-key-${uuid.v4()}`;
const member = `allAuthenticatedUsers`;
Expand All @@ -36,14 +35,6 @@ const unspecifiedKeyRingName = `projects/${projectId}/locations/global/keyRings/
const formattedKeyRingName = `projects/${projectId}/locations/global/keyRings/${keyRingName}`;
const formattedKeyName = `${formattedKeyRingName}/cryptoKeys/${keyNameOne}`;

const exec = async cmd => {
const result = await execa.shell(cmd, {cwd});
if (result.stderr) {
throw new Error(result.stderr);
}
return result.stdout;
};

describe('kms sample tests', () => {
before(async () => {
try {
Expand All @@ -64,13 +55,13 @@ describe('kms sample tests', () => {
});

it('should list key rings', async () => {
const output = await exec(`node quickstart.js "${projectId}"`);
const output = execSync(`node quickstart.js "${projectId}"`);
assert.match(output, /Key rings:/);
assert.match(output, /\/locations\/global\/keyRings\//);
});

it(`should create a key ring`, async () => {
const output = await exec(
const output = execSync(
`node createKeyring.js "${projectId}" "${keyRingName}"`
);
if (!output.includes(`KeyRing ${formattedKeyRingName} already exists`)) {
Expand All @@ -82,18 +73,18 @@ describe('kms sample tests', () => {
});

it(`should list key rings`, async () => {
const output = await exec(`node listKeyrings.js ${projectId}`);
const output = execSync(`node listKeyrings.js ${projectId}`);
assert.match(output, new RegExp(unspecifiedKeyRingName));
});

it(`should get a key ring`, async () => {
const output = await exec(`node getKeyring ${projectId} ${keyRingName}`);
const output = execSync(`node getKeyring ${projectId} ${keyRingName}`);
assert.match(output, new RegExp(`Name: ${formattedKeyRingName}`));
assert.ok(output.match(new RegExp(`Created: `)));
assert.match(output, /Created: /);
});

it(`should get a key ring's empty IAM policy`, async () => {
const output = await exec(
const output = execSync(
`node getKeyringIamPolicy.js ${projectId} ${keyRingName}`
);
assert.match(
Expand All @@ -103,7 +94,7 @@ describe('kms sample tests', () => {
});

it(`should grant access to a key ring`, async () => {
const output = await exec(
const output = execSync(
`node addMemberToKeyRingPolicy.js ${projectId} ${keyRingName} ${member} ${role}`
);
assert.match(
Expand All @@ -115,15 +106,15 @@ describe('kms sample tests', () => {
});

it(`should get a key ring's updated IAM policy`, async () => {
const output = await exec(
const output = execSync(
`node getKeyringIamPolicy.js ${projectId} ${keyRingName}`
);
assert.match(output, new RegExp(`${role}:`));
assert.match(output, new RegExp(` ${member}`));
});

it(`should revoke access to a key ring`, async () => {
const output = await exec(
const output = execSync(
`node removeMemberFromKeyRingPolicy.js ${projectId} ${keyRingName} ${member} ${role}`
);
assert.match(
Expand All @@ -135,7 +126,7 @@ describe('kms sample tests', () => {
});

it(`should create a key`, async () => {
const output = await exec(
const output = execSync(
`node createCryptoKey.js ${projectId} ${keyRingName} ${keyNameOne}`
);
if (!output.includes(`CryptoKey ${formattedKeyName} already exists`)) {
Expand All @@ -144,22 +135,22 @@ describe('kms sample tests', () => {
});

it(`should list keys`, async () => {
const output = await exec(
const output = execSync(
`node listCryptoKeys.js ${projectId} ${keyRingName}`
);
assert.match(output, new RegExp(formattedKeyName));
});

it(`should get a key`, async () => {
const output = await exec(
const output = execSync(
`node getCryptoKey.js ${projectId} ${keyRingName} ${keyNameOne}`
);
assert.match(output, new RegExp(`Name: ${formattedKeyName}`));
assert.match(output, new RegExp(`Created: `));
});

it(`should set a crypto key's primary version`, async () => {
const output = await exec(
const output = execSync(
`node setPrimaryCryptoKeyVersion.js ${projectId} ${keyRingName} ${keyNameOne} 1`
);
assert.match(
Expand All @@ -169,7 +160,7 @@ describe('kms sample tests', () => {
});

it(`should encrypt a file`, async () => {
const output = await exec(
const output = execSync(
`node encrypt.js ${projectId} ${keyRingName} ${keyNameOne} "${plaintext}" "${ciphertext}"`
);
assert.match(
Expand All @@ -182,7 +173,7 @@ describe('kms sample tests', () => {
});

it(`should decrypt a file`, async () => {
const output = await exec(
const output = execSync(
`node decrypt.js ${projectId} "${keyRingName}" "${keyNameOne}" "${ciphertext}" "${decrypted}"`
);
assert.match(
Expand All @@ -197,7 +188,7 @@ describe('kms sample tests', () => {
});

it(`should create a crypto key version`, async () => {
const output = await exec(
const output = execSync(
`node createCryptoKeyVersion ${projectId} "${keyRingName}" "${keyNameOne}"`
);
assert.match(
Expand All @@ -208,14 +199,14 @@ describe('kms sample tests', () => {
});

it(`should list crypto key versions`, async () => {
const output = await exec(
const output = execSync(
`node listCryptoKeyVersions.js ${projectId} "${keyRingName}" "${keyNameOne}"`
);
assert.match(output, new RegExp(`${formattedKeyName}/cryptoKeyVersions/1`));
});

it(`should destroy a crypto key version`, async () => {
const output = await exec(
const output = execSync(
`node destroyCryptoKeyVersion ${projectId} "${keyRingName}" "${keyNameOne}" 2`
);
assert.match(
Expand All @@ -227,7 +218,7 @@ describe('kms sample tests', () => {
});

it(`should restore a crypto key version`, async () => {
const output = await exec(
const output = execSync(
`node restoreCryptoKeyVersion ${projectId} "${keyRingName}" "${keyNameOne}" 2`
);
assert.match(
Expand All @@ -239,7 +230,7 @@ describe('kms sample tests', () => {
});

it(`should enable a crypto key version`, async () => {
const output = await exec(
const output = execSync(
`node enableCryptoKeyVersion ${projectId} "${keyRingName}" "${keyNameOne}" 2`
);
assert.match(
Expand All @@ -251,7 +242,7 @@ describe('kms sample tests', () => {
});

it(`should disable a crypto key version`, async () => {
const output = await exec(
const output = execSync(
`node disableCryptoKeyVersion ${projectId} "${keyRingName}" "${keyNameOne}" 2`
);
assert.match(
Expand All @@ -263,7 +254,7 @@ describe('kms sample tests', () => {
});

it(`should get a crypto key's empty IAM policy`, async () => {
const output = await exec(
const output = execSync(
`node getCryptoKeyIamPolicy ${projectId} "${keyRingName}" "${keyNameOne}"`
);
assert.match(
Expand All @@ -273,7 +264,7 @@ describe('kms sample tests', () => {
});

it(`should grant access to a crypto key`, async () => {
const output = await exec(
const output = execSync(
`node addMemberToCryptoKeyPolicy ${projectId} "${keyRingName}" "${keyNameOne}" "${member}" "${role}"`
);
assert.match(
Expand All @@ -285,15 +276,15 @@ describe('kms sample tests', () => {
});

it(`should get a crypto key's updated IAM policy`, async () => {
const output = await exec(
const output = execSync(
`node getCryptoKeyIamPolicy ${projectId} "${keyRingName}" "${keyNameOne}"`
);
assert.match(output, new RegExp(`${role}:`));
assert.match(output, new RegExp(` ${member}`));
});

it(`should revoke access to a crypto key`, async () => {
const output = await exec(
const output = execSync(
`node removeMemberCryptoKeyPolicy ${projectId} "${keyRingName}" "${keyNameOne}" ${member} ${role}`
);
assert.match(
Expand Down

0 comments on commit 95ac5bd

Please sign in to comment.