1"""Tests for cloud.android.driver.public.actions.create_cheeps_actions.""" 2import unittest 3import uuid 4 5from unittest import mock 6 7from acloud.create import cheeps_remote_image_remote_instance 8from acloud.internal import constants 9from acloud.internal.lib import android_build_client 10from acloud.internal.lib import android_compute_client 11from acloud.internal.lib import auth 12from acloud.internal.lib import cheeps_compute_client 13from acloud.internal.lib import driver_test_lib 14from acloud.internal.lib import ssh 15 16 17class CheepsRemoteImageRemoteInstanceTest(driver_test_lib.BaseDriverTest): 18 """Test cheeps_remote_image_remote_instance.""" 19 20 IP = ssh.IP(external="127.0.0.1", internal="10.0.0.1") 21 INSTANCE = "fake-instance" 22 IMAGE = "fake-image" 23 GPU = "nvidia-tesla-k80" 24 CHEEPS_HOST_IMAGE_NAME = "fake-stable-host-image-name" 25 CHEEPS_HOST_IMAGE_PROJECT = "fake-stable-host-image-project" 26 ANDROID_BUILD_ID = 12345 27 ANDROID_BUILD_TARGET = "fake-target" 28 DEFAULT_ADB_PORT = 9222 29 30 def setUp(self): 31 """Set up the test.""" 32 super().setUp() 33 self.build_client = mock.MagicMock() 34 self.Patch( 35 android_build_client, 36 "AndroidBuildClient", 37 return_value=self.build_client) 38 self.compute_client = mock.MagicMock() 39 self.compute_client.openwrt = False 40 self.compute_client.gce_hostname = None 41 self.Patch( 42 cheeps_compute_client, 43 "CheepsComputeClient", 44 return_value=self.compute_client) 45 self.Patch( 46 android_compute_client, 47 "AndroidComputeClient", 48 return_value=self.compute_client) 49 self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock()) 50 51 # Mock uuid 52 fake_uuid = mock.MagicMock(hex="1234") 53 self.Patch(uuid, "uuid4", return_value=fake_uuid) 54 55 # Mock compute client methods 56 self.compute_client.GetInstanceIP.return_value = self.IP 57 self.compute_client.GenerateImageName.return_value = self.IMAGE 58 self.compute_client.GenerateInstanceName.return_value = self.INSTANCE 59 60 def _CreateCfg(self): 61 """A helper method that creates a mock configuration object.""" 62 cfg = mock.MagicMock() 63 cfg.service_account_name = "[email protected]" 64 cfg.service_account_private_key_path = "/fake/path/to/key" 65 cfg.zone = "fake_zone" 66 cfg.ssh_private_key_path = "" 67 cfg.ssh_public_key_path = "" 68 cfg.stable_cheeps_host_image_name = self.CHEEPS_HOST_IMAGE_NAME 69 cfg.stable_cheeps_host_image_project = self.CHEEPS_HOST_IMAGE_PROJECT 70 return cfg 71 72 def _CreateAvdSpec(self, stable_cheeps_host_image_name=None, 73 stable_cheeps_host_image_project=None): 74 avd_spec = mock.MagicMock() 75 avd_spec.cfg = self._CreateCfg() 76 avd_spec.remote_image = {constants.BUILD_ID: self.ANDROID_BUILD_ID, 77 constants.BUILD_TARGET: self.ANDROID_BUILD_TARGET} 78 avd_spec.autoconnect = False 79 avd_spec.report_internal_ip = False 80 avd_spec.stable_cheeps_host_image_name = stable_cheeps_host_image_name 81 avd_spec.stable_cheeps_host_image_project = stable_cheeps_host_image_project 82 return avd_spec 83 84 def testCreate(self): 85 """Test CreateDevices.""" 86 avd_spec = self._CreateAvdSpec() 87 instance = cheeps_remote_image_remote_instance.CheepsRemoteImageRemoteInstance() 88 report = instance.Create(avd_spec, no_prompts=False) 89 90 # Verify 91 self.compute_client.CreateInstance.assert_called_with( 92 instance=self.INSTANCE, 93 image_name=self.CHEEPS_HOST_IMAGE_NAME, 94 image_project=self.CHEEPS_HOST_IMAGE_PROJECT, 95 avd_spec=avd_spec) 96 97 self.assertEqual(report.data, { 98 "devices": [{ 99 "build_id": self.ANDROID_BUILD_ID, 100 "instance_name": self.INSTANCE, 101 "ip": self.IP.external + ":" + str(self.DEFAULT_ADB_PORT), 102 },], 103 }) 104 self.assertEqual(report.command, "create_cheeps") 105 self.assertEqual(report.status, "SUCCESS") 106 107 # pylint: disable=invalid-name 108 def testStableCheepsHostImageArgsOverrideConfig(self): 109 """Test that Cheeps host image specifed through args (which goes into 110 avd_spec) override values set in Acloud config.""" 111 stable_cheeps_host_image_name = 'override-stable-host-image-name' 112 stable_cheeps_host_image_project = 'override-stable-host-image-project' 113 self.assertNotEqual(stable_cheeps_host_image_name, 114 self.CHEEPS_HOST_IMAGE_NAME) 115 self.assertNotEqual(stable_cheeps_host_image_project, 116 self.CHEEPS_HOST_IMAGE_PROJECT) 117 118 avd_spec = self._CreateAvdSpec(stable_cheeps_host_image_name, 119 stable_cheeps_host_image_project) 120 instance = cheeps_remote_image_remote_instance.CheepsRemoteImageRemoteInstance() 121 instance.Create(avd_spec, no_prompts=False) 122 123 # Verify 124 self.compute_client.CreateInstance.assert_called_with( 125 instance=self.INSTANCE, 126 image_name=stable_cheeps_host_image_name, 127 image_project=stable_cheeps_host_image_project, 128 avd_spec=avd_spec) 129 130if __name__ == "__main__": 131 unittest.main() 132