Skip to content

Commit

Permalink
Merge upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
LazarusX committed Aug 15, 2018
2 parents 30819d7 + fbeec13 commit 840b49c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ config
/build


py27
py36
.pypirc
tests/test_solution
Expand Down
62 changes: 48 additions & 14 deletions iotedgedev/azurecli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import sys
import subprocess
import json

import os
import signal
import subprocess
import sys
from io import StringIO

from azure.cli.core import get_default_cli
from fstrings import f

output_io_cls = StringIO


Expand All @@ -22,26 +25,43 @@ def decode(self, val):
return val.decode("utf-8").strip()

def is_posix(self):
self.envvars.is_posix()
return self.envvars.is_posix()

def prepare_az_cli_args(self, args, suppress_output=False):
if suppress_output:
args.extend(["--query", "\"[?n]|[0]\""])

az_args = ["az"]+args
if self.is_posix():
return [" ".join(az_args)]
return az_args

def invoke_az_cli_outproc(self, args, error_message=None, stdout_io=None, stderr_io=None, suppress_output=False):
def invoke_az_cli_outproc(self, args, error_message=None, stdout_io=None, stderr_io=None, suppress_output=False, timeout=None):
try:
if timeout:
timeout = int(timeout)

monitor_events = False
if 'monitor-events' in args:
monitor_events = True

# Consider using functools
if monitor_events:
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output),
shell=not self.is_posix(),
preexec_fn=os.setsid if self.is_posix() else None,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if not self.is_posix() else 0)
elif stdout_io or stderr_io:
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=not self.is_posix())
else:
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output),
shell=not self.is_posix())

if stdout_io or stderr_io:
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=not self.envvars.is_posix())
if timeout:
stdout_data, stderr_data = process.communicate(timeout=timeout)
else:
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output), shell=not self.envvars.is_posix())

stdout_data, stderr_data = process.communicate()
stdout_data, stderr_data = process.communicate()

if stderr_data and b"invalid_grant" in stderr_data:
self.output.error(self.decode(stderr_data))
Expand All @@ -64,8 +84,21 @@ def invoke_az_cli_outproc(self, args, error_message=None, stdout_io=None, stderr

if not stdout_io and not stderr_io:
self.output.line()

except Exception as e:
if hasattr(e, 'timeout') and isinstance(e, subprocess.TimeoutExpired):
if self.is_posix():
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
else:
process.send_signal(signal.CTRL_BREAK_EVENT)
process.kill()

if timeout:
self.output.info("Timeout set to {0} seconds, which expired as expected.".format(timeout))
self.output.line()
return True
else:
raise

if error_message:
self.output.error(error_message)
self.output.error(str(e))
Expand Down Expand Up @@ -243,11 +276,12 @@ def list_resource_groups(self):

def set_modules(self, device_id, connection_string, hub_name, config):
self.output.status(f("Deploying '{config}' to '{device_id}'..."))
config = os.path.join(os.getcwd(), config)

return self.invoke_az_cli_outproc(["iot", "edge", "set-modules", "-d", device_id, "-n", hub_name, "-k", config, "-l", connection_string], error_message=f("Failed to deploy '{config}' to '{device_id}'..."), suppress_output=True)

def monitor_events(self, device_id, connection_string, hub_name, timeout=300):
return self.invoke_az_cli_outproc(["iot", "hub", "monitor-events", "-d", device_id, "-n", hub_name, "-l", connection_string, '-t', str(timeout)], error_message=f("Failed to start monitoring events."), suppress_output=False)
return self.invoke_az_cli_outproc(["iot", "hub", "monitor-events", "-d", device_id, "-n", hub_name, "-l", connection_string, '-t', str(timeout), '-y'], error_message=f("Failed to start monitoring events."), suppress_output=False, timeout=timeout)

def get_free_iothub(self):
with output_io_cls() as io:
Expand Down
5 changes: 4 additions & 1 deletion iotedgedev/iothub.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ def monitor_events_node(self, timeout=0):
self.output.error(str(ex))

def monitor_events_cli(self, timeout=0):
self.azure_cli.monitor_events(self.envvars.DEVICE_CONNECTION_INFO.DeviceId, self.envvars.IOTHUB_CONNECTION_INFO.ConnectionString, self.envvars.IOTHUB_CONNECTION_INFO.HubName, timeout)
self.azure_cli.monitor_events(self.envvars.DEVICE_CONNECTION_INFO.DeviceId,
self.envvars.IOTHUB_CONNECTION_INFO.ConnectionString,
self.envvars.IOTHUB_CONNECTION_INFO.HubName,
timeout)
8 changes: 4 additions & 4 deletions tests/test_iotedgedev.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,16 @@ def test_monitor(request, capfd):

cli = __import__("iotedgedev.cli", fromlist=['main'])
runner = CliRunner()
result = runner.invoke(cli.main, ['monitor', '--timeout', '2'])
result = runner.invoke(cli.main, ['monitor', '--timeout', '5'])
out, err = capfd.readouterr()
print(out)
print(err)
print(result.output)

if PY35:
assert 'Starting event monitor' in out
else:
if not PY35:
assert 'Monitoring events from device' in out
else:
assert not err


def test_e2e(test_push_modules, test_deploy_modules):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ def test_start_solution(capfd):
def test_monitor(capfd):
cli = __import__('iotedgedev.cli', fromlist=['main'])
runner = CliRunner()
result = runner.invoke(cli.main, ['monitor', '--timeout', '10'])
result = runner.invoke(cli.main, ['monitor', '--timeout', '20'])
out, err = capfd.readouterr()
print(out)
print(err)
print(result.output)

if PY35:
assert 'Starting event monitor' in out
else:
if not PY35:
assert 'Monitoring events from device' in out
else:
assert not err
assert 'timeCreated' in out

0 comments on commit 840b49c

Please sign in to comment.