Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve / #244 #261

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
env: {
es6: true,
node: true,
mocha: true,
},
extends: "eslint:recommended",
parserOptions: {
Expand Down
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,29 @@ class ServerlessWSGI {
}

findHandler() {
return _.findKey(this.serverless.service.functions, (fun) =>
_.includes(fun.handler, "wsgi_handler.handler")
);
const functionName = this.options.function || this.options.f;

if (functionName) {
// If the function name is specified, return it directly
if (this.serverless.service.functions[functionName]) {
return functionName;
} else {
throw new Error(`Function "${functionName}" not found.`);
}
} else {
return _.findKey(this.serverless.service.functions, (fun) =>
_.includes(fun.handler, "wsgi_handler.handler")
);
}
}

invokeHandler(command, data, local) {
const handlerFunction = this.findHandler();
let handlerFunction;
try {
handlerFunction = this.findHandler();
} catch (error) {
return BbPromise.reject(error.message);
}

if (!handlerFunction) {
return BbPromise.reject(
Expand Down
109 changes: 87 additions & 22 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

/* global describe it */

Check failure on line 3 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.9

'describe' is already defined as a built-in global variable

Check failure on line 3 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.9

'it' is already defined as a built-in global variable

Check failure on line 3 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.8

'describe' is already defined as a built-in global variable

Check failure on line 3 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.8

'it' is already defined as a built-in global variable

Check failure on line 3 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.7

'describe' is already defined as a built-in global variable

Check failure on line 3 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.7

'it' is already defined as a built-in global variable
const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");
Expand Down Expand Up @@ -1545,8 +1545,8 @@
});

describe("exec", () => {
const mockCli = Object({log: () => {}});
const mockCli = Object({ log: () => {} });

it("fails when invoked without command or file", () => {
var plugin = new Plugin(
{
Expand Down Expand Up @@ -1681,7 +1681,7 @@
});

describe("exec local", () => {
const mockCli = {log: () => {}};
const mockCli = { log: () => {} };

it("fails when invoked without command or file", () => {
var plugin = new Plugin(
Expand Down Expand Up @@ -1788,7 +1788,7 @@
});

describe("command", () => {
const mockCli = {log: () => {}};
const mockCli = { log: () => {} };

it("fails when no wsgi handler is set", () => {
var plugin = new Plugin(
Expand Down Expand Up @@ -1915,7 +1915,7 @@
});

describe("command local", () => {
const mockCli = {log: () => {}};
const mockCli = { log: () => {} };

it("fails when no wsgi handler is set", () => {
var plugin = new Plugin(
Expand Down Expand Up @@ -2046,34 +2046,48 @@
});

describe("manage", () => {
const mockCli = Object({log: () => {}});
const mockCli = {
log: sinon.spy(),
};

it("calls handler to execute manage commands remotely from argument", () => {
var plugin = new Plugin(
let plugin;
let sandbox;

beforeEach(() => {
sandbox = sinon.createSandbox();
plugin = new Plugin(
{
config: { servicePath: "/tmp" },
service: {
provider: { runtime: "python2.7" },
custom: { wsgi: { app: "api.app" } },
functions: { app: { handler: "wsgi_handler.handler" } },
functions: {
app: { handler: "wsgi_handler.handler" },
otherFunc: { handler: "other_handler.handler" },
},
},
classes: { Error: Error },
cli: mockCli,
pluginManager: {
cliOptions: {},
run: (command) =>
new BbPromise((resolve) => {
expect(command).to.deep.equal(["invoke"]);
console.log('[0, "manage command output"]'); // eslint-disable-line no-console
resolve();
}),
run: sandbox.stub().resolves(),
},
},
{ command: "check" }
);
});

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

it("calls handler to execute manage commands remotely from argument", () => {
plugin.serverless.pluginManager.run.callsFake((command) => {
expect(command).to.deep.equal(["invoke"]);
console.log('[0, "manage command output"]');
return BbPromise.resolve();
});

var sandbox = sinon.createSandbox();
let loggerSpy = sandbox.spy(mockCli, "log");
return plugin.hooks["wsgi:manage:manage"]().then(() => {
expect(plugin.serverless.pluginManager.cliOptions.f).to.equal("app");
expect(plugin.options.function).to.equal("app");
Expand All @@ -2083,14 +2097,65 @@
expect(plugin.options.data).to.equal(
'{"_serverless-wsgi":{"command":"manage","data":"check"}}'
);
expect(loggerSpy.calledWith("manage command output")).to.be.true;
sandbox.restore();
expect(mockCli.log.calledWith("manage command output")).to.be.true;
});
});

it("uses the function specified by --function", () => {
plugin.options.function = "otherFunc";

return plugin.hooks["wsgi:manage:manage"]().then(() => {
expect(plugin.serverless.pluginManager.cliOptions.f).to.equal(
"otherFunc"
);
expect(plugin.options.function).to.equal("otherFunc");
});
});

it("uses the function specified by -f", () => {
plugin.options.f = "otherFunc";

return plugin.hooks["wsgi:manage:manage"]().then(() => {
expect(plugin.serverless.pluginManager.cliOptions.f).to.equal(
"otherFunc"
);
expect(plugin.options.function).to.equal("otherFunc");
});
});

it("throws an error when specified function is not found", () => {
plugin.options.function = "nonExistentFunc";

return expect(plugin.hooks["wsgi:manage:manage"]()).to.be.rejectedWith(
'Function "nonExistentFunc" not found.'
);
});

it("falls back to finding wsgi handler when no function is specified", () => {
delete plugin.options.function;
delete plugin.options.f;

return plugin.hooks["wsgi:manage:manage"]().then(() => {
expect(plugin.serverless.pluginManager.cliOptions.f).to.equal("app");
expect(plugin.options.function).to.equal("app");
});
});

it("rejects when no wsgi handler is found and no function is specified", () => {
delete plugin.options.function;
delete plugin.options.f;
plugin.serverless.service.functions = {
someFunc: { handler: "some_handler.handler" },
};

return expect(plugin.hooks["wsgi:manage:manage"]()).to.be.rejectedWith(
"No functions were found with handler: wsgi_handler.handler"
);
});
});

describe("manage local", () => {
const mockCli = Object({log: () => {}});
const mockCli = Object({ log: () => {} });

it("calls handler to execute manage commands locally from argument", () => {
var plugin = new Plugin(
Expand Down Expand Up @@ -2137,7 +2202,7 @@
});

describe("flask", () => {
const mockCli = Object({log: () => {}});
const mockCli = Object({ log: () => {} });
it("calls handler to execute flask commands remotely from argument", () => {
var plugin = new Plugin(
{
Expand Down Expand Up @@ -2180,7 +2245,7 @@
});

describe("flask local", () => {
const mockCli = Object({log: () => {}});
const mockCli = Object({ log: () => {} });
it("calls handler to execute flask commands locally from argument", () => {
var plugin = new Plugin(
{
Expand Down
Loading