• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
20from __future__ import print_function
21
22import unittest
23import mock
24
25from 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
29from acloud.internal.lib import gcompute_client
30from acloud.public import report
31from acloud.public.actions import common_operations
32
33
34class CommonOperationsTest(driver_test_lib.BaseDriverTest):
35    """Test Common Operations."""
36    IP = gcompute_client.IP(external="127.0.0.1", internal="10.0.0.1")
37    INSTANCE = "fake-instance"
38    CMD = "test-cmd"
39    AVD_TYPE = "fake-type"
40    BRANCH = "fake-branch"
41    BUILD_TARGET = "fake-target"
42    BUILD_ID = "fake-build-id"
43
44    # pylint: disable=protected-access
45    def setUp(self):
46        """Set up the test."""
47        super(CommonOperationsTest, self).setUp()
48        self.build_client = mock.MagicMock()
49        self.device_factory = mock.MagicMock()
50        self.device_factory._branch = self.BRANCH
51        self.device_factory._build_target = self.BUILD_TARGET
52        self.device_factory._build_id = self.BUILD_ID
53        self.device_factory._kernel_branch = None
54        self.device_factory._kernel_build_id = None
55        self.device_factory._kernel_build_target = None
56        self.device_factory._emulator_branch = None
57        self.device_factory._emulator_build_id = None
58        self.device_factory._emulator_build_target = None
59        self.Patch(
60            android_build_client,
61            "AndroidBuildClient",
62            return_value=self.build_client)
63        self.compute_client = mock.MagicMock()
64        self.Patch(
65            android_compute_client,
66            "AndroidComputeClient",
67            return_value=self.compute_client)
68        self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
69        self.Patch(self.compute_client, "GetInstanceIP", return_value=self.IP)
70        self.Patch(
71            self.device_factory, "CreateInstance", return_value=self.INSTANCE)
72        self.Patch(
73            self.device_factory,
74            "GetComputeClient",
75            return_value=self.compute_client)
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 = "fake@service.com"
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            }]})
114
115    def testCreateDevicesInternalIP(self):
116        """Test Create Devices and report internal IP."""
117        cfg = self._CreateCfg()
118        _report = common_operations.CreateDevices(self.CMD, cfg,
119                                                  self.device_factory, 1,
120                                                  self.AVD_TYPE,
121                                                  report_internal_ip=True)
122        self.assertEqual(_report.command, self.CMD)
123        self.assertEqual(_report.status, report.Status.SUCCESS)
124        self.assertEqual(
125            _report.data,
126            {"devices": [{
127                "ip": self.IP.internal,
128                "instance_name": self.INSTANCE,
129                "branch": self.BRANCH,
130                "build_id": self.BUILD_ID,
131                "build_target": self.BUILD_TARGET,
132            }]})
133
134if __name__ == "__main__":
135    unittest.main()
136