blob: c616beefe10ec27159b6708d0b9b04707e979cce [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
mbligh7a3ebe32008-12-01 17:10:33 +000065 def assertOutput(self, obj, results,
mblighbe630eb2008-08-01 16:41:48 +000066 out_words_ok=[], out_words_no=[],
67 err_words_ok=[], err_words_no=[]):
68 self.god.mock_io()
mbligh7a3ebe32008-12-01 17:10:33 +000069 obj.output(results)
mblighbe630eb2008-08-01 16:41:48 +000070 obj.show_all_failures()
71 (out, err) = self.god.unmock_io()
72 self._check_output(out, out_words_ok, out_words_no,
73 err, err_words_ok, err_words_no)
74
75
76 def mock_rpcs(self, rpcs):
77 """rpcs is a list of tuples, each representing one RPC:
78 (op, **dargs, success, expected)"""
79 for (op, dargs, success, expected) in rpcs:
80 comm = rpc.afe_comm.run
81 if success:
82 comm.expect_call(op, **dargs).and_return(expected)
83 else:
84 comm.expect_call(op, **dargs).and_raises(proxy.JSONRPCException(expected))
85
86
87
88 def run_cmd(self, argv, rpcs=[], exit_code=None,
89 out_words_ok=[], out_words_no=[],
90 err_words_ok=[], err_words_no=[]):
91 """Runs the command in argv.
92 rpcs is a list of tuples, each representing one RPC:
93 (op, **dargs, success, expected)
94 exit_code should be set if you expect the command
95 to fail
96 The words are lists of words that are expected"""
97 sys.argv = argv
98
99 self.mock_rpcs(rpcs)
100
mblighb9a8b162008-10-29 16:47:29 +0000101 if not (CLI_USING_PDB and CLI_UT_DEBUG):
mblighbe630eb2008-08-01 16:41:48 +0000102 self.god.mock_io()
mblighd876f452008-12-03 15:09:17 +0000103 if exit_code is not None:
mblighbe630eb2008-08-01 16:41:48 +0000104 sys.exit.expect_call(exit_code).and_raises(ExitException)
105 self.assertRaises(ExitException, atest.main)
106 else:
107 atest.main()
108 (out, err) = self.god.unmock_io()
109 self.god.check_playback()
110 self._check_output(out, out_words_ok, out_words_no,
111 err, err_words_ok, err_words_no)
112 return (out, err)