This is an automated email from the ASF dual-hosted git repository.

shuaijinchao pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/apisix-python-plugin-runner.git


The following commit(s) were added to refs/heads/master by this push:
     new 631de5a  fix: config loading local path error (#26)
631de5a is described below

commit 631de5a374c63dad46b5d3e137588d130361886d
Author: 帅进超 <[email protected]>
AuthorDate: Mon Aug 23 13:05:38 2021 +0800

    fix: config loading local path error (#26)
    
    * fix: config loading local path error
    
    * test: add config test case
---
 apisix/main.py                     |  3 ++-
 apisix/runner/server/config.py     | 14 ++++++++++----
 tests/runner/server/test_config.py | 22 +++++++++++++++++++++-
 3 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/apisix/main.py b/apisix/main.py
index ce8e7db..00ae269 100644
--- a/apisix/main.py
+++ b/apisix/main.py
@@ -15,6 +15,7 @@
 # limitations under the License.
 #
 
+import os
 import click
 
 from apisix.runner.server.server import Server as NewServer
@@ -31,7 +32,7 @@ def runner() -> None:
 
 @runner.command()
 def start() -> None:
-    config = NewConfig()
+    config = NewConfig(os.path.dirname(os.path.abspath(__file__)))
     server = NewServer(config)
     server.receive()
 
diff --git a/apisix/runner/server/config.py b/apisix/runner/server/config.py
index f3c09cf..9e9f4a9 100644
--- a/apisix/runner/server/config.py
+++ b/apisix/runner/server/config.py
@@ -79,15 +79,17 @@ class _ConfigLogging:
 
 class Config:
 
-    def __init__(self, config_name: str = "config.yaml"):
+    def __init__(self, config_path: str = "", config_name: str = 
"config.yaml"):
         """
         init config
+        :param config_path:
+            local config file path
         :param config_name:
             local config file name
         """
         self.socket = _ConfigSocket()
         self.logging = _ConfigLogging()
-        self._loading_config(config_name)
+        self._loading_config(config_path, config_name)
 
     @staticmethod
     def _get_env_config(config: str):
@@ -101,13 +103,17 @@ class Config:
             return os.getenv(env_name)
         return config
 
-    def _loading_config(self, config_name: str):
+    def _loading_config(self, config_path: str, config_name: str):
         """
         load local configuration file
+        :param config_path:
         :param config_name:
         :return:
         """
-        abs_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
+        if len(config_path) and os.path.exists(config_path):
+            abs_path = config_path
+        else:
+            abs_path = 
os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
         cf_path = "%s/%s" % (abs_path, config_name)
         if not os.path.exists(cf_path):
             print("ERR: config file `%s` not exists" % cf_path)
diff --git a/tests/runner/server/test_config.py 
b/tests/runner/server/test_config.py
index 56df878..a5df767 100644
--- a/tests/runner/server/test_config.py
+++ b/tests/runner/server/test_config.py
@@ -15,11 +15,12 @@
 # limitations under the License.
 #
 
+import os
 import logging
 from apisix.runner.server.config import Config as NewServerConfig
 
 
-def test_config():
+def test_config_default():
     config = NewServerConfig()
 
     config.logging.level = "INFO"
@@ -36,3 +37,22 @@ def test_config():
 
     config.socket.file = "/test/runner.sock"
     assert config.socket.file == "/test/runner.sock"
+
+
+def test_config_custom():
+    config = NewServerConfig("%s/apisix" % 
os.path.abspath(os.path.join(os.getcwd())), "config.yaml")
+
+    config.logging.level = "NOTSET"
+    assert config.logging.level == logging.NOTSET
+
+    config.logging.level = "INFO"
+    assert config.logging.level == logging.INFO
+
+    config.logging.level = "ERROR"
+    assert config.logging.level == logging.ERROR
+
+    config.logging.level = "WARN"
+    assert config.logging.level == logging.WARNING
+
+    config.socket.file = "/test/runner.sock"
+    assert config.socket.file == "/test/runner.sock"

Reply via email to