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