Merge ~cjwatson/lp-archive:service-config-env into lp-archive:main

Proposed by Colin Watson
Status: Merged
Merged at revision: 37092f8e792c34c335c23d5fc05793f70e9ef105
Proposed branch: ~cjwatson/lp-archive:service-config-env
Merge into: lp-archive:main
Diff against target: 93 lines (+34/-14)
2 files modified
lp_archive/__init__.py (+14/-3)
tests/test_factory.py (+20/-11)
Reviewer Review Type Date Requested Status
Guruprasad Approve
Review via email: mp+436338@code.launchpad.net

Commit message

Accept SERVICE_CONFIG environment variable

Description of the change

This is used in some of our other services, and is easier to handle in deployments than just reading `config.toml` from the current directory.

To post a comment you must log in.
Revision history for this message
Guruprasad (lgp171188) wrote :

LGTM 👍

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lp_archive/__init__.py b/lp_archive/__init__.py
2index dc1b052..70f6062 100644
3--- a/lp_archive/__init__.py
4+++ b/lp_archive/__init__.py
5@@ -3,11 +3,14 @@
6
7 """The Launchpad archive service."""
8
9+import os
10+from pathlib import Path
11+from typing import Any
12+
13 try:
14 import tomllib
15 except ModuleNotFoundError:
16 import tomli as tomllib # type: ignore
17-from typing import Any
18
19 import talisker.flask
20 from flask import Flask, jsonify
21@@ -15,12 +18,20 @@ from flask import Flask, jsonify
22 from lp_archive import archive, root, routing
23 from lp_archive.cache import cache
24
25+ROOT = Path(__file__).parent.parent.resolve()
26+
27
28 def create_app(test_config: dict[str, Any] | None = None) -> Flask:
29 app = Flask(__name__, static_folder=None, host_matching=True)
30 if test_config is None:
31- with open("config.toml", "rb") as f:
32- app.config.from_mapping(tomllib.load(f))
33+ try:
34+ config_text = Path(
35+ os.environ.get("SERVICE_CONFIG", ROOT / "config.toml")
36+ ).read_text()
37+ except FileNotFoundError:
38+ pass
39+ else:
40+ app.config.from_mapping(tomllib.loads(config_text))
41 else:
42 app.config.from_mapping(test_config)
43 app.url_map.converters["primary"] = routing.PrimaryArchiveConverter
44diff --git a/tests/test_factory.py b/tests/test_factory.py
45index aaa9d81..70818d1 100644
46--- a/tests/test_factory.py
47+++ b/tests/test_factory.py
48@@ -1,7 +1,6 @@
49 # Copyright 2022 Canonical Ltd. This software is licensed under the
50 # GNU Affero General Public License version 3 (see the file LICENSE).
51
52-import os
53 import warnings
54 from pathlib import Path
55 from tempfile import TemporaryDirectory
56@@ -9,17 +8,27 @@ from tempfile import TemporaryDirectory
57 from lp_archive import create_app
58
59
60-def test_config_from_file():
61+def test_config_from_file(monkeypatch):
62 with TemporaryDirectory() as empty:
63- old_cwd = os.getcwd()
64- try:
65- os.chdir(empty)
66- Path("config.toml").touch()
67- with warnings.catch_warnings():
68- warnings.filterwarnings("ignore", module="flask_caching")
69- assert not create_app().testing
70- finally:
71- os.chdir(old_cwd)
72+ config_path = Path(empty) / "config.toml"
73+ config_path.write_text('SENTINEL = "1"\n')
74+ monkeypatch.setenv("SERVICE_CONFIG", str(config_path))
75+ with warnings.catch_warnings():
76+ warnings.filterwarnings("ignore", module="flask_caching")
77+ app = create_app()
78+ assert app.config["SENTINEL"] == "1"
79+ assert not app.testing
80+
81+
82+def test_missing_config_file(monkeypatch):
83+ with TemporaryDirectory() as empty:
84+ config_path = Path(empty) / "config.toml"
85+ monkeypatch.setenv("SERVICE_CONFIG", str(config_path))
86+ with warnings.catch_warnings():
87+ warnings.filterwarnings("ignore", module="flask_caching")
88+ app = create_app()
89+ assert "SENTINEL" not in app.config
90+ assert not app.testing
91
92
93 def test_config_for_testing():

Subscribers

People subscribed via source and target branches