job_unittest.py revision 6fee7fd5493d49fecc3aaa71e71f11c593699f93
1#!/usr/bin/python
2#
3# Copyright 2008 Google Inc. All Rights Reserved.
4
5"""Tests for job."""
6
7import copy, getpass, unittest, sys, os
8
9import common
10from autotest_lib.cli import cli_mock, topic_common, job
11
12
13class job_unittest(cli_mock.cli_unittest):
14    def setUp(self):
15        super(job_unittest, self).setUp()
16        self.god.stub_function(getpass, 'getuser')
17        getpass.getuser.expect_call().and_return('user0')
18        self.values = copy.deepcopy(self.values_template)
19
20    results = [{u'status_counts': {u'Aborted': 1},
21                u'control_file':
22                u"job.run_test('sleeptest')\n",
23                u'name': u'test_job0',
24                u'control_type': u'Server',
25                u'synchronizing': 0,
26                u'priority':
27                u'Medium',
28                u'owner': u'user0',
29                u'created_on':
30                u'2008-07-08 17:45:44',
31                u'synch_count': 1,
32                u'synch_type':
33                u'Synchronous',
34                u'id': 180},
35               {u'status_counts': {u'Queued': 1},
36                u'control_file':
37                u"job.run_test('sleeptest')\n",
38                u'name': u'test_job1',
39                u'control_type': u'Client',
40                u'synchronizing':0,
41                u'priority':
42                u'High',
43                u'owner': u'user0',
44                u'created_on':
45                u'2008-07-08 12:17:47',
46                u'synch_count': 1,
47                u'synch_type':
48                u'Asynchronous',
49                u'id': 338}]
50
51
52    values_template = [{u'id': 180,          # Valid job
53                        u'priority': u'Low',
54                        u'name': u'test_job0',
55                        u'owner': u'Cringer',
56                        u'invalid': 0,
57                        u'created_on': u'2008-07-02 13:02:40',
58                        u'control_type': u'Server',
59                        u'status_counts': {u'Queued': 1},
60                        u'synch_type': u'Synchornous'},
61                       {u'id': 338,          # Valid job
62                        u'priority': 'High',
63                        u'name': u'test_job1',
64                        u'owner': u'Fisto',
65                        u'invalid': 0,
66                        u'created_on': u'2008-07-06 14:05:33',
67                        u'control_type': u'Client',
68                        u'status_counts': {u'Queued': 1},
69                        u'synch_type': u'Asynchronous'},
70                       {u'id': 339,          # Valid job
71                        u'priority': 'Medium',
72                        u'name': u'test_job2',
73                        u'owner': u'Roboto',
74                        u'invalid': 0,
75                        u'created_on': u'2008-07-07 15:33:18',
76                        u'control_type': u'Server',
77                        u'status_counts': {u'Queued': 1},
78                        u'synch_type': u'Asynchronous'},
79                       {u'id': 340,          # Invalid job priority
80                        u'priority': u'Uber',
81                        u'name': u'test_job3',
82                        u'owner': u'Panthor',
83                        u'invalid': 1,
84                        u'created_on': u'2008-07-04 00:00:01',
85                        u'control_type': u'Server',
86                        u'status_counts': {u'Queued': 1},
87                        u'synch_type': u'Synchronous'},
88                       {u'id': 350,          # Invalid job created_on
89                        u'priority': 'Medium',
90                        u'name': u'test_job4',
91                        u'owner': u'Icer',
92                        u'invalid': 1,
93                        u'created_on': u'Today',
94                        u'control_type': u'Client',
95                        u'status_counts': {u'Queued': 1},
96                        u'synch_type': u'Asynchronous'},
97                       {u'id': 420,          # Invalid job control_type
98                        u'priority': 'Urgent',
99                        u'name': u'test_job5',
100                        u'owner': u'Spikor',
101                        u'invalid': 1,
102                        u'created_on': u'2012-08-08 18:54:37',
103                        u'control_type': u'Child',
104                        u'status_counts': {u'Queued': 1},
105                        u'synch_type': u'Synchronous'}]
106
107
108class job_list_unittest(job_unittest):
109    def test_job_list_jobs(self):
110        getpass.getuser.expect_call().and_return('user0')
111        self.run_cmd(argv=['atest', 'job', 'list'],
112                     rpcs=[('get_jobs_summary', {'owner': 'user0',
113                                                 'running': None},
114                            True, self.values)],
115                     out_words_ok=['test_job0', 'test_job1', 'test_job2'],
116                     out_words_no=['Uber', 'Today', 'Child'])
117
118
119    def test_job_list_jobs_only_user(self):
120        values = [item for item in self.values if item['owner'] == 'Cringer']
121        self.run_cmd(argv=['atest', 'job', 'list', '-u', 'Cringer'],
122                     rpcs=[('get_jobs_summary', {'owner': 'Cringer',
123                                                 'running': None},
124                            True, values)],
125                     out_words_ok=['Cringer'],
126                     out_words_no=['Fisto', 'Roboto', 'Panthor', 'Icer',
127                                   'Spikor'])
128
129
130    def test_job_list_jobs_all(self):
131        self.run_cmd(argv=['atest', 'job', 'list', '--all'],
132                     rpcs=[('get_jobs_summary', {'running': None},
133                            True, self.values)],
134                     out_words_ok=['Fisto', 'Roboto', 'Panthor',
135                                   'Icer', 'Spikor', 'Cringer'],
136                     out_words_no=['Created', 'Priority'])
137
138
139    def test_job_list_jobs_id(self):
140        self.run_cmd(argv=['atest', 'job', 'list', '5964'],
141                     rpcs=[('get_jobs_summary', {'id__in': ['5964'],
142                                                 'running': None},
143                            True,
144                            [{u'status_counts': {u'Completed': 1},
145                              u'control_file': u'kernel = \'8210088647656509311.kernel-smp-2.6.18-220.5.x86_64.rpm\'\ndef step_init():\n    job.next_step([step_test])\n    testkernel = job.kernel(\'8210088647656509311.kernel-smp-2.6.18-220.5.x86_64.rpm\')\n    \n    testkernel.install()\n    testkernel.boot(args=\'console_always_print=1\')\n\ndef step_test():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "Autotest Team"\n    NAME = "Sleeptest"\n    TIME = "SHORT"\n    TEST_CATEGORY = "Functional"\n    TEST_CLASS = "General"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    This test simply sleeps for 1 second by default.  It\'s a good way to test\n    profilers and double check that autotest is working.\n    The seconds argument can also be modified to make the machine sleep for as\n    long as needed.\n    """\n    \n    job.run_test(\'sleeptest\',                             seconds = 1)',
146                              u'name': u'mytest',
147                              u'control_type': u'Client',
148                              u'synchronizing': 0,
149                              u'run_verify': 1,
150                              u'priority': u'Medium',
151                              u'owner': u'user0',
152                              u'created_on': u'2008-07-28 12:42:52',
153                              u'timeout': 144,
154                              u'synch_count': None,
155                              u'synch_type': u'Asynchronous',
156                              u'id': 5964}])],
157                     out_words_ok=['user0', 'Completed', '1', '5964'],
158                     out_words_no=['sleeptest', 'Priority', 'Client', '2008'])
159
160
161    def test_job_list_jobs_id_verbose(self):
162        self.run_cmd(argv=['atest', 'job', 'list', '5964', '-v'],
163                     rpcs=[('get_jobs_summary', {'id__in': ['5964'],
164                                                 'running': None},
165                            True,
166                            [{u'status_counts': {u'Completed': 1},
167                              u'control_file': u'kernel = \'8210088647656509311.kernel-smp-2.6.18-220.5.x86_64.rpm\'\ndef step_init():\n    job.next_step([step_test])\n    testkernel = job.kernel(\'8210088647656509311.kernel-smp-2.6.18-220.5.x86_64.rpm\')\n    \n    testkernel.install()\n    testkernel.boot(args=\'console_always_print=1\')\n\ndef step_test():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "Autotest Team"\n    NAME = "Sleeptest"\n    TIME = "SHORT"\n    TEST_CATEGORY = "Functional"\n    TEST_CLASS = "General"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    This test simply sleeps for 1 second by default.  It\'s a good way to test\n    profilers and double check that autotest is working.\n    The seconds argument can also be modified to make the machine sleep for as\n    long as needed.\n    """\n    \n    job.run_test(\'sleeptest\',                             seconds = 1)',
168                              u'name': u'mytest',
169                              u'control_type': u'Client',
170                              u'synchronizing': 0,
171                              u'run_verify': 1,
172                              u'priority': u'Medium',
173                              u'owner': u'user0',
174                              u'created_on': u'2008-07-28 12:42:52',
175                              u'timeout': 144,
176                              u'synch_count': None,
177                              u'synch_type': u'Asynchronous',
178                              u'id': 5964}])],
179                     out_words_ok=['user0', 'Completed', '1', '5964',
180                                   'Client', '2008', 'Priority'],
181                     out_words_no=['sleeptest'])
182
183
184    def test_job_list_jobs_name(self):
185        self.run_cmd(argv=['atest', 'job', 'list', 'myt*'],
186                     rpcs=[('get_jobs_summary', {'name__startswith': 'myt',
187                                                 'running': None},
188                            True,
189                            [{u'status_counts': {u'Completed': 1},
190                              u'control_file': u'kernel = \'8210088647656509311.kernel-smp-2.6.18-220.5.x86_64.rpm\'\ndef step_init():\n    job.next_step([step_test])\n    testkernel = job.kernel(\'8210088647656509311.kernel-smp-2.6.18-220.5.x86_64.rpm\')\n    \n    testkernel.install()\n    testkernel.boot(args=\'console_always_print=1\')\n\ndef step_test():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "Autotest Team"\n    NAME = "Sleeptest"\n    TIME = "SHORT"\n    TEST_CATEGORY = "Functional"\n    TEST_CLASS = "General"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    This test simply sleeps for 1 second by default.  It\'s a good way to test\n    profilers and double check that autotest is working.\n    The seconds argument can also be modified to make the machine sleep for as\n    long as needed.\n    """\n    \n    job.run_test(\'sleeptest\',                             seconds = 1)',
191                              u'name': u'mytest',
192                              u'control_type': u'Client',
193                              u'synchronizing': 0,
194                              u'run_verify': 1,
195                              u'priority': u'Medium',
196                              u'owner': u'user0',
197                              u'created_on': u'2008-07-28 12:42:52',
198                              u'timeout': 144,
199                              u'synch_count': None,
200                              u'synch_type': u'Asynchronous',
201                              u'id': 5964}])],
202                     out_words_ok=['user0', 'Completed', '1', '5964'],
203                     out_words_no=['sleeptest', 'Priority', 'Client', '2008'])
204
205
206    def test_job_list_jobs_all_verbose(self):
207        self.run_cmd(argv=['atest', 'job', 'list', '--all', '--verbose'],
208                     rpcs=[('get_jobs_summary', {'running': None},
209                            True, self.values)],
210                     out_words_ok=['Fisto', 'Spikor', 'Cringer', 'Priority',
211                                   'Created'])
212
213
214    def test_job_list_jobs_all_and_user(self):
215        testjob = job.job_list()
216        sys.argv = ['atest', 'job', 'list', '-a', '-u', 'user0']
217        self.god.mock_io()
218        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
219        self.assertRaises(cli_mock.ExitException, testjob.parse)
220        self.god.unmock_io()
221
222
223class job_stat_unittest(job_unittest):
224    def test_job_stat_job(self):
225        results = copy.deepcopy(self.results)
226        self.run_cmd(argv=['atest', 'job', 'stat', '180'],
227                     rpcs=[('get_jobs_summary', {'id__in': ['180']}, True,
228                            [results[0]]),
229                           ('get_host_queue_entries', {'job__in': ['180']},
230                            True,
231                            [{u'status': u'Failed',
232                              u'complete': 1,
233                              u'host': {u'status': u'Repair Failed',
234                                        u'locked': 0,
235                                        u'hostname': u'host0',
236                                        u'invalid': 1,
237                                        u'id': 4432,
238                                        u'synch_id': None},
239                              u'priority': 1,
240                              u'meta_host': None,
241                              u'job': {u'control_file': u"def run(machine):\n\thost = hosts.create_host(machine)\n\tat = autotest.Autotest(host)\n\tat.run_test('sleeptest')\n\nparallel_simple(run, machines)",
242                                       u'name': u'test_sleep',
243                                       u'control_type': u'Server',
244                                       u'synchronizing': 0,
245                                       u'priority': u'Medium',
246                                       u'owner': u'user0',
247                                       u'created_on': u'2008-03-18 11:27:29',
248                                       u'synch_count': None,
249                                       u'synch_type': u'Asynchronous',
250                                       u'id': 180},
251                              u'active': 0,
252                              u'id': 101084}])],
253                     out_words_ok=['test_job0', 'host0', 'Failed',
254                                   'Aborted'])
255
256
257    def test_job_stat_job_multiple_hosts(self):
258        self.run_cmd(argv=['atest', 'job', 'stat', '6761'],
259                     rpcs=[('get_jobs_summary', {'id__in': ['6761']}, True,
260                            [{u'status_counts': {u'Running': 1,
261                                                 u'Queued': 4},
262                              u'control_file': u'def step_init():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "mbligh@google.com (Martin Bligh)"\n    NAME = "Kernbench"\n    TIME = "SHORT"\n    TEST_CLASS = "Kernel"\n    TEST_CATEGORY = "Benchmark"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    A standard CPU benchmark. Runs a kernel compile and measures the performance.\n    """\n    \n    job.run_test(\'kernbench\')',
263                              u'name': u'test_on_meta_hosts',
264                              u'control_type': u'Client',
265                              u'synchronizing': 0,
266                              u'run_verify': 1,
267                              u'priority': u'Medium',
268                              u'owner': u'user0',
269                              u'created_on': u'2008-07-30 22:15:43',
270                              u'timeout': 144,
271                              u'synch_count': None,
272                              u'synch_type': u'Asynchronous',
273                              u'id': 6761}]),
274                           ('get_host_queue_entries', {'job__in': ['6761']},
275                            True,
276                            [{u'status': u'Queued',
277                              u'complete': 0,
278                              u'deleted': 0,
279                              u'host': None,
280                              u'priority': 1,
281                              u'meta_host': u'Xeon',
282                              u'job': {u'control_file': u'def step_init():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "mbligh@google.com (Martin Bligh)"\n    NAME = "Kernbench"\n    TIME = "SHORT"\n    TEST_CLASS = "Kernel"\n    TEST_CATEGORY = "Benchmark"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    A standard CPU benchmark. Runs a kernel compile and measures the performance.\n    """\n    \n    job.run_test(\'kernbench\')',
283                                       u'name': u'test_on_meta_hosts',
284                                       u'control_type': u'Client',
285                                       u'synchronizing': 0,
286                                       u'run_verify': 1,
287                                       u'priority': u'Medium',
288                                       u'owner': u'user0',
289                                       u'created_on': u'2008-07-30 22:15:43',
290                                       u'timeout': 144,
291                                       u'synch_count': None,
292                                       u'synch_type': u'Asynchronous',
293                                       u'id': 6761},
294                              u'active': 0,
295                              u'id': 193166},
296                             {u'status': u'Queued',
297                              u'complete': 0,
298                              u'deleted': 0,
299                              u'host': None,
300                              u'priority': 1,
301                              u'meta_host': u'Xeon',
302                              u'job': {u'control_file': u'def step_init():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "mbligh@google.com (Martin Bligh)"\n    NAME = "Kernbench"\n    TIME = "SHORT"\n    TEST_CLASS = "Kernel"\n    TEST_CATEGORY = "Benchmark"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    A standard CPU benchmark. Runs a kernel compile and measures the performance.\n    """\n    \n    job.run_test(\'kernbench\')',
303                                       u'name': u'test_on_meta_hosts',
304                                       u'control_type': u'Client',
305                                       u'synchronizing': 0,
306                                       u'run_verify': 1,
307                                       u'priority': u'Medium',
308                                       u'owner': u'user0',
309                                       u'created_on': u'2008-07-30 22:15:43',
310                                       u'timeout': 144,
311                                       u'synch_count': None,
312                                       u'synch_type': u'Asynchronous',
313                                       u'id': 6761},
314                              u'active': 0,
315                              u'id': 193167},
316                             {u'status': u'Queued',
317                              u'complete': 0,
318                              u'deleted': 0,
319                              u'host': None,
320                              u'priority': 1,
321                              u'meta_host': u'Athlon',
322                              u'job': {u'control_file': u'def step_init():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "mbligh@google.com (Martin Bligh)"\n    NAME = "Kernbench"\n    TIME = "SHORT"\n    TEST_CLASS = "Kernel"\n    TEST_CATEGORY = "Benchmark"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    A standard CPU benchmark. Runs a kernel compile and measures the performance.\n    """\n    \n    job.run_test(\'kernbench\')',
323                                       u'name': u'test_on_meta_hosts',
324                                       u'control_type': u'Client',
325                                       u'synchronizing': 0,
326                                       u'run_verify': 1,
327                                       u'priority': u'Medium',
328                                       u'owner': u'user0',
329                                       u'created_on': u'2008-07-30 22:15:43',
330                                       u'timeout': 144,
331                                       u'synch_count': None,
332                                       u'synch_type': u'Asynchronous',
333                                       u'id': 6761},
334                              u'active': 0,
335                              u'id': 193168},
336                             {u'status': u'Queued',
337                              u'complete': 0,
338                              u'deleted': 0,
339                              u'host': None,
340                              u'priority': 1,
341                              u'meta_host': u'x286',
342                              u'job': {u'control_file': u'def step_init():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "mbligh@google.com (Martin Bligh)"\n    NAME = "Kernbench"\n    TIME = "SHORT"\n    TEST_CLASS = "Kernel"\n    TEST_CATEGORY = "Benchmark"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    A standard CPU benchmark. Runs a kernel compile and measures the performance.\n    """\n    \n    job.run_test(\'kernbench\')',
343                                       u'name': u'test_on_meta_hosts',
344                                       u'control_type': u'Client',
345                                       u'synchronizing': 0,
346                                       u'run_verify': 1,
347                                       u'priority': u'Medium',
348                                       u'owner': u'user0',
349                                       u'created_on': u'2008-07-30 22:15:43',
350                                       u'timeout': 144,
351                                       u'synch_count': None,
352                                       u'synch_type': u'Asynchronous',
353                                       u'id': 6761},
354                              u'active': 0,
355                              u'id': 193169},
356                             {u'status': u'Running',
357                              u'complete': 0,
358                              u'deleted': 0,
359                              u'host': {u'status': u'Running',
360                                        u'lock_time': None,
361                                        u'hostname': u'host42',
362                                        u'locked': 0,
363                                        u'locked_by': None,
364                                        u'invalid': 0,
365                                        u'id': 4833,
366                                        u'protection': u'Repair filesystem only',
367                                        u'synch_id': None},
368                              u'priority': 1,
369                              u'meta_host': u'Athlon',
370                              u'job': {u'control_file': u'def step_init():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "mbligh@google.com (Martin Bligh)"\n    NAME = "Kernbench"\n    TIME = "SHORT"\n    TEST_CLASS = "Kernel"\n    TEST_CATEGORY = "Benchmark"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    A standard CPU benchmark. Runs a kernel compile and measures the performance.\n    """\n    \n    job.run_test(\'kernbench\')',
371                                       u'name': u'test_on_meta_hosts',
372                                       u'control_type': u'Client',
373                                       u'synchronizing': 0,
374                                       u'run_verify': 1,
375                                       u'priority': u'Medium',
376                                       u'owner': u'user0',
377                                       u'created_on': u'2008-07-30 22:15:43',
378                                       u'timeout': 144,
379                                       u'synch_count': None,
380                                       u'synch_type': u'Asynchronous',
381                                       u'id': 6761},
382                              u'active': 1,
383                              u'id': 193170} ])],
384                     out_words_ok=['test_on_meta_hosts',
385                                   'host42', 'Queued', 'Running'],
386                     out_words_no=['Athlon', 'Xeon', 'x286'])
387
388
389    def test_job_stat_job_no_host_in_qes(self):
390        results = copy.deepcopy(self.results)
391        self.run_cmd(argv=['atest', 'job', 'stat', '180'],
392                     rpcs=[('get_jobs_summary', {'id__in': ['180']}, True,
393                            [results[0]]),
394                           ('get_host_queue_entries', {'job__in': ['180']},
395                            True,
396                            [{u'status': u'Failed',
397                              u'complete': 1,
398                              u'host': None,
399                              u'priority': 1,
400                              u'meta_host': None,
401                              u'job': {u'control_file': u"def run(machine):\n\thost = hosts.create_host(machine)\n\tat = autotest.Autotest(host)\n\tat.run_test('sleeptest')\n\nparallel_simple(run, machines)",
402                                       u'name': u'test_sleep',
403                                       u'control_type': u'Server',
404                                       u'synchronizing': 0,
405                                       u'priority': u'Medium',
406                                       u'owner': u'user0',
407                                       u'created_on': u'2008-03-18 11:27:29',
408                                       u'synch_count': None,
409                                       u'synch_type': u'Asynchronous',
410                                       u'id': 180},
411                              u'active': 0,
412                              u'id': 101084}])],
413                     out_words_ok=['test_job0', 'Aborted'])
414
415
416    def test_job_stat_multi_jobs(self):
417        results = copy.deepcopy(self.results)
418        self.run_cmd(argv=['atest', 'job', 'stat', '180', '338'],
419                     rpcs=[('get_jobs_summary', {'id__in': ['180', '338']},
420                            True, results),
421                           ('get_host_queue_entries',
422                            {'job__in': ['180', '338']},
423                            True,
424                            [{u'status': u'Failed',
425                              u'complete': 1,
426                              u'host': {u'status': u'Repair Failed',
427                                        u'locked': 0,
428                                        u'hostname': u'host0',
429                                        u'invalid': 1,
430                                        u'id': 4432,
431                                        u'synch_id': None},
432                              u'priority': 1,
433                              u'meta_host': None,
434                              u'job': {u'control_file': u"def run(machine):\n\thost = hosts.create_host(machine)\n\tat = autotest.Autotest(host)\n\tat.run_test('sleeptest')\n\nparallel_simple(run, machines)",
435                                       u'name': u'test_sleep',
436                                       u'control_type': u'Server',
437                                       u'synchronizing': 0,
438                                       u'priority': u'Medium',
439                                       u'owner': u'user0',
440                                       u'created_on': u'2008-03-18 11:27:29',
441                                       u'synch_count': None,
442                                       u'synch_type': u'Asynchronous',
443                                       u'id': 180},
444                              u'active': 0,
445                              u'id': 101084},
446                             {u'status': u'Failed',
447                              u'complete': 1,
448                              u'host': {u'status': u'Repair Failed',
449                                        u'locked': 0,
450                                        u'hostname': u'host10',
451                                        u'invalid': 1,
452                                        u'id': 4432,
453                                        u'synch_id': None},
454                              u'priority': 1,
455                              u'meta_host': None,
456                              u'job': {u'control_file': u"def run(machine):\n\thost = hosts.create_host(machine)\n\tat = autotest.Autotest(host)\n\tat.run_test('sleeptest')\n\nparallel_simple(run, machines)",
457                                       u'name': u'test_sleep',
458                                       u'control_type': u'Server',
459                                       u'synchronizing': 0,
460                                       u'priority': u'Medium',
461                                       u'owner': u'user0',
462                                       u'created_on': u'2008-03-18 11:27:29',
463                                       u'synch_count': None,
464                                       u'synch_type': u'Asynchronous',
465                                       u'id': 338},
466                              u'active': 0,
467                              u'id': 101084}])],
468                     out_words_ok=['test_job0', 'test_job1'])
469
470
471    def test_job_stat_multi_jobs_name_id(self):
472        self.run_cmd(argv=['atest', 'job', 'stat', 'mytest', '180'],
473                     rpcs=[('get_jobs_summary', {'id__in': ['180']},
474                            True,
475                            [{u'status_counts': {u'Aborted': 1},
476                             u'control_file':
477                             u"job.run_test('sleeptest')\n",
478                             u'name': u'job0',
479                             u'control_type': u'Server',
480                             u'synchronizing': 0,
481                             u'priority':
482                             u'Medium',
483                             u'owner': u'user0',
484                             u'created_on':
485                             u'2008-07-08 17:45:44',
486                             u'synch_count': 1,
487                             u'synch_type':
488                             u'Synchronous',
489                             u'id': 180}]),
490                           ('get_jobs_summary', {'name__in': ['mytest']},
491                            True,
492                            [{u'status_counts': {u'Queued': 1},
493                             u'control_file':
494                             u"job.run_test('sleeptest')\n",
495                             u'name': u'mytest',
496                             u'control_type': u'Client',
497                             u'synchronizing':0,
498                             u'priority':
499                             u'High',
500                             u'owner': u'user0',
501                             u'created_on': u'2008-07-08 12:17:47',
502                             u'synch_count': 1,
503                             u'synch_type':
504                             u'Asynchronous',
505                             u'id': 338}]),
506                           ('get_host_queue_entries',
507                            {'job__in': ['180']},
508                            True,
509                            [{u'status': u'Failed',
510                              u'complete': 1,
511                              u'host': {u'status': u'Repair Failed',
512                                        u'locked': 0,
513                                        u'hostname': u'host0',
514                                        u'invalid': 1,
515                                        u'id': 4432,
516                                        u'synch_id': None},
517                              u'priority': 1,
518                              u'meta_host': None,
519                              u'job': {u'control_file': u"def run(machine):\n\thost = hosts.create_host(machine)\n\tat = autotest.Autotest(host)\n\tat.run_test('sleeptest')\n\nparallel_simple(run, machines)",
520                                       u'name': u'test_sleep',
521                                       u'control_type': u'Server',
522                                       u'synchronizing': 0,
523                                       u'priority': u'Medium',
524                                       u'owner': u'user0',
525                                       u'created_on': u'2008-03-18 11:27:29',
526                                       u'synch_count': None,
527                                       u'synch_type': u'Asynchronous',
528                                       u'id': 180},
529                              u'active': 0,
530                              u'id': 101084}]),
531                           ('get_host_queue_entries',
532                            {'job__name__in': ['mytest']},
533                            True,
534                            [{u'status': u'Failed',
535                              u'complete': 1,
536                              u'host': {u'status': u'Repair Failed',
537                                        u'locked': 0,
538                                        u'hostname': u'host10',
539                                        u'invalid': 1,
540                                        u'id': 4432,
541                                        u'synch_id': None},
542                              u'priority': 1,
543                              u'meta_host': None,
544                              u'job': {u'control_file': u"def run(machine):\n\thost = hosts.create_host(machine)\n\tat = autotest.Autotest(host)\n\tat.run_test('sleeptest')\n\nparallel_simple(run, machines)",
545                                       u'name': u'test_sleep',
546                                       u'control_type': u'Server',
547                                       u'synchronizing': 0,
548                                       u'priority': u'Medium',
549                                       u'owner': u'user0',
550                                       u'created_on': u'2008-03-18 11:27:29',
551                                       u'synch_count': None,
552                                       u'synch_type': u'Asynchronous',
553                                       u'id': 338},
554                              u'active': 0,
555                              u'id': 101084}])],
556                     out_words_ok=['job0', 'mytest', 'Aborted', 'Queued',
557                                   'Failed', 'Medium', 'High'])
558
559
560class job_create_unittest(cli_mock.cli_unittest):
561    ctrl_file = '\ndef step_init():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "Autotest Team"\n    NAME = "Sleeptest"\n  TIME =\n    "SHORT"\n    TEST_CATEGORY = "Functional"\n    TEST_CLASS = "General"\n\n    TEST_TYPE = "client"\n \n    DOC = """\n    This test simply sleeps for 1\n    second by default.  It\'s a good way to test\n    profilers and double check\n    that autotest is working.\n The seconds argument can also be modified to\n    make the machine sleep for as\n    long as needed.\n    """\n   \n\n    job.run_test(\'sleeptest\', seconds = 1)'
562
563    kernel_ctrl_file = 'kernel = \'kernel\'\ndef step_init():\n    job.next_step([step_test])\n    testkernel = job.kernel(\'kernel\')\n    \n    testkernel.install()\n    testkernel.boot(args=\'console_always_print=1\')\n\ndef step_test():\n    job.next_step(\'step0\')\n\ndef step0():\n    AUTHOR = "Autotest Team"\n    NAME = "Sleeptest"\n    TIME = "SHORT"\n    TEST_CATEGORY = "Functional"\n    TEST_CLASS = "General"\n    TEST_TYPE = "client"\n    \n    DOC = """\n    This test simply sleeps for 1 second by default.  It\'s a good way to test\n    profilers and double check that autotest is working.\n    The seconds argument can also be modified to make the machine sleep for as\n    long as needed.\n    """\n    \n    job.run_test(\'sleeptest\', seconds = 1)'
564
565    data = {'priority': 'Medium', 'control_file': ctrl_file, 'hosts': ['host0'],
566            'name': 'test_job0', 'control_type': 'Client', 'email_list': '',
567            'meta_hosts': [], 'is_synchronous': False, 'dependencies': []}
568
569
570    def test_execute_create_job(self):
571        self.run_cmd(argv=['atest', 'job', 'create', '-t', 'sleeptest',
572                           'test_job0', '-m', 'host0'],
573                     rpcs=[('generate_control_file',
574                            {'tests': ['sleeptest'], 'use_container': False},
575                            True,
576                            {'control_file' : self.ctrl_file,
577                             'is_synchronous' : False,
578                             'is_server' : False,
579                             'dependencies' : []}),
580                           ('create_job', self.data, True, 180)],
581                     out_words_ok=['test_job0', 'Created'],
582                     out_words_no=['Uploading', 'Done'])
583
584
585    def test_execute_create_job_with_control(self):
586        filename = cli_mock.create_file(self.ctrl_file)
587        self.run_cmd(argv=['atest', 'job', 'create', '-f', filename,
588                           'test_job0', '-m', 'host0'],
589                     rpcs=[('create_job', self.data, True, 42)],
590                     out_words_ok=['test_job0', 'Created'],
591                     out_words_no=['Uploading', 'Done'])
592
593
594    def test_execute_create_job_with_kernel(self):
595        data = self.data.copy()
596        data['control_file'] = self.kernel_ctrl_file
597        self.run_cmd(argv=['atest', 'job', 'create', '-t', 'sleeptest',
598                           '-k', 'kernel', 'test_job0', '-m', 'host0'],
599                     rpcs=[('generate_control_file',
600                            {'tests': ['sleeptest'], 'use_container': False,
601                             'kernel': 'kernel', 'do_push_packages': True},
602                            True,
603                            {'control_file' : self.kernel_ctrl_file,
604                             'is_synchronous' : False,
605                             'is_server' : False,
606                             'dependencies' : []}),
607                           ('create_job', data, True, 180)],
608                     out_words_ok=['test_job0', 'Created',
609                                   'Uploading', 'Done'])
610
611
612    def test_execute_create_job_with_kernel_spaces(self):
613        data = self.data.copy()
614        data['control_file'] = self.kernel_ctrl_file
615        data['name'] = 'test job	with  spaces'
616        self.run_cmd(argv=['atest', 'job', 'create', '-t', 'sleeptest',
617                           '-k', 'kernel', 'test job	with  spaces',
618                           '-m', 'host0'],
619                     rpcs=[('generate_control_file',
620                            {'tests': ['sleeptest'], 'use_container': False,
621                             'kernel': 'kernel', 'do_push_packages': True},
622                            True,
623                            {'control_file' : self.kernel_ctrl_file,
624                             'is_synchronous' : False,
625                             'is_server' : False,
626                             'dependencies' : []}),
627                           ('create_job', data, True, 180)],
628                     # This is actually 8 spaces,
629                     # the tab has been converted by print.
630                     out_words_ok=['test job        with  spaces', 'Created',
631                                   'id', '180'])
632
633
634    def test_execute_create_job_no_args(self):
635        testjob = job.job_create()
636        sys.argv = ['atest', 'job', 'create']
637        self.god.mock_io()
638        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
639        self.assertRaises(cli_mock.ExitException, testjob.parse)
640        self.god.unmock_io()
641
642
643    def test_execute_create_job_no_hosts(self):
644        testjob = job.job_create()
645        filename = cli_mock.create_file(self.ctrl_file)
646        sys.argv = ['atest', '-f', filename, 'test_job0']
647        self.god.mock_io()
648        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
649        self.assertRaises(cli_mock.ExitException, testjob.parse)
650        self.god.unmock_io()
651
652
653    def test_execute_create_job_cfile_and_tests(self):
654        testjob = job.job_create()
655        sys.argv = ['atest', 'job', 'create', '-t', 'sleeptest', '-f',
656                    'control_file', 'test_job0', '-m', 'host0']
657        self.god.mock_io()
658        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
659        self.assertRaises(cli_mock.ExitException, testjob.parse)
660        self.god.unmock_io()
661
662
663    def test_execute_create_job_container_and_server(self):
664        testjob = job.job_create()
665        sys.argv = ['atest', 'job', 'create', '-t', 'sleeptest', '-s', '-c',
666                    'test_job0', '-m', 'host0']
667        self.god.mock_io()
668        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
669        self.assertRaises(cli_mock.ExitException, testjob.parse)
670        self.god.unmock_io()
671
672
673    def test_execute_create_job_cfile_and_kernel(self):
674        testjob = job.job_create()
675        sys.argv = ['atest', 'job', 'create', '-f', 'control_file', '-k',
676                    'kernel', 'test_job0', '-m', 'host0']
677        self.god.mock_io()
678        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
679        self.assertRaises(cli_mock.ExitException, testjob.parse)
680        self.god.unmock_io()
681
682
683    def test_execute_create_job_cfile_and_container(self):
684        testjob = job.job_create()
685        sys.argv = ['atest', 'job', 'create', '-f', 'control_file', '-c',
686                    'test_job0', '-m', 'host0']
687        self.god.mock_io()
688        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
689        self.assertRaises(cli_mock.ExitException, testjob.parse)
690        self.god.unmock_io()
691
692
693    def test_execute_create_job_bad_cfile(self):
694        testjob = job.job_create()
695        sys.argv = ['atest', 'job', 'create', '-f', 'control_file', 'test_job0',
696                    '-m', 'host0']
697        self.god.mock_io()
698        sys.exit.expect_call(1).and_raises(IOError)
699        self.assertRaises(IOError, testjob.parse)
700        self.god.unmock_io()
701
702
703    def test_execute_create_job_bad_priority(self):
704        testjob = job.job_create()
705        sys.argv = ['atest', 'job', 'create', '-t', 'sleeptest', '-p', 'Uber',
706                    '-m', 'host0', 'test_job0']
707        self.god.mock_io()
708        sys.exit.expect_call(2).and_raises(cli_mock.ExitException)
709        self.assertRaises(cli_mock.ExitException, testjob.parse)
710        self.god.unmock_io()
711
712
713    def test_execute_create_job_with_mfile(self):
714        data = self.data.copy()
715        data['hosts'] = ['host3', 'host2', 'host1', 'host0']
716        cfile = cli_mock.create_file(self.ctrl_file)
717        filename = cli_mock.create_file('host0\nhost1\nhost2\nhost3')
718        self.run_cmd(argv=['atest', 'job', 'create', '--mlist', filename, '-f',
719                           cfile, 'test_job0'],
720                     rpcs=[('create_job', data, True, 42)],
721                     out_words_ok=['test_job0', 'Created'])
722
723
724    def test_execute_create_job_with_container(self):
725        data = self.data.copy()
726        self.run_cmd(argv=['atest', 'job', 'create', '-t', 'sleeptest',
727                           '-c', 'test_job0', '-m', 'host0'],
728                     rpcs=[('generate_control_file',
729                            {'tests': ['sleeptest'], 'use_container': True},
730                            True,
731                            {'control_file' : self.ctrl_file,
732                             'is_synchronous' : False,
733                             'is_server' : False,
734                             'dependencies' : []}),
735                           ('create_job', data, True, 42)],
736                     out_words_ok=['test_job0', 'Created'])
737
738
739    def _test_parse_hosts(self, args, exp_hosts=[], exp_meta_hosts=[]):
740        testjob = job.job_create()
741        (hosts, meta_hosts) = testjob.parse_hosts(args)
742        self.assertEqualNoOrder(hosts, exp_hosts)
743        self.assertEqualNoOrder(meta_hosts, exp_meta_hosts)
744
745
746    def test_parse_hosts_regular(self):
747        self._test_parse_hosts(['host0'], ['host0'])
748
749
750    def test_parse_hosts_regulars(self):
751        self._test_parse_hosts(['host0', 'host1'], ['host0', 'host1'])
752
753
754    def test_parse_hosts_meta_one(self):
755        self._test_parse_hosts(['*meta0'], [], ['meta0'])
756
757
758    def test_parse_hosts_meta_five(self):
759        self._test_parse_hosts(['5*meta0'], [], ['meta0']*5)
760
761
762    def test_parse_hosts_metas_five(self):
763        self._test_parse_hosts(['5*meta0', '2*meta1'], [],
764                               ['meta0']*5 + ['meta1']*2)
765
766
767    def test_parse_hosts_mix(self):
768        self._test_parse_hosts(['5*meta0', 'host0', '2*meta1', 'host1',
769                                '*meta2'], ['host0', 'host1'],
770                               ['meta0']*5 + ['meta1']*2 + ['meta2'])
771
772
773class job_abort_unittest(cli_mock.cli_unittest):
774    results = [{u'status_counts': {u'Aborted': 1}, u'control_file':
775                u"job.run_test('sleeptest')\n", u'name': u'test_job0',
776                u'control_type': u'Server', u'synchronizing': 0, u'priority':
777                u'Medium', u'owner': u'user0', u'created_on':
778                u'2008-07-08 17:45:44', u'synch_count': 1, u'synch_type':
779                u'Synchronous', u'id': 180}]
780
781    def test_execute_job_abort(self):
782        self.run_cmd(argv=['atest', 'job', 'abort', '180'],
783                     rpcs=[('abort_job', {'id': '180'}, True, None)],
784                     out_words_ok=['Aborted', '180'])
785
786
787    def test_execute_job_abort_bad_id(self):
788        self.run_cmd(argv=['atest', 'job', 'abort', '777'],
789                     rpcs=[('abort_job', {'id': '777'}, False,
790                            'ValidationError:{DoesNotExist: Job matching query'
791                            'does not exist}')],
792                     err_words_ok=['Operation', 'abort_job', 'failed'])
793
794
795if __name__ == '__main__':
796    unittest.main()
797