blob: 205a321fd2d29d733727f12380720973545968f4 [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"
Kevin Chengb5963882018-05-09 00:06:27 -070039
Kevin Cheng3031f8a2018-05-16 13:21:51 -070040 def setUp(self):
41 """Set up the test."""
42 super(CommonOperationsTest, self).setUp()
43 self.build_client = mock.MagicMock()
44 self.device_factory = mock.MagicMock()
45 self.Patch(
46 android_build_client,
47 "AndroidBuildClient",
48 return_value=self.build_client)
49 self.compute_client = mock.MagicMock()
50 self.Patch(
51 android_compute_client,
52 "AndroidComputeClient",
53 return_value=self.compute_client)
54 self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
55 self.Patch(self.compute_client, "GetInstanceIP", return_value=self.IP)
56 self.Patch(
57 self.device_factory, "CreateInstance", return_value=self.INSTANCE)
58 self.Patch(
59 self.device_factory,
60 "GetComputeClient",
61 return_value=self.compute_client)
Kevin Chengb5963882018-05-09 00:06:27 -070062
Kevin Cheng3031f8a2018-05-16 13:21:51 -070063 @staticmethod
64 def _CreateCfg():
65 """A helper method that creates a mock configuration object."""
66 cfg = mock.MagicMock()
67 cfg.service_account_name = "fake@service.com"
68 cfg.service_account_private_key_path = "/fake/path/to/key"
69 cfg.zone = "fake_zone"
70 cfg.disk_image_name = "fake_image.tar.gz"
71 cfg.disk_image_mime_type = "fake/type"
72 cfg.ssh_private_key_path = ""
73 cfg.ssh_public_key_path = ""
74 return cfg
Kevin Chengb5963882018-05-09 00:06:27 -070075
Kevin Cheng3031f8a2018-05-16 13:21:51 -070076 def testDevicePoolCreateDevices(self):
77 """Test Device Pool Create Devices."""
78 pool = common_operations.DevicePool(self.device_factory)
79 pool.CreateDevices(5)
80 self.assertEqual(self.device_factory.CreateInstance.call_count, 5)
81 self.assertEqual(len(pool.devices), 5)
Kevin Chengb5963882018-05-09 00:06:27 -070082
Kevin Cheng3031f8a2018-05-16 13:21:51 -070083 def testCreateDevices(self):
84 """Test Create Devices."""
85 cfg = self._CreateCfg()
Kevin Cheng86d43c72018-08-30 10:59:14 -070086 _report = common_operations.CreateDevices(self.CMD, cfg,
87 self.device_factory, 1)
Kevin Cheng3031f8a2018-05-16 13:21:51 -070088 self.assertEqual(_report.command, self.CMD)
89 self.assertEqual(_report.status, report.Status.SUCCESS)
90 self.assertEqual(
91 _report.data,
92 {"devices": [{
Kevin Cheng86d43c72018-08-30 10:59:14 -070093 "ip": self.IP.external,
Kevin Cheng3031f8a2018-05-16 13:21:51 -070094 "instance_name": self.INSTANCE
95 }]})
Kevin Chengb5963882018-05-09 00:06:27 -070096
Kevin Cheng86d43c72018-08-30 10:59:14 -070097 def testCreateDevicesInternalIP(self):
98 """Test Create Devices and report internal IP."""
99 cfg = self._CreateCfg()
100 _report = common_operations.CreateDevices(self.CMD, cfg,
101 self.device_factory, 1,
102 report_internal_ip=True)
103 self.assertEqual(_report.command, self.CMD)
104 self.assertEqual(_report.status, report.Status.SUCCESS)
105 self.assertEqual(
106 _report.data,
107 {"devices": [{
108 "ip": self.IP.internal,
109 "instance_name": self.INSTANCE
110 }]})
Kevin Chengb5963882018-05-09 00:06:27 -0700111
112if __name__ == "__main__":
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700113 unittest.main()