xref: /aosp_15_r20/tools/acloud/restart/restart_test.py (revision 800a58d989c669b8eb8a71d8df53b1ba3d411444)
1*800a58d9SAndroid Build Coastguard Worker# Copyright 2021 - The Android Open Source Project
2*800a58d9SAndroid Build Coastguard Worker#
3*800a58d9SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*800a58d9SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*800a58d9SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*800a58d9SAndroid Build Coastguard Worker#
7*800a58d9SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*800a58d9SAndroid Build Coastguard Worker#
9*800a58d9SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*800a58d9SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*800a58d9SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*800a58d9SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*800a58d9SAndroid Build Coastguard Worker# limitations under the License.
14*800a58d9SAndroid Build Coastguard Worker"""Tests for restart."""
15*800a58d9SAndroid Build Coastguard Workerimport sys
16*800a58d9SAndroid Build Coastguard Worker
17*800a58d9SAndroid Build Coastguard Workerimport unittest
18*800a58d9SAndroid Build Coastguard Worker
19*800a58d9SAndroid Build Coastguard Workerfrom unittest import mock
20*800a58d9SAndroid Build Coastguard Worker
21*800a58d9SAndroid Build Coastguard Workerfrom acloud import errors
22*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import driver_test_lib
23*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib.ssh import Ssh
24*800a58d9SAndroid Build Coastguard Workerfrom acloud.list import list as list_instances
25*800a58d9SAndroid Build Coastguard Workerfrom acloud.powerwash import powerwash
26*800a58d9SAndroid Build Coastguard Workerfrom acloud.public import config
27*800a58d9SAndroid Build Coastguard Workerfrom acloud.public import report
28*800a58d9SAndroid Build Coastguard Workerfrom acloud.reconnect import reconnect
29*800a58d9SAndroid Build Coastguard Workerfrom acloud.restart import restart
30*800a58d9SAndroid Build Coastguard Worker
31*800a58d9SAndroid Build Coastguard Worker
32*800a58d9SAndroid Build Coastguard Workerclass RestartTest(driver_test_lib.BaseDriverTest):
33*800a58d9SAndroid Build Coastguard Worker    """Test restart."""
34*800a58d9SAndroid Build Coastguard Worker    @mock.patch.object(sys, "exit")
35*800a58d9SAndroid Build Coastguard Worker    @mock.patch.object(restart, "RestartFromInstance")
36*800a58d9SAndroid Build Coastguard Worker    def testRun(self, mock_restart, mock_exit):
37*800a58d9SAndroid Build Coastguard Worker        """test Run."""
38*800a58d9SAndroid Build Coastguard Worker        cfg = mock.MagicMock()
39*800a58d9SAndroid Build Coastguard Worker        args = mock.MagicMock()
40*800a58d9SAndroid Build Coastguard Worker        instance_obj = mock.MagicMock()
41*800a58d9SAndroid Build Coastguard Worker        # Test case with provided instance name.
42*800a58d9SAndroid Build Coastguard Worker        args.instance_name = "instance_1"
43*800a58d9SAndroid Build Coastguard Worker        args.instance_id = 1
44*800a58d9SAndroid Build Coastguard Worker        args.powerwash = False
45*800a58d9SAndroid Build Coastguard Worker        self.Patch(config, "GetAcloudConfig", return_value=cfg)
46*800a58d9SAndroid Build Coastguard Worker        self.Patch(list_instances, "GetInstancesFromInstanceNames",
47*800a58d9SAndroid Build Coastguard Worker                   return_value=[instance_obj])
48*800a58d9SAndroid Build Coastguard Worker        restart.Run(args)
49*800a58d9SAndroid Build Coastguard Worker        mock_restart.assert_has_calls([
50*800a58d9SAndroid Build Coastguard Worker            mock.call(cfg, instance_obj, args.instance_id, args.powerwash)])
51*800a58d9SAndroid Build Coastguard Worker
52*800a58d9SAndroid Build Coastguard Worker        # Test case for user select one instance to restart AVD.
53*800a58d9SAndroid Build Coastguard Worker        selected_instance = mock.MagicMock()
54*800a58d9SAndroid Build Coastguard Worker        self.Patch(list_instances, "GetCFRemoteInstances",
55*800a58d9SAndroid Build Coastguard Worker                   return_value=selected_instance)
56*800a58d9SAndroid Build Coastguard Worker        self.Patch(list_instances, "ChooseOneRemoteInstance",
57*800a58d9SAndroid Build Coastguard Worker                   return_value=selected_instance)
58*800a58d9SAndroid Build Coastguard Worker        args.instance_name = None
59*800a58d9SAndroid Build Coastguard Worker        restart.Run(args)
60*800a58d9SAndroid Build Coastguard Worker        mock_restart.assert_has_calls([
61*800a58d9SAndroid Build Coastguard Worker            mock.call(cfg, selected_instance, args.instance_id, args.powerwash)])
62*800a58d9SAndroid Build Coastguard Worker
63*800a58d9SAndroid Build Coastguard Worker        # Test case for not support local instances.
64*800a58d9SAndroid Build Coastguard Worker        local_instances = mock.MagicMock()
65*800a58d9SAndroid Build Coastguard Worker        self.Patch(list_instances, "GetCFRemoteInstances",
66*800a58d9SAndroid Build Coastguard Worker                   return_value=None)
67*800a58d9SAndroid Build Coastguard Worker        self.Patch(list_instances, "GetLocalInstances",
68*800a58d9SAndroid Build Coastguard Worker                   return_value=local_instances)
69*800a58d9SAndroid Build Coastguard Worker        restart.Run(args)
70*800a58d9SAndroid Build Coastguard Worker        mock_exit.assert_called_once()
71*800a58d9SAndroid Build Coastguard Worker
72*800a58d9SAndroid Build Coastguard Worker    # pylint: disable=no-member
73*800a58d9SAndroid Build Coastguard Worker    def testRestartFromInstance(self):
74*800a58d9SAndroid Build Coastguard Worker        """test RestartFromInstance."""
75*800a58d9SAndroid Build Coastguard Worker        cfg = mock.MagicMock()
76*800a58d9SAndroid Build Coastguard Worker        cfg.ssh_private_key_path = "fake_path"
77*800a58d9SAndroid Build Coastguard Worker        cfg.extra_args_ssh_tunnel = ""
78*800a58d9SAndroid Build Coastguard Worker        instance = mock.MagicMock()
79*800a58d9SAndroid Build Coastguard Worker        instance.ip = "0.0.0.0"
80*800a58d9SAndroid Build Coastguard Worker        instance.name = "ins-name"
81*800a58d9SAndroid Build Coastguard Worker        self.Patch(powerwash, "PowerwashDevice")
82*800a58d9SAndroid Build Coastguard Worker        self.Patch(reconnect, "ReconnectInstance")
83*800a58d9SAndroid Build Coastguard Worker        self.Patch(report, "Report")
84*800a58d9SAndroid Build Coastguard Worker        self.Patch(Ssh, "Run")
85*800a58d9SAndroid Build Coastguard Worker        # should powerwash
86*800a58d9SAndroid Build Coastguard Worker        restart.RestartFromInstance(cfg, instance, 1, True)
87*800a58d9SAndroid Build Coastguard Worker        powerwash.PowerwashDevice.assert_called_once()
88*800a58d9SAndroid Build Coastguard Worker
89*800a58d9SAndroid Build Coastguard Worker        # should restart
90*800a58d9SAndroid Build Coastguard Worker        restart.RestartFromInstance(cfg, instance, 1, False)
91*800a58d9SAndroid Build Coastguard Worker        Ssh.Run.assert_called_once()
92*800a58d9SAndroid Build Coastguard Worker
93*800a58d9SAndroid Build Coastguard Worker        # coverage for except
94*800a58d9SAndroid Build Coastguard Worker        self.Patch(Ssh, "Run",
95*800a58d9SAndroid Build Coastguard Worker                   side_effect=errors.DeviceConnectionError())
96*800a58d9SAndroid Build Coastguard Worker        restart.RestartFromInstance(cfg, instance, 1, False)
97*800a58d9SAndroid Build Coastguard Worker
98*800a58d9SAndroid Build Coastguard Worker
99*800a58d9SAndroid Build Coastguard Workerif __name__ == '__main__':
100*800a58d9SAndroid Build Coastguard Worker    unittest.main()
101