blob: b38c405d2c831420c8343c3232e942c103892f60 [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 }
51 cfg.ssh_private_key_path = ""
52 cfg.ssh_public_key_path = ""
53
54 return cfg
55
56
Tri Vo29ac1822016-10-01 17:06:29 -070057class DeviceDriverTest(driver_test_lib.BaseDriverTest):
58 """Test device_driver."""
59
60 def setUp(self):
61 """Set up the test."""
62 super(DeviceDriverTest, self).setUp()
63 self.build_client = mock.MagicMock()
64 self.Patch(android_build_client, "AndroidBuildClient",
Kevin Cheng5c124ec2018-05-16 13:28:51 -070065 return_value=self.build_client)
Tri Vo29ac1822016-10-01 17:06:29 -070066 self.storage_client = mock.MagicMock()
67 self.Patch(
68 gstorage_client, "StorageClient", return_value=self.storage_client)
69 self.compute_client = mock.MagicMock()
70 self.Patch(
71 android_compute_client,
72 "AndroidComputeClient",
73 return_value=self.compute_client)
74 self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
75
Tri Vo29ac1822016-10-01 17:06:29 -070076 def testCreateAndroidVirtualDevices(self):
77 """Test CreateAndroidVirtualDevices."""
Kevin Cheng5c124ec2018-05-16 13:28:51 -070078 cfg = _CreateCfg()
Tri Vo29ac1822016-10-01 17:06:29 -070079 fake_gs_url = "fake_gs_url"
Kevin Cheng86d43c72018-08-30 10:59:14 -070080 fake_ip = gcompute_client.IP(external="140.1.1.1", internal="10.1.1.1")
Tri Vo29ac1822016-10-01 17:06:29 -070081 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
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700100 report = device_driver.CreateAndroidVirtualDevices(
101 cfg, fake_build_target, fake_build_id)
Tri Vo29ac1822016-10-01 17:06:29 -0700102 self.build_client.CopyTo.assert_called_with(
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700103 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 Vo29ac1822016-10-01 17:06:29 -0700106 self.compute_client.CreateImage.assert_called_with(
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700107 image_name=fake_image, source_uri=fake_gs_url)
Tri Vo29ac1822016-10-01 17:06:29 -0700108 self.compute_client.CreateInstance.assert_called_with(
Kevin Chengb5963882018-05-09 00:06:27 -0700109 instance=fake_instance,
110 image_name=fake_image,
111 extra_disk_name=disk_name)
Tri Vo29ac1822016-10-01 17:06:29 -0700112 self.compute_client.DeleteImage.assert_called_with(fake_image)
113 self.storage_client.Delete(cfg.storage_bucket_name, fake_gs_object)
114
115 self.assertEquals(
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700116 report.data,
Tri Vo29ac1822016-10-01 17:06:29 -0700117 {
118 "devices": [
119 {
120 "instance_name": fake_instance,
Kevin Cheng86d43c72018-08-30 10:59:14 -0700121 "ip": fake_ip.external,
Tri Vo29ac1822016-10-01 17:06:29 -0700122 },
123 ],
124 }
125 )
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700126 self.assertEquals(report.command, "create")
127 self.assertEquals(report.status, "SUCCESS")
Tri Vo29ac1822016-10-01 17:06:29 -0700128
Kevin Cheng86d43c72018-08-30 10:59:14 -0700129 # pylint: disable=invalid-name
130 def testCreateAndroidVirtualDevicesInternalIP(self):
131 """Test CreateAndroidVirtualDevices with internal IP."""
132 cfg = _CreateCfg()
133 fake_ip = gcompute_client.IP(external="140.1.1.1", internal="10.1.1.1")
134 fake_instance = "fake-instance"
135 fake_build_target = "fake_target"
136 fake_build_id = "12345"
137
138 self.compute_client.GetInstanceIP.return_value = fake_ip
139 self.compute_client.GenerateInstanceName.return_value = fake_instance
140
141 report = device_driver.CreateAndroidVirtualDevices(
142 cfg, fake_build_target, fake_build_id, report_internal_ip=True)
143
144 self.assertEquals(
145 report.data,
146 {
147 "devices": [
148 {
149 "instance_name": fake_instance,
150 "ip": fake_ip.internal,
151 },
152 ],
153 }
154 )
Tri Vo29ac1822016-10-01 17:06:29 -0700155
156 def testDeleteAndroidVirtualDevices(self):
157 """Test DeleteAndroidVirtualDevices."""
158 instance_names = ["fake-instance-1", "fake-instance-2"]
159 self.compute_client.DeleteInstances.return_value = (instance_names, [],
160 [])
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700161 cfg = _CreateCfg()
162 report = device_driver.DeleteAndroidVirtualDevices(cfg, instance_names)
Tri Vo29ac1822016-10-01 17:06:29 -0700163 self.compute_client.DeleteInstances.assert_called_once_with(
164 instance_names, cfg.zone)
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700165 self.assertEquals(report.data, {
Tri Vo29ac1822016-10-01 17:06:29 -0700166 "deleted": [
167 {
168 "name": instance_names[0],
169 "type": "instance",
170 },
171 {
172 "name": instance_names[1],
173 "type": "instance",
174 },
175 ],
176 })
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700177 self.assertEquals(report.command, "delete")
178 self.assertEquals(report.status, "SUCCESS")
Tri Vo29ac1822016-10-01 17:06:29 -0700179
180 def testCleanup(self):
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700181 """Test Cleanup."""
Tri Vo29ac1822016-10-01 17:06:29 -0700182 expiration_mins = 30
183 before_deadline = "2015-10-29T12:00:30.018-07:00"
184 after_deadline = "2015-10-29T12:45:30.018-07:00"
185 now = "2015-10-29T13:00:30.018-07:00"
186 self.Patch(device_driver, "datetime")
187 device_driver.datetime.datetime.now.return_value = dateutil.parser.parse(
188 now)
189 device_driver.datetime.timedelta.return_value = datetime.timedelta(
190 minutes=expiration_mins)
191 fake_instances = [
192 {
193 "name": "fake_instance_1",
194 "creationTimestamp": before_deadline,
195 }, {
196 "name": "fake_instance_2",
197 "creationTimestamp": after_deadline,
198 }
199 ]
200 fake_images = [
201 {
202 "name": "extradisk-image-4gb",
203 "creationTimestamp": before_deadline,
204 }, {
205 "name": "fake_image_1",
206 "creationTimestamp": before_deadline,
207 }, {
208 "name": "fake_image_2",
209 "creationTimestamp": after_deadline,
210 }
211 ]
212 fake_disks = [
213 {
214 "name": "fake_disk_1",
215 "creationTimestamp": before_deadline,
216 }, {
217 "name": "fake_disk_2",
218 "creationTimestamp": before_deadline,
219 "users": ["some-instance-using-the-disk"]
220 }, {
221 "name": "fake_disk_3",
222 "creationTimestamp": after_deadline,
223 }
224 ]
225 fake_objects = [
226 {
227 "name": "fake_object_1",
228 "timeCreated": before_deadline,
229 }, {
230 "name": "fake_object_2",
231 "timeCreated": after_deadline,
232 }
233 ]
234 self.compute_client.ListInstances.return_value = fake_instances
235 self.compute_client.ListImages.return_value = fake_images
236 self.compute_client.ListDisks.return_value = fake_disks
237 self.storage_client.List.return_value = fake_objects
238 self.compute_client.DeleteInstances.return_value = (
239 ["fake_instance_1"], [], [])
240 self.compute_client.DeleteImages.return_value = (["fake_image_1"], [],
241 [])
242 self.compute_client.DeleteDisks.return_value = (["fake_disk_1"], [],
243 [])
244 self.storage_client.DeleteFiles.return_value = (["fake_object_1"], [],
245 [])
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700246 cfg = _CreateCfg()
247 report = device_driver.Cleanup(cfg, expiration_mins)
248 self.assertEqual(report.errors, [])
Tri Vo29ac1822016-10-01 17:06:29 -0700249 expected_report_data = {
250 "deleted": [
251 {"name": "fake_instance_1",
252 "type": "instance"},
253 {"name": "fake_image_1",
254 "type": "image"},
255 {"name": "fake_disk_1",
256 "type": "disk"},
257 {"name": "fake_object_1",
258 "type": "cached_build_artifact"},
259 ]
260 }
Kevin Cheng5c124ec2018-05-16 13:28:51 -0700261 self.assertEqual(report.data, expected_report_data)
Tri Vo29ac1822016-10-01 17:06:29 -0700262
263 self.compute_client.ListInstances.assert_called_once_with(
264 zone=cfg.zone)
265 self.compute_client.DeleteInstances.assert_called_once_with(
266 instances=["fake_instance_1"], zone=cfg.zone)
267
268 self.compute_client.ListImages.assert_called_once_with()
269 self.compute_client.DeleteImages.assert_called_once_with(
270 image_names=["fake_image_1"])
271
272 self.compute_client.ListDisks.assert_called_once_with(zone=cfg.zone)
273 self.compute_client.DeleteDisks.assert_called_once_with(
274 disk_names=["fake_disk_1"], zone=cfg.zone)
275
276 self.storage_client.List.assert_called_once_with(
277 bucket_name=cfg.storage_bucket_name)
278 self.storage_client.DeleteFiles.assert_called_once_with(
279 bucket_name=cfg.storage_bucket_name,
280 object_names=["fake_object_1"])
281
282
283if __name__ == "__main__":
284 unittest.main()