~andreserl/maas/qa-lab-tests-bionic

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from json import loads
from unittest import TestLoader
import yaml
from testtools import TestCase
from testtools.matchers import (
    ContainsDict,
    Equals,
    MatchesDict,
)
from timeout import timeout
from utils import (
    run_maas_cli,
    wait_machines,
)

from maasserver.enum import NODE_STATUS

tests_order = [
    'test_add_rsd_pod',
    'test_precomposed_machines_commissioned',
    'test_manually_compose_machine_no_constraints',
    'test_manually_compose_machine_with_constraints',
    'test_manually_composed_machines_commissioned',
    'test_allocate_rsd_machines',
    'test_deploy_rsd_machines',
    'test_delete_composed_machine',
    'test_delete_rsd_pod',
    'test_composed_machines_removed',
]


def sorting_method(ignored, first_test, second_test):
    return tests_order.index(first_test) - tests_order.index(second_test)


TestLoader.sortTestMethodsUsing = sorting_method

RSD_CONFIG = yaml.load(open('rsd.yaml'))
RSD_POD_TYPE = 'rsd'


class TestRSD(TestCase):

    def _wait_machines(self, status=None, num_expected=None):
        if num_expected is None:
            num_expected = len(RSD_CONFIG['composed_machines'])
        return wait_machines(self, num_expected, status)

    def _run_maas_cli(self, args):
        return run_maas_cli(self, "rsd", args)

    def test_add_rsd_pod(self):
        output, _ = self._run_maas_cli([
            "pods",
            "create",
            "type={}".format(RSD_POD_TYPE),
            "power_address={}:{}".format(RSD_CONFIG['ip'],
                                         RSD_CONFIG['port']),
            "power_user={}".format(RSD_CONFIG['user']),
            "power_pass={}".format(RSD_CONFIG['pass']),
        ])
        rsd_pod = loads(output)
        pod_cfg = RSD_CONFIG['pod']
        self.assertThat(rsd_pod, ContainsDict(
            {
                "architectures": Equals(pod_cfg['architectures']),
                "type": Equals(RSD_POD_TYPE),
                "total": MatchesDict(
                    {
                        "cores": Equals(pod_cfg['total']['cores']),
                        "local_storage": Equals(
                            pod_cfg['total']['local_storage']
                        ),
                        "memory": Equals(pod_cfg['total']['memory']),
                        "local_disks": Equals(pod_cfg['total']['local_disks']),
                    }
                ),
                "available": MatchesDict(
                    {
                        "cores": Equals(pod_cfg['available']['cores']),
                        "local_storage": Equals(
                            pod_cfg['available']['local_storage']
                        ),
                        "memory": Equals(pod_cfg['available']['memory']),
                        "local_disks": Equals(
                            pod_cfg['available']['local_disks']
                        ),
                    }
                ),
                "used": MatchesDict(
                    {
                        "cores": Equals(pod_cfg['used']['cores']),
                        "local_storage": Equals(
                            pod_cfg['used']['local_storage']
                        ),
                        "memory": Equals(pod_cfg['used']['memory']),
                        "local_disks": Equals(pod_cfg['used']['local_disks']),
                    }
                ),
                "capabilities": Equals(pod_cfg['capabilities']),
            }
        ))

    @timeout(20*60)
    def test_precomposed_machines_commissioned(self):
        rsd_machines = self._wait_machines(NODE_STATUS.READY)
        hw = [
            {
                'memory': m['memory'], 'cpu_count': m['cpu_count']
            } for m in rsd_machines
        ]
        self.assertCountEqual(RSD_CONFIG['composed_machines'], hw)

    def _get_rsd_pod(self):
        output, _ = self._run_maas_cli(["pods", "read"])
        pods = loads(output)
        rsd_pods = [p for p in pods if p['type'] == RSD_POD_TYPE]
        self.assertThat(len(rsd_pods), Equals(1))
        return rsd_pods[0]

    def test_manually_compose_machine_no_constraints(self):
        # Create a tag to tag composed machines with
        output, _ = self._run_maas_cli(["tags", "create", "name=composed"])
        tag = loads(output)
        self.assertThat(tag['name'], Equals('composed'))
        rsd_pod = self._get_rsd_pod()
        output, _ = self._run_maas_cli([
            "pod",
            "compose",
            "%s" % rsd_pod['id'],
        ])
        machine = loads(output)
        output, _ = self._run_maas_cli([
            "tag",
            "update-nodes",
            "composed",
            "add=" + machine['system_id'],
        ])

    def test_manually_compose_machine_with_constraints(self):
        rsd_pod = self._get_rsd_pod()
        output, _ = self._run_maas_cli([
            "pod",
            "compose",
            "%s" % rsd_pod['id'],
            "memory=31248",
            "cores=40",
        ])
        machine = loads(output)
        output, _ = self._run_maas_cli([
            "tag",
            "update-nodes",
            "composed",
            "add=" + machine['system_id'],
        ])

    @timeout(20*60)
    def test_manually_composed_machines_commissioned(self):
        rsd_machines = self._wait_machines(
            NODE_STATUS.READY,
            num_expected=len(RSD_CONFIG['composed_machines']) + 2,
        )

    @timeout(20*60)
    def test_allocate_rsd_machines(self):
        rsd_machines = self._wait_machines(
            NODE_STATUS.READY,
            num_expected=len(RSD_CONFIG['composed_machines']) + 2,
        )
        for machine in rsd_machines:
            self._run_maas_cli([
                "machines", "allocate", "system_id={}".format(
                    machine['system_id']
                )
            ])
        rsd_machines = self._wait_machines(
            NODE_STATUS.ALLOCATED,
            num_expected=len(RSD_CONFIG['composed_machines']) + 2,
        )

    @timeout(20*60)
    def test_deploy_rsd_machines(self):
        rsd_machines = self._wait_machines(
            NODE_STATUS.ALLOCATED,
            num_expected=len(RSD_CONFIG['composed_machines']) + 2,
        )
        for machine in rsd_machines:
            self._run_maas_cli(
                ["machine", "deploy", machine['system_id']]
            )
        rsd_machines = self._wait_machines(
            NODE_STATUS.DEPLOYED,
            num_expected=len(RSD_CONFIG['composed_machines']) + 2,
        )

    def test_delete_composed_machine(self):
        output, _ = self._run_maas_cli(["tag", "machines", "composed"])
        machines = loads(output)
        for machine in machines:
            self._run_maas_cli(["machine", "delete", machine['system_id']])
        # Delete the 'composed' tag
        self._run_maas_cli(["tag", "delete", "composed"])

    def test_delete_rsd_pod(self):
        rsd_pod = self._get_rsd_pod()
        output, err = self._run_maas_cli(
            ["pod", "delete", "%s" % rsd_pod['id']]
        )
        self.assertThat(err, Equals(""))
        output, _ = self._run_maas_cli(["pods", "read"])
        # Verify that the pod was deleted
        pods = loads(output)
        self.assertThat(len(pods), Equals(0))

    def test_composed_machines_removed(self):
        output, _ = self._run_maas_cli(["machines", "read"])
        machines = loads(output)
        self.assertThat(len(machines), Equals(0))