瀏覽代碼

[conf][tests] added mac_address retrieval

Maxime Alves LIRMM@home 4 年之前
父節點
當前提交
d6ccd04e9c
共有 3 個文件被更改,包括 56 次插入6 次删除
  1. 26
    5
      pyheatpump/config.py
  2. 1
    0
      tests/conftest.py
  3. 29
    1
      tests/test_config.py

+ 26
- 5
pyheatpump/config.py 查看文件

@@ -8,11 +8,14 @@ from pprint import pprint
8 8
 import uvicorn
9 9
 import json
10 10
 
11
+from pyheatpump.logger import logger_init
12
+logger = logger_init()
13
+
11 14
 default_config = {
12 15
     'heatpump': {
13 16
         'serial_port': '/dev/ttyUSB0',
14 17
         'baudrate': 19200,
15
-        'mac_address': '00:00:00:00:00:00',
18
+        'mac_address': 'None',
16 19
         'ip_address': 'dhcp',
17 20
         'read_only': 'False',
18 21
         'database': '/var/run/pyheatpump/pyheatpump.sqlite3'
@@ -47,6 +50,27 @@ config.read(filenames=CONFIG_FILES)
47 50
 API_HOST=config.get('api', 'host')
48 51
 API_PORT=config.get('api', 'port')
49 52
 
53
+def save_config():
54
+    with open(CONFIG_FILES[0], 'w') as conf_file:
55
+        config.write(conf_file)
56
+
57
+def mac_address_init():
58
+    from netifaces import gateways, ifaddresses, AF_INET, AF_LINK
59
+    interface = gateways()['default'][AF_INET][1]
60
+    if len(ifaddresses(interface)) == 0:
61
+        logger.error("Can't find interface")
62
+        sys.exit(1)
63
+
64
+    if len(ifaddresses(interface)[AF_LINK]) == 0:
65
+        logger.error("Can't find interface")
66
+        sys.exit(1)
67
+
68
+    addr = ifaddresses(interface)[AF_LINK][0]['addr']
69
+    config.set('heatpump', 'mac_address', addr)
70
+    save_config()
71
+
72
+    return addr
73
+
50 74
 async def get_config(request):
51 75
     items = dict([ (
52 76
         section, dict([ (key, config.get(section, key)) for key in items ])
@@ -54,7 +78,6 @@ async def get_config(request):
54 78
     return JSONResponse(items)
55 79
 
56 80
 async def set_config(request):
57
-
58 81
     body = json.loads(await request.json())
59 82
 
60 83
     for sect in body.keys():
@@ -64,9 +87,7 @@ async def set_config(request):
64 87
             else:
65 88
                 config.set(sect, item, value=body[sect][item])
66 89
 
67
-    with open(CONFIG_FILES[0], 'w') as conf_file:
68
-        config.write(conf_file)
69
-
90
+    save_config()
70 91
     return PlainTextResponse('OK')
71 92
 
72 93
 async def config_route(request, *args, **kwargs):

+ 1
- 0
tests/conftest.py 查看文件

@@ -0,0 +1 @@
1
+pytest_plugins = "pytester"

+ 29
- 1
tests/test_config.py 查看文件

@@ -2,7 +2,7 @@
2 2
 import pytest
3 3
 from starlette.authentication import UnauthenticatedUser
4 4
 from starlette.testclient import TestClient
5
-from pyheatpump.config import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES
5
+from pyheatpump.config import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES, mac_address_init, save_config
6 6
 from unittest.mock import patch, MagicMock
7 7
 from pprint import pprint
8 8
 import json
@@ -10,16 +10,33 @@ from tempfile import mkstemp
10 10
 from configparser import ConfigParser
11 11
 import os
12 12
 
13
+@pytest.fixture
14
+def tmpconf(testdir):
15
+    tmp_conf_filepath = os.path.join(testdir.tmpdir, 'pyheatpump.ini')
16
+    config = ConfigParser(
17
+        default_section='heatpump')
18
+    config.read_dict(default_config)
19
+
20
+
21
+    with open(tmp_conf_filepath, 'w') as f:
22
+        f.write("[heatpump]\nmac_address = 'None'")
23
+        CONFIG_FILES = [tmp_conf_filepath]
24
+        config.read(filenames=CONFIG_FILES)
25
+
26
+    return config
27
+
13 28
 def test_get_():
14 29
     c = TestClient(app)
15 30
     r = c.get('/')
16 31
     assert r.status_code == 200
17 32
 
33
+
18 34
 class RequestMock(MagicMock):
19 35
     def __get__(self, key):
20 36
         if key == 'method':
21 37
             return 'GET'
22 38
 
39
+
23 40
 @pytest.mark.asyncio
24 41
 async def test_get_config():
25 42
     resp = await get_config(RequestMock())
@@ -30,6 +47,7 @@ async def test_get_config():
30 47
         for item in default_config[sect].keys():
31 48
             assert item in d_resp[sect].keys()
32 49
 
50
+
33 51
 def test_set_config():
34 52
     c = TestClient(app)
35 53
     _, tmpconf = mkstemp()
@@ -47,3 +65,13 @@ def test_set_config():
47 65
     assert config.get('supervisor', 'scheme') == 'test'
48 66
 
49 67
     os.unlink(tmpconf)
68
+
69
+
70
+def test_mac_address(testdir, tmpconf):
71
+    try:
72
+        assert tmpconf.get('heatpump', 'mac_address') is 'None'
73
+        mac_address = mac_address_init()
74
+        config_mac_address = tmpconf.get('heatpump', 'mac_address')
75
+        assert config_mac_address == mac_address
76
+    except Exception as e:
77
+        raise e

Loading…
取消
儲存