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 unittest
17import mock
18
19from acloud.delete import delete
20from acloud.internal.lib import driver_test_lib
21from acloud.list import list as list_instances
22from acloud.public import report
23
24
25# pylint: disable=invalid-name,protected-access,unused-argument,no-member
26class DeleteTest(driver_test_lib.BaseDriverTest):
27    """Test delete functions."""
28
29    @mock.patch("subprocess.check_call")
30    def testDeleteLocalCuttlefishInstance(self, mock_subprocess):
31        """Test DeleteLocalCuttlefishInstance."""
32        mock_subprocess.return_value = True
33        instance_object = mock.MagicMock()
34        instance_object.instance_dir = "fake_instance_dir"
35        instance_object.name = "local-instance"
36        delete_report = report.Report(command="delete")
37        delete.DeleteLocalCuttlefishInstance(instance_object, delete_report)
38        self.assertEqual(delete_report.data, {
39            "deleted": [
40                {
41                    "type": "instance",
42                    "name": "local-instance",
43                },
44            ],
45        })
46        self.assertEqual(delete_report.status, "SUCCESS")
47
48    @mock.patch("acloud.delete.delete.adb_tools.AdbTools")
49    def testDeleteLocalGoldfishInstanceSuccess(self, mock_adb_tools):
50        """Test DeleteLocalGoldfishInstance."""
51        mock_instance = mock.Mock(adb_port=5555,
52                                  device_serial="serial",
53                                  instance_dir="/unit/test")
54        # name is a positional argument of Mock().
55        mock_instance.name = "unittest"
56
57        mock_adb_tools_obj = mock.Mock()
58        mock_adb_tools.return_value = mock_adb_tools_obj
59        mock_adb_tools_obj.EmuCommand.return_value = 0
60
61        delete_report = report.Report(command="delete")
62        delete.DeleteLocalGoldfishInstance(mock_instance, delete_report)
63
64        mock_adb_tools_obj.EmuCommand.assert_called_with("kill")
65        mock_instance.DeleteCreationTimestamp.assert_called()
66        self.assertEqual(delete_report.data, {
67            "deleted": [
68                {
69                    "type": "instance",
70                    "name": "unittest",
71                },
72            ],
73        })
74        self.assertEqual(delete_report.status, "SUCCESS")
75
76    @mock.patch("acloud.delete.delete.adb_tools.AdbTools")
77    def testDeleteLocalGoldfishInstanceFailure(self, mock_adb_tools):
78        """Test DeleteLocalGoldfishInstance with adb command failure."""
79        mock_instance = mock.Mock(adb_port=5555,
80                                  device_serial="serial",
81                                  instance_dir="/unit/test")
82        # name is a positional argument of Mock().
83        mock_instance.name = "unittest"
84
85        mock_adb_tools_obj = mock.Mock()
86        mock_adb_tools.return_value = mock_adb_tools_obj
87        mock_adb_tools_obj.EmuCommand.return_value = 1
88
89        delete_report = report.Report(command="delete")
90        delete.DeleteLocalGoldfishInstance(mock_instance, delete_report)
91
92        mock_adb_tools_obj.EmuCommand.assert_called_with("kill")
93        mock_instance.DeleteCreationTimestamp.assert_called()
94        self.assertTrue(len(delete_report.errors) > 0)
95        self.assertEqual(delete_report.status, "FAIL")
96
97    @mock.patch.object(delete, "DeleteInstances", return_value="")
98    @mock.patch.object(delete, "DeleteRemoteInstances", return_value="")
99    def testDeleteInstanceByNames(self, mock_delete_remote_ins,
100                                  mock_delete_local_ins):
101        """test DeleteInstanceByNames."""
102        cfg = mock.Mock()
103        # Test delete local instances.
104        instances = ["local-instance-1", "local-instance-2"]
105        self.Patch(list_instances, "FilterInstancesByNames", return_value="")
106        self.Patch(list_instances, "GetLocalInstances", return_value=[])
107        delete.DeleteInstanceByNames(cfg, instances)
108        mock_delete_local_ins.assert_called()
109
110        # Test delete remote instances.
111        instances = ["ins-id1-cf-x86-phone-userdebug",
112                     "ins-id2-cf-x86-phone-userdebug"]
113        delete.DeleteInstanceByNames(cfg, instances)
114        mock_delete_remote_ins.assert_called()
115
116
117if __name__ == "__main__":
118    unittest.main()
119