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. 16"""Tests for acloud.public.actions.common_operations.""" 17 18from __future__ import absolute_import 19from __future__ import division 20 21import unittest 22import mock 23 24from acloud.internal.lib import android_build_client 25from acloud.internal.lib import android_compute_client 26from acloud.internal.lib import auth 27from acloud.internal.lib import driver_test_lib 28from acloud.internal.lib import ssh 29from acloud.public import report 30from acloud.public.actions import common_operations 31 32 33class CommonOperationsTest(driver_test_lib.BaseDriverTest): 34 """Test Common Operations.""" 35 IP = ssh.IP(external="127.0.0.1", internal="10.0.0.1") 36 INSTANCE = "fake-instance" 37 CMD = "test-cmd" 38 AVD_TYPE = "fake-type" 39 BRANCH = "fake-branch" 40 BUILD_TARGET = "fake-target" 41 BUILD_ID = "fake-build-id" 42 43 # pylint: disable=protected-access 44 def setUp(self): 45 """Set up the test.""" 46 super(CommonOperationsTest, self).setUp() 47 self.build_client = mock.MagicMock() 48 self.device_factory = mock.MagicMock() 49 self.Patch( 50 android_build_client, 51 "AndroidBuildClient", 52 return_value=self.build_client) 53 self.compute_client = mock.MagicMock() 54 self.Patch( 55 android_compute_client, 56 "AndroidComputeClient", 57 return_value=self.compute_client) 58 self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock()) 59 self.Patch(self.compute_client, "GetInstanceIP", return_value=self.IP) 60 self.Patch( 61 self.device_factory, "CreateInstance", return_value=self.INSTANCE) 62 self.Patch( 63 self.device_factory, 64 "GetComputeClient", 65 return_value=self.compute_client) 66 self.Patch(self.device_factory, "GetBuildInfoDict", 67 return_value={"branch": self.BRANCH, 68 "build_id": self.BUILD_ID, 69 "build_target": self.BUILD_TARGET, 70 "gcs_bucket_build_id": self.BUILD_ID}) 71 self.Patch(self.device_factory, "GetBuildInfoDict", 72 return_value={"branch": self.BRANCH, 73 "build_id": self.BUILD_ID, 74 "build_target": self.BUILD_TARGET, 75 "gcs_bucket_build_id": self.BUILD_ID}) 76 77 @staticmethod 78 def _CreateCfg(): 79 """A helper method that creates a mock configuration object.""" 80 cfg = mock.MagicMock() 81 cfg.service_account_name = "[email protected]" 82 cfg.service_account_private_key_path = "/fake/path/to/key" 83 cfg.zone = "fake_zone" 84 cfg.disk_image_name = "fake_image.tar.gz" 85 cfg.disk_image_mime_type = "fake/type" 86 cfg.ssh_private_key_path = "" 87 cfg.ssh_public_key_path = "" 88 return cfg 89 90 def testDevicePoolCreateDevices(self): 91 """Test Device Pool Create Devices.""" 92 pool = common_operations.DevicePool(self.device_factory) 93 pool.CreateDevices(5) 94 self.assertEqual(self.device_factory.CreateInstance.call_count, 5) 95 self.assertEqual(len(pool.devices), 5) 96 97 def testCreateDevices(self): 98 """Test Create Devices.""" 99 cfg = self._CreateCfg() 100 _report = common_operations.CreateDevices(self.CMD, cfg, 101 self.device_factory, 1, 102 self.AVD_TYPE) 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.external, 109 "instance_name": self.INSTANCE, 110 "branch": self.BRANCH, 111 "build_id": self.BUILD_ID, 112 "build_target": self.BUILD_TARGET, 113 "gcs_bucket_build_id": self.BUILD_ID, 114 }]}) 115 116 def testCreateDevicesInternalIP(self): 117 """Test Create Devices and report internal IP.""" 118 cfg = self._CreateCfg() 119 _report = common_operations.CreateDevices(self.CMD, cfg, 120 self.device_factory, 1, 121 self.AVD_TYPE, 122 report_internal_ip=True) 123 self.assertEqual(_report.command, self.CMD) 124 self.assertEqual(_report.status, report.Status.SUCCESS) 125 self.assertEqual( 126 _report.data, 127 {"devices": [{ 128 "ip": self.IP.internal, 129 "instance_name": self.INSTANCE, 130 "branch": self.BRANCH, 131 "build_id": self.BUILD_ID, 132 "build_target": self.BUILD_TARGET, 133 "gcs_bucket_build_id": self.BUILD_ID, 134 }]}) 135 136if __name__ == "__main__": 137 unittest.main() 138