1# Copyright (c) 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.server import test
9from autotest_lib.server import frontend
10
11class hardware_StorageQualCheckSetup(test.test):
12    """
13    Verifies the moblab and DUT setup for storage qual
14    A correct setup consists of
15        At least one pool (the default "no pool" counts as a pool)
16        Each of the labels [retention, trim, suspend] is applied to exactly
17            one DUT in the pool.
18        No duplication of the labels, all labels are applied exactly once
19            per pool
20
21    The test will verify this set up, for the pool that is selected on the
22    RunSuite page (by getting the pool of the DUT running the test). If this
23    test passes, we have confidence that this individual pool is a valid setup
24    for storage qual.
25    """
26
27    version = 1
28
29    REQUIRED_LABELS = ['retention', 'trim', 'suspend']
30
31    def _group_hosts_into_pools(self, hosts):
32        pools = {}
33        for host in hosts:
34            labels = [label.name for label in host.get_labels()]
35
36            pool_name = 'none'
37            for label in labels:
38                if 'pool:' in label:
39                    pool_name = label.replace('pool:', '')
40
41            if pool_name not in pools:
42                pools[pool_name] = []
43
44            pools[pool_name].append({
45                'host': host.hostname,
46                'labels': labels
47            })
48
49        return pools
50
51    def _get_running_host_pool(self):
52        host = list(self.job.hosts)[0]
53        pools = host.host_info_store.get().pools
54        return list(pools)[0] if len(pools) > 0 else 'none'
55
56    def run_once(self):
57        """ Tests the moblab's connected DUTs to see if the current
58        configuration is valid for storage qual
59        """
60
61        # get the pool of the host this test is running on
62        pool_name = self._get_running_host_pool()
63        logging.info('Test is running on pool %s', pool_name)
64
65        afe = frontend.AFE(server='localhost', user='moblab')
66
67        # get autotest statuses that indicate a live host
68        live_statuses = afe.host_statuses(live=True)
69
70        # get the hosts connected to autotest, find the live ones
71        hosts = []
72        for host in afe.get_hosts():
73            if host.status in live_statuses:
74                logging.info('Host %s is live, status %s',
75                        host.hostname, host.status)
76                hosts.append(host)
77            else:
78                logging.info('Host %s is not live, status %s',
79                        host.hostname, host.status)
80
81        pools = self._group_hosts_into_pools(hosts)
82
83        # verify that the pool is set up to run storage qual, with correct
84        # number of DUTs and correct labels
85        required_set = set(self.REQUIRED_LABELS)
86        provided_set = set()
87        for host in pools[pool_name]:
88            host_provided_labels = set(host['labels']) & required_set
89            # check that each DUT has at most 1 storage qual label
90            if len(host_provided_labels) > 1:
91                raise error.TestFail(
92                    ('Host %s is assigned more than '
93                        'one storage qual label %s') %
94                        (host['host'], str(host_provided_labels)))
95            if len(host_provided_labels) == 0:
96                continue
97
98            # check that each label is only on one DUT in the pool
99            provided_label = host_provided_labels.pop()
100            if provided_label in provided_set:
101                raise error.TestFail(
102                    ('Host %s is assigned label %s, which is already '
103                      'assigned to another DUT in pool %s') %
104                        (host['host'], provided_label, pool_name)
105                )
106
107            provided_set.add(provided_label)
108            logging.info(' - %s %s', host['host'], provided_label)
109
110        # check that all storage qual labels are accounted for in the pool
111        missing_labels = required_set - provided_set
112        if len(missing_labels) > 0:
113            raise error.TestFail(
114                'Pool %s is missing required labels %s' %
115                    (pool_name, str(missing_labels))
116                )
117
118        return
119