diff --git a/lib/tessel/update.js b/lib/tessel/update.js index 7f976469..9de51e88 100644 --- a/lib/tessel/update.js +++ b/lib/tessel/update.js @@ -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) { diff --git a/test/unit/update.js b/test/unit/update.js index 64213f46..8b9fd863 100644 --- a/test/unit/update.js +++ b/test/unit/update.js @@ -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'] = {