tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
219
220
221
222
223
224
225
226
#!/usr/bin/python
#
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
"""Unit tests for server/cros/host_lock_manager.py."""
 
import mox
import unittest
import common
 
from autotest_lib.server import frontend
from autotest_lib.server.cros import host_lock_manager
 
class HostLockManagerTest(mox.MoxTestBase):
    """Unit tests for host_lock_manager.HostLockManager.
 
    @attribute HOST1: a string, fake host.
    @attribute HOST2: a string, fake host.
    @attribute HOST3: a string, fake host.
    """
 
    HOST1 = 'host1'
    HOST2 = 'host2'
    HOST3 = 'host3'
 
 
    class FakeHost(object):
        """Fake version of Host object defiend in server/frontend.py.
 
        @attribute locked: a boolean, True == host is locked.
        @attribute locked_by: a string, fake user.
        @attribute lock_time: a string, fake timestamp.
        """
 
        def __init__(self, locked=False):
            """Initialize.
 
            @param locked: a boolean, True == host is locked.
            """
            self.locked = locked
            self.locked_by = 'fake_user'
            self.lock_time = 'fake time'
 
 
    class MockHostLockManager(host_lock_manager.HostLockManager):
        """Mock out _host_modifier() in HostLockManager class..
 
        @attribute locked: a boolean, True == host is locked.
        @attribute locked_by: a string, fake user.
        @attribute lock_time: a string, fake timestamp.
        """
 
        def _host_modifier(self, hosts, operation, lock_reason=''):
            """Overwrites original _host_modifier().
 
            Add hosts to self.locked_hosts for LOCK and remove hosts from
            self.locked_hosts for UNLOCK.
 
            @param a set of strings, host names.
            @param operation: a string, LOCK or UNLOCK.
            @param lock_reason: a string, a reason for locking the hosts
            """
            if operation == self.LOCK:
                assert lock_reason
                self.locked_hosts = self.locked_hosts.union(hosts)
            elif operation == self.UNLOCK:
                self.locked_hosts = self.locked_hosts.difference(hosts)
 
 
    def setUp(self):
        super(HostLockManagerTest, self).setUp()
        self.afe = self.mox.CreateMock(frontend.AFE)
        self.manager = host_lock_manager.HostLockManager(self.afe)
 
 
    def testCheckHost_SkipsUnknownHost(self):
        """Test that host unknown to AFE is skipped."""
        self.afe.get_hosts(hostname=self.HOST1).AndReturn(None)
        self.mox.ReplayAll()
        actual = self.manager._check_host(self.HOST1, None)
        self.assertEquals(None, actual)
 
 
    def testCheckHost_DetectsLockedHost(self):
        """Test that a host which is already locked is skipped."""
        host_info = [self.FakeHost(locked=True)]
        self.afe.get_hosts(hostname=self.HOST1).AndReturn(host_info)
        self.mox.ReplayAll()
        actual = self.manager._check_host(self.HOST1, self.manager.LOCK)
        self.assertEquals(None, actual)
 
 
    def testCheckHost_DetectsUnlockedHost(self):
        """Test that a host which is already unlocked is skipped."""
        host_info = [self.FakeHost()]
        self.afe.get_hosts(hostname=self.HOST1).AndReturn(host_info)
        self.mox.ReplayAll()
        actual = self.manager._check_host(self.HOST1, self.manager.UNLOCK)
        self.assertEquals(None, actual)
 
 
    def testCheckHost_ReturnsHostToLock(self):
        """Test that a host which can be locked is returned."""
        host_info = [self.FakeHost()]
        self.afe.get_hosts(hostname=self.HOST1).AndReturn(host_info)
        self.mox.ReplayAll()
        host_with_dot = '.'.join([self.HOST1, 'cros'])
        actual = self.manager._check_host(host_with_dot, self.manager.LOCK)
        self.assertEquals(self.HOST1, actual)
 
 
    def testCheckHost_ReturnsHostToUnlock(self):
        """Test that a host which can be unlocked is returned."""
        host_info = [self.FakeHost(locked=True)]
        self.afe.get_hosts(hostname=self.HOST1).AndReturn(host_info)
        self.mox.ReplayAll()
        host_with_dot = '.'.join([self.HOST1, 'cros'])
        actual = self.manager._check_host(host_with_dot, self.manager.UNLOCK)
        self.assertEquals(self.HOST1, actual)
 
 
    def testLock_WithNonOverlappingHosts(self):
        """Tests host locking, all hosts not in self.locked_hosts."""
        hosts = [self.HOST2]
        manager = self.MockHostLockManager(self.afe)
        manager.locked_hosts = set([self.HOST1])
        manager.lock(hosts, lock_reason='Locking for test')
        self.assertEquals(set([self.HOST1, self.HOST2]), manager.locked_hosts)
 
 
    def testLock_WithPartialOverlappingHosts(self):
        """Tests host locking, some hosts not in self.locked_hosts."""
        hosts = [self.HOST1, self.HOST2]
        manager = self.MockHostLockManager(self.afe)
        manager.locked_hosts = set([self.HOST1, self.HOST3])
        manager.lock(hosts, lock_reason='Locking for test')
        self.assertEquals(set([self.HOST1, self.HOST2, self.HOST3]),
                          manager.locked_hosts)
 
 
    def testLock_WithFullyOverlappingHosts(self):
        """Tests host locking, all hosts in self.locked_hosts."""
        hosts = [self.HOST1, self.HOST2]
        self.manager.locked_hosts = set(hosts)
        self.manager.lock(hosts)
        self.assertEquals(set(hosts), self.manager.locked_hosts)
 
 
    def testUnlock_WithNonOverlappingHosts(self):
        """Tests host unlocking, all hosts not in self.locked_hosts."""
        hosts = [self.HOST2]
        self.manager.locked_hosts = set([self.HOST1])
        self.manager.unlock(hosts)
        self.assertEquals(set([self.HOST1]), self.manager.locked_hosts)
 
 
    def testUnlock_WithPartialOverlappingHosts(self):
        """Tests host locking, some hosts not in self.locked_hosts."""
        hosts = [self.HOST1, self.HOST2]
        manager = self.MockHostLockManager(self.afe)
        manager.locked_hosts = set([self.HOST1, self.HOST3])
        manager.unlock(hosts)
        self.assertEquals(set([self.HOST3]), manager.locked_hosts)
 
 
    def testUnlock_WithFullyOverlappingHosts(self):
        """Tests host locking, all hosts in self.locked_hosts."""
        hosts = [self.HOST1, self.HOST2]
        manager = self.MockHostLockManager(self.afe)
        manager.locked_hosts = set([self.HOST1, self.HOST2, self.HOST3])
        manager.unlock(hosts)
        self.assertEquals(set([self.HOST3]), manager.locked_hosts)
 
 
    def testHostModifier_WithHostsToLock(self):
        """Test host locking."""
        hosts = set([self.HOST1])
        self.manager.locked_hosts = set([self.HOST2])
        self.mox.StubOutWithMock(self.manager, '_check_host')
        self.manager._check_host(self.HOST1,
                                 self.manager.LOCK).AndReturn(self.HOST1)
        self.afe.run('modify_hosts',
                     host_filter_data={'hostname__in': [self.HOST1]},
                     update_data={'locked': True, 'lock_reason': 'Test'})
        self.mox.ReplayAll()
        self.manager._host_modifier(hosts, self.manager.LOCK,
                                    lock_reason='Test')
        self.assertEquals(set([self.HOST1, self.HOST2]),
                          self.manager.locked_hosts)
 
 
    def testHostModifier_WithHostsToUnlock(self):
        """Test host unlocking."""
        hosts = set([self.HOST1])
        self.manager.locked_hosts = set([self.HOST1, self.HOST2])
        self.mox.StubOutWithMock(self.manager, '_check_host')
        self.manager._check_host(self.HOST1,
                                 self.manager.UNLOCK).AndReturn(self.HOST1)
        self.afe.run('modify_hosts',
                     host_filter_data={'hostname__in': [self.HOST1]},
                     update_data={'locked': False})
        self.mox.ReplayAll()
        self.manager._host_modifier(hosts, self.manager.UNLOCK)
        self.assertEquals(set([self.HOST2]), self.manager.locked_hosts)
 
 
    def testHostModifier_WithoutLockReason(self):
        """Test host locking without providing a lock reason."""
        hosts = set([self.HOST1])
        self.manager.locked_hosts = set([self.HOST2])
        self.mox.StubOutWithMock(self.manager, '_check_host')
        self.manager._check_host(self.HOST1,
                                 self.manager.LOCK).AndReturn(self.HOST1)
        self.afe.run('modify_hosts',
                     host_filter_data={'hostname__in': [self.HOST1]},
                     update_data={'locked': True,
                                  'lock_reason': None})
        self.mox.ReplayAll()
        self.manager._host_modifier(hosts, self.manager.LOCK)
        self.assertEquals(set([self.HOST2]), self.manager.locked_hosts)
 
 
if __name__ == '__main__':
    unittest.main()