1# Copyright (c) 2012 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
5"""Comparators for use in dynamic_suite module unit tests."""
6
7import mox
8
9class StatusContains(mox.Comparator):
10    @staticmethod
11    def CreateFromStrings(status=None, test_name=None, reason=None):
12        status_comp = mox.StrContains(status) if status else mox.IgnoreArg()
13        name_comp = mox.StrContains(test_name) if test_name else mox.IgnoreArg()
14        reason_comp = mox.StrContains(reason) if reason else mox.IgnoreArg()
15        return StatusContains(status_comp, name_comp, reason_comp)
16
17
18    def __init__(self, status=mox.IgnoreArg(), test_name=mox.IgnoreArg(),
19                 reason=mox.IgnoreArg()):
20        """Initialize.
21
22        Takes mox.Comparator objects to apply to job_status.Status
23        member variables.
24
25        @param status: status code, e.g. 'INFO', 'START', etc.
26        @param test_name: expected test name.
27        @param reason: expected reason
28        """
29        self._status = status
30        self._test_name = test_name
31        self._reason = reason
32
33
34    def equals(self, rhs):
35        """Check to see if fields match base_job.status_log_entry obj in rhs.
36
37        @param rhs: base_job.status_log_entry object to match.
38        @return boolean
39        """
40        return (self._status.equals(rhs.status_code) and
41                self._test_name.equals(rhs.operation) and
42                self._reason.equals(rhs.message))
43
44
45    def __repr__(self):
46        return '<Status containing \'%s\t%s\t%s\'>' % (self._status,
47                                                       self._test_name,
48                                                       self._reason)
49
50
51class InHostList(mox.Comparator):
52    """PyMox comparator that matches given Host.hostname to a stored list."""
53    def __init__(self, host_list):
54        self._hostname_list = [h.hostname for h in host_list]
55
56
57    def equals(self, rhs):
58        """Check to see if rhs.hostname is in stored hostname list.
59
60        @param rhs: Host object to match.
61        @return boolean
62        """
63        return rhs.hostname in self._hostname_list
64
65
66class AllInHostList(mox.Comparator):
67    """PyMox comparator that matches a list of Host base on hostname.
68
69    Matches if, for each Host to be matched, its hostname is in the list of
70    hostnames provided at initialization.
71    """
72    def __init__(self, host_list):
73        self._in_host_list = InHostList(host_list)
74
75
76    def equals(self, rhs):
77        """Check to see if all hostnames in rhs are in stored hostname list.
78
79        @param rhs: iterable of Host objects to match.
80        @return False if rhs is []; otherwise checks all objects in rhs.
81        """
82        tests = map(self._in_host_list.equals, rhs)
83        if tests:
84            return reduce(lambda a,b: a and b, tests, True)
85        return False
86