-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtest_devices.py
More file actions
90 lines (82 loc) · 3.25 KB
/
test_devices.py
File metadata and controls
90 lines (82 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Tests for `maas.client.flesh.devices`."""
from operator import itemgetter
import yaml
from .testing import TestCaseWithProfile
from .. import ArgumentParser, devices, tabular
from ...testing import make_name_without_spaces
from ...viscera.testing import bind
from ...viscera.devices import Device, Devices
from ...viscera.interfaces import Interface, Interfaces, InterfaceLink, InterfaceLinks
from ...viscera.users import User
def make_origin():
"""Make origin for devices."""
return bind(
Devices, Device, User, Interfaces, Interface, InterfaceLinks, InterfaceLink
)
class TestDevices(TestCaseWithProfile):
"""Tests for `cmd_devices`."""
def test_returns_table_with_devices(self):
origin = make_origin()
parser = ArgumentParser()
devices_objs = [
{
"hostname": make_name_without_spaces(),
"owner": make_name_without_spaces(),
"interface_set": [
{"links": [{"ip_address": "192.168.122.1"}]},
{"links": [{"ip_address": "192.168.122.2"}]},
{"links": [{}]},
],
},
{
"hostname": make_name_without_spaces(),
"owner": make_name_without_spaces(),
"interface_set": [
{"links": [{"ip_address": "192.168.122.10"}]},
{"links": [{"ip_address": "192.168.122.11"}]},
{"links": [{}]},
],
},
]
origin.Devices._handler.read.return_value = devices_objs
cmd = devices.cmd_devices(parser)
subparser = devices.cmd_devices.register(parser)
options = subparser.parse_args([])
output = yaml.safe_load(
cmd.execute(origin, options, target=tabular.RenderTarget.yaml)
)
self.assertEqual(
[
{"name": "hostname", "title": "Hostname"},
{"name": "owner", "title": "Owner"},
{"name": "ip_addresses", "title": "IP addresses"},
],
output["columns"],
)
devices_output = sorted(
[
{
"hostname": device["hostname"],
"owner": device["owner"] if device["owner"] else "(none)",
"ip_addresses": [
link["ip_address"]
for nic in device["interface_set"]
for link in nic["links"]
if link.get("ip_address")
],
}
for device in devices_objs
],
key=itemgetter("hostname"),
)
self.assertEqual(devices_output, output["data"])
def test_calls_handler_with_hostnames(self):
origin = make_origin()
parser = ArgumentParser()
origin.Devices._handler.read.return_value = []
subparser = devices.cmd_devices.register(parser)
cmd = devices.cmd_devices(parser)
hostnames = [make_name_without_spaces() for _ in range(3)]
options = subparser.parse_args(hostnames)
cmd.execute(origin, options, target=tabular.RenderTarget.yaml)
origin.Devices._handler.read.assert_called_once_with(hostname=hostnames)