Skip to content

Commit 5383ced

Browse files
authored
DLP: Updated test cases for storedInfoType related samples to use mock approach (GoogleCloudPlatform#3414)
Tests of following region tag samples updated -dlp_create_stored_infotype -dlp_update_stored_infotype
1 parent 99e30d2 commit 5383ced

File tree

4 files changed

+196
-97
lines changed

4 files changed

+196
-97
lines changed

dlp/createStoredInfoType.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// title: Create stored infotype.
1919
// description: Uses the Data Loss Prevention API to create a stored infotype.
2020
// usage: node createStoredInfoType.js projectId infoTypeId, outputPath, dataProjectId, datasetId, tableId, fieldName
21-
function main(
21+
async function main(
2222
projectId,
2323
infoTypeId,
2424
outputPath,
@@ -99,7 +99,7 @@ function main(
9999
// Print results
100100
console.log(`InfoType stored successfully: ${response.name}`);
101101
}
102-
createStoredInfoType();
102+
await createStoredInfoType();
103103
// [END dlp_create_stored_infotype]
104104
}
105105

@@ -108,4 +108,7 @@ process.on('unhandledRejection', err => {
108108
process.exitCode = 1;
109109
});
110110

111-
main(...process.argv.slice(2));
111+
// TODO(developer): Please uncomment below line before running sample
112+
// main(...process.argv.slice(2));
113+
114+
module.exports = main;

dlp/system-test/metadata.test.js

Lines changed: 133 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ const cp = require('child_process');
2020
const uuid = require('uuid');
2121
const DLP = require('@google-cloud/dlp');
2222
const {Storage} = require('@google-cloud/storage');
23+
const proxyquire = require('proxyquire');
24+
const sinon = require('sinon');
25+
26+
const {MOCK_DATA} = require('./mockdata');
2327

2428
const dataProject = 'bigquery-public-data';
2529
const dataSetId = 'samples';
2630
const tableId = 'github_nested';
27-
const fieldId = 'url';
2831

2932
const storage = new Storage();
3033
const testFile = 'resources/test.txt';
@@ -35,7 +38,6 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
3538
const client = new DLP.DlpServiceClient();
3639
describe('metadata', () => {
3740
let projectId, storedInfoTypeId;
38-
const infoTypeCloudStorageFileSet = `gs://${bucketName}/test.txt`;
3941

4042
before(async () => {
4143
projectId = await client.getProjectId();
@@ -58,6 +60,7 @@ describe('metadata', () => {
5860

5961
// Delete stored infotypes created in the snippets.
6062
afterEach(async () => {
63+
sinon.restore();
6164
if (!storedInfoTypeId) {
6265
return;
6366
}
@@ -85,108 +88,147 @@ describe('metadata', () => {
8588
});
8689

8790
// dlp_create_stored_infotype
88-
it('should create a stored infotype', () => {
89-
const infoTypeId = `stored-infoType-${uuid.v4()}`;
90-
const infoTypeOutputPath = `gs://${bucketName}`;
91-
const output = execSync(
92-
`node createStoredInfoType.js ${projectId} ${infoTypeId} ${infoTypeOutputPath} ${dataProject} ${dataSetId} ${tableId} ${fieldId}`
91+
it('should create a stored infotype', async () => {
92+
const storedInfoTypeName = 'MOCK_INFOTYPE';
93+
const infoTypeId = 'MOCK_INFOTYPE_ID';
94+
const outputPath = 'MOCK_OUTPUT_PATH';
95+
const fieldName = 'MOCK_FIELD';
96+
const DATA_CONSTANTS = MOCK_DATA.CREATE_STORED_INFOTYPE(
97+
projectId,
98+
infoTypeId,
99+
outputPath,
100+
dataProject,
101+
dataSetId,
102+
tableId,
103+
fieldName,
104+
storedInfoTypeName
105+
);
106+
const mockCreateStoredInfoType = sinon
107+
.stub()
108+
.resolves([{name: storedInfoTypeName}]);
109+
sinon.replace(
110+
DLP.DlpServiceClient.prototype,
111+
'createStoredInfoType',
112+
mockCreateStoredInfoType
113+
);
114+
115+
const mockConsoleLog = sinon.stub();
116+
sinon.replace(console, 'log', mockConsoleLog);
117+
118+
const createStoredInfoType = proxyquire('../createStoredInfoType', {
119+
'@google-cloud/dlp': {DLP: DLP},
120+
});
121+
await createStoredInfoType(
122+
projectId,
123+
infoTypeId,
124+
outputPath,
125+
dataProject,
126+
dataSetId,
127+
tableId,
128+
fieldName
129+
);
130+
sinon.assert.calledOnceWithExactly(
131+
mockCreateStoredInfoType,
132+
DATA_CONSTANTS.REQUEST_CREATE_DLP_JOB
133+
);
134+
sinon.assert.calledWithExactly(
135+
mockConsoleLog,
136+
`InfoType stored successfully: ${storedInfoTypeName}`
93137
);
94-
assert.match(output, /InfoType stored successfully:/);
95-
storedInfoTypeId = output.split(':')[1].trim();
96138
});
97139

98-
it('should handle stored infotype creation errors', () => {
99-
let output;
100-
const infoTypeId = `stored-infoType-${uuid.v4()}`;
101-
const infoTypeOutputPath = 'INFOTYPE_OUTPUT_PATH';
140+
it('should handle error while creating stored infotype', async () => {
141+
const infoTypeId = 'MOCK_INFOTYPE_ID';
142+
const outputPath = 'MOCK_OUTPUT_PATH';
143+
const fieldName = 'MOCK_FIELD';
144+
const mockCreateStoredInfoType = sinon.stub().rejects(new Error('Failed'));
145+
sinon.replace(
146+
DLP.DlpServiceClient.prototype,
147+
'createStoredInfoType',
148+
mockCreateStoredInfoType
149+
);
150+
151+
const mockConsoleLog = sinon.stub();
152+
sinon.replace(console, 'log', mockConsoleLog);
153+
154+
const createStoredInfoType = proxyquire('../createStoredInfoType', {
155+
'@google-cloud/dlp': {DLP: DLP},
156+
});
102157
try {
103-
output = execSync(
104-
`node createStoredInfoType.js BAD_PROJECT_ID ${infoTypeId} ${infoTypeOutputPath} ${dataProject} ${dataSetId} ${tableId} ${fieldId}`
158+
await createStoredInfoType(
159+
projectId,
160+
infoTypeId,
161+
outputPath,
162+
dataProject,
163+
dataSetId,
164+
tableId,
165+
fieldName
105166
);
106-
} catch (err) {
107-
output = err.message;
167+
} catch (error) {
168+
assert.equal(error.message, 'Failed');
108169
}
109-
assert.include(output, 'INVALID_ARGUMENT');
110170
});
111171

112172
// dlp_update_stored_infotype
113173
it('should update a stored infotype', async () => {
114-
let output;
115-
const infoTypeId = `stored-infoType-${uuid.v4()}`;
116-
const infoTypeOutputPath = `gs://${bucketName}`;
117-
try {
118-
// First create a temporary stored infoType
119-
const [response] = await client.createStoredInfoType({
120-
parent: `projects/${projectId}/locations/global`,
121-
config: {
122-
displayName: 'GitHub usernames',
123-
description: 'Dictionary of GitHub usernames used in commits',
124-
largeCustomDictionary: {
125-
outputPath: {
126-
path: infoTypeOutputPath,
127-
},
128-
bigQueryField: {
129-
table: {
130-
datasetId: dataSetId,
131-
projectId: dataProject,
132-
tableId: tableId,
133-
},
134-
field: {
135-
name: fieldId,
136-
},
137-
},
138-
},
139-
},
140-
storedInfoTypeId: infoTypeId,
141-
});
142-
storedInfoTypeId = response.name;
143-
// Execute the update script
144-
output = execSync(
145-
`node updateStoredInfoType.js ${projectId} ${infoTypeId} ${infoTypeOutputPath} ${infoTypeCloudStorageFileSet}`
146-
);
147-
} catch (err) {
148-
output = err.message;
149-
}
150-
assert.match(output, /InfoType updated successfully:/);
174+
const storedInfoTypeName = 'MOCK_INFOTYPE';
175+
const infoTypeId = 'MOCK_INFOTYPE_ID';
176+
const outputPath = 'MOCK_OUTPUT_PATH';
177+
const fileSetUrl = 'MOCK_FILE_SET_URL';
178+
const DATA_CONSTANTS = MOCK_DATA.UPDATE_STORED_INFOTYPE(
179+
projectId,
180+
infoTypeId,
181+
outputPath,
182+
fileSetUrl
183+
);
184+
const mockUpdateStoredInfoType = sinon
185+
.stub()
186+
.resolves([{name: storedInfoTypeName}]);
187+
sinon.replace(
188+
DLP.DlpServiceClient.prototype,
189+
'updateStoredInfoType',
190+
mockUpdateStoredInfoType
191+
);
192+
193+
const mockConsoleLog = sinon.stub();
194+
sinon.replace(console, 'log', mockConsoleLog);
195+
196+
const updateStoredInfoType = proxyquire('../updateStoredInfoType', {
197+
'@google-cloud/dlp': {DLP: DLP},
198+
});
199+
await updateStoredInfoType(projectId, infoTypeId, outputPath, fileSetUrl);
200+
201+
sinon.assert.calledWith(
202+
mockUpdateStoredInfoType,
203+
DATA_CONSTANTS.REQUEST_UPDATE_STORED_INFOTYPE
204+
);
205+
sinon.assert.calledWithMatch(
206+
mockConsoleLog,
207+
'InfoType updated successfully:'
208+
);
151209
});
152210

153-
it('should handle stored infotype update errors', async () => {
154-
let output;
155-
const infoTypeId = `stored-infoType-${uuid.v4()}`;
156-
const infoTypeOutputPath = 'INFOTYPE_OUTPUT_PATH';
211+
it('should handle error while updating stored infotype', async () => {
212+
const infoTypeId = 'MOCK_INFOTYPE_ID';
213+
const outputPath = 'MOCK_OUTPUT_PATH';
214+
const fileSetUrl = 'MOCK_FILE_SET_URL';
215+
const mockUpdateStoredInfoType = sinon.stub().rejects(new Error('Failed'));
216+
sinon.replace(
217+
DLP.DlpServiceClient.prototype,
218+
'updateStoredInfoType',
219+
mockUpdateStoredInfoType
220+
);
221+
222+
const mockConsoleLog = sinon.stub();
223+
sinon.replace(console, 'log', mockConsoleLog);
224+
225+
const updateStoredInfoType = proxyquire('../updateStoredInfoType', {
226+
'@google-cloud/dlp': {DLP: DLP},
227+
});
157228
try {
158-
// First create a temporary stored infoType
159-
const [response] = await client.createStoredInfoType({
160-
parent: `projects/${projectId}/locations/global`,
161-
config: {
162-
displayName: 'GitHub usernames',
163-
description: 'Dictionary of GitHub usernames used in commits',
164-
largeCustomDictionary: {
165-
outputPath: {
166-
path: infoTypeOutputPath,
167-
},
168-
bigQueryField: {
169-
table: {
170-
datasetId: dataSetId,
171-
projectId: dataProject,
172-
tableId: tableId,
173-
},
174-
field: {
175-
name: fieldId,
176-
},
177-
},
178-
},
179-
},
180-
storedInfoTypeId: infoTypeId,
181-
});
182-
storedInfoTypeId = response.name;
183-
// Execute the update script
184-
output = execSync(
185-
`node updateStoredInfoType.js BAD_PROJECT_ID ${infoTypeId} ${infoTypeOutputPath} ${infoTypeCloudStorageFileSet}`
186-
);
187-
} catch (err) {
188-
output = err.message;
229+
await updateStoredInfoType(projectId, infoTypeId, outputPath, fileSetUrl);
230+
} catch (error) {
231+
assert.equal(error.message, 'Failed');
189232
}
190-
assert.include(output, 'INVALID_ARGUMENT');
191233
});
192234
});

dlp/system-test/mockdata.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,57 @@ const MOCK_DATA = {
11971197
nack: sinon.stub(),
11981198
},
11991199
}),
1200+
CREATE_STORED_INFOTYPE: (
1201+
projectId,
1202+
infoTypeId,
1203+
outputPath,
1204+
dataProjectId,
1205+
datasetId,
1206+
tableId,
1207+
fieldName
1208+
) => ({
1209+
REQUEST_CREATE_DLP_JOB: {
1210+
parent: `projects/${projectId}/locations/global`,
1211+
config: {
1212+
displayName: 'GitHub usernames',
1213+
description: 'Dictionary of GitHub usernames used in commits',
1214+
largeCustomDictionary: {
1215+
outputPath: {
1216+
path: outputPath,
1217+
},
1218+
bigQueryField: {
1219+
table: {
1220+
datasetId: datasetId,
1221+
projectId: dataProjectId,
1222+
tableId: tableId,
1223+
},
1224+
field: {
1225+
name: fieldName,
1226+
},
1227+
},
1228+
},
1229+
},
1230+
storedInfoTypeId: infoTypeId,
1231+
},
1232+
}),
1233+
UPDATE_STORED_INFOTYPE: (projectId, infoTypeId, outputPath, fileSetUrl) => ({
1234+
REQUEST_UPDATE_STORED_INFOTYPE: {
1235+
name: `projects/${projectId}/storedInfoTypes/${infoTypeId}`,
1236+
config: {
1237+
largeCustomDictionary: {
1238+
outputPath: {
1239+
path: outputPath,
1240+
},
1241+
cloudStorageFileSet: {
1242+
url: fileSetUrl,
1243+
},
1244+
},
1245+
},
1246+
updateMask: {
1247+
paths: ['large_custom_dictionary.cloud_storage_file_set.url'],
1248+
},
1249+
},
1250+
}),
12001251
};
12011252

12021253
module.exports = {MOCK_DATA};

dlp/updateStoredInfoType.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// title: Update stored infoType.
1919
// description: Uses the Data Loss Prevention API to update a stored infoType.
2020
// usage: node updateStoredInfoType.js projectId infoTypeId, outputPath, fileSetUrl
21-
function main(projectId, infoTypeId, outputPath, fileSetUrl) {
21+
async function main(projectId, infoTypeId, outputPath, fileSetUrl) {
2222
// [START dlp_update_stored_infotype]
2323
// Import the required libraries
2424
const dlp = require('@google-cloud/dlp');
@@ -68,7 +68,7 @@ function main(projectId, infoTypeId, outputPath, fileSetUrl) {
6868
// Print the results.
6969
console.log(`InfoType updated successfully: ${JSON.stringify(response)}`);
7070
}
71-
updateStoredInfoType();
71+
await updateStoredInfoType();
7272
// [END dlp_update_stored_infotype]
7373
}
7474

@@ -77,4 +77,7 @@ process.on('unhandledRejection', err => {
7777
process.exitCode = 1;
7878
});
7979

80-
main(...process.argv.slice(2));
80+
// TODO(developer): Please uncomment below line before running sample
81+
// main(...process.argv.slice(2));
82+
83+
module.exports = main;

0 commit comments

Comments
 (0)