Skip to content

Commit

Permalink
Merge pull request #19 from aliyun/add_check
Browse files Browse the repository at this point in the history
Add create and update function runtime check
  • Loading branch information
rockuw committed Feb 11, 2018
2 parents 9427cbb + cdc924a commit fc72a79
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
27 changes: 26 additions & 1 deletion es5/client.js
Expand Up @@ -127,7 +127,7 @@ var Client = function () {
}, {
key: 'request',
value: function () {
var _ref = _asyncToGenerator(_regenerator2.default.mark(function _callee(method, path, query, body) {
var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee(method, path, query, body) {
var headers = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
var url, postBody, buff, digest, md5, stringToSign, signature, response, responseBody, contentType, code, requestid, err;
return _regenerator2.default.wrap(function _callee$(_context) {
Expand Down Expand Up @@ -428,8 +428,32 @@ var Client = function () {
}, {
key: 'createFunction',
value: function createFunction(serviceName, options, headers) {
this.normalizeParams(options);
return this.post(`/services/${serviceName}/functions`, options, headers);
}
}, {
key: 'normalizeParams',
value: function normalizeParams(opts) {
if (opts.functionName) {
opts.functionName = String(opts.functionName);
}

if (opts.runtime) {
opts.runtime = String(opts.runtime);
}

if (opts.handler) {
opts.handler = String(opts.handler);
}

if (opts.memorySize) {
opts.memorySize = parseInt(opts.memorySize);
}

if (opts.timeout) {
opts.timeout = parseInt(opts.timeout);
}
}

/**
* 获取Function列表
Expand Down Expand Up @@ -494,6 +518,7 @@ var Client = function () {
}, {
key: 'updateFunction',
value: function updateFunction(serviceName, functionName, options, headers) {
this.normalizeParams(options);
var path = `/services/${serviceName}/functions/${functionName}`;
return this.put(path, options, headers);
}
Expand Down
25 changes: 25 additions & 0 deletions lib/client.js
Expand Up @@ -322,9 +322,33 @@ class Client {
* @return {Promise} 返回 Function 信息
*/
createFunction(serviceName, options, headers) {
this.normalizeParams(options)
return this.post(`/services/${serviceName}/functions`, options, headers);
}

normalizeParams(opts) {
if (opts.functionName){
opts.functionName = String(opts.functionName)
}

if (opts.runtime){
opts.runtime = String(opts.runtime)
}

if (opts.handler){
opts.handler = String(opts.handler)
}

if (opts.memorySize){
opts.memorySize = parseInt(opts.memorySize)
}

if (opts.timeout){
opts.timeout = parseInt(opts.timeout)

}
}

/**
* 获取Function列表
*
Expand Down Expand Up @@ -373,6 +397,7 @@ class Client {
* @return {Promise} 返回 Object(包含headers和data属性[Function信息])
*/
updateFunction(serviceName, functionName, options, headers) {
this.normalizeParams(options)
const path = `/services/${serviceName}/functions/${functionName}`;
return this.put(path, options, headers);
}
Expand Down
33 changes: 33 additions & 0 deletions test/client.test.js
Expand Up @@ -304,6 +304,39 @@ describe('client test', function () {
}).to.throwException(/"event" must be String or Buffer/);
});

it('createFunction with invalid runtime should fail', async function() {
try {
await client.createFunction(serviceName, {
functionName: "test_invalid_runtime_function",
description: 'function desc',
memorySize: 128,
handler: 'main.handler',
runtime: 10,
timeout: 10,
code: {
zipFile: fs.readFileSync(path.join(__dirname, 'figures/test.zip'), 'base64')
}
});
} catch (ex) {
expect(ex.stack).to.contain('FCInvalidArgumentError');
expect(ex.stack).to.contain('Runtime is set to an invalid value');
}

});

it('updateFunction with invalid runtime should fail', async function() {
try {
await client.updateFunction(serviceName, functionName, {
description: 'updated function desc',
runtime: 10
});
} catch (ex) {
expect(ex.stack).to.contain('FCInvalidArgumentError');
expect(ex.stack).to.contain('Runtime is set to an invalid value');
}

});

it('deleteFunction should ok', async function() {
await client.deleteFunction(serviceName, functionName);
// No exception, no failed
Expand Down

0 comments on commit fc72a79

Please sign in to comment.