blob: 60b0fb1df3c32483d8e370479b8bb2bc22f1993f [file] [log] [blame]
Tri Vo29ac1822016-10-01 17:06:29 -07001#!/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
19import datetime
20import uuid
21
Kevin Cheng5c124ec2018-05-16 13:28:51 -070022import unittest
Tri Vo29ac1822016-10-01 17:06:29 -070023import mock
24
Kevin Cheng86d43c72018-08-30 10:59:14 -070025# pylint: disable=import-error
Kevin Cheng5c124ec2018-05-16 13:28:51 -070026import dateutil.parser
27
Tri Vo29ac1822016-10-01 17:06:29 -070028from acloud.internal.lib import auth
Tri Vo05f01892017-03-02 11:28:31 -080029from acloud.internal.lib import android_build_client
Tri Vo29ac1822016-10-01 17:06:29 -070030from acloud.internal.lib import android_compute_client
31from acloud.internal.lib import driver_test_lib
Kevin Cheng86d43c72018-08-30 10:59:14 -070032from acloud.internal.lib import gcompute_client
Tri Vo29ac1822016-10-01 17:06:29 -070033from acloud.internal.lib import gstorage_client
34from acloud.public import device_driver
35
36
Kevin Cheng5c124ec2018-05-16 13:28:51 -070037def _CreateCfg():
38 """A helper method that creates a mock configuration object."""
39 cfg = mock.MagicMock()
40 cfg.service_account_name = "fake@service.com"
41 cfg.service_account_private_key_path = "/fake/path/to/key"
42 cfg.zone = "fake_zone"
43 cfg.disk_image_name = "fake_image.tar.gz"
44 cfg.disk_image_mime_type = "fake/type"
45 cfg.storage_bucket_name = "fake_bucket"
46 cfg.extra_data_disk_size_gb = 4
47 cfg.precreated_data_image_map = {
48 4: "extradisk-image-4gb",
49 10: "extradisk-image-10gb"
50 }
Kevin Chengc330f6f2019-05-13 09:32:42 -070051 cfg.extra_scopes = None
Kevin Cheng5c124ec2018-05-16 13:28:51 -070052 cfg.ssh_private_key_path = ""
53 cfg.ssh_public_key_path = ""
54
55 return cfg
56
57
Tri Vo29ac1822016-10-01 17:06:29 -070058class DeviceDriverTest(driver_test_lib.BaseDriverTest):
59 """Test device_driver."""
60
61 def setUp(self):
62 """Set up the test."""
63 super(DeviceDriverTest, self).setUp()
64 self.build_client = mock.MagicMock()
65 self.Patch(android_build_client, "AndroidBuildClient",
Kevin Cheng5c124ec2018-05-16 13:28:51 -070066 return_value=self.build_client)
Tri Vo29ac1822016-10-01 17:06:29 -070067 self.storage_client = mock.MagicMock()
68 self.Patch(
69 gstorage_client, "StorageClient", return_value=self.storage_client)
70 self.compute_client = mock.MagicMock()
71 self.Patch(
72 android_compute_client,
73 "AndroidComputeClient",
74 return_value=self.compute_client)
75 self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
76
Tri Vo29ac1822016-10-01 17:06:29 -070077 def testCreateAndroidVirtualDevices(self):
78 """Test CreateAndroidVirtualDevices."""
Kevin Cheng5c124ec2018-05-16 13:28:51 -070079 cfg = _CreateCfg()
Tri Vo29ac1822016-10-01 17:06:29 -070080 fake_gs_url = "fake_gs_url"
Kevin Cheng86d43c72018-08-30 10:59:14 -070081 fake_ip = gcompute_client.IP(external="140.1.1.1", internal="10.1.1.1")
Tri Vo29ac1822016-10-01 17:06:29 -070082 fake_instance = "fake-instance"
83 fake_image = "fake-image"
84 fake_build_target = "fake_target"
85 fake_build_id = "12345"
86
87 # Mock uuid
88 fake_uuid = mock.MagicMock(hex="1234")
89 self.Patch(uuid, "uuid4", return_value=fake_uuid)
90 fake_gs_object = fake_uuid.hex + "-" + cfg.disk_image_name
91 self.storage_client.GetUrl.return_value = fake_gs_url
92
93 # Mock compute client methods
94 disk_name = "extradisk-image-4gb"
95 self.compute_client.GetInstanceIP.return_value = fake_ip
96 self.compute_client.GenerateImageName.return_value = fake_image
97 self.compute_client.GenerateInstanceName.return_value = fake_instance
98 self.compute_client.GetDataDiskName.return_value = disk_name
99
100 # Verify
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700101 report = device_driver.CreateAndroidVirtualDevices(
102 cfg, fake_build_target, fake_build_id)
Tri Vo29ac1822016-10-01 17:06:29 -0700103 self.build_client.CopyTo.assert_called_with(
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700104 fake_build_target, fake_build_id, artifact_name=cfg.disk_image_name,
105 destination_bucket=cfg.storage_bucket_name,
106 destination_path=fake_gs_object)
Tri Vo29ac1822016-10-01 17:06:29 -0700107 self.compute_client.CreateImage.assert_called_with(
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700108 image_name=fake_image, source_uri=fake_gs_url)
Tri Vo29ac1822016-10-01 17:06:29 -0700109 self.compute_client.CreateInstance.assert_called_with(
Kevin Chengb5963882018-05-09 00:06:27 -0700110 instance=fake_instance,
111 image_name=fake_image,
chojoyce7a361732018-11-26 16:26:13 +0800112 extra_disk_name=disk_name,
Kevin Chengc330f6f2019-05-13 09:32:42 -0700113 avd_spec=None,
114 extra_scopes=None)
Tri Vo29ac1822016-10-01 17:06:29 -0700115 self.compute_client.DeleteImage.assert_called_with(fake_image)
116 self.storage_client.Delete(cfg.storage_bucket_name, fake_gs_object)
117
118 self.assertEquals(
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700119 report.data,
Tri Vo29ac1822016-10-01 17:06:29 -0700120 {
121 "devices": [
122 {
123 "instance_name": fake_instance,
Kevin Cheng86d43c72018-08-30 10:59:14 -0700124 "ip": fake_ip.external,
Tri Vo29ac1822016-10-01 17:06:29 -0700125 },
126 ],
127 }
128 )
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700129 self.assertEquals(report.command, "create")
130 self.assertEquals(report.status, "SUCCESS")
Tri Vo29ac1822016-10-01 17:06:29 -0700131
Kevin Cheng86d43c72018-08-30 10:59:14 -0700132 # pylint: disable=invalid-name
133 def testCreateAndroidVirtualDevicesInternalIP(self):
134 """Test CreateAndroidVirtualDevices with internal IP."""
135 cfg = _CreateCfg()
136 fake_ip = gcompute_client.IP(external="140.1.1.1", internal="10.1.1.1")
137 fake_instance = "fake-instance"
138 fake_build_target = "fake_target"
139 fake_build_id = "12345"
140
141 self.compute_client.GetInstanceIP.return_value = fake_ip
142 self.compute_client.GenerateInstanceName.return_value = fake_instance
143
144 report = device_driver.CreateAndroidVirtualDevices(
145 cfg, fake_build_target, fake_build_id, report_internal_ip=True)
146
147 self.assertEquals(
148 report.data,
149 {
150 "devices": [
151 {
152 "instance_name": fake_instance,
153 "ip": fake_ip.internal,
154 },
155 ],
156 }
157 )
Tri Vo29ac1822016-10-01 17:06:29 -0700158
159 def testDeleteAndroidVirtualDevices(self):
160 """Test DeleteAndroidVirtualDevices."""
161 instance_names = ["fake-instance-1", "fake-instance-2"]
162 self.compute_client.DeleteInstances.return_value = (instance_names, [],
163 [])
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700164 cfg = _CreateCfg()
165 report = device_driver.DeleteAndroidVirtualDevices(cfg, instance_names)
Tri Vo29ac1822016-10-01 17:06:29 -0700166 self.compute_client.DeleteInstances.assert_called_once_with(
167 instance_names, cfg.zone)
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700168 self.assertEquals(report.data, {
Tri Vo29ac1822016-10-01 17:06:29 -0700169 "deleted": [
170 {
171 "name": instance_names[0],
172 "type": "instance",
173 },
174 {
175 "name": instance_names[1],
176 "type": "instance",
177 },
178 ],
179 })
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700180 self.assertEquals(report.command, "delete")
181 self.assertEquals(report.status, "SUCCESS")
Tri Vo29ac1822016-10-01 17:06:29 -0700182
183 def testCleanup(self):
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700184 """Test Cleanup."""
Tri Vo29ac1822016-10-01 17:06:29 -0700185 expiration_mins = 30
186 before_deadline = "2015-10-29T12:00:30.018-07:00"
187 after_deadline = "2015-10-29T12:45:30.018-07:00"
188 now = "2015-10-29T13:00:30.018-07:00"
189 self.Patch(device_driver, "datetime")
190 device_driver.datetime.datetime.now.return_value = dateutil.parser.parse(
191 now)
192 device_driver.datetime.timedelta.return_value = datetime.timedelta(
193 minutes=expiration_mins)
194 fake_instances = [
195 {
196 "name": "fake_instance_1",
197 "creationTimestamp": before_deadline,
198 }, {
199 "name": "fake_instance_2",
200 "creationTimestamp": after_deadline,
201 }
202 ]
203 fake_images = [
204 {
205 "name": "extradisk-image-4gb",
206 "creationTimestamp": before_deadline,
207 }, {
208 "name": "fake_image_1",
209 "creationTimestamp": before_deadline,
210 }, {
211 "name": "fake_image_2",
212 "creationTimestamp": after_deadline,
213 }
214 ]
215 fake_disks = [
216 {
217 "name": "fake_disk_1",
218 "creationTimestamp": before_deadline,
219 }, {
220 "name": "fake_disk_2",
221 "creationTimestamp": before_deadline,
222 "users": ["some-instance-using-the-disk"]
223 }, {
224 "name": "fake_disk_3",
225 "creationTimestamp": after_deadline,
226 }
227 ]
228 fake_objects = [
229 {
230 "name": "fake_object_1",
231 "timeCreated": before_deadline,
232 }, {
233 "name": "fake_object_2",
234 "timeCreated": after_deadline,
235 }
236 ]
237 self.compute_client.ListInstances.return_value = fake_instances
238 self.compute_client.ListImages.return_value = fake_images
239 self.compute_client.ListDisks.return_value = fake_disks
240 self.storage_client.List.return_value = fake_objects
241 self.compute_client.DeleteInstances.return_value = (
242 ["fake_instance_1"], [], [])
243 self.compute_client.DeleteImages.return_value = (["fake_image_1"], [],
244 [])
245 self.compute_client.DeleteDisks.return_value = (["fake_disk_1"], [],
246 [])
247 self.storage_client.DeleteFiles.return_value = (["fake_object_1"], [],
248 [])
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700249 cfg = _CreateCfg()
250 report = device_driver.Cleanup(cfg, expiration_mins)
251 self.assertEqual(report.errors, [])
Tri Vo29ac1822016-10-01 17:06:29 -0700252 expected_report_data = {
253 "deleted": [
254 {"name": "fake_instance_1",
255 "type": "instance"},
256 {"name": "fake_image_1",
257 "type": "image"},
258 {"name": "fake_disk_1",
259 "type": "disk"},
260 {"name": "fake_object_1",
261 "type": "cached_build_artifact"},
262 ]
263 }
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700264 self.assertEqual(report.data, expected_report_data)
Tri Vo29ac1822016-10-01 17:06:29 -0700265
266 self.compute_client.ListInstances.assert_called_once_with(
267 zone=cfg.zone)
268 self.compute_client.DeleteInstances.assert_called_once_with(
269 instances=["fake_instance_1"], zone=cfg.zone)
270
271 self.compute_client.ListImages.assert_called_once_with()
272 self.compute_client.DeleteImages.assert_called_once_with(
273 image_names=["fake_image_1"])
274
275 self.compute_client.ListDisks.assert_called_once_with(zone=cfg.zone)
276 self.compute_client.DeleteDisks.assert_called_once_with(
277 disk_names=["fake_disk_1"], zone=cfg.zone)
278
279 self.storage_client.List.assert_called_once_with(
280 bucket_name=cfg.storage_bucket_name)
281 self.storage_client.DeleteFiles.assert_called_once_with(
282 bucket_name=cfg.storage_bucket_name,
283 object_names=["fake_object_1"])
284
285
286if __name__ == "__main__":
287 unittest.main()