mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | """Test for cli.""" |
| 5 | |
mbligh | 4151539 | 2009-07-11 00:13:11 +0000 | [diff] [blame] | 6 | import unittest, os, sys, StringIO |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 7 | |
| 8 | import common |
| 9 | from autotest_lib.cli import atest, topic_common, rpc |
| 10 | from autotest_lib.frontend.afe.json_rpc import proxy |
| 11 | from autotest_lib.client.common_lib.test_utils import mock |
mbligh | 4151539 | 2009-07-11 00:13:11 +0000 | [diff] [blame] | 12 | from autotest_lib.client.common_lib import autotemp |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 13 | |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 14 | CLI_USING_PDB = False |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 15 | CLI_UT_DEBUG = False |
| 16 | |
| 17 | def create_file(content): |
mbligh | 4151539 | 2009-07-11 00:13:11 +0000 | [diff] [blame] | 18 | file_temp = autotemp.tempfile(unique_id='cli_mock', text=True) |
| 19 | os.write(file_temp.fd, content) |
| 20 | os.close(file_temp.fd) |
| 21 | return file_temp |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class ExitException(Exception): |
| 25 | pass |
| 26 | |
| 27 | |
| 28 | class cli_unittest(unittest.TestCase): |
| 29 | def setUp(self): |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 30 | super(cli_unittest, self).setUp() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 31 | self.god = mock.mock_god(debug=CLI_UT_DEBUG) |
| 32 | self.god.stub_class_method(rpc.afe_comm, 'run') |
| 33 | self.god.stub_function(sys, 'exit') |
| 34 | |
showard | f4a6899 | 2010-02-03 20:29:59 +0000 | [diff] [blame] | 35 | def stub_authorization_headers(*args, **kwargs): |
| 36 | return {} |
| 37 | self.god.stub_with(rpc, 'authorization_headers', |
| 38 | stub_authorization_headers) |
| 39 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 40 | |
| 41 | def tearDown(self): |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 42 | super(cli_unittest, self).tearDown() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 43 | self.god.unstub_all() |
| 44 | |
| 45 | |
| 46 | def assertEqualNoOrder(self, x, y, message=None): |
| 47 | self.assertEqual(set(x), set(y), message) |
| 48 | |
| 49 | |
| 50 | def assertWords(self, string, to_find=[], not_in=[]): |
| 51 | for word in to_find: |
| 52 | self.assert_(string.find(word) >= 0, |
| 53 | "Could not find '%s' in: %s" % (word, string)) |
| 54 | for word in not_in: |
| 55 | self.assert_(string.find(word) < 0, |
| 56 | "Found (and shouldn't have) '%s' in: %s" % (word, |
| 57 | string)) |
| 58 | |
| 59 | |
| 60 | def _check_output(self, out='', out_words_ok=[], out_words_no=[], |
| 61 | err='', err_words_ok=[], err_words_no=[]): |
| 62 | if out_words_ok or out_words_no: |
| 63 | self.assertWords(out, out_words_ok, out_words_no) |
| 64 | else: |
| 65 | self.assertEqual('', out) |
| 66 | |
| 67 | if err_words_ok or err_words_no: |
| 68 | self.assertWords(err, err_words_ok, err_words_no) |
| 69 | else: |
| 70 | self.assertEqual('', err) |
| 71 | |
| 72 | |
mbligh | 7a3ebe3 | 2008-12-01 17:10:33 +0000 | [diff] [blame] | 73 | def assertOutput(self, obj, results, |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 74 | out_words_ok=[], out_words_no=[], |
| 75 | err_words_ok=[], err_words_no=[]): |
| 76 | self.god.mock_io() |
mbligh | 7a3ebe3 | 2008-12-01 17:10:33 +0000 | [diff] [blame] | 77 | obj.output(results) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 78 | obj.show_all_failures() |
| 79 | (out, err) = self.god.unmock_io() |
| 80 | self._check_output(out, out_words_ok, out_words_no, |
| 81 | err, err_words_ok, err_words_no) |
| 82 | |
| 83 | |
| 84 | def mock_rpcs(self, rpcs): |
| 85 | """rpcs is a list of tuples, each representing one RPC: |
| 86 | (op, **dargs, success, expected)""" |
| 87 | for (op, dargs, success, expected) in rpcs: |
| 88 | comm = rpc.afe_comm.run |
| 89 | if success: |
| 90 | comm.expect_call(op, **dargs).and_return(expected) |
| 91 | else: |
| 92 | comm.expect_call(op, **dargs).and_raises(proxy.JSONRPCException(expected)) |
| 93 | |
| 94 | |
| 95 | |
| 96 | def run_cmd(self, argv, rpcs=[], exit_code=None, |
| 97 | out_words_ok=[], out_words_no=[], |
| 98 | err_words_ok=[], err_words_no=[]): |
| 99 | """Runs the command in argv. |
| 100 | rpcs is a list of tuples, each representing one RPC: |
| 101 | (op, **dargs, success, expected) |
| 102 | exit_code should be set if you expect the command |
| 103 | to fail |
| 104 | The words are lists of words that are expected""" |
| 105 | sys.argv = argv |
| 106 | |
| 107 | self.mock_rpcs(rpcs) |
| 108 | |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 109 | if not (CLI_USING_PDB and CLI_UT_DEBUG): |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 110 | self.god.mock_io() |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 111 | if exit_code is not None: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 112 | sys.exit.expect_call(exit_code).and_raises(ExitException) |
| 113 | self.assertRaises(ExitException, atest.main) |
| 114 | else: |
| 115 | atest.main() |
| 116 | (out, err) = self.god.unmock_io() |
| 117 | self.god.check_playback() |
| 118 | self._check_output(out, out_words_ok, out_words_no, |
| 119 | err, err_words_ok, err_words_no) |
| 120 | return (out, err) |