1#!/usr/bin/env python
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import unittest
19import vts.utils.python.common.cmd_utils as cmd_utils
20import vts.utils.python.common.cmd_result as cmd_result
21
22
23class CmdResultTest(unittest.TestCase):
24    '''Test methods for cmd_result module.'''
25
26    def setUp(self):
27        """SetUp tasks"""
28        self.res_single_no_error = cmd_result.CmdResult('stdout', '', 0)
29
30        self.res_multiple_no_error = cmd_result.CmdResult('stdout1', '', 0)
31        self.res_multiple_no_error.AddResult('stdout2', '', 0)
32
33        self.res_multiple_one_error = cmd_result.CmdResult('stdout1', '', 0)
34        self.res_multiple_one_error.AddResult('stdout2', 'stderr2', 1)
35
36        self.res_multiple_one_stderr_only = cmd_result.CmdResult('stdout1', '', 0)
37        self.res_multiple_one_stderr_only.AddResult('stdout2', 'stderr2', 0)
38
39        self.res_multiple_with_cmd_and_exception = cmd_result.CmdResult(
40            'stdout1', '', 0, cmd='cmd1')
41        self.res_multiple_with_cmd_and_exception.AddResult(
42            None, None, None, cmd='cmd2', exception='TCP connection lost')
43
44    def tearDown(self):
45        """TearDown tasks"""
46
47    def test_single_result_data(self):
48        """Tests the functionally of getting data from single command result."""
49        self.assertEqual(self.res_single_no_error.stdout, 'stdout')
50        self.assertEqual(self.res_single_no_error.stderr, '')
51        self.assertEqual(self.res_single_no_error.returncode, 0)
52        self.assertEqual(self.res_single_no_error.stdouts[-1], 'stdout')
53        self.assertEqual(self.res_single_no_error.stderrs[-1], '')
54        self.assertEqual(self.res_single_no_error.returncodes[-1], 0)
55
56    def test_multiple_result_data(self):
57        """Tests the functionally of getting data from multiple command result."""
58        self.assertEqual(self.res_multiple_no_error.stdout, 'stdout2')
59        self.assertEqual(self.res_multiple_no_error.stderr, '')
60        self.assertEqual(self.res_multiple_no_error.returncode, 0)
61        self.assertEqual(self.res_multiple_no_error.stdouts, ['stdout1', 'stdout2'])
62        self.assertEqual(self.res_multiple_no_error.stderrs, ['', ''])
63        self.assertEqual(self.res_multiple_no_error.returncodes, [0, 0])
64
65    def test_legacy_single_result_dictionary_support_no_error(self):
66        """Tests legacy dictionary getting method for single command result."""
67        self.assertEqual(self.res_single_no_error[cmd_utils.STDOUT][0], 'stdout')
68        self.assertFalse(self.res_single_no_error[cmd_utils.STDERR][0])
69        self.assertFalse(any(self.res_single_no_error[cmd_utils.STDERR]))
70        self.assertFalse(self.res_single_no_error[cmd_utils.EXIT_CODE][0])
71        self.assertFalse(any(self.res_single_no_error[cmd_utils.EXIT_CODE]))
72
73    def test_legacy_single_result_dictionary_support_no_error_wrong_key(self):
74        """Tests legacy dictionary getting method for single command result."""
75        try:
76            self.res_single_no_error["wrong key"]
77        except KeyError as e:
78            return  # test pass, end test case
79        except:
80            pass  # wrong exception, proceed to end of test function
81
82        self.assertFalse(True, "wrong key should raise KeyError exception")
83
84    def test_legacy_multiple_result_dictionary_support_no_error(self):
85        """Tests legacy dictionary getting method for multiple command results."""
86        self.assertEqual(self.res_multiple_no_error[cmd_utils.STDOUT][0], 'stdout1')
87        self.assertEqual(self.res_multiple_no_error[cmd_utils.STDOUT][1], 'stdout2')
88        self.assertFalse(any(self.res_multiple_no_error[cmd_utils.STDERR]))
89        self.assertFalse(any(self.res_multiple_no_error[cmd_utils.EXIT_CODE]))
90
91    def test_legacy_multiple_result_dictionary_support_one_error(self):
92        """Tests legacy dictionary getting method for multiple command results."""
93        self.assertTrue(any(self.res_multiple_one_error[cmd_utils.STDERR]))
94        self.assertTrue(any(self.res_multiple_one_error[cmd_utils.EXIT_CODE]))
95
96    def test_legacy_multiple_result_dictionary_support_one_stderr_only(self):
97        """Tests legacy dictionary getting method for multiple command results."""
98        self.assertTrue(any(self.res_multiple_one_stderr_only[cmd_utils.STDERR]))
99        self.assertFalse(any(self.res_multiple_one_stderr_only[cmd_utils.EXIT_CODE]))
100
101    def test_multiple_result_with_cmd_and_exception(self):
102        """Tests getting original command strings and exceptions"""
103        self.assertEqual(self.res_multiple_with_cmd_and_exception.cmd, 'cmd2')
104        self.assertEqual(self.res_multiple_with_cmd_and_exception.cmds, ['cmd1', 'cmd2'])
105        self.assertEqual(self.res_multiple_with_cmd_and_exception.exception, 'TCP connection lost')
106        self.assertEqual(self.res_multiple_with_cmd_and_exception.exceptions,
107                         [None, 'TCP connection lost'])
108
109
110if __name__ == "__main__":
111    unittest.main()
112