fix(e2e:methods): reuse test device across device methods tests
This commit is contained in:
Родитель
7650afee6f
Коммит
d1a58daabd
|
@ -26,11 +26,11 @@ var hubConnectionString = process.env.IOTHUB_CONNECTION_STRING;
|
||||||
].forEach(function(protocolCtor) {
|
].forEach(function(protocolCtor) {
|
||||||
describe('Device Methods over ' + protocolCtor.name, function() {
|
describe('Device Methods over ' + protocolCtor.name, function() {
|
||||||
this.timeout(120000);
|
this.timeout(120000);
|
||||||
|
var registry = Registry.fromConnectionString(hubConnectionString);
|
||||||
var deviceClient;
|
var deviceClient;
|
||||||
var deviceDescription;
|
var deviceDescription;
|
||||||
|
|
||||||
// create a new device for every test
|
before(function (done) {
|
||||||
beforeEach(function (done) {
|
|
||||||
deviceDescription = {
|
deviceDescription = {
|
||||||
deviceId: '0000e2etest-delete-me-node-device-method-' + uuid.v4(),
|
deviceId: '0000e2etest-delete-me-node-device-method-' + uuid.v4(),
|
||||||
status: 'enabled',
|
status: 'enabled',
|
||||||
|
@ -42,33 +42,63 @@ var hubConnectionString = process.env.IOTHUB_CONNECTION_STRING;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var registry = Registry.fromConnectionString(hubConnectionString);
|
debug('creating device: ' + deviceDescription.deviceId);
|
||||||
registry.create(deviceDescription, function (err) {
|
registry.create(deviceDescription, function (err) {
|
||||||
if (err) return done(err);
|
if (err) {
|
||||||
debug('created test device: ' + deviceDescription.deviceId);
|
debug('failed to create the device: ' + deviceDescription.deviceId + ': ' + err.toString());
|
||||||
var host = ConnectionString.parse(hubConnectionString).HostName;
|
return done(err);
|
||||||
var sas = deviceSas.create(host, deviceDescription.deviceId, deviceDescription.authentication.symmetricKey.primaryKey, anHourFromNow()).toString();
|
} else {
|
||||||
deviceClient = deviceSdk.Client.fromSharedAccessSignature(sas, protocolCtor);
|
debug('created test device: ' + deviceDescription.deviceId);
|
||||||
deviceClient.open(done);
|
return done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
after(function (done) {
|
||||||
|
debug('deleting test device: ' + deviceDescription.deviceId);
|
||||||
|
registry.delete(deviceDescription.deviceId, function (err) {
|
||||||
|
if (err) {
|
||||||
|
debug('failed to delete device: ' + deviceDescription.deviceId + ': ' + err.toString());
|
||||||
|
return done(err);
|
||||||
|
} else {
|
||||||
|
debug('device deleted: ' + deviceDescription.deviceId);
|
||||||
|
return done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// create a new device for every test
|
||||||
|
beforeEach(function (done) {
|
||||||
|
var host = ConnectionString.parse(hubConnectionString).HostName;
|
||||||
|
var sas = deviceSas.create(host, deviceDescription.deviceId, deviceDescription.authentication.symmetricKey.primaryKey, anHourFromNow()).toString();
|
||||||
|
deviceClient = deviceSdk.Client.fromSharedAccessSignature(sas, protocolCtor);
|
||||||
|
debug('connecting device client...');
|
||||||
|
deviceClient.open(function (err) {
|
||||||
|
if (err) {
|
||||||
|
debug('error connecting device client: ' + err.toString());
|
||||||
|
return done(err);
|
||||||
|
} else {
|
||||||
|
debug('device client connected');
|
||||||
|
return done();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// nuke the test device after every test
|
// nuke the test device after every test
|
||||||
afterEach(function (done) {
|
afterEach(function (done) {
|
||||||
if (!!deviceClient) {
|
if (!!deviceClient) {
|
||||||
|
debug('disconnecting device client...');
|
||||||
deviceClient.close(function(err) {
|
deviceClient.close(function(err) {
|
||||||
if (!!err) {
|
if (!!err) {
|
||||||
console.warn('Could not close connection to device ' +
|
console.warn('Could not close connection to device ' + deviceDescription.deviceId + ': ' + err.toString());
|
||||||
deviceDescription.deviceId + '. Deleting device anyway.'
|
return done(err);
|
||||||
);
|
} else {
|
||||||
|
debug('device connection closed');
|
||||||
|
return done();
|
||||||
}
|
}
|
||||||
|
|
||||||
debug('deleting test device: ' + deviceDescription.deviceId);
|
|
||||||
Registry
|
|
||||||
.fromConnectionString(hubConnectionString)
|
|
||||||
.delete(deviceDescription.deviceId, done);
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
debug('no device client. nothing to disconnect');
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Загрузка…
Ссылка в новой задаче