1# Copyright 2021 - The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tests for RemoteImageRemoteInstance.""" 15 16import unittest 17 18from unittest import mock 19 20from acloud.create import remote_image_remote_instance 21from acloud.internal import constants 22from acloud.internal.lib import driver_test_lib 23from acloud.internal.lib import engprod_client 24from acloud.public import report 25from acloud.public.actions import common_operations 26from acloud.public.actions import remote_instance_cf_device_factory 27 28 29class RemoteImageRemoteInstanceTest(driver_test_lib.BaseDriverTest): 30 """Test RemoteImageRemoteInstance method.""" 31 32 def setUp(self): 33 """Initialize new RemoteImageRemoteInstance.""" 34 super().setUp() 35 self.remote_image_remote_instance = remote_image_remote_instance.RemoteImageRemoteInstance() 36 37 # pylint: disable=protected-access 38 @mock.patch.object(remote_image_remote_instance.RemoteImageRemoteInstance, 39 "_LeaseOxygenAVD") 40 @mock.patch.object(common_operations, "CreateDevices") 41 @mock.patch.object(remote_instance_cf_device_factory, 42 "RemoteInstanceDeviceFactory") 43 def testCreateAVD(self, mock_factory, mock_create_device, mock_lease): 44 """test CreateAVD.""" 45 avd_spec = mock.Mock() 46 avd_spec.oxygen = False 47 self.remote_image_remote_instance._CreateAVD( 48 avd_spec, no_prompts=True) 49 mock_factory.assert_called_once() 50 mock_create_device.assert_called_once() 51 52 avd_spec.oxygen = True 53 self.remote_image_remote_instance._CreateAVD( 54 avd_spec, no_prompts=True) 55 mock_lease.assert_called_once() 56 57 def testLeaseOxygenAVD(self): 58 """test LeaseOxygenAVD.""" 59 avd_spec = mock.Mock() 60 avd_spec.oxygen = True 61 avd_spec.remote_image = {constants.BUILD_TARGET: "fake_target", 62 constants.BUILD_ID: "fake_id"} 63 response_success = {"device": {"sessionId": "fake_device", 64 "serverUrl": "10.1.1.1"}} 65 response_fail = {"errorMessage": "Lease device fail."} 66 self.Patch(engprod_client.EngProdClient, "LeaseDevice", 67 side_effect=[response_success, response_fail]) 68 expected_status = report.Status.SUCCESS 69 reporter = self.remote_image_remote_instance._LeaseOxygenAVD(avd_spec) 70 self.assertEqual(reporter.status, expected_status) 71 72 expected_status = report.Status.FAIL 73 reporter = self.remote_image_remote_instance._LeaseOxygenAVD(avd_spec) 74 self.assertEqual(reporter.status, expected_status) 75 76 77 def testReplaceDeviceDataKeys(self): 78 """test ReplaceDeviceDataKeys.""" 79 device_data = {"sessionId": "fake_device", "serverUrl": "10.1.1.1"} 80 expected_result = {"instance_name": "fake_device", "ip": "10.1.1.1"} 81 self.remote_image_remote_instance._ReplaceDeviceDataKeys(device_data) 82 self.assertEqual(device_data, expected_result) 83 84 85if __name__ == '__main__': 86 unittest.main() 87