Skip to content

Commit 29e6cdf

Browse files
committed
chore(modelarmor): fixed floorsetting clean up and refactoring of tests
1 parent 040ade4 commit 29e6cdf

File tree

7 files changed

+105
-35
lines changed

7 files changed

+105
-35
lines changed

model-armor/snippets/getFolderFloorSettings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ async function getFolderFloorSettings(folderId) {
4545
}
4646

4747
module.exports = getFolderFloorSettings;
48+
49+
/* c8 ignore next 10 */
50+
if (require.main === module) {
51+
main(...process.argv.slice(2)).catch(err => {
52+
console.error(err.message);
53+
process.exitCode = 1;
54+
});
55+
process.on('unhandledRejection', err => {
56+
console.error(err.message);
57+
process.exitCode = 1;
58+
});
59+
}

model-armor/snippets/getOrganizationFloorSettings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ async function getOrganizationFloorSettings(organizationId) {
4747
}
4848

4949
module.exports = getOrganizationFloorSettings;
50+
51+
/* c8 ignore next 10 */
52+
if (require.main === module) {
53+
main(...process.argv.slice(2)).catch(err => {
54+
console.error(err.message);
55+
process.exitCode = 1;
56+
});
57+
process.on('unhandledRejection', err => {
58+
console.error(err.message);
59+
process.exitCode = 1;
60+
});
61+
}

model-armor/snippets/getProjectFloorSettings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ async function getProjectFloorSettings(projectId) {
4747
}
4848

4949
module.exports = getProjectFloorSettings;
50+
51+
/* c8 ignore next 10 */
52+
if (require.main === module) {
53+
main(...process.argv.slice(2)).catch(err => {
54+
console.error(err.message);
55+
process.exitCode = 1;
56+
});
57+
process.on('unhandledRejection', err => {
58+
console.error(err.message);
59+
process.exitCode = 1;
60+
});
61+
}

model-armor/snippets/updateFolderFloorSettings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ async function updateFolderFloorSettings(folderId) {
7373
}
7474

7575
module.exports = updateFolderFloorSettings;
76+
77+
/* c8 ignore next 10 */
78+
if (require.main === module) {
79+
main(...process.argv.slice(2)).catch(err => {
80+
console.error(err.message);
81+
process.exitCode = 1;
82+
});
83+
process.on('unhandledRejection', err => {
84+
console.error(err.message);
85+
process.exitCode = 1;
86+
});
87+
}

model-armor/snippets/updateOrganizationFloorSettings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,15 @@ async function updateOrganizationFloorSettings(organizationId) {
7171
}
7272

7373
module.exports = updateOrganizationFloorSettings;
74+
75+
/* c8 ignore next 10 */
76+
if (require.main === module) {
77+
main(...process.argv.slice(2)).catch(err => {
78+
console.error(err.message);
79+
process.exitCode = 1;
80+
});
81+
process.on('unhandledRejection', err => {
82+
console.error(err.message);
83+
process.exitCode = 1;
84+
});
85+
}

model-armor/snippets/updateProjectFloorSettings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,15 @@ async function updateProjectFloorSettings(projectId) {
7171
}
7272

7373
module.exports = updateProjectFloorSettings;
74+
75+
/* c8 ignore next 10 */
76+
if (require.main === module) {
77+
main(...process.argv.slice(2)).catch(err => {
78+
console.error(err.message);
79+
process.exitCode = 1;
80+
});
81+
process.on('unhandledRejection', err => {
82+
console.error(err.message);
83+
process.exitCode = 1;
84+
});
85+
}

model-armor/test/modelarmor.test.js

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414

1515
'use strict';
1616

17-
const { assert } = require('chai');
18-
const cp = require('child_process');
19-
const { v4: uuidv4 } = require('uuid');
20-
const { ModelArmorClient } = require('@google-cloud/modelarmor').v1;
21-
const { DlpServiceClient } = require('@google-cloud/dlp');
17+
const {assert} = require('chai');
18+
const {v4: uuidv4} = require('uuid');
19+
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
20+
const {DlpServiceClient} = require('@google-cloud/dlp');
2221

2322
let projectId;
2423
const locationId = process.env.GCLOUD_LOCATION || 'us-central1';
@@ -30,7 +29,6 @@ const options = {
3029

3130
const client = new ModelArmorClient(options);
3231
const templateIdPrefix = `test-template-${uuidv4().substring(0, 8)}`;
33-
const execSync = cmd => cp.execSync(cmd, { encoding: 'utf-8' });
3432

3533
let emptyTemplateId;
3634
let basicTemplateId;
@@ -79,15 +77,19 @@ async function deleteTemplate(templateName) {
7977
}
8078
}
8179

80+
// eslint-disable-next-line no-unused-vars
8281
async function disableFloorSettings() {
8382
try {
83+
// Create global client
84+
const global_client = new ModelArmorClient();
85+
8486
// Disable project floor settings
85-
const [projectFloorSettings] = await client.getFloorSetting({
87+
const [projectFloorSettings] = await global_client.getFloorSetting({
8688
name: `projects/${projectId}/locations/global/floorSetting`,
8789
});
8890

8991
if (projectFloorSettings.enableFloorSettingEnforcement) {
90-
const [updatedProjectSettings] = await client.updateFloorSetting({
92+
const [updatedProjectSettings] = await global_client.updateFloorSetting({
9193
floorSetting: {
9294
name: `projects/${projectId}/locations/global/floorSetting`,
9395
enableFloorSettingEnforcement: false,
@@ -104,12 +106,12 @@ async function disableFloorSettings() {
104106

105107
// Disable folder floor settings if folderId is available
106108
if (folderId) {
107-
const [folderFloorSettings] = await client.getFloorSetting({
109+
const [folderFloorSettings] = await global_client.getFloorSetting({
108110
name: `folders/${folderId}/locations/global/floorSetting`,
109111
});
110112

111113
if (folderFloorSettings.enableFloorSettingEnforcement) {
112-
const [updatedFolderSettings] = await client.updateFloorSetting({
114+
const [updatedFolderSettings] = await global_client.updateFloorSetting({
113115
floorSetting: {
114116
name: `folders/${folderId}/locations/global/floorSetting`,
115117
enableFloorSettingEnforcement: false,
@@ -127,12 +129,12 @@ async function disableFloorSettings() {
127129

128130
// Disable organization floor settings if organizationId is available
129131
if (organizationId) {
130-
const [orgFloorSettings] = await client.getFloorSetting({
132+
const [orgFloorSettings] = await global_client.getFloorSetting({
131133
name: `organizations/${organizationId}/locations/global/floorSetting`,
132134
});
133135

134136
if (orgFloorSettings.enableFloorSettingEnforcement) {
135-
const [updatedOrgSettings] = await client.updateFloorSetting({
137+
const [updatedOrgSettings] = await global_client.updateFloorSetting({
136138
floorSetting: {
137139
name: `organizations/${organizationId}/locations/global/floorSetting`,
138140
enableFloorSettingEnforcement: false,
@@ -369,12 +371,10 @@ describe('Model Armor tests', () => {
369371

370372
after(async () => {
371373
for (const templateName of templatesToDelete) {
372-
373-
// TODO: Enable clean up once tests are enabled.
374+
// TODO(b/424365799): Uncomment below code once the mentioned issue is resolved
374375
// Disable floor settings to restore original state
375376
// await disableFloorSettings();
376377

377-
378378
await deleteTemplate(templateName);
379379
}
380380

@@ -383,60 +383,58 @@ describe('Model Armor tests', () => {
383383

384384
// =================== Floor Settings Tests ===================
385385

386-
// TODO: Enable these tests once floor settings API issue is resolved.
386+
// TODO(b/424365799): Enable below tests once the mentioned issue is resolved
387387

388388
it.skip('should get organization floor settings', () => {
389-
const output = execSync(
390-
`node snippets/getOrganizationFloorSettings.js ${organizationId}`
391-
).toString();
389+
const getOrganizationFloorSettings = require('../snippets/getOrganizationFloorSettings');
390+
391+
const output = getOrganizationFloorSettings(organizationId).toString();
392+
392393
const expectedName = `organizations/${organizationId}/locations/global/floorSetting`;
393394
assert.match(output, new RegExp(expectedName.replace(/\//g, '\\/')));
394395
});
395396

396397
it.skip('should get folder floor settings', () => {
397-
const output = execSync(
398-
`node snippets/getFolderFloorSettings.js ${folderId}`
399-
).toString();
398+
const getFolderFloorSettings = require('../snippets/getFolderFloorSettings');
399+
400+
const output = getFolderFloorSettings(folderId).toString();
400401

401402
// Check for expected name format in output
402403
const expectedName = `folders/${folderId}/locations/global/floorSetting`;
403404
assert.match(output, new RegExp(expectedName.replace(/\//g, '\\/')));
404405
});
405406

406407
it.skip('should get project floor settings', () => {
407-
const output = execSync(
408-
`node snippets/getProjectFloorSettings.js ${projectId}`
409-
).toString();
408+
const getProjectFloorSettings = require('../snippets/getProjectFloorSettings');
409+
410+
const output = getProjectFloorSettings(projectId).toString();
410411

411412
// Check for expected name format in output
412413
const expectedName = `projects/${projectId}/locations/global/floorSetting`;
413414
assert.match(output, new RegExp(expectedName.replace(/\//g, '\\/')));
414415
});
415416

416417
it.skip('should update organization floor settings', () => {
417-
const output = execSync(
418-
`node snippets/updateOrganizationFloorSettings.js ${organizationId}`
419-
).toString();
418+
const updateOrganizationFloorSettings = require('../snippets/updateOrganizationFloorSettings');
419+
const output = updateOrganizationFloorSettings(organizationId).toString();
420420
// Check that the update was performed
421421
assert.match(output, /Updated organization floor settings/);
422422
// Check that the response contains enableFloorSettingEnforcement=true
423423
assert.match(output, /enableFloorSettingEnforcement:\s*true/);
424424
});
425425

426426
it.skip('should update folder floor settings', () => {
427-
const output = execSync(
428-
`node snippets/updateFolderFloorSettings.js ${folderId}`
429-
).toString();
427+
const updateFolderFloorSettings = require('../snippets/updateFolderFloorSettings');
428+
const output = updateFolderFloorSettings(folderId).toString();
430429
// Check that the update was performed
431430
assert.match(output, /Updated folder floor settings/);
432431
// Check that the response contains enableFloorSettingEnforcement=true
433432
assert.match(output, /enableFloorSettingEnforcement:\s*true/);
434433
});
435434

436435
it.skip('should update project floor settings', () => {
437-
const output = execSync(
438-
`node snippets/updateProjectFloorSettings.js ${projectId}`
439-
).toString();
436+
const updateProjectFloorSettings = require('../snippets/updateProjectFloorSettings');
437+
const output = updateProjectFloorSettings(projectId).toString();
440438
// Check that the update was performed
441439
assert.match(output, /Updated project floor settings/);
442440
// Check that the response contains enableFloorSettingEnforcement=true
@@ -999,4 +997,4 @@ describe('Model Armor tests', () => {
999997
'NO_MATCH_FOUND'
1000998
);
1001999
});
1002-
});
1000+
});

0 commit comments

Comments
 (0)