blob: 91fcbd653939963de9c7289061a31e8b058498da [file] [log] [blame]
Kevin Chengb5963882018-05-09 00:06:27 -07001#!/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.
Kevin Chengb5963882018-05-09 00:06:27 -070016"""Tests for acloud.public.actions.common_operations."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21
Kevin Cheng3031f8a2018-05-16 13:21:51 -070022import unittest
Kevin Chengb5963882018-05-09 00:06:27 -070023import mock
24
Kevin Chengb5963882018-05-09 00:06:27 -070025from 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.public import report
30from acloud.public.actions import common_operations
31
32
33class CommonOperationsTest(driver_test_lib.BaseDriverTest):
Kevin Cheng3031f8a2018-05-16 13:21:51 -070034 """Test Common Operations."""
35 IP = "127.0.0.1"
36 INSTANCE = "fake-instance"
37 CMD = "test-cmd"
Kevin Chengb5963882018-05-09 00:06:27 -070038
Kevin Cheng3031f8a2018-05-16 13:21:51 -070039 def setUp(self):
40 """Set up the test."""
41 super(CommonOperationsTest, self).setUp()
42 self.build_client = mock.MagicMock()
43 self.device_factory = mock.MagicMock()
44 self.Patch(
45 android_build_client,
46 "AndroidBuildClient",
47 return_value=self.build_client)
48 self.compute_client = mock.MagicMock()
49 self.Patch(
50 android_compute_client,
51 "AndroidComputeClient",
52 return_value=self.compute_client)
53 self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
54 self.Patch(self.compute_client, "GetInstanceIP", return_value=self.IP)
55 self.Patch(
56 self.device_factory, "CreateInstance", return_value=self.INSTANCE)
57 self.Patch(
58 self.device_factory,
59 "GetComputeClient",
60 return_value=self.compute_client)
Kevin Chengb5963882018-05-09 00:06:27 -070061
Kevin Cheng3031f8a2018-05-16 13:21:51 -070062 @staticmethod
63 def _CreateCfg():
64 """A helper method that creates a mock configuration object."""
65 cfg = mock.MagicMock()
66 cfg.service_account_name = "fake@service.com"
67 cfg.service_account_private_key_path = "/fake/path/to/key"
68 cfg.zone = "fake_zone"
69 cfg.disk_image_name = "fake_image.tar.gz"
70 cfg.disk_image_mime_type = "fake/type"
71 cfg.ssh_private_key_path = ""
72 cfg.ssh_public_key_path = ""
73 return cfg
Kevin Chengb5963882018-05-09 00:06:27 -070074
Kevin Cheng3031f8a2018-05-16 13:21:51 -070075 def testDevicePoolCreateDevices(self):
76 """Test Device Pool Create Devices."""
77 pool = common_operations.DevicePool(self.device_factory)
78 pool.CreateDevices(5)
79 self.assertEqual(self.device_factory.CreateInstance.call_count, 5)
80 self.assertEqual(len(pool.devices), 5)
Kevin Chengb5963882018-05-09 00:06:27 -070081
Kevin Cheng3031f8a2018-05-16 13:21:51 -070082 def testCreateDevices(self):
83 """Test Create Devices."""
84 cfg = self._CreateCfg()
85 _report = common_operations.CreateDevices(self.CMD, cfg, self.device_factory, 1)
86 self.assertEqual(_report.command, self.CMD)
87 self.assertEqual(_report.status, report.Status.SUCCESS)
88 self.assertEqual(
89 _report.data,
90 {"devices": [{
91 "ip": self.IP,
92 "instance_name": self.INSTANCE
93 }]})
Kevin Chengb5963882018-05-09 00:06:27 -070094
95
96if __name__ == "__main__":
Kevin Cheng3031f8a2018-05-16 13:21:51 -070097 unittest.main()