Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for HTTP device sample #534

Merged
merged 7 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions iot/http_example/cloudiot_http_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ var argv = require(`yargs`)
.strict()
.argv;

// [START iot_http_variables]
// A unique string that identifies this device. For Google Cloud IoT Core, it
// must be in the format below.

let iatTime = parseInt(Date.now() / 1000);
let authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: s/snake_case/camelCase/g

const devicePath = `projects/${argv.project_id}/locations/${argv.cloud_region}/registries/${argv.registry_id}/devices/${argv.device_id}`;

// The request path, set accordingly depending on the message type.
const pathSuffix = argv.message_type === 'events'
? ':publishEvent' : ':setState';
const urlBase = `https://${argv.http_bridge_address}/v1beta1/${devicePath}`;
const url = `${urlBase}${pathSuffix}`;
// [END iot_http_variables]

// Create a Cloud IoT Core JWT for the given project ID, signed with the given
// private key.
// [START iot_http_jwt]
Expand All @@ -115,7 +130,7 @@ function createJwt (projectId, privateKeyFile, algorithm) {
// messageCount. Telemetry events are published at a rate of 1 per second and
// states at a rate of 1 every 2 seconds.
// [START iot_http_publish]
function publishAsync (messageCount, numMessages) {
function publishAsync (authToken, messageCount, numMessages) {
const payload = `${argv.registry_id}/${argv.device_id}-payload-${messageCount}`;
console.log('Publishing message:', payload);
const binaryData = Buffer.from(payload).toString('base64');
Expand All @@ -129,10 +144,9 @@ function publishAsync (messageCount, numMessages) {
const options = {
url: url,
headers: {
'authorization': 'Bearer ' + authToken,
'authorization': `Bearer ${authToken}`,
'content-type': 'application/json',
'cache-control': 'no-cache'

},
json: true,
body: postData
Expand All @@ -155,7 +169,6 @@ function publishAsync (messageCount, numMessages) {
if (secsFromIssue > argv.token_exp_mins * 60) {
iatTime = parseInt(Date.now() / 1000);
console.log(`\tRefreshing token after ${secsFromIssue} seconds.`);

authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm);
}

Expand All @@ -167,13 +180,12 @@ function publishAsync (messageCount, numMessages) {
// [END iot_http_publish]

// [START iot_http_getconfig]
function getConfig (version) {
console.log('Getting config:');
console.log(urlBase);
function getConfig (authToken, version) {
console.log(`Getting config from URL: ${urlBase}`);
const options = {
url: urlBase + '/config?local_version=' + version,
headers: {
'authorization': 'Bearer ' + authToken,
'authorization': `Bearer ${authToken}`,
'content-type': 'application/json',
'cache-control': 'no-cache'

Expand All @@ -184,7 +196,7 @@ function getConfig (version) {
if (error) {
console.error('Received error: ', error);
} else if (response.body.error) {
console.error('Received error: ' + JSON.stringify(response.body.error));
console.error(`Received error: ${JSON.stringify(response.body.error)}`);
} else {
console.log('Received config', JSON.stringify(body));
}
Expand All @@ -193,21 +205,10 @@ function getConfig (version) {
// [END iot_http_getconfig]

// [START iot_run_http]
// A unique string that identifies this device. For Google Cloud IoT Core, it
// must be in the format below.
const devicePath = `projects/${argv.project_id}/locations/${argv.cloud_region}/registries/${argv.registry_id}/devices/${argv.device_id}`;

// The request path, set accordingly depending on the message type.
const pathSuffix = argv.message_type === 'events'
? ':publishEvent' : ':setState';
const urlBase = `https://${argv.http_bridge_address}/v1beta1/${devicePath}`;
const url = `${urlBase}${pathSuffix}`;
let iatTime = parseInt(Date.now() / 1000);
let authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm);

// Print latest configuration
getConfig(0);
getConfig(authToken, 0);

// Publish messages.
publishAsync(1, argv.num_messages);
publishAsync(authToken, 1, argv.num_messages);
// [END iot_run_http]
58 changes: 24 additions & 34 deletions iot/http_example/system-test/cloudiot_http_example_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,68 +51,58 @@ test.after.always(async () => {
test(`should receive configuration message`, async (t) => {
const localDevice = `test-rsa-device`;
const localRegName = `${registryName}-rsa256`;
let output = await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
output = await tools.runAsync(
await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
await tools.runAsync(
`${helper} createRegistry ${localRegName} ${topicName}`, cwd);
output = await tools.runAsync(
await tools.runAsync(
`${helper} createRsa256Device ${localDevice} ${localRegName} resources/rsa_cert.pem`, cwd);
t.regex(output, new RegExp(`Created device`));

output = await tools.runAsync(
let output = await tools.runAsync(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can these be const instead of let? (In general, use const for variables that are never reassigned to. If you make this change, please do so throughout your PR.)

`${cmd} --message_type=events --num_messages=1 --private_key_file=resources/rsa_private.pem --algorithm=RS256`, cwd);

t.regex(output, new RegExp(`Getting config`));
t.regex(output, new RegExp(/Getting config/));

// Check / cleanup
output = await tools.runAsync(
`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
t.regex(output, new RegExp(`State`));
output = await tools.runAsync(
`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
t.regex(output, new RegExp(`Successfully deleted device`));
output = await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
await tools.runAsync(`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
await tools.runAsync(`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
});

test(`should send event message`, async (t) => {
const localDevice = `test-rsa-device`;
const localRegName = `${registryName}-rsa256`;
let output = await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
output = await tools.runAsync(
await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
await tools.runAsync(
`${helper} createRegistry ${localRegName} ${topicName}`, cwd);
output = await tools.runAsync(
await tools.runAsync(
`${helper} createRsa256Device ${localDevice} ${localRegName} resources/rsa_cert.pem`, cwd);

output = await tools.runAsync(
let output = await tools.runAsync(
`${cmd} --message_type=events --num_messages=1 --private_key_file=resources/rsa_private.pem --algorithm=RS256`, cwd);

t.regex(output, new RegExp(`Publishing message`));
t.regex(output, new RegExp(/Publishing message/));

// Check / cleanup
output = await tools.runAsync(
`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
output = await tools.runAsync(
`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
output = await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
await tools.runAsync(`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
await tools.runAsync(`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
});

test(`should send event message`, async (t) => {
const localDevice = `test-rsa-device`;
const localRegName = `${registryName}-rsa256`;
let output = await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
output = await tools.runAsync(
await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
await tools.runAsync(
`${helper} createRegistry ${localRegName} ${topicName}`, cwd);
output = await tools.runAsync(
await tools.runAsync(
`${helper} createRsa256Device ${localDevice} ${localRegName} resources/rsa_cert.pem`, cwd);

output = await tools.runAsync(
let output = await tools.runAsync(
`${cmd} --message_type=state --num_messages=1 --private_key_file=resources/rsa_private.pem --algorithm=RS256`, cwd);

t.regex(output, new RegExp(`Publishing message`));
t.regex(output, new RegExp(/Publishing message/));

// Check / cleanup
output = await tools.runAsync(
`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
output = await tools.runAsync(
`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
output = await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
await tools.runAsync(`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
await tools.runAsync(`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
});