Skip to content

Commit 069e758

Browse files
authored
Fix machine details for a machine with tags (#236)
Viscera layer produces a Tags.Managed object for machines with tags, this can't be str()ified.
1 parent 6822d83 commit 069e758

3 files changed

Lines changed: 78 additions & 6 deletions

File tree

maas/client/bones/testing/server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ def __init__(self, description):
2222
super(ApplicationBuilder, self).__init__()
2323
self._description = desc.Description(description)
2424
self._application = aiohttp.web.Application()
25-
self._rootpath, self._basepath, self._version = (
26-
self._discover_version_and_paths()
27-
)
25+
(
26+
self._rootpath,
27+
self._basepath,
28+
self._version,
29+
) = self._discover_version_and_paths()
2830
self._wire_up_description()
2931
self._actions = {}
3032
self._views = {}

maas/client/flesh/tables.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ def render(self, target, data):
155155
return super().render(target, data.name)
156156

157157

158+
class NodeTagsColumn(Column):
159+
def render(self, target, data):
160+
if data:
161+
return super().render(target, [tag.name for tag in data])
162+
else:
163+
return ""
164+
165+
158166
class NodesTable(Table):
159167
def __init__(self):
160168
super().__init__(
@@ -215,7 +223,7 @@ def __init__(self, with_type=False):
215223
NodeResourcePoolColumn("pool", "Resource pool"),
216224
NodeZoneColumn("zone", "Zone"),
217225
NodeOwnerColumn("owner", "Owner"),
218-
Column("tags", "Tags"),
226+
NodeTagsColumn("tags", "Tags"),
219227
]
220228
if with_type:
221229
columns.insert(1, NodeTypeColumn("node_type", "Type"))

maas/client/flesh/tests/test_machines.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for `maas.client.flesh.machines`."""
22

3+
from functools import partial
34
from operator import itemgetter
45
import yaml
56

@@ -10,13 +11,14 @@
1011
from ...viscera.testing import bind
1112
from ...viscera.machines import Machine, Machines
1213
from ...viscera.resource_pools import ResourcePool
14+
from ...viscera.tags import Tag, Tags
1315
from ...viscera.users import User
1416
from ...viscera.zones import Zone
1517

1618

1719
def make_origin():
1820
"""Make origin for machines."""
19-
return bind(Machines, Machine, User, ResourcePool, Zone)
21+
return bind(Machines, Machine, User, ResourcePool, Zone, Tag, Tags)
2022

2123

2224
class TestMachines(TestCaseWithProfile):
@@ -55,7 +57,7 @@ def test_returns_table_with_machines(self):
5557
cmd = machines.cmd_machines(parser)
5658
subparser = machines.cmd_machines.register(parser)
5759
options = subparser.parse_args([])
58-
output = yaml.load(
60+
output = yaml.safe_load(
5961
cmd.execute(origin, options, target=tabular.RenderTarget.yaml)
6062
)
6163
self.assertEquals(
@@ -101,3 +103,63 @@ def test_calls_handler_with_hostnames(self):
101103
options = subparser.parse_args(hostnames)
102104
cmd.execute(origin, options, target=tabular.RenderTarget.yaml)
103105
origin.Machines._handler.read.assert_called_once_with(hostname=hostnames)
106+
107+
108+
class TestMachine(TestCaseWithProfile):
109+
"""Tests for `cmd_machine`."""
110+
111+
def setUp(self):
112+
super().setUp()
113+
origin = make_origin()
114+
parser = ArgumentParser()
115+
self.hostname = make_name_without_spaces()
116+
machine_objs = [
117+
{
118+
"hostname": self.hostname,
119+
"architecture": "amd64/generic",
120+
"status": NodeStatus.READY.value,
121+
"status_name": NodeStatus.READY.name,
122+
"owner": None,
123+
"power_state": PowerState.OFF.value,
124+
"cpu_count": 2,
125+
"memory": 1024,
126+
"pool": {"id": 1, "name": "pool1", "description": "pool1"},
127+
"zone": {"id": 1, "name": "zone1", "description": "zone1"},
128+
"tag_names": ["tag1", "tag2"],
129+
"distro_series": "",
130+
"power_type": "Manual",
131+
},
132+
]
133+
origin.Machines._handler.read.return_value = machine_objs
134+
cmd = machines.cmd_machine(parser)
135+
subparser = machines.cmd_machine.register(parser)
136+
options = subparser.parse_args([machine_objs[0]["hostname"]])
137+
self.cmd = partial(cmd.execute, origin, options)
138+
139+
def test_yaml_machine_details_with_tags(self):
140+
yaml_output = yaml.safe_load(self.cmd(target=tabular.RenderTarget.yaml))
141+
self.assertEqual(yaml_output.get("tags"), ["tag1", "tag2"])
142+
143+
def test_plain_machine_details_with_tags(self):
144+
plain_output = self.cmd(target=tabular.RenderTarget.plain)
145+
self.assertEqual(
146+
plain_output,
147+
f"""\
148+
+---------------+-------------+
149+
| Hostname | {self.hostname} |
150+
| Status | READY |
151+
| Image | (none) |
152+
| Power | Off |
153+
| Power Type | Manual |
154+
| Arch | amd64 |
155+
| #CPUs | 2 |
156+
| RAM | 1.0 GB |
157+
| Interfaces | 0 physical |
158+
| IP addresses | |
159+
| Resource pool | pool1 |
160+
| Zone | zone1 |
161+
| Owner | (none) |
162+
| Tags | tag1 |
163+
| | tag2 |
164+
+---------------+-------------+""",
165+
)

0 commit comments

Comments
 (0)