|
1 | 1 | """Tests for `maas.client.flesh.machines`.""" |
2 | 2 |
|
| 3 | +from functools import partial |
3 | 4 | from operator import itemgetter |
4 | 5 | import yaml |
5 | 6 |
|
|
10 | 11 | from ...viscera.testing import bind |
11 | 12 | from ...viscera.machines import Machine, Machines |
12 | 13 | from ...viscera.resource_pools import ResourcePool |
| 14 | +from ...viscera.tags import Tag, Tags |
13 | 15 | from ...viscera.users import User |
14 | 16 | from ...viscera.zones import Zone |
15 | 17 |
|
16 | 18 |
|
17 | 19 | def make_origin(): |
18 | 20 | """Make origin for machines.""" |
19 | | - return bind(Machines, Machine, User, ResourcePool, Zone) |
| 21 | + return bind(Machines, Machine, User, ResourcePool, Zone, Tag, Tags) |
20 | 22 |
|
21 | 23 |
|
22 | 24 | class TestMachines(TestCaseWithProfile): |
@@ -55,7 +57,7 @@ def test_returns_table_with_machines(self): |
55 | 57 | cmd = machines.cmd_machines(parser) |
56 | 58 | subparser = machines.cmd_machines.register(parser) |
57 | 59 | options = subparser.parse_args([]) |
58 | | - output = yaml.load( |
| 60 | + output = yaml.safe_load( |
59 | 61 | cmd.execute(origin, options, target=tabular.RenderTarget.yaml) |
60 | 62 | ) |
61 | 63 | self.assertEquals( |
@@ -101,3 +103,63 @@ def test_calls_handler_with_hostnames(self): |
101 | 103 | options = subparser.parse_args(hostnames) |
102 | 104 | cmd.execute(origin, options, target=tabular.RenderTarget.yaml) |
103 | 105 | 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