Skip to content

Commit

Permalink
Adds test for resolving builds within controller
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyman727 committed Feb 17, 2016
1 parent c5e138e commit e39398a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 23 deletions.
46 changes: 23 additions & 23 deletions lib/tessel/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,33 @@ Tessel.prototype.updateOpenWRT = function(image) {
logs.info('Updating OpenWRT (1/2)');

return new Promise((resolve, reject) => {
// Write the new image to a file in the /tmp dir
this.connection.exec(commands.openStdinToFile(updatePath), (err, remoteProc) => {
if (err) {
return reject(err);
}
// Write the new image to a file in the /tmp dir
this.connection.exec(commands.openStdinToFile(updatePath), (err, remoteProc) => {
if (err) {
return reject(err);
}

logs.info('Transferring image of size', (image.length / 1e6).toFixed(2), 'MB');
// When we finish writing the image
remoteProc.once('close', resolve);
// Write the image
remoteProc.stdin.end(image);
});
})
.then(() => {
return new Promise((resolve) => {
// Begin the sysupgrade
logs.info('Starting OpenWRT update. Please do not remove power from Tessel.');
// The USBDaemon will cut out or the SSH command will close
this.connection.exec(commands.sysupgrade(updatePath), (err, remoteProc) => {
remoteProc.stdout.on('data', function(d) {
if (d.toString().includes('Upgrade completed')) {
resolve();
}
logs.info('Transferring image of size', (image.length / 1e6).toFixed(2), 'MB');
// When we finish writing the image
remoteProc.once('close', resolve);
// Write the image
remoteProc.stdin.end(image);
});
})
.then(() => {
return new Promise((resolve) => {
// Begin the sysupgrade
logs.info('Starting OpenWRT update. Please do not remove power from Tessel.');
// The USBDaemon will cut out or the SSH command will close
this.connection.exec(commands.sysupgrade(updatePath), (err, remoteProc) => {
remoteProc.stdout.on('data', function(d) {
if (d.toString().includes('Upgrade completed')) {
resolve();
}
});
});
});
});
});
};

Tessel.prototype.updateFirmware = function(image) {
Expand Down
66 changes: 66 additions & 0 deletions test/unit/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,72 @@ exports['controller.update'] = {
test.done();
}.bind(this));
},

properBuildCompare: function(test) {

// use builds where the string compare of the versions
// would lead to incorrect comparison ('0.0.7 > 0.0.10')
var mixedBuilds = [{
sha: 'ac4d8d8a5bfd671f7f174c2eaa258856bd82fe29',
released: '2015-05-18T02:21:57.856Z',
version: '0.0.7'
}, {
sha: '9a85c84f5a03c715908921baaaa9e7397985bc7f',
released: '2015-08-12T03:01:57.856Z',
version: '0.0.10'
}];

// This Tessel instance MUST be connected via BOTH
//
// - USB
// - LAN (authorized)
//
this.tessel = TesselSimulator({
type: 'LAN',
authorized: true,
});

this.tessel.addConnection({
connectionType: 'USB',
end: function() {
return Promise.resolve();
}
});

var binaries = {
firmware: new Buffer(0),
openwrt: new Buffer(0)
};

this.fetchCurrentBuildInfo.restore();
this.fetchCurrentBuildInfo = this.sandbox.stub(Tessel.prototype, 'fetchCurrentBuildInfo', function() {
// Resolve with earlier build (0.0.7)
return Promise.resolve(mixedBuilds[0].sha);
});


this.requestBuildList.restore();
this.requestBuildList = this.sandbox.stub(updates, 'requestBuildList', function() {
// Return our two mixed builds
return Promise.resolve(mixedBuilds);
});

this.fetchBuild = this.sandbox.stub(updates, 'fetchBuild', function() {
return Promise.resolve(binaries);
});

controller.update({})
.then(function() {
// It should attempt to fetch a build
test.equal(this.fetchBuild.callCount, 1);
// We should be requesting the latest build
test.equal(this.fetchBuild.calledWith(mixedBuilds[1]), true);
test.done();
}.bind(this))
.catch(function() {
test.fail('Update should not reject with valid options and builds.');
});
}
};

exports['update-fetch'] = {
Expand Down

0 comments on commit e39398a

Please sign in to comment.