Skip to content

Commit

Permalink
fix fun mount code dir bugs on windows and add travis test on windows…
Browse files Browse the repository at this point in the history
…, mac (#105)

* fix docker mount java code uri bug in windows

* add travis windows and mac test
  • Loading branch information
tanhe123 authored Dec 14, 2018
1 parent 54c245c commit 93c6b64
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 102 deletions.
19 changes: 18 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
language: node_js

os:
- linux
- osx
- windows

node_js:
- '8'
- '9'
- '10'

cache:
directories:
- node_modules
script: make test

script:
- |
if [ "$TRAVIS_OS_NAME" = 'windows' ]; then
mocha ./test/ -t 20000 -R spec --recursive -name *.test.js
else
make test
fi
deploy:
provider: npm
email: [email protected]
Expand Down
14 changes: 12 additions & 2 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var containers = new Set();

function waitingForContainerStopped() {
// see https://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js
let rl;
if (process.platform === 'win32') {
var rl = require('readline').createInterface({
rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
Expand Down Expand Up @@ -67,6 +68,13 @@ function waitingForContainerStopped() {
}
});

// Just fix test on windows
// Because process.emit('SIGINT') in test/docker.test.js will not trigger rl.on('SIGINT')
// And when listening to stdin the process never finishes until you send a SIGINT signal explicitly.
if (rl) {
rl.close();
}

if (!success) {
process.exit(-1);
}
Expand Down Expand Up @@ -100,7 +108,9 @@ async function resolveCodeUriToMount(codeUri) {
if (stats.isDirectory()) {
target = '/code';
} else {
target = path.join('/code', path.basename(codeUri));
// could not use path.join('/code', xxx)
// in windows, it will be translate to \code\xxx, and will not be recorgnized as a valid path in linux container
target = path.posix.join('/code', path.basename(codeUri));
}

// Mount the code directory as read only
Expand Down
7 changes: 5 additions & 2 deletions lib/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const os = require('os');
const yaml = require('js-yaml');
const debug = require('debug')('fun:profile');

const { red } = require('colors');

const dotenv = require('dotenv').config();

const readFile = util.promisify(fs.readFile);
Expand Down Expand Up @@ -164,8 +166,9 @@ async function getProfile() {
const profile = await getProfileFromDotEnv();

if (!isAllRequiredExist(profile)) {
console.warn('Fun is not properly configured.');
console.warn('Please run `fun config` first.');
console.error(red('Fun is not properly configured.'));
console.error(red('Please run `fun config` first.'));
throw new Error('Please run `fun config` first.');
}

return profile;
Expand Down
21 changes: 11 additions & 10 deletions test/commands/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ const expect = require('expect.js');
const mocki = require('../inquirer-mock-prompt');
const config = require('../../lib/commands/config');

const { setProcess } = require('../test-utils');

const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);


describe('config prompt', () => {

var prevHome;
let restoreProcess;

before(async () => {
prevHome = os.homedir();
process.env.HOME = os.tmpdir();


restoreProcess = setProcess({
HOME: os.tmpdir(),
});

await mkdirp(`${os.homedir()}/.fcli/`);
await writeFile(`${os.homedir()}/.fcli/config.yaml`, yaml.dump({
endpoint: `https://123344234.cn-hangzhou.fc.aliyuncs.com`,
Expand All @@ -36,14 +41,10 @@ describe('config prompt', () => {
sls_endpoint: `cn-hangzhou.log.aliyuncs.com`
}));
});

after(async () => {
rimraf.sync(`${os.homedir()}/.fcli/`);

process.env.HOME = prevHome;
delete process.env.ACCOUNT_ID;
delete process.env.ACCESS_KEY_ID;
delete process.env.ACCESS_KEY_SECRET;
delete process.env.DEFAULT_REGION;
restoreProcess();
});

it('using default value', async () => {
Expand Down
15 changes: 12 additions & 3 deletions test/commands/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ const exec = util.promisify(require('child_process').exec);

const expect = require('expect.js');

const { setProcess } = require('../test-utils');

const deploy = require('../../lib/commands/deploy');

describe.skip('deploy template', () => {
var prevCWD;

let restoreProcess;

beforeEach(() => {
prevCWD = process.cwd();
restoreProcess = setProcess({
ACCOUNT_ID: 'testAccountId',
ACCESS_KEY_ID: 'testKeyId',
ACCESS_KEY_SECRET: 'testKeySecret',
});

});
afterEach(() => {
process.chdir(prevCWD);
restoreProcess();
});

it('deploy datahub example', async () => {
Expand Down
47 changes: 28 additions & 19 deletions test/deploy/deploy-by-tpl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ const proxyquire = require('proxyquire');
const sinon = require('sinon');
const sandbox = sinon.createSandbox();
const assert = sinon.assert;
const path = require('path');

const deploySupport = require('../../lib/deploy/deploy-support');
const ram = require('../../lib/ram');

const { setProcess } = require('../test-utils');

describe('deploy', () => {
let restoreProcess;

beforeEach(() => {
Object.keys(deploySupport).forEach(m => {
sandbox.stub(deploySupport, m).resolves({});
Expand All @@ -25,21 +30,25 @@ describe('deploy', () => {
sandbox.stub(ram, m).resolves({});
}
});

restoreProcess = setProcess({
ACCOUNT_ID: 'testAccountId',
ACCESS_KEY_ID: 'testKeyId',
ACCESS_KEY_SECRET: 'testKeySecret',
});

});

afterEach(() => {
sandbox.restore();
restoreProcess();
});

async function deploy(example) {
await proxyquire('../../lib/deploy/deploy-by-tpl', {
'./deploy-support': deploySupport,
'../ram': ram
})(`./examples/${example}/template.yml`);

// await proxyquire('../../lib/deploy/deploy-support', {

// })(`./examples/${example}/template.yml`);
})(path.join('./examples', example, 'template.yml'));
}

it('deploy datahub', async () => {
Expand All @@ -55,7 +64,7 @@ describe('deploy', () => {
});

assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/datahub`, {
path.join(process.cwd(), 'examples', 'datahub'), {
codeUri: 'datahub.js',
description: undefined,
functionName: 'MyFunction',
Expand All @@ -82,7 +91,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/helloworld`, {
path.join(process.cwd(), 'examples', 'helloworld'), {
codeUri: './',
description: undefined,
functionName: 'helloworld',
Expand All @@ -109,7 +118,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/java`, {
path.join(process.cwd(), 'examples', 'java'), {
codeUri: './demo.jar',
description: 'Hello world!',
functionName: 'helloworld',
Expand Down Expand Up @@ -138,7 +147,7 @@ describe('deploy', () => {
});

assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/openid_connect`, {
path.join(process.cwd(), 'examples', 'openid_connect'), {
codeUri: './',
description: 'Hello world!',
functionName: 'helloworld',
Expand Down Expand Up @@ -197,7 +206,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/tablestore-trigger`,{
path.join(process.cwd(), 'examples', 'tablestore-trigger'), {
codeUri: './',
handler: 'main.index',
initializer: undefined,
Expand Down Expand Up @@ -234,7 +243,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/sls_demo`, {
path.join(process.cwd(), 'examples', 'sls_demo'), {
codeUri: './',
handler: 'index.handler',
initializer: undefined,
Expand Down Expand Up @@ -273,7 +282,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/rds-trigger`, {
path.join(process.cwd(), 'examples', 'rds-trigger'), {
codeUri: './',
handler: 'index.handler',
initializer: undefined,
Expand Down Expand Up @@ -313,7 +322,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/mnsTopic-trigger`, {
path.join(process.cwd(), 'examples', 'mnsTopic-trigger'), {
codeUri: './',
handler: 'index.handler',
initializer: undefined,
Expand Down Expand Up @@ -349,7 +358,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/python`, {
path.join(process.cwd(), 'examples', 'python'), {
codeUri: './',
description: 'Hello world with python!',
functionName: 'hello',
Expand Down Expand Up @@ -400,7 +409,7 @@ describe('deploy', () => {
});

assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/segment`, {
path.join(process.cwd(), 'examples', 'segment'), {
codeUri: './',
description: 'do segment',
functionName: 'doSegment',
Expand Down Expand Up @@ -448,7 +457,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/timer`, {
path.join(process.cwd(), 'examples', 'timer'), {
codeUri: './',
description: 'send hangzhou weather',
functionName: 'MyFunction',
Expand Down Expand Up @@ -485,7 +494,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction.firstCall,
`${process.cwd()}/examples/wechat`, {
path.join(process.cwd(), 'examples', 'wechat'), {
codeUri: './',
description: 'Wechat get handler',
functionName: 'get',
Expand Down Expand Up @@ -528,7 +537,7 @@ describe('deploy', () => {
});

assert.calledWith(deploySupport.makeFunction.secondCall,
`${process.cwd()}/examples/wechat`, {
path.join(process.cwd(), 'examples', 'wechat'), {
codeUri: './',
description: 'Wechat post handler',
functionName: 'post',
Expand Down Expand Up @@ -600,7 +609,7 @@ describe('deploy', () => {
vpcConfig: undefined
});
assert.calledWith(deploySupport.makeFunction,
`${process.cwd()}/examples/initializer`,{
path.join(process.cwd(), 'examples', 'initializer'), {
codeUri: './',
description: 'Hello world with initializer!',
environmentVariables: undefined,
Expand Down
20 changes: 12 additions & 8 deletions test/deploy/deploy-support.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ var nock = require('nock');

const deploySupport = require('../../lib/deploy/deploy-support');

const { setProcess } = require('../test-utils');

describe('make', () => {

let restoreProcess;

beforeEach(() => {
process.env.ACCOUNT_ID = '12384123985012938421';
process.env.DEFAULT_REGION = 'cn-shanghai';
process.env.ACCESS_KEY_ID = 'LTAIsgxsdfDokKbBS';
process.env.ACCESS_KEY_SECRET = 'Icngqpy03DtasdfasJWvLHDF2C2szm5ZgM';
restoreProcess = setProcess({
ACCOUNT_ID: '12384123985012938421',
DEFAULT_REGION: 'cn-shanghai',
ACCESS_KEY_ID: 'LTAIsgxsdfDokKbBS',
ACCESS_KEY_SECRET: 'Icngqpy03DtasdfasJWvLHDF2C2szm5ZgM',
});

if (!nock.isActive()) {
nock.activate();
}
});

afterEach(() => {
delete process.env.ACCOUNT_ID;
delete process.env.DEFAULT_REGION;
delete process.env.ACCESS_KEY_ID;
delete process.env.ACCESS_KEY_SECRET;
restoreProcess();
nock.cleanAll();
nock.restore();
});
Expand Down
Loading

0 comments on commit 93c6b64

Please sign in to comment.