| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2018 - 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. |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 16 | """Tests for acloud.public.actions.common_operations.""" |
| 17 | |
| 18 | from __future__ import absolute_import |
| 19 | from __future__ import division |
| 20 | from __future__ import print_function |
| 21 | |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 22 | import unittest |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 23 | import mock |
| 24 | |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 25 | from acloud.internal.lib import android_build_client |
| 26 | from acloud.internal.lib import android_compute_client |
| 27 | from acloud.internal.lib import auth |
| 28 | from acloud.internal.lib import driver_test_lib |
| Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 29 | from acloud.internal.lib import gcompute_client |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 30 | from acloud.public import report |
| 31 | from acloud.public.actions import common_operations |
| 32 | |
| 33 | |
| 34 | class CommonOperationsTest(driver_test_lib.BaseDriverTest): |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 35 | """Test Common Operations.""" |
| Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 36 | IP = gcompute_client.IP(external="127.0.0.1", internal="10.0.0.1") |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 37 | INSTANCE = "fake-instance" |
| 38 | CMD = "test-cmd" |
| herbertxue | 543457e | 2019-03-18 18:13:34 +0800 | [diff] [blame] | 39 | AVD_TYPE = "fake-type" |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 40 | |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 41 | def setUp(self): |
| 42 | """Set up the test.""" |
| 43 | super(CommonOperationsTest, self).setUp() |
| 44 | self.build_client = mock.MagicMock() |
| 45 | self.device_factory = mock.MagicMock() |
| 46 | self.Patch( |
| 47 | android_build_client, |
| 48 | "AndroidBuildClient", |
| 49 | return_value=self.build_client) |
| 50 | self.compute_client = mock.MagicMock() |
| 51 | self.Patch( |
| 52 | android_compute_client, |
| 53 | "AndroidComputeClient", |
| 54 | return_value=self.compute_client) |
| 55 | self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock()) |
| 56 | self.Patch(self.compute_client, "GetInstanceIP", return_value=self.IP) |
| 57 | self.Patch( |
| 58 | self.device_factory, "CreateInstance", return_value=self.INSTANCE) |
| 59 | self.Patch( |
| 60 | self.device_factory, |
| 61 | "GetComputeClient", |
| 62 | return_value=self.compute_client) |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 63 | |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 64 | @staticmethod |
| 65 | def _CreateCfg(): |
| 66 | """A helper method that creates a mock configuration object.""" |
| 67 | cfg = mock.MagicMock() |
| 68 | cfg.service_account_name = "fake@service.com" |
| 69 | cfg.service_account_private_key_path = "/fake/path/to/key" |
| 70 | cfg.zone = "fake_zone" |
| 71 | cfg.disk_image_name = "fake_image.tar.gz" |
| 72 | cfg.disk_image_mime_type = "fake/type" |
| 73 | cfg.ssh_private_key_path = "" |
| 74 | cfg.ssh_public_key_path = "" |
| 75 | return cfg |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 76 | |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 77 | def testDevicePoolCreateDevices(self): |
| 78 | """Test Device Pool Create Devices.""" |
| 79 | pool = common_operations.DevicePool(self.device_factory) |
| 80 | pool.CreateDevices(5) |
| 81 | self.assertEqual(self.device_factory.CreateInstance.call_count, 5) |
| 82 | self.assertEqual(len(pool.devices), 5) |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 83 | |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 84 | def testCreateDevices(self): |
| 85 | """Test Create Devices.""" |
| 86 | cfg = self._CreateCfg() |
| Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 87 | _report = common_operations.CreateDevices(self.CMD, cfg, |
| herbertxue | 543457e | 2019-03-18 18:13:34 +0800 | [diff] [blame] | 88 | self.device_factory, 1, |
| 89 | self.AVD_TYPE) |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 90 | self.assertEqual(_report.command, self.CMD) |
| 91 | self.assertEqual(_report.status, report.Status.SUCCESS) |
| 92 | self.assertEqual( |
| 93 | _report.data, |
| 94 | {"devices": [{ |
| Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 95 | "ip": self.IP.external, |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 96 | "instance_name": self.INSTANCE |
| 97 | }]}) |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 98 | |
| Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 99 | def testCreateDevicesInternalIP(self): |
| 100 | """Test Create Devices and report internal IP.""" |
| 101 | cfg = self._CreateCfg() |
| 102 | _report = common_operations.CreateDevices(self.CMD, cfg, |
| 103 | self.device_factory, 1, |
| herbertxue | 543457e | 2019-03-18 18:13:34 +0800 | [diff] [blame] | 104 | self.AVD_TYPE, |
| Kevin Cheng | 86d43c7 | 2018-08-30 10:59:14 -0700 | [diff] [blame] | 105 | report_internal_ip=True) |
| 106 | self.assertEqual(_report.command, self.CMD) |
| 107 | self.assertEqual(_report.status, report.Status.SUCCESS) |
| 108 | self.assertEqual( |
| 109 | _report.data, |
| 110 | {"devices": [{ |
| 111 | "ip": self.IP.internal, |
| 112 | "instance_name": self.INSTANCE |
| 113 | }]}) |
| Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 114 | |
| 115 | if __name__ == "__main__": |
| Kevin Cheng | 3031f8a | 2018-05-16 13:21:51 -0700 | [diff] [blame] | 116 | unittest.main() |