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