Repository: scoutapp/scout_apm_python
Pull request #569 (2020-09-02 10:18:52)

Extract derivation of socket path into shared function
Refs #553.


Patch:
diff --git a/src/scout_apm/core/config.py b/src/scout_apm/core/config.py
index 06b72a7..24bcc2c 100644
--- a/src/scout_apm/core/config.py
+++ b/src/scout_apm/core/config.py
@@ -190,12 +190,6 @@ class Derived(object):
         func_name = "derive_" + key
         return getattr(self, func_name, None)
 
-    def derive_core_agent_socket_path(self):
-        return "{}/{}/scout-agent.sock".format(
-            self.config.value("core_agent_dir"),
-            self.config.value("core_agent_full_name"),
-        )
-
     def derive_core_agent_full_name(self):
         triple = self.config.value("core_agent_triple")
         if not platform_detection.is_valid_triple(triple):
diff --git a/src/scout_apm/core/core_agent_manager.py b/src/scout_apm/core/core_agent_manager.py
index e17ecb9..6702be0 100644
--- a/src/scout_apm/core/core_agent_manager.py
+++ b/src/scout_apm/core/core_agent_manager.py
@@ -86,11 +86,7 @@ class CoreAgentManager(object):
         return ["--daemonize", "true"]
 
     def socket_path(self):
-        # Old deprecated name "socket_path"
-        socket_path = scout_config.value("socket_path")
-        if socket_path is None:
-            socket_path = scout_config.value("core_agent_socket_path")
-        return ["--socket", socket_path]
+        return ["--socket", get_socket_path()]
 
     def log_level(self):
         # Old deprecated name "log_level"
@@ -288,3 +284,18 @@ def sha256_digest(filename, block_size=65536):
     except OSError as exc:
         logger.debug("Error on digest", exc_info=exc)
         return None
+
+
+def get_socket_path():
+    # Old deprecated name "socket_path"
+    socket_path = scout_config.value("socket_path")
+    # New name, but not set with any default so it can be derived
+    if socket_path is None:
+        socket_path = scout_config.value("core_agent_socket_path")
+    if socket_path is None:
+        # Derive it
+        socket_path = "{}/{}/scout-agent.sock".format(
+            scout_config.value("core_agent_dir"),
+            scout_config.value("core_agent_full_name"),
+        )
+    return socket_path
diff --git a/src/scout_apm/core/socket.py b/src/scout_apm/core/socket.py
index 0f6a754..0d41f6d 100644
--- a/src/scout_apm/core/socket.py
+++ b/src/scout_apm/core/socket.py
@@ -12,6 +12,7 @@ import time
 from scout_apm.compat import queue
 from scout_apm.core.commands import Register
 from scout_apm.core.config import scout_config
+from scout_apm.core.core_agent_manager import get_socket_path
 from scout_apm.core.threading import SingletonThread
 
 # Time unit - monkey-patched in tests to make them run faster
@@ -65,11 +66,7 @@ class CoreAgentSocketThread(SingletonThread):
         return queue_empty
 
     def run(self):
-        # Old deprecated name "socket_path"
-        socket_path = scout_config.value("socket_path")
-        if socket_path is None:
-            socket_path = scout_config.value("core_agent_socket_path")
-        self.socket_path = socket_path
+        self.socket_path = get_socket_path()
         self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
 
         try:


Test patch:
diff --git a/tests/unit/core/test_config.py b/tests/unit/core/test_config.py
index 334dbe9..67e2866 100644
--- a/tests/unit/core/test_config.py
+++ b/tests/unit/core/test_config.py
@@ -4,7 +4,6 @@ from __future__ import absolute_import, division, print_function, unicode_litera
 import logging
 import os
 import pprint
-import re
 import sys
 
 import pytest
@@ -37,22 +36,6 @@ def test_unset_config_value_from_python():
     assert ScoutConfig().value("revision_sha") == ""  # from defaults
 
 
-def test_get_derived_config_value():
-    ScoutConfig.set(core_agent_version="v1.1.8")
-    config = ScoutConfig()
-    try:
-        assert re.match(
-            r"/tmp/scout_apm_core/scout_apm_core"
-            r"-v1\.1\.8"
-            r"-(x86_64|i686|unknown)"
-            r"-(unknown-linux-gnu|apple-darwin|unknown)"
-            r"/scout-agent\.sock",
-            config.value("core_agent_socket_path"),
-        )
-    finally:
-        ScoutConfig.reset_all()
-
-
 def test_override_triple(caplog):
     triple = "unknown-unknown-linux-musl"
     ScoutConfig.set(core_agent_triple=triple)
diff --git a/tests/unit/core/test_core_agent_manager.py b/tests/unit/core/test_core_agent_manager.py
index 6aa3d8c..18ff0b0 100644
--- a/tests/unit/core/test_core_agent_manager.py
+++ b/tests/unit/core/test_core_agent_manager.py
@@ -5,7 +5,8 @@ import errno
 import logging
 import sys
 
-from scout_apm.core.core_agent_manager import parse_manifest
+from scout_apm.core.config import scout_config
+from scout_apm.core.core_agent_manager import get_socket_path, parse_manifest
 from tests.compat import mock
 
 
@@ -109,3 +110,26 @@ class TestParseManifest(object):
         assert logger == "scout_apm.core.core_agent_manager"
         assert level == logging.DEBUG
         assert message.startswith("Core Agent manifest json: ")
+
+
+class TestGetSocketPath(object):
+    def test_from_socket_path(self):
+        scout_config.set(socket_path="/tmp/my.sock")
+        try:
+            assert get_socket_path() == "/tmp/my.sock"
+        finally:
+            scout_config.reset_all()
+
+    def test_from_core_agent_socket_path(self):
+        scout_config.set(core_agent_socket_path="/tmp/that.sock")
+        try:
+            assert get_socket_path() == "/tmp/that.sock"
+        finally:
+            scout_config.reset_all()
+
+    def test_derived(self):
+        scout_config.set(core_agent_dir="/tmp/mydir", core_agent_full_name="my-agent")
+        try:
+            assert get_socket_path() == "/tmp/mydir/my-agent/scout-agent.sock"
+        finally:
+            scout_config.reset_all()