Skip to content

Commit c16785a

Browse files
author
Roberto Sora
committed
Updated pyinvoke to use async feature to test the daemon
1 parent 5e9a347 commit c16785a

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

test/conftest.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import os
1616

1717
import pytest
18-
from invoke.context import Context
1918
import simplejson as json
19+
from invoke import Local
20+
from invoke.context import Context
2021

2122
from .common import Board
2223

@@ -57,7 +58,7 @@ def run_command(pytestconfig, data_dir, downloads_dir, working_dir):
5758
will work in the same temporary folder.
5859
5960
Useful reference:
60-
http://docs.pyinvoke.org/en/1.2/api/runners.html#invoke.runners.Result
61+
http://docs.pyinvoke.org/en/1.4/api/runners.html#invoke.runners.Result
6162
"""
6263
cli_path = os.path.join(str(pytestconfig.rootdir), "..", "arduino-cli")
6364
env = {
@@ -67,7 +68,7 @@ def run_command(pytestconfig, data_dir, downloads_dir, working_dir):
6768
}
6869
os.makedirs(os.path.join(data_dir, "packages"))
6970

70-
def _run(cmd_string):
71+
def _run(cmd_string, asynchronous=False):
7172
cli_full_line = "{} {}".format(cli_path, cmd_string)
7273
run_context = Context()
7374
with run_context.cd(working_dir):
@@ -78,6 +79,35 @@ def _run(cmd_string):
7879
return _run
7980

8081

82+
@pytest.fixture(scope="function")
83+
def daemon_runner(pytestconfig, data_dir, downloads_dir, working_dir):
84+
"""
85+
Provide an invoke's `Local` object that has started the arduino-cli in daemon mode.
86+
This way is simple to start and kill the daemon when the test is finished
87+
via the kill() function
88+
89+
Useful reference:
90+
http://docs.pyinvoke.org/en/1.4/api/runners.html#invoke.runners.Local
91+
http://docs.pyinvoke.org/en/1.4/api/runners.html
92+
"""
93+
cli_full_line = os.path.join(str(pytestconfig.rootdir), "..", "arduino-cli daemon")
94+
env = {
95+
"ARDUINO_DATA_DIR": data_dir,
96+
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
97+
"ARDUINO_SKETCHBOOK_DIR": data_dir,
98+
}
99+
os.makedirs(os.path.join(data_dir, "packages"))
100+
run_context = Context()
101+
run_context.cd(working_dir)
102+
# Local Class is the implementation of a Runner abstract class
103+
runner = Local(run_context)
104+
runner.run(
105+
cli_full_line, echo=False, hide=True, warn=True, env=env, asynchronous=True
106+
)
107+
108+
return runner
109+
110+
81111
@pytest.fixture(scope="function")
82112
def detected_boards(run_command):
83113
"""

test/requirements.in

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ pyyaml==5.3
66
prometheus-client==0.7.1
77
requests==2.22.0
88
pytest-timeout==1.3.4
9-
# temporary, replaces invoke==1.3.0 in favour of https://github.com/pyinvoke/invoke/pull/661
10-
git+https://github.com/flazzarini/invoke.git
9+
invoke==1.4.1

test/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ certifi==2019.11.28 # via requests
99
chardet==3.0.4 # via requests
1010
idna==2.8 # via requests
1111
importlib-metadata==1.5.0 # via pluggy, pytest
12-
git+https://github.com/flazzarini/invoke.git
12+
invoke==1.4.1
1313
more-itertools==7.1.0 # via pytest
1414
packaging==19.0 # via pytest
1515
pluggy==0.13.1 # via pytest

test/test_daemon.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,18 @@
1414
# a commercial license, send an email to license@arduino.cc.
1515

1616
import os
17-
import subprocess
1817
import time
18+
1919
import pytest
2020
import requests
2121
import yaml
2222
from prometheus_client.parser import text_string_to_metric_families
2323

24-
@pytest.mark.timeout(30)
25-
def test_telemetry_prometheus_endpoint(pytestconfig, data_dir, downloads_dir):
26-
27-
# Use raw subprocess here due to a missing functionality in pinned pyinvoke ver,
28-
# in order to launch and detach the cli process in daemon mode
29-
cli_path = os.path.join(str(pytestconfig.rootdir), "..", "arduino-cli")
30-
env = os.environ.copy()
31-
env["ARDUINO_DATA_DIR"] = data_dir
32-
env["ARDUINO_DOWNLOADS_DIR"] = downloads_dir
33-
env["ARDUINO_SKETCHBOOK_DIR"] = data_dir
34-
daemon = subprocess.Popen([cli_path, "daemon"], env=env)
3524

36-
37-
# wait for and then parse repertory file
25+
@pytest.mark.timeout(100)
26+
def test_telemetry_prometheus_endpoint(daemon_runner, data_dir):
27+
# Wait for the repertory file to be created and then parse it
28+
# in order to check the generated ids
3829
repertory_file = os.path.join(data_dir, "repertory.yaml")
3930
while not os.path.exists(repertory_file):
4031
time.sleep(1)
@@ -47,5 +38,6 @@ def test_telemetry_prometheus_endpoint(pytestconfig, data_dir, downloads_dir):
4738
family = next(text_string_to_metric_families(metrics))
4839
sample = family.samples[0]
4940
assert repertory["installation"]["id"] == sample.labels["installationID"]
50-
#add a fixture here!
51-
daemon.kill()
41+
42+
# Kill the runner's process as we finished our test
43+
daemon_runner.kill()

0 commit comments

Comments
 (0)