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