Skip to content

Commit

Permalink
Test fixes (#718)
Browse files Browse the repository at this point in the history
* Test fixes

- don't use NodeRSA in tests
- stub fs operations whenever possible
- stub public key operations whenever possible
- interval until activeSeeker is available

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* ci: less specific node version in appveyor

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* ci: clean up `grunt test`

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* ci: more testing on windows

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>

* ci: before_test

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Apr 29, 2016
1 parent 81f0d14 commit 557bcc3
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ before_install:
- npm install -g grunt-cli

install: npm install
script: grunt
script: grunt test
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-jsbeautifier');


// 'npm test' runs these tasks
grunt.registerTask('test', ['jshint', 'jscs', 'jsbeautifier', 'nodeunit']);
grunt.registerTask('test', ['jshint', 'jscs', 'nodeunit']);
grunt.registerTask('all', ['jsbeautifier', 'jshint', 'jscs', 'nodeunit']);

// Default task.
grunt.registerTask('default', ['test']);
grunt.registerTask('default', ['all']);


// Support running a single test suite
Expand Down
14 changes: 12 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ clone_depth: 10

environment:
matrix:
- nodejs_version: 4.0
- nodejs_version: 4
- nodejs_version: 5

platform:
- x86
- x64

install:
- ps: Install-Product node $env:nodejs_version
- npm install -g npm
- npm install

build: off

before_test:
- npm install -g grunt-cli

test_script:
- npm test
- ps: "grunt test # PowerShell"
- cmd: grunt test

cache:
- node_modules
Expand Down
12 changes: 5 additions & 7 deletions lib/tessel/provision.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ var util = require('util');
// Third Party Dependencies
var async = require('async');
var fs = require('fs-extra');
var NodeRSA = require('node-rsa');
var osenv = require('osenv');
var sshpk = require('sshpk');

// Internal
var commands = require('./commands');
var logs = require('../logs');
var RSA = require('./rsa-delegation');
var Tessel = require('./tessel');


Expand Down Expand Up @@ -83,7 +83,7 @@ actions.setupLocal = function(keyFile) {
logs.info('Creating public and private keys for Tessel authentication...');

// Generate SSH key
var key = new NodeRSA({
var key = new RSA({
b: 2048
});
var privateKey = key.exportKey('private');
Expand All @@ -100,12 +100,12 @@ actions.setupLocal = function(keyFile) {
// owner can read and write
var fileOptions = {
encoding: 'utf8',
mode: 384
mode: 0o600,
};
async.parallel([
(cb) => fs.writeFile(keyFile + '.pub', publicKey, fileOptions, cb), (cb) => fs.writeFile(keyFile, privateKey, fileOptions, cb),
],
function(err) {
(err) => {
if (err) {
return reject(err);
}
Expand All @@ -125,9 +125,7 @@ actions.authTessel = function(tessel, filepath) {
actions.checkAuthFileExists(tessel, remoteAuthFile)
.then(function readKey() {
// Read the public key
fs.readFile(filepath + '.pub', {
encoding: 'utf-8'
}, function(err, pubKey) {
fs.readFile(filepath + '.pub', 'utf8', (err, pubKey) => {
if (err) {
return reject(err);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/tessel/rsa-delegation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function MockRSA() {}

MockRSA.prototype.exportKey = function() {};

module.exports = global.IS_TEST_ENV ? MockRSA : require('node-rsa');
1 change: 1 addition & 0 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"mdns": true,
"mkdirp": true,
"NodeRSA": true,
"RSA": true,
"osenv": true,
"path": true,
"Preferences": true,
Expand Down
2 changes: 1 addition & 1 deletion test/common/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ global.Ignore = require('fstream-ignore');
global.inquirer = require('inquirer');
global.mdns = require('mdns-js');
global.mkdirp = require('mkdirp');
global.NodeRSA = require('node-rsa');
global.osenv = require('osenv');
global.Project = require('t2-project');
global.request = require('request');
Expand All @@ -42,6 +41,7 @@ global.Tessel = require('../../lib/tessel/tessel');
global.commands = require('../../lib/tessel/commands');
global.deploy = require('../../lib/tessel/deploy');
global.provision = require('../../lib/tessel/provision');
global.RSA = require('../../lib/tessel/rsa-delegation');
global.deployLists = require('../../lib/tessel/deploy-lists');

// ./lib/*
Expand Down
6 changes: 5 additions & 1 deletion test/unit/crash-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ exports['CrashReporter.status'] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.logsInfo = this.sandbox.stub(logs, 'info');
this.crPost = this.sandbox.spy(CrashReporter, 'status');
this.prefLoad = this.sandbox.stub(Preferences, 'load', () => {
return Promise.resolve({
'crash.reporter.preference': 'on'
});
});
done();
},

Expand Down
33 changes: 21 additions & 12 deletions test/unit/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ exports['provision.setDefaultKey'] = {
this.logsBasic = this.sandbox.stub(logs, 'basic', function() {});
this.setDefaultKeySpy = this.sandbox.spy(provision, 'setDefaultKey');
this.fsSpyStatSync = this.sandbox.spy(fs, 'statSync');

this.parseKey = this.sandbox.stub(sshpk, 'parseKey', _ => _);
this.RSA = this.sandbox.stub(global, 'RSA', () => {
return {
exportKey: _ => _
};
});

done();
},

Expand Down Expand Up @@ -75,16 +83,17 @@ exports['provision.setDefaultKey'] = {
validAlternateKey: function(test) {
test.expect(4);
// Create folders for the folder that we'd like to
createKeyTestFolder((err) => {
if (err) {
console.log('createKeyTestFolder: ' + err);
process.exit();
createKeyTestFolder((error) => {
if (error) {
test.ok(false, error.message);
test.done();
}
// Attempt to set up local keys
// Generate SSH key
var key = new NodeRSA({
var key = new RSA({
b: 2048
});

var privateKey = key.exportKey('private');
var publicKey = sshpk.parseKey(key.exportKey('public'), 'pem').toString('ssh') + '\n';

Expand All @@ -93,22 +102,22 @@ exports['provision.setDefaultKey'] = {
// owner can read and write
var fileOpts = {
encoding: 'utf8',
mode: 384,
mode: 0o600,
};

async.parallel([
(next) => fs.writeFile(testPath + '.pub', publicKey, fileOpts, next), (next) => fs.writeFile(testPath, privateKey, fileOpts, next),
], (err) => {
if (err) {
console.log('parallel writer: ' + err);
process.exit();
], (error) => {
if (error) {
test.ok(false, error.message);
test.done();
}
provision.setDefaultKey(testPath)
.then(() => {
fs.remove(testDir, (err) => {
if (err) {
console.log('Error removing tmp directory: ' + err);
process.exit();
test.ok(false, 'Error removing tmp directory: ' + err);
test.done();
}
test.equal(this.setDefaultKeySpy.callCount, 1);
test.equal(fs.statSync.callCount, 2);
Expand Down

0 comments on commit 557bcc3

Please sign in to comment.