Skip to content

Commit

Permalink
update config message test, fix lint issues (#1000)
Browse files Browse the repository at this point in the history
* add test for receiving initial config message, fix linting issues with iot

* remove iot from eslintignore
  • Loading branch information
Alex Hong authored Dec 5, 2018
1 parent 7c3eda8 commit a5ac074
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 21 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
**/node_modules/*
functions/**
iot/*
appengine/parse-server/cloud/main.js
7 changes: 7 additions & 0 deletions iot/beta-features/commands/receive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
"main": "cloudiot_mqtt_example_nodejs.js",
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"lint": "repo-tools lint",
"pretest": "npm run lint",
Expand Down
3 changes: 1 addition & 2 deletions iot/beta-features/commands/receive/receive.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ client.on('connect', success => {
if (!success) {
console.log('Client not connected...');
} else {
// TODO: wait for commands
console.log('Client connected, waiting for commands');
}
});
Expand All @@ -159,7 +158,7 @@ client.on('error', err => {
console.log('error', err);
});

client.on('message', (topic, message, packet) => {
client.on('message', (topic, message) => {
console.log(
'message received: ',
Buffer.from(message, 'base64').toString('ascii')
Expand Down
1 change: 0 additions & 1 deletion iot/beta-features/commands/receive/receiveTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const helper = `node manager.js`;
const receiveCmd = `node receive.js`;
const sendCmd = `node send.js`;
const receiveCmdSuffix = `--privateKeyFile=resources/rsa_private.pem --algorithm=RS256`;
// TODO: update manager paths when going from beta->GA
const cwdHelper = path.join(__dirname, `../../../manager`);
const cwdSend = path.join(__dirname, `../send`);
const installDeps = `npm install`;
Expand Down
2 changes: 1 addition & 1 deletion iot/beta-features/commands/send/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=4.3.2"
"node": ">=8.0.0"
},
"scripts": {
"lint": "repo-tools lint",
Expand Down
1 change: 0 additions & 1 deletion iot/beta-features/commands/send/sendTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const helper = `node manager.js`;
const receiveCmd = `node receive.js`;
const sendCmd = `node send.js`;
const receiveCmdSuffix = `--privateKeyFile=resources/rsa_private.pem --algorithm=RS256`;
// TODO: update manager path when going from beta->GA
const cwdHelper = path.join(__dirname, `../../../manager`);
const cwdRcv = path.join(__dirname, `../receive`);
const cwdSend = path.join(__dirname, `../send`);
Expand Down
2 changes: 1 addition & 1 deletion iot/http_example/cloudiot_http_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function publishAsync(authToken, messageCount, numMessages) {
// Send events for high-frequency updates, update state only occasionally.
const delayMs = argv.messageType === 'events' ? 1000 : 2000;
console.log(JSON.stringify(request));
request(options, function(error, response, body) {
request(options, function(error, response) {
if (error) {
console.error('Received error: ', error);
} else if (response.body.error) {
Expand Down
7 changes: 7 additions & 0 deletions iot/http_example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
"license": "Apache-2.0",
"author": "Google Inc.",
"main": "cloudiot_http_example_nodejs.js",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
},
Expand Down
16 changes: 11 additions & 5 deletions iot/manager/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ function createIotTopic(topicName) {
// Instantiates a client
const pubsub = new PubSub();

pubsub.createTopic(topicName).then(results => {
pubsub.createTopic(topicName).then(() => {
setupIotTopic(topicName);
});
}

// Lookup the registry, assuming that it exists.
function lookupRegistry(client, registryId, projectId, cloudRegion, cb) {
function lookupRegistry(client, registryId, projectId, cloudRegion) {
// [START iot_lookup_registry]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
Expand Down Expand Up @@ -191,8 +191,7 @@ function createUnauthDevice(
deviceId,
registryId,
projectId,
cloudRegion,
body
cloudRegion
) {
// [START iot_create_unauth_device]
// Client retrieved in callback
Expand Down Expand Up @@ -713,7 +712,14 @@ function setDeviceConfig(
}

// sends a command to a specified device subscribed to the commands topic
function sendCommand(client, deviceId, registryId, projectId, cloudRegion, commandMessage) {
function sendCommand(
client,
deviceId,
registryId,
projectId,
cloudRegion,
commandMessage
) {
// [START iot_send_command]
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
Expand Down
2 changes: 1 addition & 1 deletion iot/mqtt_example/cloudiot_mqtt_example_nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ client.on('message', (topic, message) => {
messageStr = 'Command message received: ';
}

messageStr += Buffer.from(message, 'base64').toString('ascii')
messageStr += Buffer.from(message, 'base64').toString('ascii');
console.log(messageStr);
});

Expand Down
7 changes: 7 additions & 0 deletions iot/mqtt_example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
"license": "Apache-2.0",
"main": "cloudiot_mqtt_example_nodejs.js",
"name": "nodejs-docs-samples-iot-mqtt-example",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "repo-tools test run --cmd ava -- -T 3m --verbose system-test/*.test.js"
},
Expand Down
32 changes: 24 additions & 8 deletions iot/mqtt_example/system-test/cloudiot_mqtt_example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ test(`should receive configuration message`, async t => {
`${cmd} --messageType=events --registryId="${localRegName}" --deviceId="${localDevice}" ${cmdSuffix}`,
cwd
);
// TODO: Figure out how to guarantee configuration update happens on connect
t.regex(output, new RegExp(`connect`));

t.regex(output, new RegExp('connect'));
t.regex(output, new RegExp('Config message received:'));

// Check / cleanup
await tools.runAsync(
Expand Down Expand Up @@ -153,16 +154,31 @@ test(`should receive command message`, async t => {
const message = 'rotate 180 degrees';

await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
await tools.runAsync(`${helper} createRegistry ${localRegName} ${topicName}`, cwd);
await tools.runAsync(`${helper} createRsa256Device ${localDevice} ${localRegName} resources/rsa_cert.pem`, cwd);
await tools.runAsync(
`${helper} createRegistry ${localRegName} ${topicName}`,
cwd
);
await tools.runAsync(
`${helper} createRsa256Device ${localDevice} ${localRegName} resources/rsa_cert.pem`,
cwd
);

let output = tools.runAsync(`${cmd} --registryId=${localRegName} --deviceId=${localDevice} --numMessages=30 --privateKeyFile=resources/rsa_private.pem --algorithm=RS256 --mqttBridgePort=443`, cwd);
let output = tools.runAsync(
`${cmd} --registryId=${localRegName} --deviceId=${localDevice} --numMessages=30 --privateKeyFile=resources/rsa_private.pem --algorithm=RS256 --mqttBridgePort=443`,
cwd
);

await tools.runAsync(`${helper} sendCommand ${localDevice} ${localRegName} "${message}"`, cwd);
await tools.runAsync(
`${helper} sendCommand ${localDevice} ${localRegName} "${message}"`,
cwd
);

t.regex(await output, new RegExp(`Command message received: ${message}`));

// Cleanup
await tools.runAsync(`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
await tools.runAsync(
`${helper} deleteDevice ${localDevice} ${localRegName}`,
cwd
);
await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
});
});

0 comments on commit a5ac074

Please sign in to comment.