aboutsummaryrefslogtreecommitdiff
path: root/create/remote_image_remote_instance_test.py
blob: 33bba172da9863f905c9cd7fece8ec015c45baba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2021 - The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for RemoteImageRemoteInstance."""

import unittest

from unittest import mock

from acloud.create import remote_image_remote_instance
from acloud.internal import constants
from acloud.internal.lib import driver_test_lib
from acloud.internal.lib import oxygen_client
from acloud.internal.lib import utils
from acloud.public import report
from acloud.public.actions import common_operations
from acloud.public.actions import remote_instance_cf_device_factory


ONE_LINE_LEASE_RESPONSE = ("2021/08/02 11:28:52 session_id:\"fake_device\" "
                           "server_url:\"10.1.1.1\" ports:{type:WATERFALL value:0}")
MULTIPLE_LINES_LEASE_RESPONSE = """
2021/08/02 11:28:52
session_id:"fake_device"
server_url:"10.1.1.1"
"""
LEASE_FAILURE_RESPONSE = """
2021/08/16 18:07:36 message 1
2021/08/16 18:11:36 Error received while trying to lease device: rpc error: details
"""


class RemoteImageRemoteInstanceTest(driver_test_lib.BaseDriverTest):
    """Test RemoteImageRemoteInstance method."""

    def setUp(self):
        """Initialize new RemoteImageRemoteInstance."""
        super().setUp()
        self.remote_image_remote_instance = remote_image_remote_instance.RemoteImageRemoteInstance()

    # pylint: disable=protected-access
    @mock.patch.object(utils, "LaunchVNCFromReport")
    @mock.patch.object(utils, "LaunchBrowserFromReport")
    @mock.patch.object(remote_image_remote_instance.RemoteImageRemoteInstance,
                       "_LeaseOxygenAVD")
    @mock.patch.object(remote_instance_cf_device_factory,
                       "RemoteInstanceDeviceFactory")
    def testCreateAVD(self, mock_factory, mock_lease, mock_launch_browser,
                      mock_launch_vnc):
        """test CreateAVD."""
        avd_spec = mock.Mock()
        avd_spec.oxygen = False
        avd_spec.connect_webrtc = True
        avd_spec.connect_vnc = False
        create_report = mock.Mock()
        create_report.status = report.Status.SUCCESS
        self.Patch(common_operations, "CreateDevices",
                   return_value=create_report)
        self.remote_image_remote_instance._CreateAVD(
            avd_spec, no_prompts=True)
        mock_factory.assert_called_once()
        mock_launch_browser.assert_called_once()

        # Test launch VNC case.
        avd_spec.connect_webrtc = False
        avd_spec.connect_vnc = True
        self.remote_image_remote_instance._CreateAVD(
            avd_spec, no_prompts=True)
        mock_launch_vnc.assert_called_once()

        # Test launch with Oxgen case.
        avd_spec.oxygen = True
        self.remote_image_remote_instance._CreateAVD(
            avd_spec, no_prompts=True)
        mock_lease.assert_called_once()

    def testLeaseOxygenAVD(self):
        """test LeaseOxygenAVD."""
        avd_spec = mock.Mock()
        avd_spec.oxygen = True
        avd_spec.remote_image = {constants.BUILD_TARGET: "fake_target",
                                 constants.BUILD_ID: "fake_id",
                                 constants.BUILD_BRANCH: "fake_branch"}
        response_fail = "Lease device fail."
        self.Patch(oxygen_client.OxygenClient, "LeaseDevice",
                   side_effect=[ONE_LINE_LEASE_RESPONSE, response_fail])
        expected_status = report.Status.SUCCESS
        reporter = self.remote_image_remote_instance._LeaseOxygenAVD(avd_spec)
        self.assertEqual(reporter.status, expected_status)

        expected_status = report.Status.FAIL
        reporter = self.remote_image_remote_instance._LeaseOxygenAVD(avd_spec)
        self.assertEqual(reporter.status, expected_status)

    def testOxygenLeaseFailure(self):
        """test LeaseOxygenAVD when the lease call failed."""
        avd_spec = mock.Mock()
        avd_spec.oxygen = True
        avd_spec.remote_image = {constants.BUILD_TARGET: "fake_target",
                                 constants.BUILD_ID: "fake_id",
                                 constants.BUILD_BRANCH: "fake_branch"}
        response_fail = "Lease device fail."
        self.Patch(oxygen_client.OxygenClient, "LeaseDevice",
                   side_effect=[LEASE_FAILURE_RESPONSE, response_fail])
        expected_status = report.Status.FAIL
        reporter = self.remote_image_remote_instance._LeaseOxygenAVD(avd_spec)
        self.assertEqual(reporter.status, expected_status)
        self.assertEqual(reporter.error_type, constants.ACLOUD_OXYGEN_LEASE_ERROR)
        self.assertEqual(reporter.errors, ["rpc error: details"])

    def testGetDeviceInfoFromResponse(self):
        """test GetDeviceInfoFromResponse."""
        expect_session_id = "fake_device"
        expect_server_url = "10.1.1.1"
        self.assertEqual(
            self.remote_image_remote_instance._GetDeviceInfoFromResponse(
                ONE_LINE_LEASE_RESPONSE),
            (expect_session_id, expect_server_url))

        self.assertEqual(
            self.remote_image_remote_instance._GetDeviceInfoFromResponse(
                MULTIPLE_LINES_LEASE_RESPONSE),
            (expect_session_id, expect_server_url))

    def testReplaceDeviceDataKeys(self):
        """test ReplaceDeviceDataKeys."""
        device_data = {"sessionId": "fake_device", "serverUrl": "10.1.1.1"}
        expected_result = {"instance_name": "fake_device", "ip": "10.1.1.1"}
        self.remote_image_remote_instance._ReplaceDeviceDataKeys(device_data)
        self.assertEqual(device_data, expected_result)


if __name__ == '__main__':
    unittest.main()