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