-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add python-disable-gunicorn flag #10708
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
Draft
IzaakGough
wants to merge
7
commits into
main
Choose a base branch
from
@invertase/feat-add-python-disable-gunicorn-flag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a368747
feat: add python-disable-gunicorn flag
IzaakGough ec96c6e
Merge branch 'main' into @invertase/feat-add-python-disable-gunicorn-…
IzaakGough fac8b0b
Merge branch 'main' into @invertase/feat-add-python-disable-gunicorn-…
IzaakGough 495990d
fix: preserve existing sitecustomize
IzaakGough 26fc457
fix: cleanup temp directory
IzaakGough 121d4c6
chore: fix lint
IzaakGough 85f8e93
fix: preserve sitecustomize import with python gunicorn shim
IzaakGough File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import { expect } from "chai"; | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
| import * as portfinder from "portfinder"; | ||
| import * as sinon from "sinon"; | ||
|
|
||
| import { FunctionsEmulator } from "./functionsEmulator"; | ||
| import * as functionsPython from "../functions/python"; | ||
|
|
||
| describe("FunctionsEmulator", () => { | ||
| const sandbox = sinon.createSandbox(); | ||
|
|
||
| afterEach(async () => { | ||
| sandbox.restore(); | ||
| await makeEmulator(true).stop(); | ||
| }); | ||
|
|
||
| function makeEmulator(pythonDisableGunicorn = false): FunctionsEmulator { | ||
| return new FunctionsEmulator({ | ||
| projectId: "project-id", | ||
| projectDir: "/workspace", | ||
| emulatableBackends: [], | ||
| debugPort: false, | ||
| pythonDisableGunicorn, | ||
| }); | ||
| } | ||
|
|
||
| function makePythonBackend() { | ||
| return { | ||
| functionsDir: "/workspace/functions", | ||
| codebase: "default", | ||
| env: {}, | ||
| secretEnv: [], | ||
| runtime: "python311", | ||
| }; | ||
| } | ||
|
|
||
| it("should inject a sitecustomize shim when pythonDisableGunicorn is enabled", async () => { | ||
| sandbox.stub(portfinder, "getPortPromise").resolves(9191); | ||
| const runWithVirtualEnv = sandbox.stub(functionsPython, "runWithVirtualEnv").returns({} as any); | ||
| const mkdtempSync = sandbox.stub(fs, "mkdtempSync").returns("/tmp/firebase-tools-python-shim"); | ||
| const writeFileSync = sandbox.stub(fs, "writeFileSync"); | ||
|
|
||
| const emulator = makeEmulator(true); | ||
| const logLabeled = sandbox.stub((emulator as any).logger, "logLabeled"); | ||
|
|
||
| await (emulator as any).startPython(makePythonBackend(), { PYTHONPATH: "/existing/path" }); | ||
|
|
||
| expect(runWithVirtualEnv).to.have.been.calledOnce; | ||
| expect(runWithVirtualEnv.firstCall.args[0]).to.deep.equal(["functions-framework"]); | ||
| expect(runWithVirtualEnv.firstCall.args[1]).to.equal("/workspace/functions"); | ||
| expect(runWithVirtualEnv.firstCall.args[2]).to.include({ | ||
| FIREBASE_FUNCTIONS_EMULATOR_DISABLE_GUNICORN: "1", | ||
| PYTHONUNBUFFERED: "1", | ||
| DEBUG: "False", | ||
| HOST: "127.0.0.1", | ||
| PORT: "9191", | ||
| }); | ||
| expect(runWithVirtualEnv.firstCall.args[2].PYTHONPATH).to.equal( | ||
| `/tmp/firebase-tools-python-shim${path.delimiter}/existing/path`, | ||
| ); | ||
| expect(mkdtempSync).to.have.been.calledOnce; | ||
| expect(writeFileSync).to.have.been.calledOnceWith( | ||
| "/tmp/firebase-tools-python-shim/sitecustomize.py", | ||
| sinon.match.string, | ||
| "utf8", | ||
| ); | ||
| const shim = writeFileSync.firstCall.args[1]; | ||
| expect(shim).to.include("PathFinder.find_spec("); | ||
| expect(shim).to.include('"sitecustomize", _firebase_remaining_paths'); | ||
| expect(shim).to.include("module_from_spec"); | ||
| expect(shim).to.include('sys.modules["sitecustomize"] = _firebase_user_sitecustomize_module'); | ||
| expect(shim).to.include("exec_module"); | ||
| expect(logLabeled).to.have.been.calledOnceWith( | ||
| "BULLET", | ||
| "functions", | ||
| "Python emulator gunicorn disabled; using Flask server in non-debug mode.", | ||
| ); | ||
| }); | ||
|
|
||
| it("should leave python runtime envs unchanged when pythonDisableGunicorn is disabled", async () => { | ||
| sandbox.stub(portfinder, "getPortPromise").resolves(9292); | ||
| const runWithVirtualEnv = sandbox.stub(functionsPython, "runWithVirtualEnv").returns({} as any); | ||
| const mkdtempSync = sandbox.stub(fs, "mkdtempSync"); | ||
| const writeFileSync = sandbox.stub(fs, "writeFileSync"); | ||
|
|
||
| const emulator = makeEmulator(false); | ||
| const logLabeled = sandbox.stub((emulator as any).logger, "logLabeled"); | ||
|
|
||
| await (emulator as any).startPython(makePythonBackend(), {}); | ||
|
|
||
| expect(runWithVirtualEnv).to.have.been.calledOnce; | ||
| expect(runWithVirtualEnv.firstCall.args[2]).to.include({ | ||
| PYTHONUNBUFFERED: "1", | ||
| DEBUG: "False", | ||
| HOST: "127.0.0.1", | ||
| PORT: "9292", | ||
| }); | ||
| expect(runWithVirtualEnv.firstCall.args[2]).to.not.have.property( | ||
| "FIREBASE_FUNCTIONS_EMULATOR_DISABLE_GUNICORN", | ||
| ); | ||
| expect(runWithVirtualEnv.firstCall.args[2].PYTHONPATH).to.equal(process.env.PYTHONPATH); | ||
| expect(mkdtempSync).to.not.have.been.called; | ||
| expect(writeFileSync).to.not.have.been.called; | ||
| expect(logLabeled).to.not.have.been.called; | ||
| }); | ||
|
|
||
| it("should recursively remove the shim dir during stop and clear the cached path", async () => { | ||
| sandbox.stub(portfinder, "getPortPromise").resolves(9393); | ||
| const runWithVirtualEnv = sandbox.stub(functionsPython, "runWithVirtualEnv").returns({} as any); | ||
| const mkdtempSync = sandbox.stub(fs, "mkdtempSync"); | ||
| mkdtempSync.onFirstCall().returns("/tmp/firebase-tools-python-shim-one"); | ||
| mkdtempSync.onSecondCall().returns("/tmp/firebase-tools-python-shim-two"); | ||
| sandbox.stub(fs, "writeFileSync"); | ||
| const rmSync = sandbox.stub(fs, "rmSync"); | ||
|
|
||
| const emulator = makeEmulator(true); | ||
|
|
||
| await (emulator as any).startPython(makePythonBackend(), {}); | ||
| await emulator.stop(); | ||
| await (emulator as any).startPython(makePythonBackend(), {}); | ||
|
|
||
| expect(rmSync).to.have.been.calledOnceWith("/tmp/firebase-tools-python-shim-one", { | ||
| recursive: true, | ||
| }); | ||
| expect(mkdtempSync).to.have.been.calledTwice; | ||
| expect(runWithVirtualEnv.secondCall.args[2].PYTHONPATH).to.equal( | ||
| "/tmp/firebase-tools-python-shim-two", | ||
| ); | ||
| }); | ||
|
|
||
| it("should ignore ENOENT during shim dir cleanup and clear the cached path", async () => { | ||
| sandbox.stub(portfinder, "getPortPromise").resolves(9494); | ||
| const runWithVirtualEnv = sandbox.stub(functionsPython, "runWithVirtualEnv").returns({} as any); | ||
| const mkdtempSync = sandbox.stub(fs, "mkdtempSync"); | ||
| mkdtempSync.onFirstCall().returns("/tmp/firebase-tools-python-shim-missing"); | ||
| mkdtempSync.onSecondCall().returns("/tmp/firebase-tools-python-shim-recreated"); | ||
| sandbox.stub(fs, "writeFileSync"); | ||
| const rmSync = sandbox | ||
| .stub(fs, "rmSync") | ||
| .throws(Object.assign(new Error("missing"), { code: "ENOENT" })); | ||
|
|
||
| const emulator = makeEmulator(true); | ||
| const log = sandbox.stub((emulator as any).logger, "log"); | ||
|
|
||
| await (emulator as any).startPython(makePythonBackend(), {}); | ||
| await emulator.stop(); | ||
| await (emulator as any).startPython(makePythonBackend(), {}); | ||
|
|
||
| expect(rmSync).to.have.been.calledOnceWith("/tmp/firebase-tools-python-shim-missing", { | ||
| recursive: true, | ||
| }); | ||
| expect(mkdtempSync).to.have.been.calledTwice; | ||
| expect(runWithVirtualEnv.secondCall.args[2].PYTHONPATH).to.equal( | ||
| "/tmp/firebase-tools-python-shim-recreated", | ||
| ); | ||
| expect(log).to.not.have.been.called; | ||
| }); | ||
|
|
||
| it("should recreate the shim dir after cleanup fails with a non-ENOENT error", async () => { | ||
| sandbox.stub(portfinder, "getPortPromise").resolves(9595); | ||
| const runWithVirtualEnv = sandbox.stub(functionsPython, "runWithVirtualEnv").returns({} as any); | ||
| const mkdtempSync = sandbox.stub(fs, "mkdtempSync"); | ||
| mkdtempSync.onFirstCall().returns("/tmp/firebase-tools-python-shim-error"); | ||
| mkdtempSync.onSecondCall().returns("/tmp/firebase-tools-python-shim-recreated"); | ||
| sandbox.stub(fs, "writeFileSync"); | ||
| const rmSync = sandbox.stub(fs, "rmSync").throws(new Error("permission denied")); | ||
|
|
||
| const emulator = makeEmulator(true); | ||
| const log = sandbox.stub((emulator as any).logger, "log"); | ||
|
|
||
| await (emulator as any).startPython(makePythonBackend(), {}); | ||
| await emulator.stop(); | ||
| await (emulator as any).startPython(makePythonBackend(), {}); | ||
|
|
||
| expect(rmSync).to.have.been.calledOnceWith("/tmp/firebase-tools-python-shim-error", { | ||
| recursive: true, | ||
| }); | ||
| expect(mkdtempSync).to.have.been.calledTwice; | ||
| expect(runWithVirtualEnv.secondCall.args[2].PYTHONPATH).to.equal( | ||
| "/tmp/firebase-tools-python-shim-recreated", | ||
| ); | ||
| expect(log).to.have.been.calledWith( | ||
| "DEBUG", | ||
| sinon.match("Failed to clean up python-disable-gunicorn shim dir"), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_firebase_import_without_gunicornhook currently uses a fixed signature(name, globals=None, locals=None, fromlist=(), level=0). To ensure maximum compatibility across different Python versions, alternative interpreters, or third-party packages that might call__import__with different positional/keyword arguments, it is more robust to use*argsand**kwargsand extract thenameargument dynamically.