Skip to content

Commit 9271b94

Browse files
committed
Fixes from http in tests / naming
1 parent e0379ee commit 9271b94

File tree

2 files changed

+46
-49
lines changed

2 files changed

+46
-49
lines changed

iot/mqtt_example/cloudiot_mqtt_example_nodejs.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,31 @@ const mqtt = require('mqtt');
2424
console.log('Google Cloud IoT Core MQTT example.');
2525
var argv = require(`yargs`)
2626
.options({
27-
project_id: {
27+
projectId: {
2828
default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT,
2929
description: 'The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variables.',
3030
requiresArg: true,
3131
type: 'string'
3232
},
33-
cloud_region: {
33+
cloudRegion: {
3434
default: 'us-central1',
3535
description: 'GCP cloud region.',
3636
requiresArg: true,
3737
type: 'string'
3838
},
39-
registry_id: {
39+
registryId: {
4040
description: 'Cloud IoT registry ID.',
4141
requiresArg: true,
4242
demandOption: true,
4343
type: 'string'
4444
},
45-
device_id: {
45+
deviceId: {
4646
description: 'Cloud IoT device ID.',
4747
requiresArg: true,
4848
demandOption: true,
4949
type: 'string'
5050
},
51-
private_key_file: {
51+
privateKeyFile: {
5252
description: 'Path to private key file.',
5353
requiresArg: true,
5454
demandOption: true,
@@ -61,39 +61,39 @@ var argv = require(`yargs`)
6161
choices: ['RS256', 'ES256'],
6262
type: 'string'
6363
},
64-
num_messages: {
64+
numMessages: {
6565
default: 100,
6666
description: 'Number of messages to publish.',
6767
requiresArg: true,
6868
type: 'number'
6969
},
70-
token_exp_mins: {
70+
tokenExpMins: {
7171
default: 20,
7272
description: 'Minutes to JWT token expiration.',
7373
requiresArg: true,
7474
type: 'number'
7575
},
76-
mqtt_bridge_hostname: {
76+
mqttBridgeHostname: {
7777
default: 'mqtt.googleapis.com',
7878
description: 'MQTT bridge hostname.',
7979
requiresArg: true,
8080
type: 'string'
8181
},
82-
mqtt_bridge_port: {
82+
mqttBridgePort: {
8383
default: 8883,
8484
description: 'MQTT bridge port.',
8585
requiresArg: true,
8686
type: 'number'
8787
},
88-
message_type: {
88+
messageType: {
8989
default: 'events',
9090
description: 'Message type to publish.',
9191
requiresArg: true,
9292
choices: ['events', 'state'],
9393
type: 'string'
9494
}
9595
})
96-
.example(`node $0 cloudiot_mqtt_example_nodejs.js --project_id=blue-jet-123 --registry_id=my-registry --device_id=my-node-device --private_key_file=../rsa_private.pem --algorithm=RS256`)
96+
.example(`node $0 cloudiot_mqtt_example_nodejs.js --projectId=blue-jet-123 --registryId=my-registry --deviceId=my-node-device --privateKeyFile=../rsa_private.pem --algorithm=RS256`)
9797
.wrap(120)
9898
.recommendCommands()
9999
.epilogue(`For more information, see https://cloud.google.com/iot-core/docs`)
@@ -122,30 +122,30 @@ function createJwt (projectId, privateKeyFile, algorithm) {
122122
// messageCount.
123123
// [START iot_mqtt_publish]
124124
function publishAsync (messageCount, numMessages) {
125-
const payload = `${argv.registry_id}/${argv.device_id}-payload-${messageCount}`;
125+
const payload = `${argv.registryId}/${argv.deviceId}-payload-${messageCount}`;
126126
// Publish "payload" to the MQTT topic. qos=1 means at least once delivery.
127127
// Cloud IoT Core also supports qos=0 for at most once delivery.
128128
console.log('Publishing message:', payload);
129129
client.publish(mqttTopic, payload, { qos: 1 });
130130

131-
const delayMs = argv.message_type === 'events' ? 1000 : 2000;
131+
const delayMs = argv.messageType === 'events' ? 1000 : 2000;
132132
if (messageCount < numMessages) {
133133
// If we have published fewer than numMessage messages, publish payload
134134
// messageCount + 1 in 1 second.
135135
setTimeout(function () {
136136
let secsFromIssue = parseInt(Date.now() / 1000) - iatTime;
137-
if (secsFromIssue > argv.token_exp_mins * 60) {
137+
if (secsFromIssue > argv.tokenExpMins * 60) {
138138
iatTime = parseInt(Date.now() / 1000);
139139
console.log(`\tRefreshing token after ${secsFromIssue} seconds.`);
140140

141141
client.end();
142-
connectionArgs.password = createJwt(argv.project_id, argv.private_key_file, argv.algorithm);
142+
connectionArgs.password = createJwt(argv.projectId, argv.privateKeyFile, argv.algorithm);
143143
client = mqtt.connect(connectionArgs);
144144

145145
client.on('connect', (success) => {
146146
console.log('connect');
147147
if (success) {
148-
publishAsync(1, argv.num_messages);
148+
publishAsync(1, argv.numMessages);
149149
} else {
150150
console.log('Client not connected...');
151151
}
@@ -180,18 +180,18 @@ function publishAsync (messageCount, numMessages) {
180180
// [START iot_mqtt_run]
181181
// The mqttClientId is a unique string that identifies this device. For Google
182182
// Cloud IoT Core, it must be in the format below.
183-
const mqttClientId = `projects/${argv.project_id}/locations/${argv.cloud_region}/registries/${argv.registry_id}/devices/${argv.device_id}`;
183+
const mqttClientId = `projects/${argv.projectId}/locations/${argv.cloudRegion}/registries/${argv.registryId}/devices/${argv.deviceId}`;
184184

185185
// With Google Cloud IoT Core, the username field is ignored, however it must be
186186
// non-empty. The password field is used to transmit a JWT to authorize the
187187
// device. The "mqtts" protocol causes the library to connect using SSL, which
188188
// is required for Cloud IoT Core.
189189
let connectionArgs = {
190-
host: argv.mqtt_bridge_hostname,
191-
port: argv.mqtt_bridge_port,
190+
host: argv.mqttBridgeHostname,
191+
port: argv.mqttBridgePort,
192192
clientId: mqttClientId,
193193
username: 'unused',
194-
password: createJwt(argv.project_id, argv.private_key_file, argv.algorithm),
194+
password: createJwt(argv.projectId, argv.privateKeyFile, argv.algorithm),
195195
protocol: 'mqtts',
196196
secureProtocol: 'TLSv1_2_method'
197197
};
@@ -200,18 +200,18 @@ let connectionArgs = {
200200
let iatTime = parseInt(Date.now() / 1000);
201201
let client = mqtt.connect(connectionArgs);
202202

203-
client.subscribe(`/devices/${argv.device_id}/config`);
203+
client.subscribe(`/devices/${argv.deviceId}/config`);
204204

205205
// The MQTT topic that this device will publish data to. The MQTT
206206
// topic name is required to be in the format below. The topic name must end in
207207
// 'state' to publish state and 'events' to publish telemetry. Note that this is
208208
// not the same as the device registry's Cloud Pub/Sub topic.
209-
const mqttTopic = `/devices/${argv.device_id}/${argv.message_type}`;
209+
const mqttTopic = `/devices/${argv.deviceId}/${argv.messageType}`;
210210

211211
client.on('connect', (success) => {
212212
console.log('connect');
213213
if (success) {
214-
publishAsync(1, argv.num_messages);
214+
publishAsync(1, argv.numMessages);
215215
} else {
216216
console.log('Client not connected...');
217217
}

iot/mqtt_example/system-test/cloudiot_http_example_test.js renamed to iot/mqtt_example/system-test/cloudiot_mqtt_example_test.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const topicName = `nodejs-docs-samples-test-iot-${uuid.v4()}`;
2525
const registryName = `nodejs-test-registry-iot-${uuid.v4()}`;
2626
const helper = `node ../manager/manager.js`;
2727
const cmd = `node cloudiot_mqtt_example_nodejs.js `;
28-
const cmdSuffix = ` --num_messages=1 --private_key_file=resources/rsa_private.pem --algorithm=RS256`;
28+
const cmdSuffix = ` --numMessages=1 --privateKeyFile=resources/rsa_private.pem --algorithm=RS256`;
2929
const cwd = path.join(__dirname, `..`);
3030

3131
test.before(tools.checkCredentials);
@@ -53,67 +53,64 @@ test(`should receive configuration message`, async (t) => {
5353
const localRegName = `${registryName}-rsa256`;
5454

5555
let output = await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
56-
output = await tools.runAsync(
56+
await tools.runAsync(
5757
`${helper} createRegistry ${localRegName} ${topicName}`, cwd);
58-
output = await tools.runAsync(
58+
await tools.runAsync(
5959
`${helper} createRsa256Device ${localDevice} ${localRegName} resources/rsa_cert.pem`, cwd);
60-
t.regex(output, new RegExp(`Created device`));
6160

6261
output = await tools.runAsync(
63-
`${cmd} --message_type=events --registry_id="${localRegName}" --device_id="${localDevice}" ${cmdSuffix}`,
62+
`${cmd} --messageType=events --registryId="${localRegName}" --deviceId="${localDevice}" ${cmdSuffix}`,
6463
cwd);
6564
t.regex(output, new RegExp(`message received`));
6665

6766
// Check / cleanup
68-
output = await tools.runAsync(
67+
await tools.runAsync(
6968
`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
70-
t.regex(output, new RegExp(`State`));
71-
output = await tools.runAsync(
69+
await tools.runAsync(
7270
`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
73-
t.regex(output, new RegExp(`Successfully deleted device`));
74-
output = await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
71+
await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
7572
});
7673

7774
test(`should send event message`, async (t) => {
7875
const localDevice = `test-rsa-device`;
7976
const localRegName = `${registryName}-rsa256`;
80-
let output = await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
81-
output = await tools.runAsync(
77+
await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
78+
await tools.runAsync(
8279
`${helper} createRegistry ${localRegName} ${topicName}`, cwd);
83-
output = await tools.runAsync(
80+
await tools.runAsync(
8481
`${helper} createRsa256Device ${localDevice} ${localRegName} resources/rsa_cert.pem`, cwd);
8582

86-
output = await tools.runAsync(
87-
`${cmd} --message_type=events --registry_id="${localRegName}" --device_id="${localDevice}" ${cmdSuffix}`,
83+
const output = await tools.runAsync(
84+
`${cmd} --messageType=events --registryId="${localRegName}" --deviceId="${localDevice}" ${cmdSuffix}`,
8885
cwd);
8986
t.regex(output, new RegExp(`Publishing message:`));
9087

9188
// Check / cleanup
92-
output = await tools.runAsync(
89+
await tools.runAsync(
9390
`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
94-
output = await tools.runAsync(
91+
await tools.runAsync(
9592
`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
96-
output = await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
93+
await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
9794
});
9895

9996
test(`should send event message`, async (t) => {
10097
const localDevice = `test-rsa-device`;
10198
const localRegName = `${registryName}-rsa256`;
102-
let output = await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
103-
output = await tools.runAsync(
99+
await tools.runAsync(`${helper} setupIotTopic ${topicName}`, cwd);
100+
await tools.runAsync(
104101
`${helper} createRegistry ${localRegName} ${topicName}`, cwd);
105-
output = await tools.runAsync(
102+
await tools.runAsync(
106103
`${helper} createRsa256Device ${localDevice} ${localRegName} resources/rsa_cert.pem`, cwd);
107104

108-
output = await tools.runAsync(
109-
`${cmd} --message_type=state --registry_id="${localRegName}" --device_id="${localDevice}" ${cmdSuffix}`,
105+
const output = await tools.runAsync(
106+
`${cmd} --messageType=state --registryId="${localRegName}" --deviceId="${localDevice}" ${cmdSuffix}`,
110107
cwd);
111108
t.regex(output, new RegExp(`Publishing message:`));
112109

113110
// Check / cleanup
114-
output = await tools.runAsync(
111+
await tools.runAsync(
115112
`${helper} getDeviceState ${localDevice} ${localRegName}`, cwd);
116-
output = await tools.runAsync(
113+
await tools.runAsync(
117114
`${helper} deleteDevice ${localDevice} ${localRegName}`, cwd);
118-
output = await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
115+
await tools.runAsync(`${helper} deleteRegistry ${localRegName}`, cwd);
119116
});

0 commit comments

Comments
 (0)