blob: a2341625a4a17c176a2067a00cc01e204350ae2e [file] [log] [blame]
Kevin Chengb5963882018-05-09 00:06:27 -07001#!/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 Chengb5963882018-05-09 00:06:27 -070016"""Tests for acloud.public.actions.common_operations."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21
Kevin Cheng3031f8a2018-05-16 13:21:51 -070022import unittest
Kevin Chengb5963882018-05-09 00:06:27 -070023import mock
24
Kevin Chengb5963882018-05-09 00:06:27 -070025from acloud.internal.lib import android_build_client
26from acloud.internal.lib import android_compute_client
27from acloud.internal.lib import auth
28from acloud.internal.lib import driver_test_lib
Kevin Cheng86d43c72018-08-30 10:59:14 -070029from acloud.internal.lib import gcompute_client
Kevin Chengb5963882018-05-09 00:06:27 -070030from acloud.public import report
31from acloud.public.actions import common_operations
32
33
34class CommonOperationsTest(driver_test_lib.BaseDriverTest):
Kevin Cheng3031f8a2018-05-16 13:21:51 -070035 """Test Common Operations."""
Kevin Cheng86d43c72018-08-30 10:59:14 -070036 IP = gcompute_client.IP(external="127.0.0.1", internal="10.0.0.1")
Kevin Cheng3031f8a2018-05-16 13:21:51 -070037 INSTANCE = "fake-instance"
38 CMD = "test-cmd"
herbertxue543457e2019-03-18 18:13:34 +080039 AVD_TYPE = "fake-type"
Kevin Chengb5963882018-05-09 00:06:27 -070040
Kevin Cheng3031f8a2018-05-16 13:21:51 -070041 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 Chengb5963882018-05-09 00:06:27 -070063
Kevin Cheng3031f8a2018-05-16 13:21:51 -070064 @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 Chengb5963882018-05-09 00:06:27 -070076
Kevin Cheng3031f8a2018-05-16 13:21:51 -070077 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 Chengb5963882018-05-09 00:06:27 -070083
Kevin Cheng3031f8a2018-05-16 13:21:51 -070084 def testCreateDevices(self):
85 """Test Create Devices."""
86 cfg = self._CreateCfg()
Kevin Cheng86d43c72018-08-30 10:59:14 -070087 _report = common_operations.CreateDevices(self.CMD, cfg,
herbertxue543457e2019-03-18 18:13:34 +080088 self.device_factory, 1,
89 self.AVD_TYPE)
Kevin Cheng3031f8a2018-05-16 13:21:51 -070090 self.assertEqual(_report.command, self.CMD)
91 self.assertEqual(_report.status, report.Status.SUCCESS)
92 self.assertEqual(
93 _report.data,
94 {"devices": [{
Kevin Cheng86d43c72018-08-30 10:59:14 -070095 "ip": self.IP.external,
Kevin Cheng3031f8a2018-05-16 13:21:51 -070096 "instance_name": self.INSTANCE
97 }]})
Kevin Chengb5963882018-05-09 00:06:27 -070098
Kevin Cheng86d43c72018-08-30 10:59:14 -070099 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,
herbertxue543457e2019-03-18 18:13:34 +0800104 self.AVD_TYPE,
Kevin Cheng86d43c72018-08-30 10:59:14 -0700105 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 Chengb5963882018-05-09 00:06:27 -0700114
115if __name__ == "__main__":
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700116 unittest.main()