Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 - 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 | |
| 17 | """Tests for acloud.public.device_driver.""" |
| 18 | |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 19 | import uuid |
| 20 | |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 21 | import unittest |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 22 | import mock |
| 23 | |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 24 | from acloud.internal.lib import auth |
Tri Vo | 05f0189 | 2017-03-02 11:28:31 -0800 | [diff] [blame] | 25 | from acloud.internal.lib import android_build_client |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 26 | from acloud.internal.lib import android_compute_client |
| 27 | from acloud.internal.lib import driver_test_lib |
| 28 | from acloud.internal.lib import gstorage_client |
herbertxue | 140b6b4 | 2019-10-14 20:24:10 +0800 | [diff] [blame] | 29 | from acloud.internal.lib import ssh |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 30 | from acloud.public import device_driver |
| 31 | |
| 32 | |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 33 | def _CreateCfg(): |
| 34 | """A helper method that creates a mock configuration object.""" |
| 35 | cfg = mock.MagicMock() |
| 36 | cfg.service_account_name = "fake@service.com" |
| 37 | cfg.service_account_private_key_path = "/fake/path/to/key" |
| 38 | cfg.zone = "fake_zone" |
| 39 | cfg.disk_image_name = "fake_image.tar.gz" |
| 40 | cfg.disk_image_mime_type = "fake/type" |
| 41 | cfg.storage_bucket_name = "fake_bucket" |
| 42 | cfg.extra_data_disk_size_gb = 4 |
| 43 | cfg.precreated_data_image_map = { |
| 44 | 4: "extradisk-image-4gb", |
| 45 | 10: "extradisk-image-10gb" |
| 46 | } |
Kevin Cheng | c330f6f | 2019-05-13 09:32:42 -0700 | [diff] [blame] | 47 | cfg.extra_scopes = None |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 48 | cfg.ssh_private_key_path = "" |
| 49 | cfg.ssh_public_key_path = "" |
| 50 | |
| 51 | return cfg |
| 52 | |
| 53 | |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 54 | class DeviceDriverTest(driver_test_lib.BaseDriverTest): |
| 55 | """Test device_driver.""" |
| 56 | |
| 57 | def setUp(self): |
| 58 | """Set up the test.""" |
| 59 | super(DeviceDriverTest, self).setUp() |
| 60 | self.build_client = mock.MagicMock() |
| 61 | self.Patch(android_build_client, "AndroidBuildClient", |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 62 | return_value=self.build_client) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 63 | self.storage_client = mock.MagicMock() |
| 64 | self.Patch( |
| 65 | gstorage_client, "StorageClient", return_value=self.storage_client) |
| 66 | self.compute_client = mock.MagicMock() |
| 67 | self.Patch( |
| 68 | android_compute_client, |
| 69 | "AndroidComputeClient", |
| 70 | return_value=self.compute_client) |
| 71 | self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock()) |
chojoyce | c60fa58 | 2019-07-11 16:09:02 +0800 | [diff] [blame] | 72 | self.fake_avd_spec = mock.MagicMock() |
| 73 | self.fake_avd_spec.unlock_screen = False |
| 74 | self.fake_avd_spec.client_adb_port = 1234 |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 75 | |
chojoyce | acc404b | 2019-06-25 16:06:45 +0800 | [diff] [blame] | 76 | def testCreateGCETypeAVD(self): |
| 77 | """Test CreateGCETypeAVD.""" |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 78 | cfg = _CreateCfg() |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 79 | fake_gs_url = "fake_gs_url" |
herbertxue | 140b6b4 | 2019-10-14 20:24:10 +0800 | [diff] [blame] | 80 | fake_ip = ssh.IP(external="140.1.1.1", internal="10.1.1.1") |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 81 | fake_instance = "fake-instance" |
| 82 | fake_image = "fake-image" |
| 83 | fake_build_target = "fake_target" |
| 84 | fake_build_id = "12345" |
| 85 | |
| 86 | # Mock uuid |
| 87 | fake_uuid = mock.MagicMock(hex="1234") |
| 88 | self.Patch(uuid, "uuid4", return_value=fake_uuid) |
| 89 | fake_gs_object = fake_uuid.hex + "-" + cfg.disk_image_name |
| 90 | self.storage_client.GetUrl.return_value = fake_gs_url |
| 91 | |
| 92 | # Mock compute client methods |
| 93 | disk_name = "extradisk-image-4gb" |
| 94 | self.compute_client.GetInstanceIP.return_value = fake_ip |
| 95 | self.compute_client.GenerateImageName.return_value = fake_image |
| 96 | self.compute_client.GenerateInstanceName.return_value = fake_instance |
| 97 | self.compute_client.GetDataDiskName.return_value = disk_name |
| 98 | |
| 99 | # Verify |
chojoyce | acc404b | 2019-06-25 16:06:45 +0800 | [diff] [blame] | 100 | report = device_driver.CreateGCETypeAVD( |
chojoyce | c60fa58 | 2019-07-11 16:09:02 +0800 | [diff] [blame] | 101 | cfg, fake_build_target, fake_build_id, avd_spec=self.fake_avd_spec) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 102 | self.build_client.CopyTo.assert_called_with( |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 103 | fake_build_target, fake_build_id, artifact_name=cfg.disk_image_name, |
| 104 | destination_bucket=cfg.storage_bucket_name, |
| 105 | destination_path=fake_gs_object) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 106 | self.compute_client.CreateImage.assert_called_with( |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 107 | image_name=fake_image, source_uri=fake_gs_url) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 108 | self.compute_client.CreateInstance.assert_called_with( |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 109 | instance=fake_instance, |
| 110 | image_name=fake_image, |
chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 111 | extra_disk_name=disk_name, |
chojoyce | c60fa58 | 2019-07-11 16:09:02 +0800 | [diff] [blame] | 112 | avd_spec=self.fake_avd_spec, |
Kevin Cheng | c330f6f | 2019-05-13 09:32:42 -0700 | [diff] [blame] | 113 | extra_scopes=None) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 114 | self.compute_client.DeleteImage.assert_called_with(fake_image) |
| 115 | self.storage_client.Delete(cfg.storage_bucket_name, fake_gs_object) |
| 116 | |
chojoyce | 7307f5f | 2019-09-18 17:56:05 +0800 | [diff] [blame] | 117 | self.assertEqual( |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 118 | report.data, |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 119 | { |
| 120 | "devices": [ |
| 121 | { |
| 122 | "instance_name": fake_instance, |
Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 123 | "ip": fake_ip.external, |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 124 | }, |
| 125 | ], |
| 126 | } |
| 127 | ) |
chojoyce | 7307f5f | 2019-09-18 17:56:05 +0800 | [diff] [blame] | 128 | self.assertEqual(report.command, "create") |
| 129 | self.assertEqual(report.status, "SUCCESS") |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 130 | |
Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 131 | # pylint: disable=invalid-name |
chojoyce | acc404b | 2019-06-25 16:06:45 +0800 | [diff] [blame] | 132 | def testCreateGCETypeAVDInternalIP(self): |
| 133 | """Test CreateGCETypeAVD with internal IP.""" |
Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 134 | cfg = _CreateCfg() |
herbertxue | 140b6b4 | 2019-10-14 20:24:10 +0800 | [diff] [blame] | 135 | fake_ip = ssh.IP(external="140.1.1.1", internal="10.1.1.1") |
Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 136 | fake_instance = "fake-instance" |
| 137 | fake_build_target = "fake_target" |
| 138 | fake_build_id = "12345" |
| 139 | |
| 140 | self.compute_client.GetInstanceIP.return_value = fake_ip |
| 141 | self.compute_client.GenerateInstanceName.return_value = fake_instance |
| 142 | |
chojoyce | acc404b | 2019-06-25 16:06:45 +0800 | [diff] [blame] | 143 | report = device_driver.CreateGCETypeAVD( |
chojoyce | c60fa58 | 2019-07-11 16:09:02 +0800 | [diff] [blame] | 144 | cfg, fake_build_target, fake_build_id, report_internal_ip=True, |
| 145 | avd_spec=self.fake_avd_spec) |
Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 146 | |
chojoyce | 7307f5f | 2019-09-18 17:56:05 +0800 | [diff] [blame] | 147 | self.assertEqual( |
Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 148 | report.data, |
| 149 | { |
| 150 | "devices": [ |
| 151 | { |
| 152 | "instance_name": fake_instance, |
| 153 | "ip": fake_ip.internal, |
| 154 | }, |
| 155 | ], |
| 156 | } |
| 157 | ) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 158 | |
| 159 | def testDeleteAndroidVirtualDevices(self): |
| 160 | """Test DeleteAndroidVirtualDevices.""" |
herbertxue | 7a50121 | 2019-08-29 15:37:13 +0800 | [diff] [blame] | 161 | cfg = _CreateCfg() |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 162 | instance_names = ["fake-instance-1", "fake-instance-2"] |
herbertxue | 7a50121 | 2019-08-29 15:37:13 +0800 | [diff] [blame] | 163 | self.compute_client.GetZonesByInstances.return_value = ( |
| 164 | {cfg.zone: instance_names}) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 165 | self.compute_client.DeleteInstances.return_value = (instance_names, [], |
| 166 | []) |
Kevin Cheng | 5c124ec | 2018-05-16 13:28:51 -0700 | [diff] [blame] | 167 | report = device_driver.DeleteAndroidVirtualDevices(cfg, instance_names) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 168 | self.compute_client.DeleteInstances.assert_called_once_with( |
| 169 | instance_names, cfg.zone) |
chojoyce | 7307f5f | 2019-09-18 17:56:05 +0800 | [diff] [blame] | 170 | self.assertEqual(report.data, { |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 171 | "deleted": [ |
| 172 | { |
| 173 | "name": instance_names[0], |
| 174 | "type": "instance", |
| 175 | }, |
| 176 | { |
| 177 | "name": instance_names[1], |
| 178 | "type": "instance", |
| 179 | }, |
| 180 | ], |
| 181 | }) |
chojoyce | 7307f5f | 2019-09-18 17:56:05 +0800 | [diff] [blame] | 182 | self.assertEqual(report.command, "delete") |
| 183 | self.assertEqual(report.status, "SUCCESS") |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 184 | |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 185 | |
| 186 | if __name__ == "__main__": |
| 187 | unittest.main() |