1#!/usr/bin/env python3
2#
3#   Copyright 2019 - 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.
16import logging
17import unittest
18
19import mock
20import os
21
22from acts.controllers import iperf_client
23from acts.controllers.iperf_client import IPerfClient
24from acts.controllers.iperf_client import IPerfClientBase
25from acts.controllers.iperf_client import IPerfClientOverAdb
26from acts.controllers.iperf_client import IPerfClientOverSsh
27
28# The position in the call tuple that represents the args array.
29ARGS = 0
30
31# The position in the call tuple that represents the kwargs dict.
32KWARGS = 1
33
34
35class IPerfClientModuleTest(unittest.TestCase):
36    """Tests the acts.controllers.iperf_client module functions."""
37
38    def test_create_can_create_client_over_adb(self):
39        self.assertIsInstance(
40            iperf_client.create([{'AndroidDevice': 'foo'}])[0],
41            IPerfClientOverAdb,
42            'Unable to create IPerfClientOverAdb from create().'
43        )
44
45    def test_create_can_create_client_over_ssh(self):
46        self.assertIsInstance(
47            iperf_client.create([{'ssh_config': {'user': '', 'host': ''}}])[0],
48            IPerfClientOverSsh,
49            'Unable to create IPerfClientOverSsh from create().'
50        )
51
52    def test_create_can_create_local_client(self):
53        self.assertIsInstance(
54            iperf_client.create([{}])[0],
55            IPerfClient,
56            'Unable to create IPerfClient from create().'
57        )
58
59
60class IPerfClientBaseTest(unittest.TestCase):
61    """Tests acts.controllers.iperf_client.IPerfClientBase."""
62
63    @mock.patch('os.makedirs')
64    def test_get_full_file_path_creates_parent_directory(self, mock_makedirs):
65        # Will never actually be created/used.
66        logging.log_path = '/tmp/unit_test_garbage'
67
68        full_file_path = IPerfClientBase._get_full_file_path(0)
69
70        self.assertTrue(
71            mock_makedirs.called,
72            'Did not attempt to create a directory.'
73        )
74        self.assertEqual(
75            os.path.dirname(full_file_path),
76            mock_makedirs.call_args[ARGS][0],
77            'The parent directory of the full file path was not created.'
78        )
79
80
81class IPerfClientTest(unittest.TestCase):
82    """Tests acts.controllers.iperf_client.IPerfClient."""
83
84    @mock.patch('builtins.open')
85    @mock.patch('subprocess.call')
86    def test_start_writes_to_full_file_path(self, mock_call, mock_open):
87        client = IPerfClient()
88        file_path = '/path/to/foo'
89        client._get_full_file_path = lambda _: file_path
90
91        client.start('127.0.0.1', 'IPERF_ARGS', 'TAG')
92
93        mock_open.assert_called_with(file_path, 'w')
94        self.assertEqual(
95            mock_call.call_args[KWARGS]['stdout'],
96            mock_open().__enter__.return_value,
97            'IPerfClient did not write the logs to the expected file.'
98        )
99
100
101class IPerfClientOverSshTest(unittest.TestCase):
102    """Test acts.controllers.iperf_client.IPerfClientOverSshTest."""
103
104    @mock.patch('builtins.open')
105    def test_start_writes_output_to_full_file_path(self, mock_open):
106        client = IPerfClientOverSsh({'host': '', 'user': ''})
107        client._ssh_session = mock.Mock()
108        file_path = '/path/to/foo'
109        client._get_full_file_path = lambda _: file_path
110
111        client.start('127.0.0.1', 'IPERF_ARGS', 'TAG')
112
113        mock_open.assert_called_with(file_path, 'w')
114        mock_open().__enter__().write.assert_called_with(
115            client._ssh_session.run().stdout
116        )
117
118
119class IPerfClientOverAdbTest(unittest.TestCase):
120    """Test acts.controllers.iperf_client.IPerfClientOverAdb."""
121
122    @mock.patch('builtins.open')
123    def test_start_writes_output_to_full_file_path(self, mock_open):
124        client = IPerfClientOverAdb(None)
125        file_path = '/path/to/foo'
126        expected_output = 'output'
127        client._get_full_file_path = lambda _: file_path
128
129        with mock.patch('acts.controllers.iperf_client.'
130                        'IPerfClientOverAdb._android_device') as adb_device:
131            adb_device.adb.shell.return_value = 'output'
132            client.start('127.0.0.1', 'IPERF_ARGS', 'TAG')
133
134        mock_open.assert_called_with(file_path, 'w')
135        mock_open().__enter__().write.assert_called_with('output')
136
137
138if __name__ == '__main__':
139    unittest.main()
140