• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python2
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import logging
7
8import common
9
10class AuditError(Exception):
11  """Generic error raised during audit."""
12
13
14class _BaseVerifier(object):
15    """Base verify provide and keep base information and methods.
16
17    Verifiers run audit against target host specified by 'get_host()'
18    method in subclasses. CrosHost for audit actions against DUT, ServoHost
19    for actions against servo and its dependencies.
20
21    Main logic located in '_verify()' method.
22    """
23
24    def __init__(self, dut_host):
25        self._dut_host = dut_host
26        self._result_dir = None
27
28    def verify(self):
29        """Main method to start the verifier"""
30        raise NotImplementedError("'verify' method not implemented")
31
32    def _verify(self):
33        """Main method to run the logic of the verifier.
34
35        Access to the host provided by `self.get_host()`.
36        """
37        raise NotImplementedError("'verify' method not implemented")
38
39    def get_host(self):
40        """Provide access to target host"""
41        raise NotImplementedError("'get_host' method not implemented")
42
43    def _set_host_info_state(self, prefix, state):
44        """Update state value to the label in the host_info
45
46        @param host: dut host presentation to provide access to host_info
47        @param prefix: label prefix. (ex. label_prefix:value)
48        @param state: new state value for the label
49        """
50        if self._dut_host and prefix:
51            host_info = self._dut_host.host_info_store.get()
52            old_state = host_info.get_label_value(prefix)
53            host_info.set_version_label(prefix, state)
54            logging.info('Set %s as `%s` (previous: `%s`)',
55                            prefix, state, old_state)
56            self._dut_host.host_info_store.commit(host_info)
57
58    def host_is_up(self):
59        """Check if the host is up and available by ssh"""
60        return self._dut_host.is_up(timeout=20)
61
62    def servo_is_up(self):
63        """Check if servo host is up and servod is initialized"""
64        return self.servo_host_is_up() and bool(self._dut_host.servo)
65
66    def servo_host_is_up(self):
67        """Check if servo host is up and available by ssh"""
68        return (self._dut_host._servo_host
69            and self._dut_host._servo_host.is_up(timeout=20))
70
71    def set_result_dir(self, result_dir):
72        """Set result directory path."""
73        logging.debug('Set result_dir: %s', result_dir)
74        self._result_dir = result_dir
75
76    def get_result_dir(self):
77        """Provide result directory path."""
78        return self._result_dir
79
80
81class _BaseDUTVerifier(_BaseVerifier):
82    """Base verify check availability of DUT before run actual verifier.
83
84    Verifier run audit actions against CrosHost.
85    """
86
87    def get_host(self):
88        """Return CrosHost"""
89        return self._dut_host
90
91    def verify(self, **args):
92        """Vallidate the host reachable by SSH and run verifier"""
93        if not self._dut_host:
94            raise AuditError('host is not present')
95        self._verify(**args)
96
97
98class _BaseServoVerifier(_BaseVerifier):
99    """Base verify check availability of Servo before run actual verifier
100
101    Verifier run audit actions against ServoHost.
102    """
103    def get_host(self):
104        """Return ServoHost"""
105        return self._dut_host._servo_host
106
107    def verify(self):
108        """Vallidate the host and servo initialized and run verifier"""
109        if not self._dut_host:
110            raise AuditError('host is not present')
111        self._verify()
112