Skip to content

Commit

Permalink
test: fix failing tests due to leaks (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and Ace Nassri committed Nov 10, 2022
1 parent 6b2b672 commit 328c888
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
7 changes: 6 additions & 1 deletion monitoring/snippets/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ async function backupPolicies(projectId) {
name: client.projectPath(projectId),
};

const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
let [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);

// filter out any policies created by tests for this sample
policies = policies.filter(policy => {
return !policy.displayName.startsWith('gcloud-tests-');
});

fs.writeFileSync(
'./policies_backup.json',
Expand Down
7 changes: 5 additions & 2 deletions monitoring/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": "googleapis/nodejs-monitoring",
"files": [ "*.js" ],
"files": [
"*.js"
],
"engines": {
"node": ">=8"
},
Expand All @@ -20,6 +22,7 @@
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"mocha": "^5.0.0",
"proxyquire": "^2.0.1",
"sinon": "^7.0.0"
"sinon": "^7.0.0",
"uuid": "^3.3.2"
}
}
2 changes: 0 additions & 2 deletions monitoring/snippets/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ env:
mocha: true
rules:
node/no-unpublished-require: off
node/no-unsupported-features: off
no-empty: off
18 changes: 11 additions & 7 deletions monitoring/snippets/system-test/alerts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const monitoring = require(`@google-cloud/monitoring`);
const path = require(`path`);
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);
const uuid = require('uuid');

const client = new monitoring.AlertPolicyServiceClient();
const channelClient = new monitoring.NotificationChannelServiceClient();
Expand All @@ -28,15 +29,16 @@ const projectId = process.env.GCLOUD_PROJECT;

let policyOneName, policyTwoName, channelName;

before(tools.checkCredentials);
const testPrefix = `gcloud-test-${uuid.v4().split('-')[0]}`;

before(tools.checkCredentials);
before(async () => {
try {
tools.checkCredentials;
let results = await client.createAlertPolicy({
name: client.projectPath(projectId),
alertPolicy: {
displayName: 'first_policy',
displayName: `${testPrefix}-first-policy`,
combiner: 1,
documentation: {
content: 'Test',
Expand Down Expand Up @@ -71,7 +73,7 @@ before(async () => {
results = await client.createAlertPolicy({
name: client.projectPath(projectId),
alertPolicy: {
displayName: 'second',
displayName: `${testPrefix}-second`,
combiner: 1,
conditions: [
{
Expand Down Expand Up @@ -110,7 +112,9 @@ before(async () => {
},
});
channelName = results[0].name;
} catch (err) {} // ignore error
} catch (err) {
// ignore error
}
});

async function deletePolicies() {
Expand Down Expand Up @@ -147,7 +151,7 @@ it(`should replace notification channels`, async () => {
it(`should disable policies`, async () => {
const results = await tools.spawnAsyncWithIO(
`node`,
[`alerts.js`, `disable`, projectId, `'display_name.size < 7'`],
[`alerts.js`, `disable`, projectId, `'display_name.size < 28'`],
cwd
);
assert.strictEqual(results.output.includes('Disabled projects'), true);
Expand All @@ -158,7 +162,7 @@ it(`should disable policies`, async () => {
it(`should enable policies`, async () => {
const results = await tools.spawnAsyncWithIO(
`node`,
[`alerts.js`, `enable`, projectId, `'display_name.size < 7'`],
[`alerts.js`, `enable`, projectId, `'display_name.size < 28'`],
cwd
);
assert.strictEqual(results.output.includes('Enabled projects'), true);
Expand All @@ -173,7 +177,7 @@ it(`should list policies`, async () => {
cwd
);
assert.strictEqual(results.output.includes('Policies:'), true);
assert.strictEqual(results.output.includes('first_policy'), true);
assert.strictEqual(results.output.includes('first-policy'), true);
assert.strictEqual(results.output.includes('Test'), true);
assert.strictEqual(results.output.includes('second'), true);
});
Expand Down
8 changes: 3 additions & 5 deletions monitoring/snippets/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
const proxyquire = require(`proxyquire`).noPreserveCache();
const sinon = require(`sinon`);
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);

const monitoring = proxyquire(`@google-cloud/monitoring`, {});
const client = new monitoring.MetricServiceClient();

beforeEach(tools.stubConsole);
afterEach(tools.restoreConsole);

it(`should list time series`, async () => {
const clientMock = {
projectPath: projectId => client.projectPath(projectId),
Expand All @@ -42,7 +38,9 @@ it(`should list time series`, async () => {
`Done writing time series data.`,
{},
]);
} catch (err) {}
} catch (err) {
// ignore error
}
}, 200);

return result;
Expand Down

0 comments on commit 328c888

Please sign in to comment.