xref: /aosp_15_r20/tools/acloud/internal/lib/cvd_runtime_config_test.py (revision 800a58d989c669b8eb8a71d8df53b1ba3d411444)
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
20
21from unittest import mock
22
23from acloud import errors
24from acloud.internal.lib import cvd_runtime_config as cf_cfg
25from acloud.internal.lib import driver_test_lib
26
27
28class CvdRuntimeconfigTest(driver_test_lib.BaseDriverTest):
29    """Test CvdRuntimeConfig."""
30
31    CF_RUNTIME_CONFIG = """
32{"x_display" : ":20",
33 "display_configs" :
34 [
35  {
36   "dpi" : 320,
37   "x_res" : 720,
38   "y_res" : 1280
39  }
40 ],
41 "instances": {
42   "2":{
43       "adb_ip_and_port": "127.0.0.1:6520",
44       "adb_host_port": 6520,
45       "instance_dir": "/path-to-instance-dir",
46       "crosvm_binary" : "/home/vsoc-01/bin/crosvm",
47       "vnc_server_port": 6444
48   }
49 }
50}
51"""
52
53    CF_RUNTIME_CONFIG_WEBRTC = """
54{"x_display" : ":20",
55 "display_configs" :
56 [
57  {
58   "dpi" : 320,
59   "x_res" : 720,
60   "y_res" : 1280
61  }
62 ],
63 "instances" : {
64   "1":{
65       "adb_ip_and_port": "127.0.0.1:6520",
66       "adb_host_port": 6520,
67       "instance_dir": "/path-to-instance-dir",
68       "vnc_server_port": 6444,
69       "virtual_disk_paths": ["/path-to-image"]
70   }
71 },
72 "enable_webrtc" : true,
73 "vnc_server_binary" : "/home/vsoc-01/bin/vnc_server",
74 "crosvm_binary" : "/home/vsoc-01/bin/crosvm",
75 "webrtc_assets_dir" : "/home/vsoc-01/usr/share/webrtc/assets",
76 "webrtc_binary" : "/home/vsoc-01/bin/webRTC",
77 "webrtc_certs_dir" : "/home/vsoc-01/usr/share/webrtc/certs",
78 "webrtc_public_ip" : "127.0.0.1"
79}
80"""
81
82    CF_RUNTIME_CONFIG_NO_INSTANCES = """
83{"x_display" : ":20",
84 "display_configs" :
85 [
86  {
87   "dpi" : 320,
88   "x_res" : 720,
89   "y_res" : 1280
90  }
91 ],
92 "instance_dir" : "fake_instance_dir",
93 "instances": {}
94}
95"""
96
97
98    # pylint: disable=protected-access, no-member
99    def testGetCuttlefishRuntimeConfig(self):
100        """Test GetCuttlefishRuntimeConfig."""
101        # Should raise error when file does not exist.
102        self.Patch(os.path, "exists", return_value=False)
103        # Verify return data.
104        self.Patch(os.path, "exists", return_value=True)
105        expected_dict = {
106                         'display_configs': [{'dpi': 320, 'x_res': 720, 'y_res': 1280}],
107                         'x_display': ':20',
108                         'instances':
109                             {'2':
110                                  {'adb_ip_and_port': '127.0.0.1:6520',
111                                   'crosvm_binary': '/home/vsoc-01/bin/crosvm',
112                                   'adb_host_port': 6520,
113                                   'instance_dir': '/path-to-instance-dir',
114                                   'vnc_server_port': 6444}
115                             },
116                        }
117        mock_open = mock.mock_open(read_data=self.CF_RUNTIME_CONFIG)
118        cf_cfg_path = "/fake-path/local-instance-2/fake.config"
119        with mock.patch("builtins.open", mock_open):
120            fake_cvd_runtime_config = cf_cfg.CvdRuntimeConfig(cf_cfg_path)
121            self.assertEqual(fake_cvd_runtime_config._config_dict, expected_dict)
122            self.assertEqual(fake_cvd_runtime_config.enable_webrtc, None)
123            self.assertEqual(fake_cvd_runtime_config.config_path,
124                             "/fake-path/local-instance-2/fake.config")
125            self.assertEqual(fake_cvd_runtime_config.instance_id, "2")
126
127        # Test read runtime config from raw_data and webrtc AVD.
128        self.Patch(cf_cfg, "_GetIdFromInstanceDirStr")
129        fake_cvd_runtime_config_webrtc = cf_cfg.CvdRuntimeConfig(
130            raw_data=self.CF_RUNTIME_CONFIG_WEBRTC)
131        cf_cfg._GetIdFromInstanceDirStr.assert_not_called()
132        self.assertEqual(fake_cvd_runtime_config_webrtc.config_path, None)
133        self.assertEqual(fake_cvd_runtime_config_webrtc.instance_id, "1")
134        self.assertEqual(fake_cvd_runtime_config_webrtc.instance_ids, ["1"])
135        self.assertEqual(fake_cvd_runtime_config_webrtc.enable_webrtc, True)
136        self.assertEqual(fake_cvd_runtime_config_webrtc.display_configs,
137                         [{'dpi': 320, 'x_res': 720, 'y_res': 1280}])
138        self.assertEqual(fake_cvd_runtime_config_webrtc.adb_ip_port, "127.0.0.1:6520")
139        self.assertEqual(fake_cvd_runtime_config_webrtc.instance_dir, "/path-to-instance-dir")
140        self.assertEqual(fake_cvd_runtime_config_webrtc.vnc_port, 6444)
141        self.assertEqual(fake_cvd_runtime_config_webrtc.adb_port, 6520)
142        self.assertEqual(fake_cvd_runtime_config_webrtc.virtual_disk_paths, ['/path-to-image'])
143        self.assertEqual(fake_cvd_runtime_config_webrtc.cvd_tools_path, "/home/vsoc-01/bin")
144
145        # Test read runtime config with no instances data.
146        fake_cvd_runtime_config_no_instances = cf_cfg.CvdRuntimeConfig(
147            raw_data=self.CF_RUNTIME_CONFIG_NO_INSTANCES)
148        self.assertEqual(fake_cvd_runtime_config_no_instances.instance_id, "1")
149        self.assertEqual(fake_cvd_runtime_config_no_instances.instance_ids, ["1"])
150
151        # Test exception with no config file and no raw_data.
152        self.assertRaises(errors.ConfigError,
153                          cf_cfg.CvdRuntimeConfig,
154                          config_path=None,
155                          raw_data=None)
156
157
158class CvdRuntimeconfigFunctionTest(driver_test_lib.BaseDriverTest):
159    """Test CvdRuntimeconfigFunctionTest class."""
160
161    # pylint: disable=protected-access
162    def testGetIdFromInstanceDirStr(self):
163        """Test GetIdFromInstanceDirStr."""
164        fake_instance_dir = "/path-to-instance-dir"
165        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir, None), None)
166
167        fake_instance_dir = "/fake-path/local-instance-1/"
168        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir, None), "1")
169
170        fake_home_path = "/home/fake_user/"
171        self.Patch(os.path, 'expanduser', return_value=fake_home_path)
172        fake_instance_dir = "/home/fake_user/local-instance/"
173        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir, None), "1")
174
175
176if __name__ == "__main__":
177    unittest.main()
178