blob: 70112527a7103699e064be5028f19d8b5f08eef5 [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#!/usr/bin/python
2#
3# Copyright 2008 Google Inc. All Rights Reserved.
4
5"""Test for host."""
6
Richard Barnette41e617b2016-05-19 16:18:16 -07007# pylint: disable=missing-docstring
8
Justin Giorgid76ed502016-06-21 10:27:52 -07009import sys
10import unittest
mblighbe630eb2008-08-01 16:41:48 +000011
Justin Giorgi5208eaa2016-07-02 20:12:12 -070012import mock
13
mblighbe630eb2008-08-01 16:41:48 +000014import common
15from autotest_lib.cli import cli_mock, host
Aviv Keshet3dd8beb2013-05-13 17:36:04 -070016from autotest_lib.client.common_lib import control_data
Justin Giorgid76ed502016-06-21 10:27:52 -070017from autotest_lib.server import hosts
Aviv Keshet3dd8beb2013-05-13 17:36:04 -070018CLIENT = control_data.CONTROL_TYPE_NAMES.CLIENT
19SERVER = control_data.CONTROL_TYPE_NAMES.SERVER
mblighbe630eb2008-08-01 16:41:48 +000020
21class host_ut(cli_mock.cli_unittest):
22 def test_parse_lock_options_both_set(self):
23 hh = host.host()
24 class opt(object):
25 lock = True
26 unlock = True
27 options = opt()
28 self.usage = "unused"
29 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
30 self.god.mock_io()
31 self.assertRaises(cli_mock.ExitException,
32 hh._parse_lock_options, options)
33 self.god.unmock_io()
34
35
36 def test_cleanup_labels_with_platform(self):
37 labels = ['l0', 'l1', 'l2', 'p0', 'l3']
38 hh = host.host()
39 self.assertEqual(['l0', 'l1', 'l2', 'l3'],
40 hh._cleanup_labels(labels, 'p0'))
41
42
43 def test_cleanup_labels_no_platform(self):
44 labels = ['l0', 'l1', 'l2', 'l3']
45 hh = host.host()
46 self.assertEqual(['l0', 'l1', 'l2', 'l3'],
47 hh._cleanup_labels(labels))
48
49
50 def test_cleanup_labels_with_non_avail_platform(self):
51 labels = ['l0', 'l1', 'l2', 'l3']
52 hh = host.host()
53 self.assertEqual(['l0', 'l1', 'l2', 'l3'],
54 hh._cleanup_labels(labels, 'p0'))
55
56
57class host_list_unittest(cli_mock.cli_unittest):
58 def test_parse_host_not_required(self):
59 hl = host.host_list()
60 sys.argv = ['atest']
61 (options, leftover) = hl.parse()
62 self.assertEqual([], hl.hosts)
63 self.assertEqual([], leftover)
64
65
66 def test_parse_with_hosts(self):
67 hl = host.host_list()
68 mfile = cli_mock.create_file('host0\nhost3\nhost4\n')
mbligh41515392009-07-11 00:13:11 +000069 sys.argv = ['atest', 'host1', '--mlist', mfile.name, 'host3']
mblighbe630eb2008-08-01 16:41:48 +000070 (options, leftover) = hl.parse()
71 self.assertEqualNoOrder(['host0', 'host1','host3', 'host4'],
72 hl.hosts)
73 self.assertEqual(leftover, [])
mbligh41515392009-07-11 00:13:11 +000074 mfile.clean()
mblighbe630eb2008-08-01 16:41:48 +000075
76
77 def test_parse_with_labels(self):
78 hl = host.host_list()
79 sys.argv = ['atest', '--label', 'label0']
80 (options, leftover) = hl.parse()
jamesrenc2863162010-07-12 21:20:51 +000081 self.assertEqual(['label0'], hl.labels)
mbligh6444c6b2008-10-27 20:55:13 +000082 self.assertEqual(leftover, [])
83
84
85 def test_parse_with_multi_labels(self):
86 hl = host.host_list()
87 sys.argv = ['atest', '--label', 'label0,label2']
88 (options, leftover) = hl.parse()
jamesrenc2863162010-07-12 21:20:51 +000089 self.assertEqualNoOrder(['label0', 'label2'], hl.labels)
90 self.assertEqual(leftover, [])
91
92
93 def test_parse_with_escaped_commas_label(self):
94 hl = host.host_list()
95 sys.argv = ['atest', '--label', 'label\\,0']
96 (options, leftover) = hl.parse()
97 self.assertEqual(['label,0'], hl.labels)
98 self.assertEqual(leftover, [])
99
100
101 def test_parse_with_escaped_commas_multi_labels(self):
102 hl = host.host_list()
103 sys.argv = ['atest', '--label', 'label\\,0,label\\,2']
104 (options, leftover) = hl.parse()
105 self.assertEqualNoOrder(['label,0', 'label,2'], hl.labels)
mblighbe630eb2008-08-01 16:41:48 +0000106 self.assertEqual(leftover, [])
107
108
109 def test_parse_with_both(self):
110 hl = host.host_list()
111 mfile = cli_mock.create_file('host0\nhost3\nhost4\n')
mbligh41515392009-07-11 00:13:11 +0000112 sys.argv = ['atest', 'host1', '--mlist', mfile.name, 'host3',
mblighbe630eb2008-08-01 16:41:48 +0000113 '--label', 'label0']
114 (options, leftover) = hl.parse()
115 self.assertEqualNoOrder(['host0', 'host1','host3', 'host4'],
116 hl.hosts)
jamesrenc2863162010-07-12 21:20:51 +0000117 self.assertEqual(['label0'], hl.labels)
mblighbe630eb2008-08-01 16:41:48 +0000118 self.assertEqual(leftover, [])
mbligh41515392009-07-11 00:13:11 +0000119 mfile.clean()
mblighbe630eb2008-08-01 16:41:48 +0000120
121
122 def test_execute_list_all_no_labels(self):
Allen Lib774aa22017-02-01 17:42:15 -0800123 self.run_cmd(argv=['atest', 'host', 'list'],
mblighbe630eb2008-08-01 16:41:48 +0000124 rpcs=[('get_hosts', {},
125 True,
126 [{u'status': u'Ready',
127 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000128 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000129 u'locked_by': 'user0',
130 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700131 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000132 u'labels': [],
mbligh0887d402009-01-30 00:50:29 +0000133 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000134 u'platform': None,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800135 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000136 u'id': 1},
137 {u'status': u'Ready',
138 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000139 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000140 u'locked_by': 'user0',
141 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700142 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000143 u'labels': [u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000144 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000145 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800146 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000147 u'id': 2}])],
148 out_words_ok=['host0', 'host1', 'Ready',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800149 'plat1', 'False', 'True', 'None'])
mblighbe630eb2008-08-01 16:41:48 +0000150
151
152 def test_execute_list_all_with_labels(self):
Allen Lib774aa22017-02-01 17:42:15 -0800153 self.run_cmd(argv=['atest', 'host', 'list'],
mblighbe630eb2008-08-01 16:41:48 +0000154 rpcs=[('get_hosts', {},
155 True,
156 [{u'status': u'Ready',
157 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000158 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000159 u'locked_by': 'user0',
160 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700161 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000162 u'labels': [u'label0', u'label1'],
mbligh0887d402009-01-30 00:50:29 +0000163 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000164 u'platform': None,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800165 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000166 u'id': 1},
167 {u'status': u'Ready',
168 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000169 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000170 u'locked_by': 'user0',
171 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700172 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000173 u'labels': [u'label2', u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000174 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800175 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000176 u'platform': u'plat1',
177 u'id': 2}])],
178 out_words_ok=['host0', 'host1', 'Ready', 'plat1',
179 'label0', 'label1', 'label2', 'label3',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800180 'False', 'True', 'None'])
mblighbe630eb2008-08-01 16:41:48 +0000181
182
183 def test_execute_list_filter_one_host(self):
Allen Lib774aa22017-02-01 17:42:15 -0800184 self.run_cmd(argv=['atest', 'host', 'list', 'host1'],
mblighbe630eb2008-08-01 16:41:48 +0000185 rpcs=[('get_hosts', {'hostname__in': ['host1']},
186 True,
187 [{u'status': u'Ready',
188 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000189 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000190 u'locked_by': 'user0',
191 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700192 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000193 u'labels': [u'label2', u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000194 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000195 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800196 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000197 u'id': 2}])],
198 out_words_ok=['host1', 'Ready', 'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800199 'label2', 'label3', 'True', 'None'],
mblighbe630eb2008-08-01 16:41:48 +0000200 out_words_no=['host0', 'host2',
201 'label1', 'label4', 'False'])
202
203
204 def test_execute_list_filter_two_hosts(self):
205 mfile = cli_mock.create_file('host2')
206 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800207 '--mlist', mfile.name],
mblighbe630eb2008-08-01 16:41:48 +0000208 # This is a bit fragile as the list order may change...
209 rpcs=[('get_hosts', {'hostname__in': ['host2', 'host1']},
210 True,
211 [{u'status': u'Ready',
212 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000213 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000214 u'locked_by': 'user0',
215 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700216 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000217 u'labels': [u'label2', u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000218 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000219 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800220 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000221 u'id': 2},
222 {u'status': u'Ready',
223 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000224 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000225 u'locked_by': 'user0',
226 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700227 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000228 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000229 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800230 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000231 u'platform': u'plat1',
232 u'id': 3}])],
233 out_words_ok=['host1', 'Ready', 'plat1',
234 'label2', 'label3', 'True',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800235 'host2', 'label4', 'None'],
mblighbe630eb2008-08-01 16:41:48 +0000236 out_words_no=['host0', 'label1', 'False'])
mbligh41515392009-07-11 00:13:11 +0000237 mfile.clean()
mblighbe630eb2008-08-01 16:41:48 +0000238
239
240 def test_execute_list_filter_two_hosts_one_not_found(self):
241 mfile = cli_mock.create_file('host2')
242 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800243 '--mlist', mfile.name],
mblighbe630eb2008-08-01 16:41:48 +0000244 # This is a bit fragile as the list order may change...
245 rpcs=[('get_hosts', {'hostname__in': ['host2', 'host1']},
246 True,
247 [{u'status': u'Ready',
248 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000249 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000250 u'locked_by': 'user0',
251 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700252 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000253 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000254 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800255 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000256 u'platform': u'plat1',
257 u'id': 3}])],
258 out_words_ok=['Ready', 'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800259 'label3', 'label4', 'True', 'None'],
mblighbe630eb2008-08-01 16:41:48 +0000260 out_words_no=['host1', 'False'],
261 err_words_ok=['host1'])
mbligh41515392009-07-11 00:13:11 +0000262 mfile.clean()
mblighbe630eb2008-08-01 16:41:48 +0000263
264
265 def test_execute_list_filter_two_hosts_none_found(self):
266 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800267 'host1', 'host2'],
mblighbe630eb2008-08-01 16:41:48 +0000268 # This is a bit fragile as the list order may change...
269 rpcs=[('get_hosts', {'hostname__in': ['host2', 'host1']},
270 True,
271 [])],
jadmanskic79a3982009-01-23 22:29:11 +0000272 out_words_ok=[],
mblighbe630eb2008-08-01 16:41:48 +0000273 out_words_no=['Hostname', 'Status'],
274 err_words_ok=['Unknown', 'host1', 'host2'])
275
276
277 def test_execute_list_filter_label(self):
278 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800279 '-b', 'label3'],
mblighf703fb42009-01-30 00:35:05 +0000280 rpcs=[('get_hosts', {'labels__name__in': ['label3']},
mblighbe630eb2008-08-01 16:41:48 +0000281 True,
282 [{u'status': u'Ready',
283 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000284 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000285 u'locked_by': 'user0',
286 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700287 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000288 u'labels': [u'label2', u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000289 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000290 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800291 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000292 u'id': 2},
293 {u'status': u'Ready',
294 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000295 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000296 u'locked_by': 'user0',
297 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700298 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000299 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000300 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800301 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000302 u'platform': u'plat1',
303 u'id': 3}])],
304 out_words_ok=['host1', 'Ready', 'plat1',
305 'label2', 'label3', 'True',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800306 'host2', 'label4', 'None'],
mblighbe630eb2008-08-01 16:41:48 +0000307 out_words_no=['host0', 'label1', 'False'])
308
309
mbligh6444c6b2008-10-27 20:55:13 +0000310 def test_execute_list_filter_multi_labels(self):
311 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800312 '-b', 'label3,label2'],
jamesrenc2863162010-07-12 21:20:51 +0000313 rpcs=[('get_hosts', {'multiple_labels': ['label2',
314 'label3']},
mbligh6444c6b2008-10-27 20:55:13 +0000315 True,
316 [{u'status': u'Ready',
317 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000318 u'locked': True,
mbligh6444c6b2008-10-27 20:55:13 +0000319 u'locked_by': 'user0',
320 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700321 u'lock_reason': u'',
mbligh6444c6b2008-10-27 20:55:13 +0000322 u'labels': [u'label2', u'label3', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000323 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800324 u'shard': None,
mbligh6444c6b2008-10-27 20:55:13 +0000325 u'platform': u'plat0',
326 u'id': 2},
327 {u'status': u'Ready',
328 u'hostname': u'host3',
mbligh0887d402009-01-30 00:50:29 +0000329 u'locked': True,
mbligh6444c6b2008-10-27 20:55:13 +0000330 u'locked_by': 'user0',
331 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700332 u'lock_reason': u'',
mbligh6444c6b2008-10-27 20:55:13 +0000333 u'labels': [u'label3', u'label2', u'plat2'],
mbligh0887d402009-01-30 00:50:29 +0000334 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800335 u'shard': None,
mbligh6444c6b2008-10-27 20:55:13 +0000336 u'platform': u'plat2',
337 u'id': 4}])],
338 out_words_ok=['host1', 'host3', 'Ready', 'plat0',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800339 'label2', 'label3', 'plat2', 'None'],
mbligh6444c6b2008-10-27 20:55:13 +0000340 out_words_no=['host2', 'label4', 'False', 'plat1'])
341
342
343 def test_execute_list_filter_three_labels(self):
344 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800345 '-b', 'label3,label2, label4'],
jamesrenc2863162010-07-12 21:20:51 +0000346 rpcs=[('get_hosts', {'multiple_labels': ['label2',
347 'label3',
mbligh6444c6b2008-10-27 20:55:13 +0000348 'label4']},
349 True,
350 [{u'status': u'Ready',
351 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000352 u'locked': True,
mbligh6444c6b2008-10-27 20:55:13 +0000353 u'locked_by': 'user0',
354 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700355 u'lock_reason': u'',
mbligh6444c6b2008-10-27 20:55:13 +0000356 u'labels': [u'label3', u'label2', u'label4',
357 u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000358 u'invalid': False,
mbligh6444c6b2008-10-27 20:55:13 +0000359 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800360 u'shard': None,
mbligh6444c6b2008-10-27 20:55:13 +0000361 u'id': 3}])],
362 out_words_ok=['host2', 'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800363 'label2', 'label3', 'label4', 'None'],
mbligh6444c6b2008-10-27 20:55:13 +0000364 out_words_no=['host1', 'host3'])
365
366
mblighf703fb42009-01-30 00:35:05 +0000367 def test_execute_list_filter_wild_labels(self):
368 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800369 '-b', 'label*'],
mblighf703fb42009-01-30 00:35:05 +0000370 rpcs=[('get_hosts',
371 {'labels__name__startswith': 'label'},
372 True,
373 [{u'status': u'Ready',
374 u'hostname': u'host2',
375 u'locked': 1,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800376 u'shard': None,
mblighf703fb42009-01-30 00:35:05 +0000377 u'locked_by': 'user0',
378 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700379 u'lock_reason': u'',
mblighf703fb42009-01-30 00:35:05 +0000380 u'labels': [u'label3', u'label2', u'label4',
381 u'plat1'],
382 u'invalid': 0,
mblighf703fb42009-01-30 00:35:05 +0000383 u'platform': u'plat1',
384 u'id': 3}])],
385 out_words_ok=['host2', 'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800386 'label2', 'label3', 'label4', 'None'],
mblighf703fb42009-01-30 00:35:05 +0000387 out_words_no=['host1', 'host3'])
388
389
mbligh6444c6b2008-10-27 20:55:13 +0000390 def test_execute_list_filter_multi_labels_no_results(self):
391 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800392 '-b', 'label3,label2, '],
jamesrenc2863162010-07-12 21:20:51 +0000393 rpcs=[('get_hosts', {'multiple_labels': ['label2',
394 'label3']},
mbligh6444c6b2008-10-27 20:55:13 +0000395 True,
396 [])],
397 out_words_ok=[],
398 out_words_no=['host1', 'host2', 'host3',
399 'label2', 'label3', 'label4'])
400
mblighbe630eb2008-08-01 16:41:48 +0000401
402 def test_execute_list_filter_label_and_hosts(self):
403 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800404 '-b', 'label3', 'host2'],
mblighf703fb42009-01-30 00:35:05 +0000405 rpcs=[('get_hosts', {'labels__name__in': ['label3'],
mblighbe630eb2008-08-01 16:41:48 +0000406 'hostname__in': ['host2', 'host1']},
407 True,
408 [{u'status': u'Ready',
409 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000410 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000411 u'locked_by': 'user0',
412 u'lock_time': u'2008-07-23 12:54:15',
413 u'labels': [u'label2', u'label3', u'plat1'],
Matthew Sartori68186332015-04-27 17:19:53 -0700414 u'lock_reason': u'',
mbligh0887d402009-01-30 00:50:29 +0000415 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000416 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800417 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000418 u'id': 2},
419 {u'status': u'Ready',
420 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000421 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000422 u'locked_by': 'user0',
423 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700424 u'lock_reason': u'',
mblighbe630eb2008-08-01 16:41:48 +0000425 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000426 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800427 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000428 u'platform': u'plat1',
429 u'id': 3}])],
430 out_words_ok=['host1', 'Ready', 'plat1',
431 'label2', 'label3', 'True',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800432 'host2', 'label4', 'None'],
mblighbe630eb2008-08-01 16:41:48 +0000433 out_words_no=['host0', 'label1', 'False'])
434
435
436 def test_execute_list_filter_label_and_hosts_none(self):
437 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800438 '-b', 'label3', 'host2'],
mblighf703fb42009-01-30 00:35:05 +0000439 rpcs=[('get_hosts', {'labels__name__in': ['label3'],
mblighbe630eb2008-08-01 16:41:48 +0000440 'hostname__in': ['host2', 'host1']},
441 True,
442 [])],
jadmanskic79a3982009-01-23 22:29:11 +0000443 out_words_ok=[],
mblighbe630eb2008-08-01 16:41:48 +0000444 out_words_no=['Hostname', 'Status'],
445 err_words_ok=['Unknown', 'host1', 'host2'])
446
447
mblighcd8eb972008-08-25 19:20:39 +0000448 def test_execute_list_filter_status(self):
449 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800450 '-s', 'Ready'],
mblighcd8eb972008-08-25 19:20:39 +0000451 rpcs=[('get_hosts', {'status__in': ['Ready']},
452 True,
453 [{u'status': u'Ready',
454 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000455 u'locked': True,
mblighcd8eb972008-08-25 19:20:39 +0000456 u'locked_by': 'user0',
457 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700458 u'lock_reason': u'',
mblighcd8eb972008-08-25 19:20:39 +0000459 u'labels': [u'label2', u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000460 u'invalid': False,
mblighcd8eb972008-08-25 19:20:39 +0000461 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800462 u'shard': None,
mblighcd8eb972008-08-25 19:20:39 +0000463 u'id': 2},
464 {u'status': u'Ready',
465 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000466 u'locked': True,
mblighcd8eb972008-08-25 19:20:39 +0000467 u'locked_by': 'user0',
468 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700469 u'lock_reason': u'',
mblighcd8eb972008-08-25 19:20:39 +0000470 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000471 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800472 u'shard': None,
mblighcd8eb972008-08-25 19:20:39 +0000473 u'platform': u'plat1',
474 u'id': 3}])],
475 out_words_ok=['host1', 'Ready', 'plat1',
476 'label2', 'label3', 'True',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800477 'host2', 'label4', 'None'],
mblighcd8eb972008-08-25 19:20:39 +0000478 out_words_no=['host0', 'label1', 'False'])
479
480
481
482 def test_execute_list_filter_status_and_hosts(self):
483 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800484 '-s', 'Ready', 'host2'],
mblighcd8eb972008-08-25 19:20:39 +0000485 rpcs=[('get_hosts', {'status__in': ['Ready'],
486 'hostname__in': ['host2', 'host1']},
487 True,
488 [{u'status': u'Ready',
489 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000490 u'locked': True,
mblighcd8eb972008-08-25 19:20:39 +0000491 u'locked_by': 'user0',
492 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700493 u'lock_reason': u'',
mblighcd8eb972008-08-25 19:20:39 +0000494 u'labels': [u'label2', u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000495 u'invalid': False,
mblighcd8eb972008-08-25 19:20:39 +0000496 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800497 u'shard': None,
mblighcd8eb972008-08-25 19:20:39 +0000498 u'id': 2},
499 {u'status': u'Ready',
500 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000501 u'locked': True,
mblighcd8eb972008-08-25 19:20:39 +0000502 u'locked_by': 'user0',
503 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700504 u'lock_reason': u'',
mblighcd8eb972008-08-25 19:20:39 +0000505 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000506 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800507 u'shard': None,
mblighcd8eb972008-08-25 19:20:39 +0000508 u'platform': u'plat1',
509 u'id': 3}])],
510 out_words_ok=['host1', 'Ready', 'plat1',
511 'label2', 'label3', 'True',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800512 'host2', 'label4', 'None'],
mblighcd8eb972008-08-25 19:20:39 +0000513 out_words_no=['host0', 'label1', 'False'])
514
515
516 def test_execute_list_filter_status_and_hosts_none(self):
517 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
518 '--status', 'Repair',
Allen Lib774aa22017-02-01 17:42:15 -0800519 'host2'],
mblighcd8eb972008-08-25 19:20:39 +0000520 rpcs=[('get_hosts', {'status__in': ['Repair'],
521 'hostname__in': ['host2', 'host1']},
522 True,
523 [])],
jadmanskic79a3982009-01-23 22:29:11 +0000524 out_words_ok=[],
mblighcd8eb972008-08-25 19:20:39 +0000525 out_words_no=['Hostname', 'Status'],
526 err_words_ok=['Unknown', 'host2'])
527
528
mbligh6444c6b2008-10-27 20:55:13 +0000529 def test_execute_list_filter_statuses_and_hosts_none(self):
530 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
mbligh3df90c92009-03-09 21:09:29 +0000531 '--status', 'Repair',
Allen Lib774aa22017-02-01 17:42:15 -0800532 'host2'],
mbligh3df90c92009-03-09 21:09:29 +0000533 rpcs=[('get_hosts', {'status__in': ['Repair'],
mbligh6444c6b2008-10-27 20:55:13 +0000534 'hostname__in': ['host2', 'host1']},
535 True,
536 [])],
jadmanskic79a3982009-01-23 22:29:11 +0000537 out_words_ok=[],
mbligh6444c6b2008-10-27 20:55:13 +0000538 out_words_no=['Hostname', 'Status'],
539 err_words_ok=['Unknown', 'host2'])
540
541
mbligh91e0efd2009-02-26 01:02:16 +0000542 def test_execute_list_filter_locked(self):
543 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800544 '--locked', 'host2'],
mbligh91e0efd2009-02-26 01:02:16 +0000545 rpcs=[('get_hosts', {'locked': True,
546 'hostname__in': ['host2', 'host1']},
547 True,
548 [{u'status': u'Ready',
549 u'hostname': u'host1',
550 u'locked': True,
551 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700552 u'lock_reason': u'',
mbligh91e0efd2009-02-26 01:02:16 +0000553 u'lock_time': u'2008-07-23 12:54:15',
554 u'labels': [u'label2', u'label3', u'plat1'],
555 u'invalid': False,
mbligh91e0efd2009-02-26 01:02:16 +0000556 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800557 u'shard': None,
mbligh91e0efd2009-02-26 01:02:16 +0000558 u'id': 2},
559 {u'status': u'Ready',
560 u'hostname': u'host2',
561 u'locked': True,
562 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700563 u'lock_reason': u'',
mbligh91e0efd2009-02-26 01:02:16 +0000564 u'lock_time': u'2008-07-23 12:54:15',
565 u'labels': [u'label3', u'label4', u'plat1'],
566 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800567 u'shard': None,
mbligh91e0efd2009-02-26 01:02:16 +0000568 u'platform': u'plat1',
569 u'id': 3}])],
570 out_words_ok=['host1', 'Ready', 'plat1',
571 'label2', 'label3', 'True',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800572 'host2', 'label4', 'None'],
mbligh91e0efd2009-02-26 01:02:16 +0000573 out_words_no=['host0', 'label1', 'False'])
574
575
576 def test_execute_list_filter_unlocked(self):
577 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800578 '--unlocked'],
mbligh91e0efd2009-02-26 01:02:16 +0000579 rpcs=[('get_hosts', {'locked': False},
580 True,
581 [{u'status': u'Ready',
582 u'hostname': u'host1',
583 u'locked': False,
584 u'locked_by': 'user0',
585 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700586 u'lock_reason': u'',
mbligh91e0efd2009-02-26 01:02:16 +0000587 u'labels': [u'label2', u'label3', u'plat1'],
588 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800589 u'shard': None,
mbligh91e0efd2009-02-26 01:02:16 +0000590 u'platform': u'plat1',
591 u'id': 2},
592 {u'status': u'Ready',
593 u'hostname': u'host2',
594 u'locked': False,
595 u'locked_by': 'user0',
596 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700597 u'lock_reason': u'',
mbligh91e0efd2009-02-26 01:02:16 +0000598 u'labels': [u'label3', u'label4', u'plat1'],
599 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800600 u'shard': None,
mbligh91e0efd2009-02-26 01:02:16 +0000601 u'platform': u'plat1',
602 u'id': 3}])],
603 out_words_ok=['host1', 'Ready', 'plat1',
604 'label2', 'label3', 'False',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800605 'host2', 'label4', 'None'],
mbligh91e0efd2009-02-26 01:02:16 +0000606 out_words_no=['host0', 'label1', 'True'])
607
608
mblighbe630eb2008-08-01 16:41:48 +0000609class host_stat_unittest(cli_mock.cli_unittest):
610 def test_execute_stat_two_hosts(self):
611 # The order of RPCs between host1 and host0 could change...
Allen Lib774aa22017-02-01 17:42:15 -0800612 self.run_cmd(argv=['atest', 'host', 'stat', 'host0', 'host1'],
mblighbe630eb2008-08-01 16:41:48 +0000613 rpcs=[('get_hosts', {'hostname': 'host1'},
614 True,
615 [{u'status': u'Ready',
616 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000617 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000618 u'lock_time': u'2008-07-23 12:54:15',
619 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700620 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000621 u'protection': 'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000622 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000623 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800624 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000625 u'platform': u'plat1',
Simran Basi0739d682015-02-25 16:22:56 -0800626 u'id': 3,
627 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000628 ('get_hosts', {'hostname': 'host0'},
629 True,
630 [{u'status': u'Ready',
631 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000632 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000633 u'locked_by': 'user0',
634 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700635 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000636 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000637 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000638 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800639 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000640 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800641 u'id': 2,
642 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000643 ('get_acl_groups', {'hosts__hostname': 'host1'},
644 True,
645 [{u'description': u'',
646 u'hosts': [u'host0', u'host1'],
647 u'id': 1,
648 u'name': u'Everyone',
649 u'users': [u'user2', u'debug_user', u'user0']}]),
650 ('get_labels', {'host__hostname': 'host1'},
651 True,
652 [{u'id': 2,
653 u'platform': 1,
654 u'name': u'jme',
mbligh0887d402009-01-30 00:50:29 +0000655 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000656 u'kernel_config': u''}]),
657 ('get_acl_groups', {'hosts__hostname': 'host0'},
658 True,
659 [{u'description': u'',
660 u'hosts': [u'host0', u'host1'],
661 u'id': 1,
662 u'name': u'Everyone',
663 u'users': [u'user0', u'debug_user']},
664 {u'description': u'myacl0',
665 u'hosts': [u'host0'],
666 u'id': 2,
667 u'name': u'acl0',
668 u'users': [u'user0']}]),
669 ('get_labels', {'host__hostname': 'host0'},
670 True,
671 [{u'id': 4,
672 u'platform': 0,
673 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000674 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000675 u'kernel_config': u''},
676 {u'id': 5,
677 u'platform': 1,
678 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000679 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000680 u'kernel_config': u''}])],
681 out_words_ok=['host0', 'host1', 'plat0', 'plat1',
682 'Everyone', 'acl0', 'label0'])
683
684
685 def test_execute_stat_one_bad_host_verbose(self):
686 self.run_cmd(argv=['atest', 'host', 'stat', 'host0',
Allen Lib774aa22017-02-01 17:42:15 -0800687 'host1', '-v'],
mblighbe630eb2008-08-01 16:41:48 +0000688 rpcs=[('get_hosts', {'hostname': 'host1'},
689 True,
690 []),
691 ('get_hosts', {'hostname': 'host0'},
692 True,
693 [{u'status': u'Ready',
694 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000695 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000696 u'locked_by': 'user0',
697 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700698 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000699 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000700 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000701 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000702 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800703 u'id': 2,
704 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000705 ('get_acl_groups', {'hosts__hostname': 'host0'},
706 True,
707 [{u'description': u'',
708 u'hosts': [u'host0', u'host1'],
709 u'id': 1,
710 u'name': u'Everyone',
711 u'users': [u'user0', u'debug_user']},
712 {u'description': u'myacl0',
713 u'hosts': [u'host0'],
714 u'id': 2,
715 u'name': u'acl0',
716 u'users': [u'user0']}]),
717 ('get_labels', {'host__hostname': 'host0'},
718 True,
719 [{u'id': 4,
720 u'platform': 0,
721 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000722 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000723 u'kernel_config': u''},
724 {u'id': 5,
725 u'platform': 1,
726 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000727 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000728 u'kernel_config': u''}])],
729 out_words_ok=['host0', 'plat0',
730 'Everyone', 'acl0', 'label0'],
731 out_words_no=['host1'],
732 err_words_ok=['host1', 'Unknown host'],
733 err_words_no=['host0'])
734
735
736 def test_execute_stat_one_bad_host(self):
Allen Lib774aa22017-02-01 17:42:15 -0800737 self.run_cmd(argv=['atest', 'host', 'stat', 'host0', 'host1'],
mblighbe630eb2008-08-01 16:41:48 +0000738 rpcs=[('get_hosts', {'hostname': 'host1'},
739 True,
740 []),
741 ('get_hosts', {'hostname': 'host0'},
742 True,
743 [{u'status': u'Ready',
744 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000745 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000746 u'locked_by': 'user0',
747 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700748 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000749 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000750 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000751 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000752 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800753 u'id': 2,
754 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000755 ('get_acl_groups', {'hosts__hostname': 'host0'},
756 True,
757 [{u'description': u'',
758 u'hosts': [u'host0', u'host1'],
759 u'id': 1,
760 u'name': u'Everyone',
761 u'users': [u'user0', u'debug_user']},
762 {u'description': u'myacl0',
763 u'hosts': [u'host0'],
764 u'id': 2,
765 u'name': u'acl0',
766 u'users': [u'user0']}]),
767 ('get_labels', {'host__hostname': 'host0'},
768 True,
769 [{u'id': 4,
770 u'platform': 0,
771 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000772 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000773 u'kernel_config': u''},
774 {u'id': 5,
775 u'platform': 1,
776 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000777 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000778 u'kernel_config': u''}])],
779 out_words_ok=['host0', 'plat0',
780 'Everyone', 'acl0', 'label0'],
781 out_words_no=['host1'],
782 err_words_ok=['host1', 'Unknown host'],
783 err_words_no=['host0'])
784
785
786 def test_execute_stat_wildcard(self):
787 # The order of RPCs between host1 and host0 could change...
Allen Lib774aa22017-02-01 17:42:15 -0800788 self.run_cmd(argv=['atest', 'host', 'stat', 'ho*'],
mblighbe630eb2008-08-01 16:41:48 +0000789 rpcs=[('get_hosts', {'hostname__startswith': 'ho'},
790 True,
791 [{u'status': u'Ready',
792 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000793 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000794 u'lock_time': u'2008-07-23 12:54:15',
795 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700796 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000797 u'protection': 'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000798 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000799 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000800 u'platform': u'plat1',
Simran Basi0739d682015-02-25 16:22:56 -0800801 u'id': 3,
802 u'attributes': {}},
mblighbe630eb2008-08-01 16:41:48 +0000803 {u'status': u'Ready',
804 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000805 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000806 u'locked_by': 'user0',
807 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700808 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000809 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000810 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000811 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000812 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800813 u'id': 2,
814 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000815 ('get_acl_groups', {'hosts__hostname': 'host1'},
816 True,
817 [{u'description': u'',
818 u'hosts': [u'host0', u'host1'],
819 u'id': 1,
820 u'name': u'Everyone',
821 u'users': [u'user2', u'debug_user', u'user0']}]),
822 ('get_labels', {'host__hostname': 'host1'},
823 True,
824 [{u'id': 2,
825 u'platform': 1,
826 u'name': u'jme',
mbligh0887d402009-01-30 00:50:29 +0000827 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000828 u'kernel_config': u''}]),
829 ('get_acl_groups', {'hosts__hostname': 'host0'},
830 True,
831 [{u'description': u'',
832 u'hosts': [u'host0', u'host1'],
833 u'id': 1,
834 u'name': u'Everyone',
835 u'users': [u'user0', u'debug_user']},
836 {u'description': u'myacl0',
837 u'hosts': [u'host0'],
838 u'id': 2,
839 u'name': u'acl0',
840 u'users': [u'user0']}]),
841 ('get_labels', {'host__hostname': 'host0'},
842 True,
843 [{u'id': 4,
844 u'platform': 0,
845 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000846 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000847 u'kernel_config': u''},
848 {u'id': 5,
849 u'platform': 1,
850 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000851 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000852 u'kernel_config': u''}])],
853 out_words_ok=['host0', 'host1', 'plat0', 'plat1',
854 'Everyone', 'acl0', 'label0'])
855
856
857 def test_execute_stat_wildcard_and_host(self):
858 # The order of RPCs between host1 and host0 could change...
Allen Lib774aa22017-02-01 17:42:15 -0800859 self.run_cmd(argv=['atest', 'host', 'stat', 'ho*', 'newhost0'],
mblighbe630eb2008-08-01 16:41:48 +0000860 rpcs=[('get_hosts', {'hostname': 'newhost0'},
861 True,
862 [{u'status': u'Ready',
863 u'hostname': u'newhost0',
mbligh0887d402009-01-30 00:50:29 +0000864 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000865 u'locked_by': 'user0',
866 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700867 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000868 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000869 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000870 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000871 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800872 u'id': 5,
873 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000874 ('get_hosts', {'hostname__startswith': 'ho'},
875 True,
876 [{u'status': u'Ready',
877 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000878 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000879 u'lock_time': u'2008-07-23 12:54:15',
880 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700881 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000882 u'protection': 'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000883 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000884 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000885 u'platform': u'plat1',
Simran Basi0739d682015-02-25 16:22:56 -0800886 u'id': 3,
887 u'attributes': {}},
mblighbe630eb2008-08-01 16:41:48 +0000888 {u'status': u'Ready',
889 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000890 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000891 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700892 u'lock_reason': u'',
mbligh536a5242008-10-18 14:35:54 +0000893 u'protection': 'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000894 u'lock_time': u'2008-07-23 12:54:15',
895 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000896 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000897 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800898 u'id': 2,
899 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000900 ('get_acl_groups', {'hosts__hostname': 'newhost0'},
901 True,
902 [{u'description': u'',
903 u'hosts': [u'newhost0', 'host1'],
904 u'id': 42,
905 u'name': u'my_acl',
906 u'users': [u'user0', u'debug_user']},
907 {u'description': u'my favorite acl',
908 u'hosts': [u'newhost0'],
909 u'id': 2,
910 u'name': u'acl10',
911 u'users': [u'user0']}]),
912 ('get_labels', {'host__hostname': 'newhost0'},
913 True,
914 [{u'id': 4,
915 u'platform': 0,
916 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000917 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000918 u'kernel_config': u''},
919 {u'id': 5,
920 u'platform': 1,
921 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000922 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000923 u'kernel_config': u''}]),
924 ('get_acl_groups', {'hosts__hostname': 'host1'},
925 True,
926 [{u'description': u'',
927 u'hosts': [u'host0', u'host1'],
928 u'id': 1,
929 u'name': u'Everyone',
930 u'users': [u'user2', u'debug_user', u'user0']}]),
931 ('get_labels', {'host__hostname': 'host1'},
932 True,
933 [{u'id': 2,
934 u'platform': 1,
935 u'name': u'jme',
mbligh0887d402009-01-30 00:50:29 +0000936 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000937 u'kernel_config': u''}]),
938 ('get_acl_groups', {'hosts__hostname': 'host0'},
939 True,
940 [{u'description': u'',
941 u'hosts': [u'host0', u'host1'],
942 u'id': 1,
943 u'name': u'Everyone',
944 u'users': [u'user0', u'debug_user']},
945 {u'description': u'myacl0',
946 u'hosts': [u'host0'],
947 u'id': 2,
948 u'name': u'acl0',
949 u'users': [u'user0']}]),
950 ('get_labels', {'host__hostname': 'host0'},
951 True,
952 [{u'id': 4,
953 u'platform': 0,
954 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000955 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000956 u'kernel_config': u''},
957 {u'id': 5,
958 u'platform': 1,
959 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000960 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000961 u'kernel_config': u''}])],
962 out_words_ok=['host0', 'host1', 'newhost0',
963 'plat0', 'plat1',
mblighe163b032008-10-18 14:30:27 +0000964 'Everyone', 'acl10', 'label0'])
mblighbe630eb2008-08-01 16:41:48 +0000965
966
967class host_jobs_unittest(cli_mock.cli_unittest):
968 def test_execute_jobs_one_host(self):
Allen Lib774aa22017-02-01 17:42:15 -0800969 self.run_cmd(argv=['atest', 'host', 'jobs', 'host0'],
mblighbe630eb2008-08-01 16:41:48 +0000970 rpcs=[('get_host_queue_entries',
mbligh1494eca2008-08-13 21:24:22 +0000971 {'host__hostname': 'host0', 'query_limit': 20,
showard6958c722009-09-23 20:03:16 +0000972 'sort_by': ['-job__id']},
mblighbe630eb2008-08-01 16:41:48 +0000973 True,
974 [{u'status': u'Failed',
975 u'complete': 1,
976 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +0000977 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000978 u'locked_by': 'user0',
979 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000980 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -0800981 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +0000982 u'priority': 0,
983 u'meta_host': u'meta0',
984 u'job': {u'control_file':
985 (u"def step_init():\n"
986 "\tjob.next_step([step_test])\n"
mblighbe630eb2008-08-01 16:41:48 +0000987 "def step_test():\n"
988 "\tjob.run_test('kernbench')\n\n"),
989 u'name': u'kernel-smp-2.6.xyz.x86_64',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -0700990 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +0000991 u'synchronizing': None,
992 u'priority': u'Low',
993 u'owner': u'user0',
994 u'created_on': u'2008-01-09 10:45:12',
995 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +0000996 u'id': 216},
997 u'active': 0,
998 u'id': 2981},
999 {u'status': u'Aborted',
1000 u'complete': 1,
1001 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001002 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001003 u'locked_by': 'user0',
1004 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001005 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001006 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001007 u'priority': 0,
1008 u'meta_host': None,
1009 u'job': {u'control_file':
1010 u"job.run_test('sleeptest')\n\n",
1011 u'name': u'testjob',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001012 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001013 u'synchronizing': 0,
1014 u'priority': u'Low',
1015 u'owner': u'user1',
1016 u'created_on': u'2008-01-17 15:04:53',
1017 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001018 u'id': 289},
1019 u'active': 0,
1020 u'id': 3167}])],
1021 out_words_ok=['216', 'user0', 'Failed',
1022 'kernel-smp-2.6.xyz.x86_64', 'Aborted',
1023 '289', 'user1', 'Aborted',
1024 'testjob'])
1025
1026
1027 def test_execute_jobs_wildcard(self):
Allen Lib774aa22017-02-01 17:42:15 -08001028 self.run_cmd(argv=['atest', 'host', 'jobs', 'ho*'],
mblighbe630eb2008-08-01 16:41:48 +00001029 rpcs=[('get_hosts', {'hostname__startswith': 'ho'},
1030 True,
1031 [{u'status': u'Ready',
1032 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +00001033 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001034 u'lock_time': u'2008-07-23 12:54:15',
1035 u'locked_by': 'user0',
1036 u'labels': [u'label3', u'label4', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +00001037 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +00001038 u'platform': u'plat1',
1039 u'id': 3},
1040 {u'status': u'Ready',
1041 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001042 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +00001043 u'locked_by': 'user0',
1044 u'lock_time': u'2008-07-23 12:54:15',
1045 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +00001046 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +00001047 u'platform': u'plat0',
1048 u'id': 2}]),
1049 ('get_host_queue_entries',
mbligh1494eca2008-08-13 21:24:22 +00001050 {'host__hostname': 'host1', 'query_limit': 20,
showard6958c722009-09-23 20:03:16 +00001051 'sort_by': ['-job__id']},
mblighbe630eb2008-08-01 16:41:48 +00001052 True,
1053 [{u'status': u'Failed',
1054 u'complete': 1,
1055 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001056 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001057 u'locked_by': 'user0',
1058 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +00001059 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001060 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001061 u'priority': 0,
1062 u'meta_host': u'meta0',
1063 u'job': {u'control_file':
1064 (u"def step_init():\n"
1065 "\tjob.next_step([step_test])\n"
mblighbe630eb2008-08-01 16:41:48 +00001066 "def step_test():\n"
1067 "\tjob.run_test('kernbench')\n\n"),
1068 u'name': u'kernel-smp-2.6.xyz.x86_64',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001069 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001070 u'synchronizing': None,
1071 u'priority': u'Low',
1072 u'owner': u'user0',
1073 u'created_on': u'2008-01-09 10:45:12',
1074 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001075 u'id': 216},
1076 u'active': 0,
1077 u'id': 2981},
1078 {u'status': u'Aborted',
1079 u'complete': 1,
1080 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001081 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001082 u'locked_by': 'user0',
1083 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +00001084 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001085 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001086 u'priority': 0,
1087 u'meta_host': None,
1088 u'job': {u'control_file':
1089 u"job.run_test('sleeptest')\n\n",
1090 u'name': u'testjob',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001091 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001092 u'synchronizing': 0,
1093 u'priority': u'Low',
1094 u'owner': u'user1',
1095 u'created_on': u'2008-01-17 15:04:53',
1096 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001097 u'id': 289},
1098 u'active': 0,
1099 u'id': 3167}]),
1100 ('get_host_queue_entries',
mbligh1494eca2008-08-13 21:24:22 +00001101 {'host__hostname': 'host0', 'query_limit': 20,
showard6958c722009-09-23 20:03:16 +00001102 'sort_by': ['-job__id']},
mblighbe630eb2008-08-01 16:41:48 +00001103 True,
1104 [{u'status': u'Failed',
1105 u'complete': 1,
1106 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001107 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001108 u'locked_by': 'user0',
1109 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001110 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001111 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001112 u'priority': 0,
1113 u'meta_host': u'meta0',
1114 u'job': {u'control_file':
1115 (u"def step_init():\n"
1116 "\tjob.next_step([step_test])\n"
mblighbe630eb2008-08-01 16:41:48 +00001117 "def step_test():\n"
1118 "\tjob.run_test('kernbench')\n\n"),
1119 u'name': u'kernel-smp-2.6.xyz.x86_64',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001120 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001121 u'synchronizing': None,
1122 u'priority': u'Low',
1123 u'owner': u'user0',
1124 u'created_on': u'2008-01-09 10:45:12',
1125 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001126 u'id': 216},
1127 u'active': 0,
1128 u'id': 2981},
1129 {u'status': u'Aborted',
1130 u'complete': 1,
1131 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001132 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001133 u'locked_by': 'user0',
1134 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001135 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001136 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001137 u'priority': 0,
1138 u'meta_host': None,
1139 u'job': {u'control_file':
1140 u"job.run_test('sleeptest')\n\n",
1141 u'name': u'testjob',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001142 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001143 u'synchronizing': 0,
1144 u'priority': u'Low',
1145 u'owner': u'user1',
1146 u'created_on': u'2008-01-17 15:04:53',
1147 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001148 u'id': 289},
1149 u'active': 0,
1150 u'id': 3167}])],
1151 out_words_ok=['216', 'user0', 'Failed',
1152 'kernel-smp-2.6.xyz.x86_64', 'Aborted',
1153 '289', 'user1', 'Aborted',
1154 'testjob'])
1155
mbligh6996fe82008-08-13 00:32:27 +00001156
1157 def test_execute_jobs_one_host_limit(self):
Allen Lib774aa22017-02-01 17:42:15 -08001158 self.run_cmd(argv=['atest', 'host', 'jobs', 'host0', '-q', '10'],
mbligh6996fe82008-08-13 00:32:27 +00001159 rpcs=[('get_host_queue_entries',
mbligh1494eca2008-08-13 21:24:22 +00001160 {'host__hostname': 'host0', 'query_limit': 10,
showard6958c722009-09-23 20:03:16 +00001161 'sort_by': ['-job__id']},
mbligh6996fe82008-08-13 00:32:27 +00001162 True,
1163 [{u'status': u'Failed',
1164 u'complete': 1,
1165 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001166 u'locked': True,
mbligh6996fe82008-08-13 00:32:27 +00001167 u'locked_by': 'user0',
1168 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001169 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001170 u'id': 3232},
mbligh6996fe82008-08-13 00:32:27 +00001171 u'priority': 0,
1172 u'meta_host': u'meta0',
1173 u'job': {u'control_file':
1174 (u"def step_init():\n"
1175 "\tjob.next_step([step_test])\n"
mbligh6996fe82008-08-13 00:32:27 +00001176 "def step_test():\n"
1177 "\tjob.run_test('kernbench')\n\n"),
1178 u'name': u'kernel-smp-2.6.xyz.x86_64',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001179 u'control_type': CLIENT,
mbligh6996fe82008-08-13 00:32:27 +00001180 u'synchronizing': None,
1181 u'priority': u'Low',
1182 u'owner': u'user0',
1183 u'created_on': u'2008-01-09 10:45:12',
1184 u'synch_count': None,
mbligh6996fe82008-08-13 00:32:27 +00001185 u'id': 216},
1186 u'active': 0,
1187 u'id': 2981},
1188 {u'status': u'Aborted',
1189 u'complete': 1,
1190 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001191 u'locked': True,
mbligh6996fe82008-08-13 00:32:27 +00001192 u'locked_by': 'user0',
1193 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001194 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001195 u'id': 3232},
mbligh6996fe82008-08-13 00:32:27 +00001196 u'priority': 0,
1197 u'meta_host': None,
1198 u'job': {u'control_file':
1199 u"job.run_test('sleeptest')\n\n",
1200 u'name': u'testjob',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001201 u'control_type': CLIENT,
mbligh6996fe82008-08-13 00:32:27 +00001202 u'synchronizing': 0,
1203 u'priority': u'Low',
1204 u'owner': u'user1',
1205 u'created_on': u'2008-01-17 15:04:53',
1206 u'synch_count': None,
mbligh6996fe82008-08-13 00:32:27 +00001207 u'id': 289},
1208 u'active': 0,
1209 u'id': 3167}])],
1210 out_words_ok=['216', 'user0', 'Failed',
1211 'kernel-smp-2.6.xyz.x86_64', 'Aborted',
1212 '289', 'user1', 'Aborted',
1213 'testjob'])
1214
1215
Justin Giorgi16bba562016-06-22 10:42:13 -07001216class host_mod_create_tests(object):
1217
1218 def _gen_attributes_rpcs(self, host, attributes):
1219 """Generate RPCs expected to add attributes to host.
1220
1221 @param host: hostname
1222 @param attributes: dict of attributes
1223
1224 @return: list of rpcs to expect
1225 """
1226 rpcs = []
1227 for attr, val in attributes.iteritems():
1228 rpcs.append(('set_host_attribute',
1229 {
1230 'hostname': host,
1231 'attribute': attr,
1232 'value': val,
1233 },
1234 True, None))
1235 return rpcs
mblighbe630eb2008-08-01 16:41:48 +00001236
1237
Justin Giorgi16bba562016-06-22 10:42:13 -07001238 def _gen_labels_rpcs(self, labels, platform=False, host_id=None):
1239 """Generate RPCS expected to add labels.
1240
1241 @param labels: list of label names
1242 @param platform: labels are platform labels
1243 @param host_id: Host id old labels will be deleted from (if host exists)
1244 """
1245 rpcs = []
1246 if host_id:
1247 rpcs.append(('get_labels', {'host': host_id}, True, []))
1248 for label in labels:
1249 rpcs += [
1250 ('get_labels', {'name': label}, True, []),
1251 ('add_label', {'name': label}, True, None)
1252 ]
1253 if platform:
1254 rpcs[-1][1]['platform'] = True
1255 return rpcs
mblighbe630eb2008-08-01 16:41:48 +00001256
1257
Justin Giorgi16bba562016-06-22 10:42:13 -07001258 def _gen_acls_rpcs(self, hosts, acls, host_ids=[]):
1259 """Generate RPCs expected to add acls.
1260
1261 @param hosts: list of hostnames
1262 @param acls: list of acl names
1263 @param host_ids: List of host_ids if hosts already exist
1264 """
1265 rpcs = []
1266 for host_id in host_ids:
1267 rpcs.append(('get_acl_groups', {'hosts': host_id}, True, []))
1268 for acl in acls:
1269 rpcs.append(('get_acl_groups', {'name': acl}, True, []))
1270 rpcs.append(('add_acl_group', {'name': acl}, True, None))
1271 for acl in acls:
1272 rpcs.append((
1273 'acl_group_add_hosts',
1274 {
1275 'hosts': hosts,
1276 'id': acl,
1277 },
1278 True,
1279 None,
1280 ))
1281 return rpcs
Shuqian Zhao4c0d2902016-01-12 17:03:15 -08001282
1283
Justin Giorgi16bba562016-06-22 10:42:13 -07001284 def test_lock_one_host(self):
1285 """Test locking host / creating host locked."""
1286 lock_reason = 'Because'
1287 rpcs, out = self._gen_expectations(locked=True, lock_reason=lock_reason)
1288 self.run_cmd(argv=self._command_single + ['--lock', '--lock_reason',
1289 lock_reason],
1290 rpcs=rpcs, out_words_ok=out)
Shuqian Zhao4c0d2902016-01-12 17:03:15 -08001291
1292
Justin Giorgi16bba562016-06-22 10:42:13 -07001293 def test_unlock_multiple_hosts(self):
1294 """Test unlocking host / creating host unlocked."""
1295 rpcs, out = self._gen_expectations(hosts=self._hosts, locked=False)
1296 self.run_cmd(argv=self._command_multiple + ['--unlock'], rpcs=rpcs,
1297 out_words_ok=out)
mbligh6eca4602009-01-07 16:48:50 +00001298
1299
Justin Giorgi16bba562016-06-22 10:42:13 -07001300 def test_machine_list(self):
1301 """Test action an machines from machine list file."""
1302 mfile = cli_mock.create_file(','.join(self._hosts))
1303 rpcs, out = self._gen_expectations(hosts=self._hosts, locked=False)
Justin Giorgid76ed502016-06-21 10:27:52 -07001304 try:
Justin Giorgi16bba562016-06-22 10:42:13 -07001305 self.run_cmd(argv=self._command_multiple + ['--unlock'], rpcs=rpcs,
1306 out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001307 finally:
1308 mfile.clean()
mblighbe630eb2008-08-01 16:41:48 +00001309
Justin Giorgi16bba562016-06-22 10:42:13 -07001310
1311 def test_single_attributes(self):
1312 """Test applying one attribute to one host."""
1313 attrs = {'foo': 'bar'}
Justin Giorgi16bba562016-06-22 10:42:13 -07001314 rpcs, out = self._gen_expectations(attributes=attrs)
Justin Giorgi9261f9f2016-07-11 17:48:52 -07001315 self.run_cmd(self._command_single + ['--attribute', 'foo=bar'],
Justin Giorgi16bba562016-06-22 10:42:13 -07001316 rpcs=rpcs, out_words_ok=out)
mblighbe630eb2008-08-01 16:41:48 +00001317
1318
Justin Giorgi9261f9f2016-07-11 17:48:52 -07001319 def test_attributes_comma(self):
1320 """Test setting an attribute with a comma in the value."""
Kevin Cheng6f47a2d2016-06-28 15:36:18 -07001321 attrs = {'foo': 'bar,zip'}
Kevin Cheng6f47a2d2016-06-28 15:36:18 -07001322 rpcs, out = self._gen_expectations(attributes=attrs)
Justin Giorgi9261f9f2016-07-11 17:48:52 -07001323 self.run_cmd(self._command_single + ['--attribute', 'foo=bar,zip'],
1324 rpcs=rpcs, out_words_ok=out)
1325
1326
1327 def test_multiple_attributes_comma(self):
1328 """Test setting attributes when one of the values contains a comma."""
1329 attrs = {'foo': 'bar,zip', 'zang': 'poodle'}
1330 rpcs, out = self._gen_expectations(attributes=attrs)
1331 self.run_cmd(self._command_single + ['--attribute', 'foo=bar,zip',
1332 '--attribute', 'zang=poodle'],
Kevin Cheng6f47a2d2016-06-28 15:36:18 -07001333 rpcs=rpcs, out_words_ok=out)
1334
1335
Justin Giorgi16bba562016-06-22 10:42:13 -07001336 def test_multiple_attributes_multiple_hosts(self):
1337 """Test applying multiple attributes to multiple hosts."""
1338 attrs = {'foo': 'bar', 'baz': 'zip'}
Justin Giorgi16bba562016-06-22 10:42:13 -07001339 rpcs, out = self._gen_expectations(hosts=self._hosts, attributes=attrs)
Justin Giorgi9261f9f2016-07-11 17:48:52 -07001340 self.run_cmd(self._command_multiple + ['--attribute', 'foo=bar',
1341 '--attribute', 'baz=zip'],
Justin Giorgi16bba562016-06-22 10:42:13 -07001342 rpcs=rpcs, out_words_ok=out)
1343
1344
1345 def test_platform(self):
1346 """Test applying platform label."""
1347 rpcs, out = self._gen_expectations(platform='some_platform')
1348 self.run_cmd(argv=self._command_single + ['--platform',
1349 'some_platform'],
1350 rpcs=rpcs, out_words_ok=out)
1351
1352
1353 def test_labels(self):
1354 """Test applying labels."""
1355 labels = ['label0', 'label1']
1356 rpcs, out = self._gen_expectations(labels=labels)
1357 self.run_cmd(argv=self._command_single + ['--labels', ','.join(labels)],
1358 rpcs=rpcs, out_words_ok=out)
1359
1360
1361 def test_labels_from_file(self):
1362 """Test applying labels from file."""
1363 labels = ['label0', 'label1']
1364 rpcs, out = self._gen_expectations(labels=labels)
1365 labelsf = cli_mock.create_file(','.join(labels))
1366 try:
1367 self.run_cmd(argv=self._command_single + ['--blist', labelsf.name],
1368 rpcs=rpcs, out_words_ok=out)
1369 finally:
1370 labelsf.clean()
1371
1372
1373 def test_acls(self):
1374 """Test applying acls."""
1375 acls = ['acl0', 'acl1']
1376 rpcs, out = self._gen_expectations(acls=acls)
1377 self.run_cmd(argv=self._command_single + ['--acls', ','.join(acls)],
1378 rpcs=rpcs, out_words_ok=out)
1379
1380
1381 def test_acls_from_file(self):
1382 """Test applying acls from file."""
1383 acls = ['acl0', 'acl1']
1384 rpcs, out = self._gen_expectations(acls=acls)
1385 aclsf = cli_mock.create_file(','.join(acls))
1386 try:
1387 self.run_cmd(argv=self._command_single + ['-A', aclsf.name],
1388 rpcs=rpcs, out_words_ok=out)
1389 finally:
1390 aclsf.clean()
1391
1392
1393 def test_protection(self):
1394 """Test applying host protection."""
1395 protection = 'Do not repair'
1396 rpcs, out = self._gen_expectations(protection=protection)
1397 self.run_cmd(argv=self._command_single + ['--protection', protection],
1398 rpcs=rpcs,out_words_ok=out)
1399
1400
1401 def test_protection_invalid(self):
1402 """Test invalid protection causes failure."""
1403 protection = 'Invalid protection'
1404 rpcs, out = self._gen_expectations(hosts=[])
1405 self.run_cmd(argv=self._command_single + ['--protection', protection],
1406 exit_code=2, err_words_ok=['invalid', 'choice'] +
1407 protection.split())
1408
1409
1410 def test_complex(self):
1411 """Test applying multiple modifications / creating a complex host."""
1412 lock_reason = 'Because I said so.'
1413 platform = 'some_platform'
1414 labels = ['label0', 'label1']
1415 acls = ['acl0', 'acl1']
1416 protection = 'Do not verify'
1417 labelsf = cli_mock.create_file(labels[1])
1418 aclsf = cli_mock.create_file(acls[1])
1419 cmd_args = ['-l', '-r', lock_reason, '-t', platform, '-b', labels[0],
1420 '-B', labelsf.name, '-a', acls[0], '-A', aclsf.name, '-p',
1421 protection]
1422 rpcs, out = self._gen_expectations(locked=True, lock_reason=lock_reason,
1423 acls=acls, labels=labels,
1424 platform=platform,
1425 protection=protection)
1426
1427 try:
1428 self.run_cmd(argv=self._command_single + cmd_args, rpcs=rpcs,
1429 out_words_ok=out)
1430 finally:
1431 labelsf.clean()
1432 aclsf.clean()
1433
1434
1435class host_mod_unittest(host_mod_create_tests, cli_mock.cli_unittest):
1436 """Tests specific to the mod action and expectation generator for shared
1437 tests.
1438 """
1439 _hosts = ['localhost', '127.0.0.1']
1440 _host_ids = [1, 2]
1441 _command_base = ['atest', 'host', 'mod']
1442 _command_single = _command_base + [_hosts[0]]
1443 _command_multiple = _command_base + _hosts
1444
1445 def _gen_expectations(self, hosts=['localhost'], locked=None,
1446 lock_reason='', force_lock=False, protection=None,
1447 acls=[], labels=[], platform=None, attributes={}):
1448 rpcs = []
1449 out = set()
1450 hosts = hosts[:]
1451 hosts.reverse()
1452
1453 # Genarate result for get_hosts command to include all known hosts
1454 host_dicts = []
1455 for h, h_id in zip(self._hosts, self._host_ids):
1456 host_dicts.append({'hostname': h, 'id': h_id})
1457 rpcs.append(('get_hosts', {'hostname__in': hosts}, True, host_dicts))
1458
1459 # Expect actions only for known hosts
1460 host_ids = []
1461 for host in hosts:
1462 if host not in self._hosts:
1463 continue
1464 host_id = self._host_ids[self._hosts.index(host)]
1465 host_ids.append(host_id)
1466 modify_args = {'id': host}
1467
1468 if locked is not None:
1469 out.add('Locked' if locked else 'Unlocked')
1470 modify_args['locked'] = locked
1471 modify_args['lock_reason'] = lock_reason
1472 if force_lock:
1473 modify_args['force_modify_locking'] = True
1474 if protection:
1475 modify_args['protection'] = protection
1476
1477 if len(modify_args.keys()) > 1:
1478 out.add(host)
1479 rpcs.append(('modify_host', modify_args, True, None))
1480
1481 if labels:
1482 rpcs += self._gen_labels_rpcs(labels, host_id=host_id)
1483 rpcs.append(('host_add_labels', {'id': host, 'labels': labels},
1484 True, None))
1485
1486 if platform:
1487 rpcs += self._gen_labels_rpcs([platform], platform=True,
1488 host_id=host_id)
1489 rpcs.append(('host_add_labels', {'id': host,
1490 'labels': [platform]},
1491 True, None))
1492
1493 rpcs += self._gen_attributes_rpcs(host, attributes)
1494
1495 if acls:
1496 rpcs += self._gen_acls_rpcs(hosts, acls, host_ids=host_ids)
1497
1498 return rpcs, list(out)
1499
1500
1501 def test_mod_force_lock_one_host(self):
1502 """Test mod with forced locking."""
1503 lock_reason = 'Because'
1504 rpcs, out = self._gen_expectations(locked=True, force_lock=True,
1505 lock_reason=lock_reason)
1506 self.run_cmd(argv=self._command_single + [
1507 '--lock', '--force_modify_locking', '--lock_reason',
1508 lock_reason],
1509 rpcs=rpcs, out_words_ok=out)
1510
1511 def test_mod_force_unlock_one_host(self):
1512 """Test mod forced unlocking."""
1513 rpcs, out = self._gen_expectations(locked=False, force_lock=True)
1514 self.run_cmd(argv=self._command_single + ['--unlock',
1515 '--force_modify_locking'],
1516 rpcs=rpcs, out_words_ok=out)
1517
1518 def test_mod_fail_unknown_host(self):
1519 """Test mod fails with unknown host."""
1520 rpcs, out = self._gen_expectations(hosts=['nope'], locked=True)
1521 self.run_cmd(argv=self._command_base + ['nope', '--lock'],
1522 rpcs=rpcs, err_words_ok=['Cannot', 'modify', 'nope'])
1523
1524
1525class host_create_unittest(host_mod_create_tests, cli_mock.cli_unittest):
1526 """Test specific to create action and expectation generator for shared
1527 tests.
1528 """
1529 _hosts = ['localhost', '127.0.0.1']
1530 _command_base = ['atest', 'host', 'create']
1531 _command_single = _command_base + [_hosts[0]]
1532 _command_multiple = _command_base + _hosts
Justin Giorgid76ed502016-06-21 10:27:52 -07001533
Justin Giorgi5208eaa2016-07-02 20:12:12 -07001534
1535 def setUp(self):
1536 """Mock out the create_host method.
1537 """
1538 super(host_create_unittest, self).setUp()
1539 self._orig_create_host = hosts.create_host
1540
1541
1542 def tearDown(self):
1543 """Undo mock.
1544 """
1545 super(host_create_unittest, self).tearDown()
1546 hosts.create_host = self._orig_create_host
1547
1548
Justin Giorgid76ed502016-06-21 10:27:52 -07001549 def _mock_host(self, platform=None, labels=[]):
Justin Giorgi5208eaa2016-07-02 20:12:12 -07001550 """Update the return values of the mocked host object.
mblighbe630eb2008-08-01 16:41:48 +00001551
Justin Giorgi5208eaa2016-07-02 20:12:12 -07001552 @param platform: return value of Host.get_platform()
1553 @param labels: return value of Host.get_labels()
1554 """
1555 mock_host = mock.MagicMock()
1556 mock_host.get_platform.return_value = platform
1557 mock_host.get_labels.return_value = labels
1558 hosts.create_host = mock.MagicMock()
1559 hosts.create_host.return_value = mock_host
mbligh719e14a2008-12-04 01:17:08 +00001560
1561
Justin Giorgi16bba562016-06-22 10:42:13 -07001562 def _gen_expectations(self, hosts=['localhost'], locked=False,
1563 lock_reason=None, platform=None,
1564 discovered_platform=None, labels=[],
1565 discovered_labels=[], acls=[], protection=None,
1566 attributes={}):
Justin Giorgid76ed502016-06-21 10:27:52 -07001567 """Build a list of expected RPC calls based on values to host command.
1568
1569 @param hosts: list of hostname being created (default ['localhost'])
1570 @param locked: end state of host (bool)
1571 @param lock_reason: reason for host to be locked
1572 @param platform: platform label
Justin Giorgi16bba562016-06-22 10:42:13 -07001573 @param discovered_platform: platform discovered automatically by host
Justin Giorgid76ed502016-06-21 10:27:52 -07001574 @param labels: list of host labels (excluding platform)
Justin Giorgi16bba562016-06-22 10:42:13 -07001575 @param discovered_labels: list of labels discovered automatically
Justin Giorgid76ed502016-06-21 10:27:52 -07001576 @param acls: list of host acls
1577 @param protection: host protection level
1578
1579 @return: list of expect rpc calls (each call is (op, args, success,
1580 result))
1581 """
Justin Giorgi16bba562016-06-22 10:42:13 -07001582 hosts = hosts[:]
Justin Giorgid76ed502016-06-21 10:27:52 -07001583 hosts.reverse() # No idea why
1584 lock_reason = lock_reason or 'Forced lock on device creation'
1585 acls = acls or []
Justin Giorgid76ed502016-06-21 10:27:52 -07001586
Justin Giorgi16bba562016-06-22 10:42:13 -07001587 rpcs = []
1588 out = ['Added', 'host'] + hosts
Justin Giorgid76ed502016-06-21 10:27:52 -07001589
Justin Giorgi5208eaa2016-07-02 20:12:12 -07001590 # Mock platform and label detection results
1591 self._mock_host(discovered_platform, discovered_labels)
Justin Giorgid76ed502016-06-21 10:27:52 -07001592
1593 for host in hosts:
1594 add_args = {
1595 'hostname': host,
1596 'status': 'Ready',
1597 'locked': True,
1598 'lock_reason': lock_reason,
1599 }
1600 if protection:
1601 add_args['protection'] = protection
1602 rpcs.append(('add_host', add_args, True, None))
1603
Justin Giorgi16bba562016-06-22 10:42:13 -07001604 rpcs += self._gen_labels_rpcs(labels)
1605 if labels:
1606 rpcs.append(('host_add_labels', {'id': host, 'labels': labels},
1607 True, None))
Justin Giorgid76ed502016-06-21 10:27:52 -07001608
Justin Giorgi16bba562016-06-22 10:42:13 -07001609 if platform:
1610 rpcs += self._gen_labels_rpcs([platform], platform=True)
1611 rpcs.append(('host_add_labels', {'id': host,
1612 'labels': [platform]},
1613 True, None))
Justin Giorgid76ed502016-06-21 10:27:52 -07001614
Justin Giorgi16bba562016-06-22 10:42:13 -07001615 rpcs += self._gen_attributes_rpcs(host, attributes)
1616
1617 rpcs += self._gen_acls_rpcs(hosts, acls)
Justin Giorgid76ed502016-06-21 10:27:52 -07001618
1619 if not locked:
1620 for host in hosts:
1621 rpcs.append((
1622 'modify_host',
1623 {
1624 'id': host,
1625 'locked': False,
1626 'lock_reason': '',
1627 },
1628 True,
1629 None,
1630 ))
Justin Giorgi16bba562016-06-22 10:42:13 -07001631 return rpcs, out
1632
1633 def test_create_no_args(self):
1634 """Test simple creation with to arguments."""
1635 rpcs, out = self._gen_expectations()
1636 self.run_cmd(argv=self._command_single, rpcs=rpcs, out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001637
1638
Justin Giorgi16bba562016-06-22 10:42:13 -07001639 def test_create_with_discovered_platform(self):
1640 """Test discovered platform is used when platform isn't specified."""
1641 rpcs, out = self._gen_expectations(platform='some_platform',
1642 discovered_platform='some_platform')
1643 self.run_cmd(argv=self._command_single, rpcs=rpcs, out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001644
1645
1646 def test_create_specified_platform_overrides_discovered_platform(self):
Justin Giorgi16bba562016-06-22 10:42:13 -07001647 """Test that the specified platform overrides the discovered platform.
1648 """
1649 rpcs, out = self._gen_expectations(platform='some_platform',
1650 discovered_platform='wrong_platform')
1651 self.run_cmd(argv=self._command_single + ['--platform',
1652 'some_platform'],
1653 rpcs=rpcs, out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001654
1655
1656 def test_create_discovered_labels(self):
Justin Giorgi16bba562016-06-22 10:42:13 -07001657 """Test applying automatically discovered labels."""
Justin Giorgid76ed502016-06-21 10:27:52 -07001658 labels = ['label0', 'label1']
Justin Giorgi16bba562016-06-22 10:42:13 -07001659 rpcs, out = self._gen_expectations(labels=labels,
1660 discovered_labels=labels)
1661 self.run_cmd(argv=self._command_single, rpcs=rpcs, out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001662
1663
Justin Giorgid76ed502016-06-21 10:27:52 -07001664 def test_create_specified_discovered_labels_combine(self):
Justin Giorgi16bba562016-06-22 10:42:13 -07001665 """Test applying both discovered and specified labels."""
Justin Giorgid76ed502016-06-21 10:27:52 -07001666 labels = ['label0', 'label1']
Justin Giorgi16bba562016-06-22 10:42:13 -07001667 rpcs, out = self._gen_expectations(labels=labels,
1668 discovered_labels=[labels[0]])
1669 self.run_cmd(argv=self._command_single + ['--labels', labels[1]],
1670 rpcs=rpcs, out_words_ok=out)
jamesrenc2863162010-07-12 21:20:51 +00001671
1672
mblighbe630eb2008-08-01 16:41:48 +00001673if __name__ == '__main__':
1674 unittest.main()