1#!/usr/bin/env python
2#
3# Copyright 2020 - 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"""Tests for cvd_runtime_config class."""
17
18import os
19import unittest
20import mock
21import six
22
23from acloud.internal.lib import cvd_runtime_config as cf_cfg
24from acloud.internal.lib import driver_test_lib
25
26
27class CvdRuntimeconfigTest(driver_test_lib.BaseDriverTest):
28    """Test CvdRuntimeConfig."""
29
30    CF_RUNTIME_CONFIG = """
31{"x_display" : ":20",
32 "x_res" : 720,
33 "y_res" : 1280,
34 "instances": {
35   "2":{
36       "adb_ip_and_port": "127.0.0.1:6520",
37       "host_port": 6520,
38       "instance_dir": "/path-to-instance-dir",
39       "vnc_server_port": 6444
40   }
41 }
42}
43"""
44
45    CF_RUNTIME_CONFIG_WEBRTC = """
46{"x_display" : ":20",
47 "x_res" : 720,
48 "y_res" : 1280,
49 "dpi" : 320,
50 "instances" : {
51   "1":{
52       "adb_ip_and_port": "127.0.0.1:6520",
53       "host_port": 6520,
54       "instance_dir": "/path-to-instance-dir",
55       "vnc_server_port": 6444,
56       "virtual_disk_paths": ["/path-to-image"]
57   }
58 },
59 "enable_webrtc" : true,
60 "vnc_server_binary" : "/home/vsoc-01/bin/vnc_server",
61 "adb_connector_binary" : "/home/vsoc-01/bin/adb_connector",
62 "webrtc_assets_dir" : "/home/vsoc-01/usr/share/webrtc/assets",
63 "webrtc_binary" : "/home/vsoc-01/bin/webRTC",
64 "webrtc_certs_dir" : "/home/vsoc-01/usr/share/webrtc/certs",
65 "webrtc_enable_adb_websocket" : false,
66 "webrtc_public_ip" : "127.0.0.1"
67}
68"""
69
70
71    # pylint: disable=protected-access, no-member
72    def testGetCuttlefishRuntimeConfig(self):
73        """Test GetCuttlefishRuntimeConfig."""
74        # Should raise error when file does not exist.
75        self.Patch(os.path, "exists", return_value=False)
76        # Verify return data.
77        self.Patch(os.path, "exists", return_value=True)
78        expected_dict = {u'y_res': 1280,
79                         u'x_res': 720,
80                         u'x_display': u':20',
81                         u'instances':
82                             {u'2':
83                                  {u'adb_ip_and_port': u'127.0.0.1:6520',
84                                   u'host_port': 6520,
85                                   u'instance_dir': u'/path-to-instance-dir',
86                                   u'vnc_server_port': 6444}
87                             },
88                        }
89        mock_open = mock.mock_open(read_data=self.CF_RUNTIME_CONFIG)
90        cf_cfg_path = "/fake-path/local-instance-2/fake.config"
91        with mock.patch.object(six.moves.builtins, "open", mock_open):
92            fake_cvd_runtime_config = cf_cfg.CvdRuntimeConfig(cf_cfg_path)
93            self.assertEqual(fake_cvd_runtime_config._config_dict, expected_dict)
94            self.assertEqual(fake_cvd_runtime_config.enable_webrtc, None)
95            self.assertEqual(fake_cvd_runtime_config.config_path,
96                             "/fake-path/local-instance-2/fake.config")
97            self.assertEqual(fake_cvd_runtime_config.instance_id, "2")
98
99        # Test read runtime config from raw_data and webrtc AVD.
100        self.Patch(cf_cfg, "_GetIdFromInstanceDirStr")
101        fake_cvd_runtime_config_webrtc = cf_cfg.CvdRuntimeConfig(
102            raw_data=self.CF_RUNTIME_CONFIG_WEBRTC)
103        cf_cfg._GetIdFromInstanceDirStr.assert_not_called()
104        self.assertEqual(fake_cvd_runtime_config_webrtc.config_path, None)
105        self.assertEqual(fake_cvd_runtime_config_webrtc.instance_id, "1")
106        self.assertEqual(fake_cvd_runtime_config_webrtc.enable_webrtc, True)
107        self.assertEqual(fake_cvd_runtime_config_webrtc.x_res, 720)
108        self.assertEqual(fake_cvd_runtime_config_webrtc.y_res, 1280)
109        self.assertEqual(fake_cvd_runtime_config_webrtc.dpi, 320)
110        self.assertEqual(fake_cvd_runtime_config_webrtc.adb_ip_port, "127.0.0.1:6520")
111        self.assertEqual(fake_cvd_runtime_config_webrtc.instance_dir, "/path-to-instance-dir")
112        self.assertEqual(fake_cvd_runtime_config_webrtc.vnc_port, 6444)
113        self.assertEqual(fake_cvd_runtime_config_webrtc.adb_port, 6520)
114        self.assertEqual(fake_cvd_runtime_config_webrtc.virtual_disk_paths, ['/path-to-image'])
115        self.assertEqual(fake_cvd_runtime_config_webrtc.cvd_tools_path, "/home/vsoc-01/bin")
116
117
118class CvdRuntimeconfigFunctionTest(driver_test_lib.BaseDriverTest):
119    """Test CvdRuntimeconfigFunctionTest class."""
120
121    # pylint: disable=protected-access
122    def testGetIdFromInstanceDirStr(self):
123        """Test GetIdFromInstanceDirStr."""
124        fake_instance_dir = "/path-to-instance-dir"
125        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir), None)
126
127        fake_instance_dir = "/fake-path/local-instance-1/"
128        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir), "1")
129
130        fake_home_path = "/home/fake_user/"
131        self.Patch(os.path, 'expanduser', return_value=fake_home_path)
132        fake_instance_dir = "/home/fake_user/local-instance/"
133        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir), "1")
134
135
136if __name__ == "__main__":
137    unittest.main()
138