Skip to content

Commit

Permalink
fix(deploy): allow each deployer to specify test
Browse files Browse the repository at this point in the history
fixes #10
  • Loading branch information
tripodsan committed Dec 14, 2020
1 parent 03172a2 commit b004fe4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
3 changes: 3 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ const chalk = require('chalk');

const defaultConfig = require('./config/adobeioruntime-node10.js');

require('dotenv').config();

class CLI {
constructor() {
this._yargs = yargs()
.pkgConf('wsk')
.env('HLX')
.option('verbose', {
alias: 'v',
type: 'boolean',
Expand Down
7 changes: 7 additions & 0 deletions src/deploy/AWSDeployer.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ class AWSDeployer extends BaseDeployer {
}
}

async test() {
return this.testRequest({
url: this._functionURL,
idHeader: 'apigw-requestid',
});
}

async deploy() {
try {
await this.createS3Bucket();
Expand Down
36 changes: 36 additions & 0 deletions src/deploy/BaseDeployer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
* governing permissions and limitations under the License.
*/
const path = require('path');
const chalk = require('chalk');
const fetchAPI = require('@adobe/helix-fetch');

const { fetch } = process.env.HELIX_FETCH_FORCE_HTTP1
? fetchAPI.context({
httpProtocol: 'http1',
httpsProtocols: ['http1'],
})
: fetchAPI;

class BaseDeployer {
constructor(builder) {
Expand All @@ -27,6 +36,33 @@ class BaseDeployer {
get relZip() {
return path.relative(process.cwd(), this._builder.zipFile);
}

async testRequest({
url,
headers = {},
idHeader,
}) {
const testUrl = `${url}${this._builder.testPath || ''}`;
this.log.info(`--: requesting: ${chalk.blueBright(testUrl)} ...`);
const ret = await fetch(testUrl, {
headers,
});
const body = await ret.text();
const id = idHeader ? ret.headers.get(idHeader) : 'n/a';
if (ret.ok) {
this.log.info(`id: ${chalk.grey(id)}`);
this.log.info(`${chalk.green('ok:')} ${ret.status}`);
this.log.debug(chalk.grey(body));
} else {
this.log.info(`id: ${chalk.grey(id)}`);
if (ret.status === 302 || ret.status === 301) {
this.log.info(`${chalk.green('ok:')} ${ret.status}`);
this.log.debug(chalk.grey(`Location: ${ret.headers.get('location')}`));
} else {
throw new Error(`test failed: ${ret.status} ${body}`);
}
}
}
}

module.exports = BaseDeployer;
35 changes: 4 additions & 31 deletions src/deploy/OpenWhiskDeployer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
const ow = require('openwhisk');
const semver = require('semver');
const fetchAPI = require('@adobe/helix-fetch');
const os = require('os');
const fse = require('fs-extra');
const path = require('path');
Expand All @@ -20,13 +19,6 @@ const dotenv = require('dotenv');

const BaseDeployer = require('./BaseDeployer');

const { fetch } = process.env.HELIX_FETCH_FORCE_HTTP1
? fetchAPI.context({
httpProtocol: 'http1',
httpsProtocols: ['http1'],
})
: fetchAPI;

class OpenWhiskDeployer extends BaseDeployer {
constructor(builder) {
super(builder);
Expand Down Expand Up @@ -93,7 +85,7 @@ class OpenWhiskDeployer extends BaseDeployer {
async deploy() {
const openwhisk = this.getOpenwhiskClient();
const relZip = path.relative(process.cwd(), this._builder.zipFile);
this.log.info(`--: deploying ${relZip} as ${this._builder.actionName} `);
this.log.info(`--: deploying ${relZip} as ${this._builder.actionName} ...`);
const actionoptions = {
name: this._builder.actionName,
action: await fse.readFile(this._builder.zipFile),
Expand Down Expand Up @@ -182,36 +174,17 @@ class OpenWhiskDeployer extends BaseDeployer {
}

async test() {
return this.testRequest();
}

async testRequest() {
const url = `${this._wskApiHost}/api/v1/web${this.fullFunctionName}${this._builder.testPath || ''}`;
this.log.info(`--: requesting: ${chalk.blueBright(url)} ...`);
const headers = {};
if (this._builder.webSecure === true) {
headers.authorization = `Basic ${Buffer.from(this._wskAuth).toString('base64')}`;
} else if (this._builder.webSecure) {
headers['x-require-whisk-auth'] = this._webSecure;
}
const ret = await fetch(url, {
return this.testRequest({
url: `${this._wskApiHost}/api/v1/web${this.fullFunctionName}`,
headers,
idHeader: 'x-openwhisk-activation-id',
});
const body = await ret.text();
const id = ret.headers.get('x-openwhisk-activation-id');
if (ret.ok) {
this.log.info(`id: ${chalk.grey(id)}`);
this.log.info(`${chalk.green('ok:')} ${ret.status}`);
this.log.debug(chalk.grey(body));
} else {
this.log.info(`id: ${chalk.grey(id)}`);
if (ret.status === 302 || ret.status === 301) {
this.log.info(`${chalk.green('ok:')} ${ret.status}`);
this.log.debug(chalk.grey(`Location: ${ret.headers.get('location')}`));
} else {
throw new Error(`test failed: ${ret.status} ${body}`);
}
}
}

async showDeployHints() {
Expand Down

0 comments on commit b004fe4

Please sign in to comment.