blob: ac666a867eead12408fee241caf73725ff8cc1a5 [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
mblighbe630eb2008-08-01 16:41:48 +00002#
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',
Allen Li204eac12017-01-30 18:31:26 -0800201 'label1', 'False'])
mblighbe630eb2008-08-01 16:41:48 +0000202
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'',
Allen Li204eac12017-01-30 18:31:26 -0800228 u'labels': [u'label3', 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',
Allen Li204eac12017-01-30 18:31:26 -0800235 'host2', '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'',
Allen Li204eac12017-01-30 18:31:26 -0800253 u'labels': [u'label3', 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',
Allen Li204eac12017-01-30 18:31:26 -0800259 'label3', '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'',
Allen Li204eac12017-01-30 18:31:26 -0800299 u'labels': [u'label3', 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',
Allen Li204eac12017-01-30 18:31:26 -0800306 'host2', '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'],
Allen Li204eac12017-01-30 18:31:26 -0800340 out_words_no=['host2', 'False', 'plat1'])
mbligh6444c6b2008-10-27 20:55:13 +0000341
342
343 def test_execute_list_filter_three_labels(self):
344 self.run_cmd(argv=['atest', 'host', 'list',
Allen Li204eac12017-01-30 18:31:26 -0800345 '-b', 'label3,label2'],
jamesrenc2863162010-07-12 21:20:51 +0000346 rpcs=[('get_hosts', {'multiple_labels': ['label2',
Allen Li204eac12017-01-30 18:31:26 -0800347 'label3']},
mbligh6444c6b2008-10-27 20:55:13 +0000348 True,
349 [{u'status': u'Ready',
350 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000351 u'locked': True,
mbligh6444c6b2008-10-27 20:55:13 +0000352 u'locked_by': 'user0',
353 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700354 u'lock_reason': u'',
Allen Li204eac12017-01-30 18:31:26 -0800355 u'labels': [u'label3', u'label2', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000356 u'invalid': False,
mbligh6444c6b2008-10-27 20:55:13 +0000357 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800358 u'shard': None,
mbligh6444c6b2008-10-27 20:55:13 +0000359 u'id': 3}])],
360 out_words_ok=['host2', 'plat1',
Allen Li204eac12017-01-30 18:31:26 -0800361 'label2', 'label3', 'None'],
mbligh6444c6b2008-10-27 20:55:13 +0000362 out_words_no=['host1', 'host3'])
363
364
mblighf703fb42009-01-30 00:35:05 +0000365 def test_execute_list_filter_wild_labels(self):
366 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800367 '-b', 'label*'],
mblighf703fb42009-01-30 00:35:05 +0000368 rpcs=[('get_hosts',
369 {'labels__name__startswith': 'label'},
370 True,
371 [{u'status': u'Ready',
372 u'hostname': u'host2',
373 u'locked': 1,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800374 u'shard': None,
mblighf703fb42009-01-30 00:35:05 +0000375 u'locked_by': 'user0',
376 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700377 u'lock_reason': u'',
Allen Li204eac12017-01-30 18:31:26 -0800378 u'labels': [u'label3', u'label2', u'plat1'],
mblighf703fb42009-01-30 00:35:05 +0000379 u'invalid': 0,
mblighf703fb42009-01-30 00:35:05 +0000380 u'platform': u'plat1',
381 u'id': 3}])],
382 out_words_ok=['host2', 'plat1',
Allen Li204eac12017-01-30 18:31:26 -0800383 'label2', 'label3', 'None'],
mblighf703fb42009-01-30 00:35:05 +0000384 out_words_no=['host1', 'host3'])
385
386
mbligh6444c6b2008-10-27 20:55:13 +0000387 def test_execute_list_filter_multi_labels_no_results(self):
388 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800389 '-b', 'label3,label2, '],
jamesrenc2863162010-07-12 21:20:51 +0000390 rpcs=[('get_hosts', {'multiple_labels': ['label2',
391 'label3']},
mbligh6444c6b2008-10-27 20:55:13 +0000392 True,
393 [])],
394 out_words_ok=[],
395 out_words_no=['host1', 'host2', 'host3',
Allen Li204eac12017-01-30 18:31:26 -0800396 'label2', 'label3'])
mbligh6444c6b2008-10-27 20:55:13 +0000397
mblighbe630eb2008-08-01 16:41:48 +0000398
399 def test_execute_list_filter_label_and_hosts(self):
400 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800401 '-b', 'label3', 'host2'],
mblighf703fb42009-01-30 00:35:05 +0000402 rpcs=[('get_hosts', {'labels__name__in': ['label3'],
mblighbe630eb2008-08-01 16:41:48 +0000403 'hostname__in': ['host2', 'host1']},
404 True,
405 [{u'status': u'Ready',
406 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000407 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000408 u'locked_by': 'user0',
409 u'lock_time': u'2008-07-23 12:54:15',
410 u'labels': [u'label2', u'label3', u'plat1'],
Matthew Sartori68186332015-04-27 17:19:53 -0700411 u'lock_reason': u'',
mbligh0887d402009-01-30 00:50:29 +0000412 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000413 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800414 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000415 u'id': 2},
416 {u'status': u'Ready',
417 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000418 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000419 u'locked_by': 'user0',
420 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700421 u'lock_reason': u'',
Allen Li204eac12017-01-30 18:31:26 -0800422 u'labels': [u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000423 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800424 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000425 u'platform': u'plat1',
426 u'id': 3}])],
427 out_words_ok=['host1', 'Ready', 'plat1',
428 'label2', 'label3', 'True',
Allen Li204eac12017-01-30 18:31:26 -0800429 'host2', 'None'],
mblighbe630eb2008-08-01 16:41:48 +0000430 out_words_no=['host0', 'label1', 'False'])
431
432
433 def test_execute_list_filter_label_and_hosts_none(self):
434 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800435 '-b', 'label3', 'host2'],
mblighf703fb42009-01-30 00:35:05 +0000436 rpcs=[('get_hosts', {'labels__name__in': ['label3'],
mblighbe630eb2008-08-01 16:41:48 +0000437 'hostname__in': ['host2', 'host1']},
438 True,
439 [])],
jadmanskic79a3982009-01-23 22:29:11 +0000440 out_words_ok=[],
mblighbe630eb2008-08-01 16:41:48 +0000441 out_words_no=['Hostname', 'Status'],
442 err_words_ok=['Unknown', 'host1', 'host2'])
443
444
mblighcd8eb972008-08-25 19:20:39 +0000445 def test_execute_list_filter_status(self):
446 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800447 '-s', 'Ready'],
mblighcd8eb972008-08-25 19:20:39 +0000448 rpcs=[('get_hosts', {'status__in': ['Ready']},
449 True,
450 [{u'status': u'Ready',
451 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000452 u'locked': True,
mblighcd8eb972008-08-25 19:20:39 +0000453 u'locked_by': 'user0',
454 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700455 u'lock_reason': u'',
mblighcd8eb972008-08-25 19:20:39 +0000456 u'labels': [u'label2', u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000457 u'invalid': False,
mblighcd8eb972008-08-25 19:20:39 +0000458 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800459 u'shard': None,
mblighcd8eb972008-08-25 19:20:39 +0000460 u'id': 2},
461 {u'status': u'Ready',
462 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000463 u'locked': True,
mblighcd8eb972008-08-25 19:20:39 +0000464 u'locked_by': 'user0',
465 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700466 u'lock_reason': u'',
Allen Li204eac12017-01-30 18:31:26 -0800467 u'labels': [u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000468 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800469 u'shard': None,
mblighcd8eb972008-08-25 19:20:39 +0000470 u'platform': u'plat1',
471 u'id': 3}])],
472 out_words_ok=['host1', 'Ready', 'plat1',
473 'label2', 'label3', 'True',
Allen Li204eac12017-01-30 18:31:26 -0800474 'host2', 'None'],
mblighcd8eb972008-08-25 19:20:39 +0000475 out_words_no=['host0', 'label1', 'False'])
476
477
478
479 def test_execute_list_filter_status_and_hosts(self):
480 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800481 '-s', 'Ready', 'host2'],
mblighcd8eb972008-08-25 19:20:39 +0000482 rpcs=[('get_hosts', {'status__in': ['Ready'],
483 'hostname__in': ['host2', 'host1']},
484 True,
485 [{u'status': u'Ready',
486 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000487 u'locked': True,
mblighcd8eb972008-08-25 19:20:39 +0000488 u'locked_by': 'user0',
489 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700490 u'lock_reason': u'',
mblighcd8eb972008-08-25 19:20:39 +0000491 u'labels': [u'label2', u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000492 u'invalid': False,
mblighcd8eb972008-08-25 19:20:39 +0000493 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800494 u'shard': None,
mblighcd8eb972008-08-25 19:20:39 +0000495 u'id': 2},
496 {u'status': u'Ready',
497 u'hostname': u'host2',
mbligh0887d402009-01-30 00:50:29 +0000498 u'locked': True,
mblighcd8eb972008-08-25 19:20:39 +0000499 u'locked_by': 'user0',
500 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700501 u'lock_reason': u'',
Allen Li204eac12017-01-30 18:31:26 -0800502 u'labels': [u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000503 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800504 u'shard': None,
mblighcd8eb972008-08-25 19:20:39 +0000505 u'platform': u'plat1',
506 u'id': 3}])],
507 out_words_ok=['host1', 'Ready', 'plat1',
508 'label2', 'label3', 'True',
Allen Li204eac12017-01-30 18:31:26 -0800509 'host2', 'None'],
mblighcd8eb972008-08-25 19:20:39 +0000510 out_words_no=['host0', 'label1', 'False'])
511
512
513 def test_execute_list_filter_status_and_hosts_none(self):
514 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
515 '--status', 'Repair',
Allen Lib774aa22017-02-01 17:42:15 -0800516 'host2'],
mblighcd8eb972008-08-25 19:20:39 +0000517 rpcs=[('get_hosts', {'status__in': ['Repair'],
518 'hostname__in': ['host2', 'host1']},
519 True,
520 [])],
jadmanskic79a3982009-01-23 22:29:11 +0000521 out_words_ok=[],
mblighcd8eb972008-08-25 19:20:39 +0000522 out_words_no=['Hostname', 'Status'],
523 err_words_ok=['Unknown', 'host2'])
524
525
mbligh6444c6b2008-10-27 20:55:13 +0000526 def test_execute_list_filter_statuses_and_hosts_none(self):
527 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
mbligh3df90c92009-03-09 21:09:29 +0000528 '--status', 'Repair',
Allen Lib774aa22017-02-01 17:42:15 -0800529 'host2'],
mbligh3df90c92009-03-09 21:09:29 +0000530 rpcs=[('get_hosts', {'status__in': ['Repair'],
mbligh6444c6b2008-10-27 20:55:13 +0000531 'hostname__in': ['host2', 'host1']},
532 True,
533 [])],
jadmanskic79a3982009-01-23 22:29:11 +0000534 out_words_ok=[],
mbligh6444c6b2008-10-27 20:55:13 +0000535 out_words_no=['Hostname', 'Status'],
536 err_words_ok=['Unknown', 'host2'])
537
538
mbligh91e0efd2009-02-26 01:02:16 +0000539 def test_execute_list_filter_locked(self):
540 self.run_cmd(argv=['atest', 'host', 'list', 'host1',
Allen Lib774aa22017-02-01 17:42:15 -0800541 '--locked', 'host2'],
mbligh91e0efd2009-02-26 01:02:16 +0000542 rpcs=[('get_hosts', {'locked': True,
543 'hostname__in': ['host2', 'host1']},
544 True,
545 [{u'status': u'Ready',
546 u'hostname': u'host1',
547 u'locked': True,
548 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700549 u'lock_reason': u'',
mbligh91e0efd2009-02-26 01:02:16 +0000550 u'lock_time': u'2008-07-23 12:54:15',
551 u'labels': [u'label2', u'label3', u'plat1'],
552 u'invalid': False,
mbligh91e0efd2009-02-26 01:02:16 +0000553 u'platform': u'plat1',
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800554 u'shard': None,
mbligh91e0efd2009-02-26 01:02:16 +0000555 u'id': 2},
556 {u'status': u'Ready',
557 u'hostname': u'host2',
558 u'locked': True,
559 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700560 u'lock_reason': u'',
mbligh91e0efd2009-02-26 01:02:16 +0000561 u'lock_time': u'2008-07-23 12:54:15',
Allen Li204eac12017-01-30 18:31:26 -0800562 u'labels': [u'label3', u'plat1'],
mbligh91e0efd2009-02-26 01:02:16 +0000563 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800564 u'shard': None,
mbligh91e0efd2009-02-26 01:02:16 +0000565 u'platform': u'plat1',
566 u'id': 3}])],
567 out_words_ok=['host1', 'Ready', 'plat1',
568 'label2', 'label3', 'True',
Allen Li204eac12017-01-30 18:31:26 -0800569 'host2', 'None'],
mbligh91e0efd2009-02-26 01:02:16 +0000570 out_words_no=['host0', 'label1', 'False'])
571
572
573 def test_execute_list_filter_unlocked(self):
574 self.run_cmd(argv=['atest', 'host', 'list',
Allen Lib774aa22017-02-01 17:42:15 -0800575 '--unlocked'],
mbligh91e0efd2009-02-26 01:02:16 +0000576 rpcs=[('get_hosts', {'locked': False},
577 True,
578 [{u'status': u'Ready',
579 u'hostname': u'host1',
580 u'locked': False,
581 u'locked_by': 'user0',
582 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700583 u'lock_reason': u'',
mbligh91e0efd2009-02-26 01:02:16 +0000584 u'labels': [u'label2', u'label3', u'plat1'],
585 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800586 u'shard': None,
mbligh91e0efd2009-02-26 01:02:16 +0000587 u'platform': u'plat1',
588 u'id': 2},
589 {u'status': u'Ready',
590 u'hostname': u'host2',
591 u'locked': False,
592 u'locked_by': 'user0',
593 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700594 u'lock_reason': u'',
Allen Li204eac12017-01-30 18:31:26 -0800595 u'labels': [u'label3', u'plat1'],
mbligh91e0efd2009-02-26 01:02:16 +0000596 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800597 u'shard': None,
mbligh91e0efd2009-02-26 01:02:16 +0000598 u'platform': u'plat1',
599 u'id': 3}])],
600 out_words_ok=['host1', 'Ready', 'plat1',
601 'label2', 'label3', 'False',
Allen Li204eac12017-01-30 18:31:26 -0800602 'host2', 'None'],
mbligh91e0efd2009-02-26 01:02:16 +0000603 out_words_no=['host0', 'label1', 'True'])
604
605
mblighbe630eb2008-08-01 16:41:48 +0000606class host_stat_unittest(cli_mock.cli_unittest):
607 def test_execute_stat_two_hosts(self):
608 # The order of RPCs between host1 and host0 could change...
Allen Lib774aa22017-02-01 17:42:15 -0800609 self.run_cmd(argv=['atest', 'host', 'stat', 'host0', 'host1'],
mblighbe630eb2008-08-01 16:41:48 +0000610 rpcs=[('get_hosts', {'hostname': 'host1'},
611 True,
612 [{u'status': u'Ready',
613 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000614 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000615 u'lock_time': u'2008-07-23 12:54:15',
616 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700617 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000618 u'protection': 'No protection',
Allen Li204eac12017-01-30 18:31:26 -0800619 u'labels': [u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000620 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800621 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000622 u'platform': u'plat1',
Simran Basi0739d682015-02-25 16:22:56 -0800623 u'id': 3,
624 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000625 ('get_hosts', {'hostname': 'host0'},
626 True,
627 [{u'status': u'Ready',
628 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000629 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000630 u'locked_by': 'user0',
631 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700632 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000633 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000634 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000635 u'invalid': False,
Prashanth Balasubramaniana5048562014-12-12 10:14:11 -0800636 u'shard': None,
mblighbe630eb2008-08-01 16:41:48 +0000637 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800638 u'id': 2,
639 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000640 ('get_acl_groups', {'hosts__hostname': 'host1'},
641 True,
642 [{u'description': u'',
643 u'hosts': [u'host0', u'host1'],
644 u'id': 1,
645 u'name': u'Everyone',
646 u'users': [u'user2', u'debug_user', u'user0']}]),
647 ('get_labels', {'host__hostname': 'host1'},
648 True,
649 [{u'id': 2,
650 u'platform': 1,
651 u'name': u'jme',
mbligh0887d402009-01-30 00:50:29 +0000652 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000653 u'kernel_config': u''}]),
654 ('get_acl_groups', {'hosts__hostname': 'host0'},
655 True,
656 [{u'description': u'',
657 u'hosts': [u'host0', u'host1'],
658 u'id': 1,
659 u'name': u'Everyone',
660 u'users': [u'user0', u'debug_user']},
661 {u'description': u'myacl0',
662 u'hosts': [u'host0'],
663 u'id': 2,
664 u'name': u'acl0',
665 u'users': [u'user0']}]),
666 ('get_labels', {'host__hostname': 'host0'},
667 True,
668 [{u'id': 4,
669 u'platform': 0,
670 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000671 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000672 u'kernel_config': u''},
673 {u'id': 5,
674 u'platform': 1,
675 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000676 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000677 u'kernel_config': u''}])],
678 out_words_ok=['host0', 'host1', 'plat0', 'plat1',
679 'Everyone', 'acl0', 'label0'])
680
681
682 def test_execute_stat_one_bad_host_verbose(self):
683 self.run_cmd(argv=['atest', 'host', 'stat', 'host0',
Allen Lib774aa22017-02-01 17:42:15 -0800684 'host1', '-v'],
mblighbe630eb2008-08-01 16:41:48 +0000685 rpcs=[('get_hosts', {'hostname': 'host1'},
686 True,
687 []),
688 ('get_hosts', {'hostname': 'host0'},
689 True,
690 [{u'status': u'Ready',
691 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000692 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000693 u'locked_by': 'user0',
694 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700695 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000696 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000697 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000698 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000699 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800700 u'id': 2,
701 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000702 ('get_acl_groups', {'hosts__hostname': 'host0'},
703 True,
704 [{u'description': u'',
705 u'hosts': [u'host0', u'host1'],
706 u'id': 1,
707 u'name': u'Everyone',
708 u'users': [u'user0', u'debug_user']},
709 {u'description': u'myacl0',
710 u'hosts': [u'host0'],
711 u'id': 2,
712 u'name': u'acl0',
713 u'users': [u'user0']}]),
714 ('get_labels', {'host__hostname': 'host0'},
715 True,
716 [{u'id': 4,
717 u'platform': 0,
718 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000719 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000720 u'kernel_config': u''},
721 {u'id': 5,
722 u'platform': 1,
723 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000724 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000725 u'kernel_config': u''}])],
726 out_words_ok=['host0', 'plat0',
727 'Everyone', 'acl0', 'label0'],
728 out_words_no=['host1'],
729 err_words_ok=['host1', 'Unknown host'],
730 err_words_no=['host0'])
731
732
733 def test_execute_stat_one_bad_host(self):
Allen Lib774aa22017-02-01 17:42:15 -0800734 self.run_cmd(argv=['atest', 'host', 'stat', 'host0', 'host1'],
mblighbe630eb2008-08-01 16:41:48 +0000735 rpcs=[('get_hosts', {'hostname': 'host1'},
736 True,
737 []),
738 ('get_hosts', {'hostname': 'host0'},
739 True,
740 [{u'status': u'Ready',
741 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000742 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000743 u'locked_by': 'user0',
744 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700745 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000746 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000747 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000748 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000749 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800750 u'id': 2,
751 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000752 ('get_acl_groups', {'hosts__hostname': 'host0'},
753 True,
754 [{u'description': u'',
755 u'hosts': [u'host0', u'host1'],
756 u'id': 1,
757 u'name': u'Everyone',
758 u'users': [u'user0', u'debug_user']},
759 {u'description': u'myacl0',
760 u'hosts': [u'host0'],
761 u'id': 2,
762 u'name': u'acl0',
763 u'users': [u'user0']}]),
764 ('get_labels', {'host__hostname': 'host0'},
765 True,
766 [{u'id': 4,
767 u'platform': 0,
768 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000769 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000770 u'kernel_config': u''},
771 {u'id': 5,
772 u'platform': 1,
773 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000774 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000775 u'kernel_config': u''}])],
776 out_words_ok=['host0', 'plat0',
777 'Everyone', 'acl0', 'label0'],
778 out_words_no=['host1'],
779 err_words_ok=['host1', 'Unknown host'],
780 err_words_no=['host0'])
781
782
783 def test_execute_stat_wildcard(self):
784 # The order of RPCs between host1 and host0 could change...
Allen Lib774aa22017-02-01 17:42:15 -0800785 self.run_cmd(argv=['atest', 'host', 'stat', 'ho*'],
mblighbe630eb2008-08-01 16:41:48 +0000786 rpcs=[('get_hosts', {'hostname__startswith': 'ho'},
787 True,
788 [{u'status': u'Ready',
789 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000790 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000791 u'lock_time': u'2008-07-23 12:54:15',
792 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700793 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000794 u'protection': 'No protection',
Allen Li204eac12017-01-30 18:31:26 -0800795 u'labels': [u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000796 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000797 u'platform': u'plat1',
Simran Basi0739d682015-02-25 16:22:56 -0800798 u'id': 3,
799 u'attributes': {}},
mblighbe630eb2008-08-01 16:41:48 +0000800 {u'status': u'Ready',
801 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000802 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000803 u'locked_by': 'user0',
804 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700805 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000806 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000807 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000808 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000809 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800810 u'id': 2,
811 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000812 ('get_acl_groups', {'hosts__hostname': 'host1'},
813 True,
814 [{u'description': u'',
815 u'hosts': [u'host0', u'host1'],
816 u'id': 1,
817 u'name': u'Everyone',
818 u'users': [u'user2', u'debug_user', u'user0']}]),
819 ('get_labels', {'host__hostname': 'host1'},
820 True,
821 [{u'id': 2,
822 u'platform': 1,
823 u'name': u'jme',
mbligh0887d402009-01-30 00:50:29 +0000824 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000825 u'kernel_config': u''}]),
826 ('get_acl_groups', {'hosts__hostname': 'host0'},
827 True,
828 [{u'description': u'',
829 u'hosts': [u'host0', u'host1'],
830 u'id': 1,
831 u'name': u'Everyone',
832 u'users': [u'user0', u'debug_user']},
833 {u'description': u'myacl0',
834 u'hosts': [u'host0'],
835 u'id': 2,
836 u'name': u'acl0',
837 u'users': [u'user0']}]),
838 ('get_labels', {'host__hostname': 'host0'},
839 True,
840 [{u'id': 4,
841 u'platform': 0,
842 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000843 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000844 u'kernel_config': u''},
845 {u'id': 5,
846 u'platform': 1,
847 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000848 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000849 u'kernel_config': u''}])],
850 out_words_ok=['host0', 'host1', 'plat0', 'plat1',
851 'Everyone', 'acl0', 'label0'])
852
853
854 def test_execute_stat_wildcard_and_host(self):
855 # The order of RPCs between host1 and host0 could change...
Allen Lib774aa22017-02-01 17:42:15 -0800856 self.run_cmd(argv=['atest', 'host', 'stat', 'ho*', 'newhost0'],
mblighbe630eb2008-08-01 16:41:48 +0000857 rpcs=[('get_hosts', {'hostname': 'newhost0'},
858 True,
859 [{u'status': u'Ready',
860 u'hostname': u'newhost0',
mbligh0887d402009-01-30 00:50:29 +0000861 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000862 u'locked_by': 'user0',
863 u'lock_time': u'2008-07-23 12:54:15',
Matthew Sartori68186332015-04-27 17:19:53 -0700864 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000865 u'protection': u'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000866 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000867 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000868 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800869 u'id': 5,
870 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000871 ('get_hosts', {'hostname__startswith': 'ho'},
872 True,
873 [{u'status': u'Ready',
874 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +0000875 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000876 u'lock_time': u'2008-07-23 12:54:15',
877 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700878 u'lock_reason': u'',
mblighe163b032008-10-18 14:30:27 +0000879 u'protection': 'No protection',
Allen Li204eac12017-01-30 18:31:26 -0800880 u'labels': [u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +0000881 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000882 u'platform': u'plat1',
Simran Basi0739d682015-02-25 16:22:56 -0800883 u'id': 3,
884 u'attributes': {}},
mblighbe630eb2008-08-01 16:41:48 +0000885 {u'status': u'Ready',
886 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000887 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +0000888 u'locked_by': 'user0',
Matthew Sartori68186332015-04-27 17:19:53 -0700889 u'lock_reason': u'',
mbligh536a5242008-10-18 14:35:54 +0000890 u'protection': 'No protection',
mblighbe630eb2008-08-01 16:41:48 +0000891 u'lock_time': u'2008-07-23 12:54:15',
892 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +0000893 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000894 u'platform': u'plat0',
Simran Basi0739d682015-02-25 16:22:56 -0800895 u'id': 2,
896 u'attributes': {}}]),
mblighbe630eb2008-08-01 16:41:48 +0000897 ('get_acl_groups', {'hosts__hostname': 'newhost0'},
898 True,
899 [{u'description': u'',
900 u'hosts': [u'newhost0', 'host1'],
901 u'id': 42,
902 u'name': u'my_acl',
903 u'users': [u'user0', u'debug_user']},
904 {u'description': u'my favorite acl',
905 u'hosts': [u'newhost0'],
906 u'id': 2,
907 u'name': u'acl10',
908 u'users': [u'user0']}]),
909 ('get_labels', {'host__hostname': 'newhost0'},
910 True,
911 [{u'id': 4,
912 u'platform': 0,
913 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000914 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000915 u'kernel_config': u''},
916 {u'id': 5,
917 u'platform': 1,
918 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000919 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000920 u'kernel_config': u''}]),
921 ('get_acl_groups', {'hosts__hostname': 'host1'},
922 True,
923 [{u'description': u'',
924 u'hosts': [u'host0', u'host1'],
925 u'id': 1,
926 u'name': u'Everyone',
927 u'users': [u'user2', u'debug_user', u'user0']}]),
928 ('get_labels', {'host__hostname': 'host1'},
929 True,
930 [{u'id': 2,
931 u'platform': 1,
932 u'name': u'jme',
mbligh0887d402009-01-30 00:50:29 +0000933 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000934 u'kernel_config': u''}]),
935 ('get_acl_groups', {'hosts__hostname': 'host0'},
936 True,
937 [{u'description': u'',
938 u'hosts': [u'host0', u'host1'],
939 u'id': 1,
940 u'name': u'Everyone',
941 u'users': [u'user0', u'debug_user']},
942 {u'description': u'myacl0',
943 u'hosts': [u'host0'],
944 u'id': 2,
945 u'name': u'acl0',
946 u'users': [u'user0']}]),
947 ('get_labels', {'host__hostname': 'host0'},
948 True,
949 [{u'id': 4,
950 u'platform': 0,
951 u'name': u'label0',
mbligh0887d402009-01-30 00:50:29 +0000952 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000953 u'kernel_config': u''},
954 {u'id': 5,
955 u'platform': 1,
956 u'name': u'plat0',
mbligh0887d402009-01-30 00:50:29 +0000957 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +0000958 u'kernel_config': u''}])],
959 out_words_ok=['host0', 'host1', 'newhost0',
960 'plat0', 'plat1',
mblighe163b032008-10-18 14:30:27 +0000961 'Everyone', 'acl10', 'label0'])
mblighbe630eb2008-08-01 16:41:48 +0000962
963
964class host_jobs_unittest(cli_mock.cli_unittest):
965 def test_execute_jobs_one_host(self):
Allen Lib774aa22017-02-01 17:42:15 -0800966 self.run_cmd(argv=['atest', 'host', 'jobs', 'host0'],
mblighbe630eb2008-08-01 16:41:48 +0000967 rpcs=[('get_host_queue_entries',
mbligh1494eca2008-08-13 21:24:22 +0000968 {'host__hostname': 'host0', 'query_limit': 20,
showard6958c722009-09-23 20:03:16 +0000969 'sort_by': ['-job__id']},
mblighbe630eb2008-08-01 16:41:48 +0000970 True,
971 [{u'status': u'Failed',
972 u'complete': 1,
973 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +0000974 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +0000975 u'locked_by': 'user0',
976 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +0000977 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -0800978 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +0000979 u'priority': 0,
980 u'meta_host': u'meta0',
981 u'job': {u'control_file':
982 (u"def step_init():\n"
983 "\tjob.next_step([step_test])\n"
mblighbe630eb2008-08-01 16:41:48 +0000984 "def step_test():\n"
985 "\tjob.run_test('kernbench')\n\n"),
986 u'name': u'kernel-smp-2.6.xyz.x86_64',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -0700987 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +0000988 u'synchronizing': None,
989 u'priority': u'Low',
990 u'owner': u'user0',
991 u'created_on': u'2008-01-09 10:45:12',
992 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +0000993 u'id': 216},
994 u'active': 0,
995 u'id': 2981},
996 {u'status': u'Aborted',
997 u'complete': 1,
998 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +0000999 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001000 u'locked_by': 'user0',
1001 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001002 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001003 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001004 u'priority': 0,
1005 u'meta_host': None,
1006 u'job': {u'control_file':
1007 u"job.run_test('sleeptest')\n\n",
1008 u'name': u'testjob',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001009 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001010 u'synchronizing': 0,
1011 u'priority': u'Low',
1012 u'owner': u'user1',
1013 u'created_on': u'2008-01-17 15:04:53',
1014 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001015 u'id': 289},
1016 u'active': 0,
1017 u'id': 3167}])],
1018 out_words_ok=['216', 'user0', 'Failed',
1019 'kernel-smp-2.6.xyz.x86_64', 'Aborted',
1020 '289', 'user1', 'Aborted',
1021 'testjob'])
1022
1023
1024 def test_execute_jobs_wildcard(self):
Allen Lib774aa22017-02-01 17:42:15 -08001025 self.run_cmd(argv=['atest', 'host', 'jobs', 'ho*'],
mblighbe630eb2008-08-01 16:41:48 +00001026 rpcs=[('get_hosts', {'hostname__startswith': 'ho'},
1027 True,
1028 [{u'status': u'Ready',
1029 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +00001030 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001031 u'lock_time': u'2008-07-23 12:54:15',
1032 u'locked_by': 'user0',
Allen Li204eac12017-01-30 18:31:26 -08001033 u'labels': [u'label3', u'plat1'],
mbligh0887d402009-01-30 00:50:29 +00001034 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +00001035 u'platform': u'plat1',
1036 u'id': 3},
1037 {u'status': u'Ready',
1038 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001039 u'locked': False,
mblighbe630eb2008-08-01 16:41:48 +00001040 u'locked_by': 'user0',
1041 u'lock_time': u'2008-07-23 12:54:15',
1042 u'labels': [u'label0', u'plat0'],
mbligh0887d402009-01-30 00:50:29 +00001043 u'invalid': False,
mblighbe630eb2008-08-01 16:41:48 +00001044 u'platform': u'plat0',
1045 u'id': 2}]),
1046 ('get_host_queue_entries',
mbligh1494eca2008-08-13 21:24:22 +00001047 {'host__hostname': 'host1', 'query_limit': 20,
showard6958c722009-09-23 20:03:16 +00001048 'sort_by': ['-job__id']},
mblighbe630eb2008-08-01 16:41:48 +00001049 True,
1050 [{u'status': u'Failed',
1051 u'complete': 1,
1052 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001053 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001054 u'locked_by': 'user0',
1055 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +00001056 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001057 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001058 u'priority': 0,
1059 u'meta_host': u'meta0',
1060 u'job': {u'control_file':
1061 (u"def step_init():\n"
1062 "\tjob.next_step([step_test])\n"
mblighbe630eb2008-08-01 16:41:48 +00001063 "def step_test():\n"
1064 "\tjob.run_test('kernbench')\n\n"),
1065 u'name': u'kernel-smp-2.6.xyz.x86_64',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001066 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001067 u'synchronizing': None,
1068 u'priority': u'Low',
1069 u'owner': u'user0',
1070 u'created_on': u'2008-01-09 10:45:12',
1071 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001072 u'id': 216},
1073 u'active': 0,
1074 u'id': 2981},
1075 {u'status': u'Aborted',
1076 u'complete': 1,
1077 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001078 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001079 u'locked_by': 'user0',
1080 u'hostname': u'host1',
mbligh0887d402009-01-30 00:50:29 +00001081 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001082 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001083 u'priority': 0,
1084 u'meta_host': None,
1085 u'job': {u'control_file':
1086 u"job.run_test('sleeptest')\n\n",
1087 u'name': u'testjob',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001088 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001089 u'synchronizing': 0,
1090 u'priority': u'Low',
1091 u'owner': u'user1',
1092 u'created_on': u'2008-01-17 15:04:53',
1093 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001094 u'id': 289},
1095 u'active': 0,
1096 u'id': 3167}]),
1097 ('get_host_queue_entries',
mbligh1494eca2008-08-13 21:24:22 +00001098 {'host__hostname': 'host0', 'query_limit': 20,
showard6958c722009-09-23 20:03:16 +00001099 'sort_by': ['-job__id']},
mblighbe630eb2008-08-01 16:41:48 +00001100 True,
1101 [{u'status': u'Failed',
1102 u'complete': 1,
1103 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001104 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001105 u'locked_by': 'user0',
1106 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001107 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001108 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001109 u'priority': 0,
1110 u'meta_host': u'meta0',
1111 u'job': {u'control_file':
1112 (u"def step_init():\n"
1113 "\tjob.next_step([step_test])\n"
mblighbe630eb2008-08-01 16:41:48 +00001114 "def step_test():\n"
1115 "\tjob.run_test('kernbench')\n\n"),
1116 u'name': u'kernel-smp-2.6.xyz.x86_64',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001117 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001118 u'synchronizing': None,
1119 u'priority': u'Low',
1120 u'owner': u'user0',
1121 u'created_on': u'2008-01-09 10:45:12',
1122 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001123 u'id': 216},
1124 u'active': 0,
1125 u'id': 2981},
1126 {u'status': u'Aborted',
1127 u'complete': 1,
1128 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001129 u'locked': True,
mblighbe630eb2008-08-01 16:41:48 +00001130 u'locked_by': 'user0',
1131 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001132 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001133 u'id': 3232},
mblighbe630eb2008-08-01 16:41:48 +00001134 u'priority': 0,
1135 u'meta_host': None,
1136 u'job': {u'control_file':
1137 u"job.run_test('sleeptest')\n\n",
1138 u'name': u'testjob',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001139 u'control_type': CLIENT,
mblighbe630eb2008-08-01 16:41:48 +00001140 u'synchronizing': 0,
1141 u'priority': u'Low',
1142 u'owner': u'user1',
1143 u'created_on': u'2008-01-17 15:04:53',
1144 u'synch_count': None,
mblighbe630eb2008-08-01 16:41:48 +00001145 u'id': 289},
1146 u'active': 0,
1147 u'id': 3167}])],
1148 out_words_ok=['216', 'user0', 'Failed',
1149 'kernel-smp-2.6.xyz.x86_64', 'Aborted',
1150 '289', 'user1', 'Aborted',
1151 'testjob'])
1152
mbligh6996fe82008-08-13 00:32:27 +00001153
1154 def test_execute_jobs_one_host_limit(self):
Allen Lib774aa22017-02-01 17:42:15 -08001155 self.run_cmd(argv=['atest', 'host', 'jobs', 'host0', '-q', '10'],
mbligh6996fe82008-08-13 00:32:27 +00001156 rpcs=[('get_host_queue_entries',
mbligh1494eca2008-08-13 21:24:22 +00001157 {'host__hostname': 'host0', 'query_limit': 10,
showard6958c722009-09-23 20:03:16 +00001158 'sort_by': ['-job__id']},
mbligh6996fe82008-08-13 00:32:27 +00001159 True,
1160 [{u'status': u'Failed',
1161 u'complete': 1,
1162 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001163 u'locked': True,
mbligh6996fe82008-08-13 00:32:27 +00001164 u'locked_by': 'user0',
1165 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001166 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001167 u'id': 3232},
mbligh6996fe82008-08-13 00:32:27 +00001168 u'priority': 0,
1169 u'meta_host': u'meta0',
1170 u'job': {u'control_file':
1171 (u"def step_init():\n"
1172 "\tjob.next_step([step_test])\n"
mbligh6996fe82008-08-13 00:32:27 +00001173 "def step_test():\n"
1174 "\tjob.run_test('kernbench')\n\n"),
1175 u'name': u'kernel-smp-2.6.xyz.x86_64',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001176 u'control_type': CLIENT,
mbligh6996fe82008-08-13 00:32:27 +00001177 u'synchronizing': None,
1178 u'priority': u'Low',
1179 u'owner': u'user0',
1180 u'created_on': u'2008-01-09 10:45:12',
1181 u'synch_count': None,
mbligh6996fe82008-08-13 00:32:27 +00001182 u'id': 216},
1183 u'active': 0,
1184 u'id': 2981},
1185 {u'status': u'Aborted',
1186 u'complete': 1,
1187 u'host': {u'status': u'Ready',
mbligh0887d402009-01-30 00:50:29 +00001188 u'locked': True,
mbligh6996fe82008-08-13 00:32:27 +00001189 u'locked_by': 'user0',
1190 u'hostname': u'host0',
mbligh0887d402009-01-30 00:50:29 +00001191 u'invalid': False,
Allen Lie4c08272017-02-01 16:40:53 -08001192 u'id': 3232},
mbligh6996fe82008-08-13 00:32:27 +00001193 u'priority': 0,
1194 u'meta_host': None,
1195 u'job': {u'control_file':
1196 u"job.run_test('sleeptest')\n\n",
1197 u'name': u'testjob',
Aviv Keshet3dd8beb2013-05-13 17:36:04 -07001198 u'control_type': CLIENT,
mbligh6996fe82008-08-13 00:32:27 +00001199 u'synchronizing': 0,
1200 u'priority': u'Low',
1201 u'owner': u'user1',
1202 u'created_on': u'2008-01-17 15:04:53',
1203 u'synch_count': None,
mbligh6996fe82008-08-13 00:32:27 +00001204 u'id': 289},
1205 u'active': 0,
1206 u'id': 3167}])],
1207 out_words_ok=['216', 'user0', 'Failed',
1208 'kernel-smp-2.6.xyz.x86_64', 'Aborted',
1209 '289', 'user1', 'Aborted',
1210 'testjob'])
1211
1212
Justin Giorgi16bba562016-06-22 10:42:13 -07001213class host_mod_create_tests(object):
1214
1215 def _gen_attributes_rpcs(self, host, attributes):
1216 """Generate RPCs expected to add attributes to host.
1217
1218 @param host: hostname
1219 @param attributes: dict of attributes
1220
1221 @return: list of rpcs to expect
1222 """
1223 rpcs = []
1224 for attr, val in attributes.iteritems():
1225 rpcs.append(('set_host_attribute',
1226 {
1227 'hostname': host,
1228 'attribute': attr,
1229 'value': val,
1230 },
1231 True, None))
1232 return rpcs
mblighbe630eb2008-08-01 16:41:48 +00001233
1234
Justin Giorgi16bba562016-06-22 10:42:13 -07001235 def _gen_labels_rpcs(self, labels, platform=False, host_id=None):
1236 """Generate RPCS expected to add labels.
1237
1238 @param labels: list of label names
1239 @param platform: labels are platform labels
1240 @param host_id: Host id old labels will be deleted from (if host exists)
1241 """
1242 rpcs = []
1243 if host_id:
1244 rpcs.append(('get_labels', {'host': host_id}, True, []))
1245 for label in labels:
1246 rpcs += [
1247 ('get_labels', {'name': label}, True, []),
1248 ('add_label', {'name': label}, True, None)
1249 ]
1250 if platform:
1251 rpcs[-1][1]['platform'] = True
1252 return rpcs
mblighbe630eb2008-08-01 16:41:48 +00001253
1254
Justin Giorgi16bba562016-06-22 10:42:13 -07001255 def _gen_acls_rpcs(self, hosts, acls, host_ids=[]):
1256 """Generate RPCs expected to add acls.
1257
1258 @param hosts: list of hostnames
1259 @param acls: list of acl names
1260 @param host_ids: List of host_ids if hosts already exist
1261 """
1262 rpcs = []
1263 for host_id in host_ids:
1264 rpcs.append(('get_acl_groups', {'hosts': host_id}, True, []))
1265 for acl in acls:
1266 rpcs.append(('get_acl_groups', {'name': acl}, True, []))
1267 rpcs.append(('add_acl_group', {'name': acl}, True, None))
1268 for acl in acls:
1269 rpcs.append((
1270 'acl_group_add_hosts',
1271 {
1272 'hosts': hosts,
1273 'id': acl,
1274 },
1275 True,
1276 None,
1277 ))
1278 return rpcs
Shuqian Zhao4c0d2902016-01-12 17:03:15 -08001279
1280
Justin Giorgi16bba562016-06-22 10:42:13 -07001281 def test_lock_one_host(self):
1282 """Test locking host / creating host locked."""
1283 lock_reason = 'Because'
1284 rpcs, out = self._gen_expectations(locked=True, lock_reason=lock_reason)
1285 self.run_cmd(argv=self._command_single + ['--lock', '--lock_reason',
1286 lock_reason],
1287 rpcs=rpcs, out_words_ok=out)
Shuqian Zhao4c0d2902016-01-12 17:03:15 -08001288
1289
Justin Giorgi16bba562016-06-22 10:42:13 -07001290 def test_unlock_multiple_hosts(self):
1291 """Test unlocking host / creating host unlocked."""
1292 rpcs, out = self._gen_expectations(hosts=self._hosts, locked=False)
1293 self.run_cmd(argv=self._command_multiple + ['--unlock'], rpcs=rpcs,
1294 out_words_ok=out)
mbligh6eca4602009-01-07 16:48:50 +00001295
1296
Justin Giorgi16bba562016-06-22 10:42:13 -07001297 def test_machine_list(self):
1298 """Test action an machines from machine list file."""
1299 mfile = cli_mock.create_file(','.join(self._hosts))
1300 rpcs, out = self._gen_expectations(hosts=self._hosts, locked=False)
Justin Giorgid76ed502016-06-21 10:27:52 -07001301 try:
Justin Giorgi16bba562016-06-22 10:42:13 -07001302 self.run_cmd(argv=self._command_multiple + ['--unlock'], rpcs=rpcs,
1303 out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001304 finally:
1305 mfile.clean()
mblighbe630eb2008-08-01 16:41:48 +00001306
Justin Giorgi16bba562016-06-22 10:42:13 -07001307
1308 def test_single_attributes(self):
1309 """Test applying one attribute to one host."""
1310 attrs = {'foo': 'bar'}
Justin Giorgi16bba562016-06-22 10:42:13 -07001311 rpcs, out = self._gen_expectations(attributes=attrs)
Justin Giorgi9261f9f2016-07-11 17:48:52 -07001312 self.run_cmd(self._command_single + ['--attribute', 'foo=bar'],
Justin Giorgi16bba562016-06-22 10:42:13 -07001313 rpcs=rpcs, out_words_ok=out)
mblighbe630eb2008-08-01 16:41:48 +00001314
1315
Justin Giorgi9261f9f2016-07-11 17:48:52 -07001316 def test_attributes_comma(self):
1317 """Test setting an attribute with a comma in the value."""
Kevin Cheng6f47a2d2016-06-28 15:36:18 -07001318 attrs = {'foo': 'bar,zip'}
Kevin Cheng6f47a2d2016-06-28 15:36:18 -07001319 rpcs, out = self._gen_expectations(attributes=attrs)
Justin Giorgi9261f9f2016-07-11 17:48:52 -07001320 self.run_cmd(self._command_single + ['--attribute', 'foo=bar,zip'],
1321 rpcs=rpcs, out_words_ok=out)
1322
1323
1324 def test_multiple_attributes_comma(self):
1325 """Test setting attributes when one of the values contains a comma."""
1326 attrs = {'foo': 'bar,zip', 'zang': 'poodle'}
1327 rpcs, out = self._gen_expectations(attributes=attrs)
1328 self.run_cmd(self._command_single + ['--attribute', 'foo=bar,zip',
1329 '--attribute', 'zang=poodle'],
Kevin Cheng6f47a2d2016-06-28 15:36:18 -07001330 rpcs=rpcs, out_words_ok=out)
1331
1332
Justin Giorgi16bba562016-06-22 10:42:13 -07001333 def test_multiple_attributes_multiple_hosts(self):
1334 """Test applying multiple attributes to multiple hosts."""
1335 attrs = {'foo': 'bar', 'baz': 'zip'}
Justin Giorgi16bba562016-06-22 10:42:13 -07001336 rpcs, out = self._gen_expectations(hosts=self._hosts, attributes=attrs)
Justin Giorgi9261f9f2016-07-11 17:48:52 -07001337 self.run_cmd(self._command_multiple + ['--attribute', 'foo=bar',
1338 '--attribute', 'baz=zip'],
Justin Giorgi16bba562016-06-22 10:42:13 -07001339 rpcs=rpcs, out_words_ok=out)
1340
1341
1342 def test_platform(self):
1343 """Test applying platform label."""
1344 rpcs, out = self._gen_expectations(platform='some_platform')
1345 self.run_cmd(argv=self._command_single + ['--platform',
1346 'some_platform'],
1347 rpcs=rpcs, out_words_ok=out)
1348
1349
1350 def test_labels(self):
1351 """Test applying labels."""
1352 labels = ['label0', 'label1']
1353 rpcs, out = self._gen_expectations(labels=labels)
1354 self.run_cmd(argv=self._command_single + ['--labels', ','.join(labels)],
1355 rpcs=rpcs, out_words_ok=out)
1356
1357
1358 def test_labels_from_file(self):
1359 """Test applying labels from file."""
1360 labels = ['label0', 'label1']
1361 rpcs, out = self._gen_expectations(labels=labels)
1362 labelsf = cli_mock.create_file(','.join(labels))
1363 try:
1364 self.run_cmd(argv=self._command_single + ['--blist', labelsf.name],
1365 rpcs=rpcs, out_words_ok=out)
1366 finally:
1367 labelsf.clean()
1368
1369
1370 def test_acls(self):
1371 """Test applying acls."""
1372 acls = ['acl0', 'acl1']
1373 rpcs, out = self._gen_expectations(acls=acls)
1374 self.run_cmd(argv=self._command_single + ['--acls', ','.join(acls)],
1375 rpcs=rpcs, out_words_ok=out)
1376
1377
1378 def test_acls_from_file(self):
1379 """Test applying acls from file."""
1380 acls = ['acl0', 'acl1']
1381 rpcs, out = self._gen_expectations(acls=acls)
1382 aclsf = cli_mock.create_file(','.join(acls))
1383 try:
1384 self.run_cmd(argv=self._command_single + ['-A', aclsf.name],
1385 rpcs=rpcs, out_words_ok=out)
1386 finally:
1387 aclsf.clean()
1388
1389
1390 def test_protection(self):
1391 """Test applying host protection."""
1392 protection = 'Do not repair'
1393 rpcs, out = self._gen_expectations(protection=protection)
1394 self.run_cmd(argv=self._command_single + ['--protection', protection],
1395 rpcs=rpcs,out_words_ok=out)
1396
1397
1398 def test_protection_invalid(self):
1399 """Test invalid protection causes failure."""
1400 protection = 'Invalid protection'
1401 rpcs, out = self._gen_expectations(hosts=[])
1402 self.run_cmd(argv=self._command_single + ['--protection', protection],
1403 exit_code=2, err_words_ok=['invalid', 'choice'] +
1404 protection.split())
1405
1406
1407 def test_complex(self):
1408 """Test applying multiple modifications / creating a complex host."""
1409 lock_reason = 'Because I said so.'
1410 platform = 'some_platform'
1411 labels = ['label0', 'label1']
1412 acls = ['acl0', 'acl1']
1413 protection = 'Do not verify'
1414 labelsf = cli_mock.create_file(labels[1])
1415 aclsf = cli_mock.create_file(acls[1])
1416 cmd_args = ['-l', '-r', lock_reason, '-t', platform, '-b', labels[0],
1417 '-B', labelsf.name, '-a', acls[0], '-A', aclsf.name, '-p',
1418 protection]
1419 rpcs, out = self._gen_expectations(locked=True, lock_reason=lock_reason,
1420 acls=acls, labels=labels,
1421 platform=platform,
1422 protection=protection)
1423
1424 try:
1425 self.run_cmd(argv=self._command_single + cmd_args, rpcs=rpcs,
1426 out_words_ok=out)
1427 finally:
1428 labelsf.clean()
1429 aclsf.clean()
1430
1431
1432class host_mod_unittest(host_mod_create_tests, cli_mock.cli_unittest):
1433 """Tests specific to the mod action and expectation generator for shared
1434 tests.
1435 """
1436 _hosts = ['localhost', '127.0.0.1']
1437 _host_ids = [1, 2]
1438 _command_base = ['atest', 'host', 'mod']
1439 _command_single = _command_base + [_hosts[0]]
1440 _command_multiple = _command_base + _hosts
1441
1442 def _gen_expectations(self, hosts=['localhost'], locked=None,
1443 lock_reason='', force_lock=False, protection=None,
1444 acls=[], labels=[], platform=None, attributes={}):
1445 rpcs = []
1446 out = set()
1447 hosts = hosts[:]
1448 hosts.reverse()
1449
1450 # Genarate result for get_hosts command to include all known hosts
1451 host_dicts = []
1452 for h, h_id in zip(self._hosts, self._host_ids):
1453 host_dicts.append({'hostname': h, 'id': h_id})
1454 rpcs.append(('get_hosts', {'hostname__in': hosts}, True, host_dicts))
1455
1456 # Expect actions only for known hosts
1457 host_ids = []
1458 for host in hosts:
1459 if host not in self._hosts:
1460 continue
1461 host_id = self._host_ids[self._hosts.index(host)]
1462 host_ids.append(host_id)
1463 modify_args = {'id': host}
1464
1465 if locked is not None:
1466 out.add('Locked' if locked else 'Unlocked')
1467 modify_args['locked'] = locked
1468 modify_args['lock_reason'] = lock_reason
1469 if force_lock:
1470 modify_args['force_modify_locking'] = True
1471 if protection:
1472 modify_args['protection'] = protection
1473
1474 if len(modify_args.keys()) > 1:
1475 out.add(host)
1476 rpcs.append(('modify_host', modify_args, True, None))
1477
1478 if labels:
1479 rpcs += self._gen_labels_rpcs(labels, host_id=host_id)
1480 rpcs.append(('host_add_labels', {'id': host, 'labels': labels},
1481 True, None))
1482
1483 if platform:
1484 rpcs += self._gen_labels_rpcs([platform], platform=True,
1485 host_id=host_id)
1486 rpcs.append(('host_add_labels', {'id': host,
1487 'labels': [platform]},
1488 True, None))
1489
1490 rpcs += self._gen_attributes_rpcs(host, attributes)
1491
1492 if acls:
1493 rpcs += self._gen_acls_rpcs(hosts, acls, host_ids=host_ids)
1494
1495 return rpcs, list(out)
1496
1497
1498 def test_mod_force_lock_one_host(self):
1499 """Test mod with forced locking."""
1500 lock_reason = 'Because'
1501 rpcs, out = self._gen_expectations(locked=True, force_lock=True,
1502 lock_reason=lock_reason)
1503 self.run_cmd(argv=self._command_single + [
1504 '--lock', '--force_modify_locking', '--lock_reason',
1505 lock_reason],
1506 rpcs=rpcs, out_words_ok=out)
1507
1508 def test_mod_force_unlock_one_host(self):
1509 """Test mod forced unlocking."""
1510 rpcs, out = self._gen_expectations(locked=False, force_lock=True)
1511 self.run_cmd(argv=self._command_single + ['--unlock',
1512 '--force_modify_locking'],
1513 rpcs=rpcs, out_words_ok=out)
1514
1515 def test_mod_fail_unknown_host(self):
1516 """Test mod fails with unknown host."""
1517 rpcs, out = self._gen_expectations(hosts=['nope'], locked=True)
1518 self.run_cmd(argv=self._command_base + ['nope', '--lock'],
1519 rpcs=rpcs, err_words_ok=['Cannot', 'modify', 'nope'])
1520
1521
1522class host_create_unittest(host_mod_create_tests, cli_mock.cli_unittest):
1523 """Test specific to create action and expectation generator for shared
1524 tests.
1525 """
1526 _hosts = ['localhost', '127.0.0.1']
1527 _command_base = ['atest', 'host', 'create']
1528 _command_single = _command_base + [_hosts[0]]
1529 _command_multiple = _command_base + _hosts
Justin Giorgid76ed502016-06-21 10:27:52 -07001530
Justin Giorgi5208eaa2016-07-02 20:12:12 -07001531
1532 def setUp(self):
1533 """Mock out the create_host method.
1534 """
1535 super(host_create_unittest, self).setUp()
1536 self._orig_create_host = hosts.create_host
1537
1538
1539 def tearDown(self):
1540 """Undo mock.
1541 """
1542 super(host_create_unittest, self).tearDown()
1543 hosts.create_host = self._orig_create_host
1544
1545
Justin Giorgid76ed502016-06-21 10:27:52 -07001546 def _mock_host(self, platform=None, labels=[]):
Justin Giorgi5208eaa2016-07-02 20:12:12 -07001547 """Update the return values of the mocked host object.
mblighbe630eb2008-08-01 16:41:48 +00001548
Justin Giorgi5208eaa2016-07-02 20:12:12 -07001549 @param platform: return value of Host.get_platform()
1550 @param labels: return value of Host.get_labels()
1551 """
1552 mock_host = mock.MagicMock()
1553 mock_host.get_platform.return_value = platform
1554 mock_host.get_labels.return_value = labels
1555 hosts.create_host = mock.MagicMock()
1556 hosts.create_host.return_value = mock_host
mbligh719e14a2008-12-04 01:17:08 +00001557
1558
Justin Giorgi16bba562016-06-22 10:42:13 -07001559 def _gen_expectations(self, hosts=['localhost'], locked=False,
1560 lock_reason=None, platform=None,
1561 discovered_platform=None, labels=[],
1562 discovered_labels=[], acls=[], protection=None,
1563 attributes={}):
Justin Giorgid76ed502016-06-21 10:27:52 -07001564 """Build a list of expected RPC calls based on values to host command.
1565
1566 @param hosts: list of hostname being created (default ['localhost'])
1567 @param locked: end state of host (bool)
1568 @param lock_reason: reason for host to be locked
1569 @param platform: platform label
Justin Giorgi16bba562016-06-22 10:42:13 -07001570 @param discovered_platform: platform discovered automatically by host
Justin Giorgid76ed502016-06-21 10:27:52 -07001571 @param labels: list of host labels (excluding platform)
Justin Giorgi16bba562016-06-22 10:42:13 -07001572 @param discovered_labels: list of labels discovered automatically
Justin Giorgid76ed502016-06-21 10:27:52 -07001573 @param acls: list of host acls
1574 @param protection: host protection level
1575
1576 @return: list of expect rpc calls (each call is (op, args, success,
1577 result))
1578 """
Justin Giorgi16bba562016-06-22 10:42:13 -07001579 hosts = hosts[:]
Justin Giorgid76ed502016-06-21 10:27:52 -07001580 hosts.reverse() # No idea why
1581 lock_reason = lock_reason or 'Forced lock on device creation'
1582 acls = acls or []
Justin Giorgid76ed502016-06-21 10:27:52 -07001583
Justin Giorgi16bba562016-06-22 10:42:13 -07001584 rpcs = []
1585 out = ['Added', 'host'] + hosts
Justin Giorgid76ed502016-06-21 10:27:52 -07001586
Justin Giorgi5208eaa2016-07-02 20:12:12 -07001587 # Mock platform and label detection results
1588 self._mock_host(discovered_platform, discovered_labels)
Justin Giorgid76ed502016-06-21 10:27:52 -07001589
1590 for host in hosts:
1591 add_args = {
1592 'hostname': host,
1593 'status': 'Ready',
1594 'locked': True,
1595 'lock_reason': lock_reason,
1596 }
1597 if protection:
1598 add_args['protection'] = protection
1599 rpcs.append(('add_host', add_args, True, None))
1600
Justin Giorgi16bba562016-06-22 10:42:13 -07001601 rpcs += self._gen_labels_rpcs(labels)
1602 if labels:
1603 rpcs.append(('host_add_labels', {'id': host, 'labels': labels},
1604 True, None))
Justin Giorgid76ed502016-06-21 10:27:52 -07001605
Justin Giorgi16bba562016-06-22 10:42:13 -07001606 if platform:
1607 rpcs += self._gen_labels_rpcs([platform], platform=True)
1608 rpcs.append(('host_add_labels', {'id': host,
1609 'labels': [platform]},
1610 True, None))
Justin Giorgid76ed502016-06-21 10:27:52 -07001611
Justin Giorgi16bba562016-06-22 10:42:13 -07001612 rpcs += self._gen_attributes_rpcs(host, attributes)
1613
1614 rpcs += self._gen_acls_rpcs(hosts, acls)
Justin Giorgid76ed502016-06-21 10:27:52 -07001615
1616 if not locked:
1617 for host in hosts:
1618 rpcs.append((
1619 'modify_host',
1620 {
1621 'id': host,
1622 'locked': False,
1623 'lock_reason': '',
1624 },
1625 True,
1626 None,
1627 ))
Justin Giorgi16bba562016-06-22 10:42:13 -07001628 return rpcs, out
1629
1630 def test_create_no_args(self):
1631 """Test simple creation with to arguments."""
1632 rpcs, out = self._gen_expectations()
1633 self.run_cmd(argv=self._command_single, rpcs=rpcs, out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001634
1635
Justin Giorgi16bba562016-06-22 10:42:13 -07001636 def test_create_with_discovered_platform(self):
1637 """Test discovered platform is used when platform isn't specified."""
1638 rpcs, out = self._gen_expectations(platform='some_platform',
1639 discovered_platform='some_platform')
1640 self.run_cmd(argv=self._command_single, rpcs=rpcs, out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001641
1642
1643 def test_create_specified_platform_overrides_discovered_platform(self):
Justin Giorgi16bba562016-06-22 10:42:13 -07001644 """Test that the specified platform overrides the discovered platform.
1645 """
1646 rpcs, out = self._gen_expectations(platform='some_platform',
1647 discovered_platform='wrong_platform')
1648 self.run_cmd(argv=self._command_single + ['--platform',
1649 'some_platform'],
1650 rpcs=rpcs, out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001651
1652
1653 def test_create_discovered_labels(self):
Justin Giorgi16bba562016-06-22 10:42:13 -07001654 """Test applying automatically discovered labels."""
Justin Giorgid76ed502016-06-21 10:27:52 -07001655 labels = ['label0', 'label1']
Justin Giorgi16bba562016-06-22 10:42:13 -07001656 rpcs, out = self._gen_expectations(labels=labels,
1657 discovered_labels=labels)
1658 self.run_cmd(argv=self._command_single, rpcs=rpcs, out_words_ok=out)
Justin Giorgid76ed502016-06-21 10:27:52 -07001659
1660
Justin Giorgid76ed502016-06-21 10:27:52 -07001661 def test_create_specified_discovered_labels_combine(self):
Justin Giorgi16bba562016-06-22 10:42:13 -07001662 """Test applying both discovered and specified labels."""
Justin Giorgid76ed502016-06-21 10:27:52 -07001663 labels = ['label0', 'label1']
Justin Giorgi16bba562016-06-22 10:42:13 -07001664 rpcs, out = self._gen_expectations(labels=labels,
1665 discovered_labels=[labels[0]])
1666 self.run_cmd(argv=self._command_single + ['--labels', labels[1]],
1667 rpcs=rpcs, out_words_ok=out)
jamesrenc2863162010-07-12 21:20:51 +00001668
Gregory Nisbet48f1eea2019-08-05 16:18:56 -07001669 def test_remove_hostname_suffix(self):
1670 self.assertEqual(
1671 host._remove_hostname_suffix_if_present( \
1672 "a", host.MIGRATED_HOST_SUFFIX), "a")
1673 self.assertEqual( \
1674 host._remove_hostname_suffix_if_present( \
1675 "b" + host.MIGRATED_HOST_SUFFIX, \
1676 host.MIGRATED_HOST_SUFFIX), "b")
1677
jamesrenc2863162010-07-12 21:20:51 +00001678
mblighbe630eb2008-08-01 16:41:48 +00001679if __name__ == '__main__':
1680 unittest.main()