blob: d430ad7c8d5c89c8e3b51fb652cc60f372f0e734 [file] [log] [blame]
Michael Tang84a2ecf2016-06-07 15:10:53 -07001#!/usr/bin/python
2#
3# Copyright (c) 2016 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Unit tests for frontend/afe/rpc_utils.py."""
8
Allen Li9fb3a192016-12-14 16:22:37 -08009import mock
Michael Tang84a2ecf2016-06-07 15:10:53 -070010import unittest
11
12import common
13from autotest_lib.client.common_lib import control_data
14from autotest_lib.frontend import setup_django_environment
15from autotest_lib.frontend.afe import frontend_test_utils
16from autotest_lib.frontend.afe import rpc_utils
17
18
19class RpcUtilsTest(unittest.TestCase,
20 frontend_test_utils.FrontendTestMixin):
21 """Unit tests for functions in rpc_utils.py."""
22 def setUp(self):
23 self._frontend_common_setup()
24
25
26 def tearDown(self):
27 self._frontend_common_teardown()
28
29
30 def testCheckIsServer(self):
31 """Ensure that test type check is correct."""
32 self.assertFalse(rpc_utils._check_is_server_test(None))
33 self.assertFalse(rpc_utils._check_is_server_test(
34 control_data.CONTROL_TYPE.CLIENT))
35 self.assertFalse(rpc_utils._check_is_server_test('Client'))
36 self.assertTrue(rpc_utils._check_is_server_test(
37 control_data.CONTROL_TYPE.SERVER))
38 self.assertTrue(rpc_utils._check_is_server_test('Server'))
39 self.assertFalse(rpc_utils._check_is_server_test('InvalidType'))
40
41
Allen Liab8d3792016-12-12 18:00:31 -080042class ConvertToKwargsOnlyTest(unittest.TestCase):
43 """Unit tests for _convert_to_kwargs_only()."""
44
45 # pylint: disable=unused-argument,missing-docstring
46
47 def test_no_kwargs_in_spec(self):
48 """Test with function without kwargs."""
49 def func(a, b):
50 pass
51 got = rpc_utils._convert_to_kwargs_only(func, (1, 2), {})
52 self.assertEquals(got, {'a': 1, 'b': 2})
53
54 def test_pass_by_keyword(self):
55 """Test passing required args by keyword."""
56 def func(a, b):
57 pass
58 got = rpc_utils._convert_to_kwargs_only(func, (), {'a': 1, 'b': 2})
59 self.assertEquals(got, {'a': 1, 'b': 2})
60
61 def test_with_kwargs(self):
62 """Test with custom keyword arg."""
63 def func(a, b, **kwargs):
64 pass
65 got = rpc_utils._convert_to_kwargs_only(func, (1, 2), {'c': 3})
66 self.assertEquals(got, {'a': 1, 'b': 2, 'c': 3})
67
68 def test_with_kwargs_pass_by_keyword(self):
69 """Test passing required parameter by keyword."""
70 def func(a, b, **kwargs):
71 pass
72 got = rpc_utils._convert_to_kwargs_only(func, (1,), {'b': 2, 'c': 3})
73 self.assertEquals(got, {'a': 1, 'b': 2, 'c': 3})
74
75 def test_empty_kwargs(self):
76 """Test without passing kwargs."""
77 def func(a, b, **kwargs):
78 pass
79 got = rpc_utils._convert_to_kwargs_only(func, (1, 2), {})
80 self.assertEquals(got, {'a': 1, 'b': 2})
81
82 def test_with_varargs(self):
83 """Test against vararg function."""
84 def func(a, b, *args):
85 pass
86 got = rpc_utils._convert_to_kwargs_only(func, (1, 2, 3), {})
87 self.assertEquals(got, {'a': 1, 'b': 2, 'args': (3,)})
88
89
Allen Li9fb3a192016-12-14 16:22:37 -080090class AllowedHostsForMasterJobTest(unittest.TestCase):
91 """Unit tests for _allowed_hosts_for_master_job()."""
92
93 # pylint: disable=missing-docstring
94
95 @mock.patch.object(rpc_utils, 'bucket_hosts_by_shard', autospec=True)
96 def test_multiple_shards(self, bucket_mock):
97 bucket_mock.return_value = {
98 'shard1': [],
99 'shard2': [],
100 }
101 got = rpc_utils._allowed_hosts_for_master_job([])
102 self.assertFalse(got)
103
104 @mock.patch.object(rpc_utils, 'bucket_hosts_by_shard', autospec=True)
105 def test_one_shard_with_less_hosts(self, bucket_mock):
106 bucket_mock.return_value = {
107 'shard1': [1],
108 }
109 got = rpc_utils._allowed_hosts_for_master_job([1, 2])
110 self.assertFalse(got)
111
112 @mock.patch.object(rpc_utils, 'bucket_hosts_by_shard', autospec=True)
113 def test_one_shard_with_equal_hosts(self, bucket_mock):
114 bucket_mock.return_value = {
115 'shard1': [1, 2],
116 }
117 got = rpc_utils._allowed_hosts_for_master_job([1, 2])
118 self.assertTrue(got)
119
120 @mock.patch.object(rpc_utils, 'bucket_hosts_by_shard', autospec=True)
121 def test_no_shards(self, bucket_mock):
122 bucket_mock.return_value = {}
123 got = rpc_utils._allowed_hosts_for_master_job([1, 2])
124 self.assertTrue(got)
125
126
Michael Tang84a2ecf2016-06-07 15:10:53 -0700127if __name__ == '__main__':
128 unittest.main()