Repository: Kinto/kinto

Issue:
Make the location of version.json configurable
Currently @phrawzty had to put the `version.json` at the root location of the container.

If the location would be a setting we could specify via env variable in the dockerfile for example

Make the location of version.json configurable
Currently @phrawzty had to put the `version.json` at the root location of the container.

If the location would be a setting we could specify via env variable in the dockerfile for example



Discussion:

Resolving patch:
diff --git a/kinto/core/__init__.py b/kinto/core/__init__.py
--- a/kinto/core/__init__.py
+++ b/kinto/core/__init__.py
@@ -79,6 +79,7 @@
     'tm.annotate_user': False,  # Do annotate transactions with the user-id.
     'transaction_per_request': True,
     'userid_hmac_secret': '',
+    'version_json_path': 'version.json',
     'version_prefix_redirect_enabled': True,
     'trailing_slash_redirect_enabled': True,
     'multiauth.groupfinder': 'kinto.core.authorization.groupfinder',
diff --git a/kinto/core/views/version.py b/kinto/core/views/version.py
--- a/kinto/core/views/version.py
+++ b/kinto/core/views/version.py
@@ -8,27 +8,27 @@
 HERE = os.path.dirname(os.path.abspath(__file__))
 ORIGIN = os.path.dirname(os.path.dirname(HERE))
 
-VERSION_PATH = 'version.json'
-VERSION_JSON = None
-
 version = Service(name="version", path='/__version__', description="Version")
 
-VERSIONS_FILES = [
-    VERSION_PATH,  # Relative to the CURRENT_WORKING_DIR.
-    os.path.join(ORIGIN, VERSION_PATH),  # Relative to the package.
-    os.path.join(HERE, VERSION_PATH)]  # Relative to this file.
-
-for version_file in VERSIONS_FILES:
-    file_path = os.path.abspath(version_file)
-    if os.path.exists(file_path):
-        with open(file_path) as f:
-            VERSION_JSON = json.load(f)
-            break  # The first one wins
-
 
 @version.get(permission=NO_PERMISSION_REQUIRED)
 def version_view(request):
-    if VERSION_JSON is not None:
-        return VERSION_JSON
+    try:
+        return version_view.__json__
+    except AttributeError:
+        pass
+
+    location = request.registry.settings['version_json_path']
+    files = [
+        location,  # Default is current working dir.
+        os.path.join(ORIGIN, 'version.json'),  # Relative to the package root.
+        os.path.join(HERE, 'version.json')  # Relative to this file.
+    ]
+    for version_file in files:
+        file_path = os.path.abspath(version_file)
+        if os.path.exists(file_path):
+            with open(file_path) as f:
+                version_view.__json__ = json.load(f)
+                return version_view.__json__  # First one wins.
 
     raise httpexceptions.HTTPNotFound()


Test patch:
diff --git a/tests/core/test_views_version.py b/tests/core/test_views_version.py
--- a/tests/core/test_views_version.py
+++ b/tests/core/test_views_version.py
@@ -1,30 +1,42 @@
-from kinto.core.views import version
-from kinto.core.testing import unittest
+import mock
+import os
+import json
+import tempfile
+import unittest
 
+from kinto.core.views.version import version_view
 from .support import BaseWebTest
 
 
 class VersionViewTest(BaseWebTest, unittest.TestCase):
-
     def setUp(self):
-        self.VERSION_JSON_before = version.VERSION_JSON
-        self.ORIGIN_before = version.ORIGIN
-
-    def tearDown(self):
-        version.VERSION_JSON = self.VERSION_JSON_before
-        version.ORIGIN = self.ORIGIN_before
+        try:
+            delattr(version_view, '__json__')  # Clean-up memoization.
+        except AttributeError:
+            pass
 
-    def test_return_the_version_file_if_present(self):
-        version.VERSION_JSON = {
-            "commit": "ce480821debc080d9cf5cb67e9a6ba71b2c3f57e",
-            "version": "0.8.1",
-            "name": "kinto-dist",
-            "source": "https://github.com/mozilla-services/kinto-dist"
-        }
-        response = self.app.get('/__version__')
+    tearDown = setUp
 
-        assert response.json == version.VERSION_JSON
+    def test_return_the_version_file_in_current_folder_if_present(self):
+        content = {'version': '0.8.1'}
+        fake_file = mock.mock_open(read_data=json.dumps(content))
+        with mock.patch('os.path.exists'):
+            with mock.patch('kinto.core.views.version.open', fake_file, create=True):
+                response = self.app.get('/__version__')
+                assert response.json == content
 
     def test_return_a_404_if_version_file_if_not_present(self):
-        version.VERSION_JSON = None
         self.app.get('/__version__', status=404)
+
+    def test_return_the_version_file_specified_in_setting_if_present(self):
+        custom_path = tempfile.mktemp()
+        content = {'foo': 'lala'}
+        with open(custom_path, 'w') as f:
+            json.dump(content, f)
+        self.addCleanup(lambda: os.remove(custom_path))
+
+        with mock.patch.dict(self.app.app.registry.settings,
+                             [('version_json_path', custom_path)]):
+            response = self.app.get('/__version__')
+
+        assert response.json == content