• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
29    def setUp(self):
30        """Set up the test."""
31        super(CheepsRemoteImageRemoteInstanceTest, self).setUp()
32        self.build_client = mock.MagicMock()
33        self.Patch(
34            android_build_client,
35            "AndroidBuildClient",
36            return_value=self.build_client)
37        self.compute_client = mock.MagicMock()
38        self.Patch(
39            cheeps_compute_client,
40            "CheepsComputeClient",
41            return_value=self.compute_client)
42        self.Patch(
43            android_compute_client,
44            "AndroidComputeClient",
45            return_value=self.compute_client)
46        self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
47
48        # Mock uuid
49        fake_uuid = mock.MagicMock(hex="1234")
50        self.Patch(uuid, "uuid4", return_value=fake_uuid)
51
52        # Mock compute client methods
53        self.compute_client.GetInstanceIP.return_value = self.IP
54        self.compute_client.GenerateImageName.return_value = self.IMAGE
55        self.compute_client.GenerateInstanceName.return_value = self.INSTANCE
56
57    def _CreateCfg(self):
58        """A helper method that creates a mock configuration object."""
59        cfg = mock.MagicMock()
60        cfg.service_account_name = "fake@service.com"
61        cfg.service_account_private_key_path = "/fake/path/to/key"
62        cfg.zone = "fake_zone"
63        cfg.ssh_private_key_path = ""
64        cfg.ssh_public_key_path = ""
65        cfg.stable_cheeps_host_image_name = self.CHEEPS_HOST_IMAGE_NAME
66        cfg.stable_cheeps_host_image_project = self.CHEEPS_HOST_IMAGE_PROJECT
67        return cfg
68
69    def _CreateAvdSpec(self, stable_cheeps_host_image_name=None,
70                       stable_cheeps_host_image_project=None):
71        avd_spec = mock.MagicMock()
72        avd_spec.cfg = self._CreateCfg()
73        avd_spec.remote_image = {constants.BUILD_ID: self.ANDROID_BUILD_ID,
74                                 constants.BUILD_TARGET: self.ANDROID_BUILD_TARGET}
75        avd_spec.autoconnect = False
76        avd_spec.report_internal_ip = False
77        avd_spec.stable_cheeps_host_image_name = stable_cheeps_host_image_name
78        avd_spec.stable_cheeps_host_image_project = stable_cheeps_host_image_project
79        return avd_spec
80
81    def testCreate(self):
82        """Test CreateDevices."""
83        avd_spec = self._CreateAvdSpec()
84        instance = cheeps_remote_image_remote_instance.CheepsRemoteImageRemoteInstance()
85        report = instance.Create(avd_spec, no_prompts=False)
86
87        # Verify
88        self.compute_client.CreateInstance.assert_called_with(
89            instance=self.INSTANCE,
90            image_name=self.CHEEPS_HOST_IMAGE_NAME,
91            image_project=self.CHEEPS_HOST_IMAGE_PROJECT,
92            avd_spec=avd_spec)
93
94        self.assertEqual(report.data, {
95            "devices": [{
96                "build_id": self.ANDROID_BUILD_ID,
97                "instance_name": self.INSTANCE,
98                "ip": self.IP.external,
99            },],
100        })
101        self.assertEqual(report.command, "create_cheeps")
102        self.assertEqual(report.status, "SUCCESS")
103
104    def testStableCheepsHostImageArgsOverrideConfig(self):
105        """Test that Cheeps host image specifed through args (which goes into
106        avd_spec) override values set in Acloud config."""
107        stable_cheeps_host_image_name = 'override-stable-host-image-name'
108        stable_cheeps_host_image_project = 'override-stable-host-image-project'
109        self.assertNotEqual(stable_cheeps_host_image_name,
110                            self.CHEEPS_HOST_IMAGE_NAME)
111        self.assertNotEqual(stable_cheeps_host_image_project,
112                            self.CHEEPS_HOST_IMAGE_PROJECT)
113
114        avd_spec = self._CreateAvdSpec(stable_cheeps_host_image_name,
115                                       stable_cheeps_host_image_project)
116        instance = cheeps_remote_image_remote_instance.CheepsRemoteImageRemoteInstance()
117        instance.Create(avd_spec, no_prompts=False)
118
119        # Verify
120        self.compute_client.CreateInstance.assert_called_with(
121            instance=self.INSTANCE,
122            image_name=stable_cheeps_host_image_name,
123            image_project=stable_cheeps_host_image_project,
124            avd_spec=avd_spec)
125
126if __name__ == "__main__":
127    unittest.main()
128