-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtest_machines.py
More file actions
165 lines (152 loc) · 6.25 KB
/
Copy pathtest_machines.py
File metadata and controls
165 lines (152 loc) · 6.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Tests for `maas.client.flesh.machines`."""
from functools import partial
from operator import itemgetter
import yaml
from .testing import TestCaseWithProfile
from .. import ArgumentParser, machines, tabular
from ...enum import NodeStatus, PowerState
from ...testing import make_name_without_spaces
from ...viscera.testing import bind
from ...viscera.machines import Machine, Machines
from ...viscera.resource_pools import ResourcePool
from ...viscera.tags import Tag, Tags
from ...viscera.users import User
from ...viscera.zones import Zone
def make_origin():
"""Make origin for machines."""
return bind(Machines, Machine, User, ResourcePool, Zone, Tag, Tags)
class TestMachines(TestCaseWithProfile):
"""Tests for `cmd_machines`."""
def test_returns_table_with_machines(self):
origin = make_origin()
parser = ArgumentParser()
machine_objs = [
{
"hostname": make_name_without_spaces(),
"architecture": "amd64/generic",
"status": NodeStatus.READY.value,
"status_name": NodeStatus.READY.name,
"owner": None,
"power_state": PowerState.OFF.value,
"cpu_count": 2,
"memory": 1024,
"pool": {"id": 1, "name": "pool1", "description": "pool1"},
"zone": {"id": 1, "name": "zone1", "description": "zone1"},
},
{
"hostname": make_name_without_spaces(),
"architecture": "i386/generic",
"status": NodeStatus.DEPLOYED.value,
"status_name": NodeStatus.DEPLOYED.name,
"owner": make_name_without_spaces(),
"power_state": PowerState.ON.value,
"cpu_count": 4,
"memory": 4096,
"pool": {"id": 2, "name": "pool2", "description": "pool2"},
"zone": {"id": 2, "name": "zone2", "description": "zone2"},
},
]
origin.Machines._handler.read.return_value = machine_objs
cmd = machines.cmd_machines(parser)
subparser = machines.cmd_machines.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": "power", "title": "Power"},
{"name": "status", "title": "Status"},
{"name": "owner", "title": "Owner"},
{"name": "architecture", "title": "Arch"},
{"name": "cpus", "title": "#CPUs"},
{"name": "memory", "title": "RAM"},
{"name": "pool", "title": "Resource pool"},
{"name": "zone", "title": "Zone"},
],
output["columns"],
)
machines_output = sorted(
[
{
"hostname": machine["hostname"],
"power": machine["power_state"],
"status": machine["status_name"],
"owner": machine["owner"] if machine["owner"] else "(none)",
"architecture": machine["architecture"],
"cpus": machine["cpu_count"],
"memory": machine["memory"],
"pool": machine["pool"]["name"],
"zone": machine["zone"]["name"],
}
for machine in machine_objs
],
key=itemgetter("hostname"),
)
self.assertEqual(machines_output, output["data"])
def test_calls_handler_with_hostnames(self):
origin = make_origin()
parser = ArgumentParser()
origin.Machines._handler.read.return_value = []
subparser = machines.cmd_machines.register(parser)
cmd = machines.cmd_machines(parser)
hostnames = [make_name_without_spaces() for _ in range(3)]
options = subparser.parse_args(hostnames)
cmd.execute(origin, options, target=tabular.RenderTarget.yaml)
origin.Machines._handler.read.assert_called_once_with(hostname=hostnames)
class TestMachine(TestCaseWithProfile):
"""Tests for `cmd_machine`."""
def setUp(self):
super().setUp()
origin = make_origin()
parser = ArgumentParser()
self.hostname = make_name_without_spaces()
machine_objs = [
{
"hostname": self.hostname,
"architecture": "amd64/generic",
"status": NodeStatus.READY.value,
"status_name": NodeStatus.READY.name,
"owner": None,
"power_state": PowerState.OFF.value,
"cpu_count": 2,
"memory": 1024,
"pool": {"id": 1, "name": "pool1", "description": "pool1"},
"zone": {"id": 1, "name": "zone1", "description": "zone1"},
"tag_names": ["tag1", "tag2"],
"distro_series": "",
"power_type": "Manual",
},
]
origin.Machines._handler.read.return_value = machine_objs
cmd = machines.cmd_machine(parser)
subparser = machines.cmd_machine.register(parser)
options = subparser.parse_args([machine_objs[0]["hostname"]])
self.cmd = partial(cmd.execute, origin, options)
def test_yaml_machine_details_with_tags(self):
yaml_output = yaml.safe_load(self.cmd(target=tabular.RenderTarget.yaml))
self.assertEqual(yaml_output.get("tags"), ["tag1", "tag2"])
def test_plain_machine_details_with_tags(self):
plain_output = self.cmd(target=tabular.RenderTarget.plain)
self.assertEqual(
plain_output,
f"""\
+---------------+-------------+
| Hostname | {self.hostname} |
| Status | READY |
| Image | (none) |
| Power | Off |
| Power Type | Manual |
| Arch | amd64 |
| #CPUs | 2 |
| RAM | 1.0 GB |
| Interfaces | 0 physical |
| IP addresses | |
| Resource pool | pool1 |
| Zone | zone1 |
| Owner | (none) |
| Tags | tag1 |
| | tag2 |
+---------------+-------------+""",
)