• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Tests for delete."""
15
16import subprocess
17import unittest
18
19from unittest import mock
20
21from acloud.delete import delete
22from acloud.internal.lib import driver_test_lib
23from acloud.list import list as list_instances
24from acloud.public import report
25
26
27# pylint: disable=invalid-name,protected-access,unused-argument,no-member
28class DeleteTest(driver_test_lib.BaseDriverTest):
29    """Test delete functions."""
30
31    def testDeleteLocalCuttlefishInstanceSuccess(self):
32        """Test DeleteLocalCuttlefishInstance."""
33        instance_object = mock.MagicMock()
34        instance_object.name = "local-instance"
35        mock_lock = mock.Mock()
36        mock_lock.Lock.return_value = True
37        instance_object.GetLock.return_value = mock_lock
38
39        delete_report = report.Report(command="delete")
40        delete.DeleteLocalCuttlefishInstance(instance_object, delete_report)
41        self.assertEqual(delete_report.data, {
42            "deleted": [
43                {
44                    "type": "instance",
45                    "name": "local-instance",
46                },
47            ],
48        })
49        self.assertEqual(delete_report.status, "SUCCESS")
50        mock_lock.SetInUse.assert_called_once_with(False)
51        mock_lock.Unlock.assert_called_once()
52
53    def testDeleteLocalCuttlefishInstanceFailure(self):
54        """Test DeleteLocalCuttlefishInstance with command failure."""
55        instance_object = mock.MagicMock()
56        instance_object.name = "local-instance"
57        instance_object.Delete.side_effect = subprocess.CalledProcessError(
58            1, "cmd")
59        mock_lock = mock.Mock()
60        mock_lock.Lock.return_value = True
61        instance_object.GetLock.return_value = mock_lock
62
63        delete_report = report.Report(command="delete")
64        delete.DeleteLocalCuttlefishInstance(instance_object, delete_report)
65
66        self.assertEqual(delete_report.status, "FAIL")
67        mock_lock.SetInUse.assert_called_once_with(False)
68        mock_lock.Unlock.assert_called_once()
69
70    def testDeleteLocalGoldfishInstanceSuccess(self):
71        """Test DeleteLocalGoldfishInstance."""
72        mock_adb_tools = mock.Mock()
73        mock_adb_tools.EmuCommand.return_value = 0
74        mock_instance = mock.Mock(adb=mock_adb_tools,
75                                  adb_port=5555,
76                                  device_serial="serial",
77                                  instance_dir="/unit/test")
78        # name is a positional argument of Mock().
79        mock_instance.name = "unittest"
80        mock_lock = mock.Mock()
81        mock_lock.Lock.return_value = True
82        mock_instance.GetLock.return_value = mock_lock
83
84        delete_report = report.Report(command="delete")
85        delete.DeleteLocalGoldfishInstance(mock_instance, delete_report)
86
87        mock_adb_tools.EmuCommand.assert_called_with("kill")
88        self.assertEqual(delete_report.data, {
89            "deleted": [
90                {
91                    "type": "instance",
92                    "name": "unittest",
93                },
94            ],
95        })
96        self.assertEqual(delete_report.status, "SUCCESS")
97        mock_lock.SetInUse.assert_called_once_with(False)
98        mock_lock.Unlock.assert_called_once()
99
100    def testDeleteLocalGoldfishInstanceFailure(self):
101        """Test DeleteLocalGoldfishInstance with adb command failure."""
102        mock_adb_tools = mock.Mock()
103        mock_adb_tools.EmuCommand.return_value = 1
104        mock_instance = mock.Mock(adb=mock_adb_tools,
105                                  adb_port=5555,
106                                  device_serial="serial",
107                                  instance_dir="/unit/test")
108        # name is a positional argument of Mock().
109        mock_instance.name = "unittest"
110        mock_lock = mock.Mock()
111        mock_lock.Lock.return_value = True
112        mock_instance.GetLock.return_value = mock_lock
113
114        delete_report = report.Report(command="delete")
115        delete.DeleteLocalGoldfishInstance(mock_instance, delete_report)
116
117        mock_adb_tools.EmuCommand.assert_called_with("kill")
118        self.assertTrue(len(delete_report.errors) > 0)
119        self.assertEqual(delete_report.status, "FAIL")
120        mock_lock.SetInUse.assert_called_once_with(False)
121        mock_lock.Unlock.assert_called_once()
122
123    def testResetLocalInstanceLockByName(self):
124        """test ResetLocalInstanceLockByName."""
125        mock_lock = mock.Mock()
126        mock_lock.Lock.return_value = True
127        self.Patch(list_instances, "GetLocalInstanceLockByName",
128                   return_value=mock_lock)
129        delete_report = report.Report(command="delete")
130        delete.ResetLocalInstanceLockByName("unittest", delete_report)
131
132        self.assertEqual(delete_report.data, {
133            "deleted": [
134                {
135                    "type": "instance",
136                    "name": "unittest",
137                },
138            ],
139        })
140        mock_lock.Lock.assert_called_once()
141        mock_lock.SetInUse.assert_called_once_with(False)
142        mock_lock.Unlock.assert_called_once()
143
144    def testResetLocalInstanceLockByNameFailure(self):
145        """test ResetLocalInstanceLockByName with an invalid name."""
146        self.Patch(list_instances, "GetLocalInstanceLockByName",
147                   return_value=None)
148        delete_report = report.Report(command="delete")
149        delete.ResetLocalInstanceLockByName("unittest", delete_report)
150
151        self.assertTrue(len(delete_report.errors) > 0)
152        self.assertEqual(delete_report.status, "FAIL")
153
154    @mock.patch.object(delete, "DeleteInstances", return_value="")
155    @mock.patch.object(delete, "ResetLocalInstanceLockByName")
156    @mock.patch.object(delete, "DeleteRemoteInstances", return_value="")
157    def testDeleteInstanceByNames(self, mock_delete_remote_ins,
158                                  mock_reset_lock, mock_delete_local_ins):
159        """test DeleteInstanceByNames."""
160        cfg = mock.Mock()
161        # Test delete local instances.
162        instances = ["local-instance-1", "local-instance-2"]
163        mock_local_ins = mock.Mock()
164        mock_local_ins.name = "local-instance-1"
165        self.Patch(list_instances, "GetLocalInstancesByNames",
166                   return_value=[mock_local_ins])
167        delete.DeleteInstanceByNames(cfg, instances)
168        mock_delete_local_ins.assert_called_with(cfg, [mock_local_ins])
169        mock_reset_lock.assert_called_with("local-instance-2", mock.ANY)
170
171        # Test delete remote instances.
172        instances = ["ins-id1-cf-x86-phone-userdebug",
173                     "ins-id2-cf-x86-phone-userdebug"]
174        delete.DeleteInstanceByNames(cfg, instances)
175        mock_delete_remote_ins.assert_called()
176
177
178if __name__ == "__main__":
179    unittest.main()
180