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