• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright 2016 - 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.
16
17"""Tests for acloud.internal.lib.android_build_client."""
18
19import io
20import time
21
22import unittest
23import mock
24
25import apiclient
26
27from acloud import errors
28from acloud.internal.lib import android_build_client
29from acloud.internal.lib import driver_test_lib
30
31
32# pylint: disable=protected-access
33class AndroidBuildClientTest(driver_test_lib.BaseDriverTest):
34    """Test AndroidBuildClient."""
35
36    BUILD_BRANCH = "fake_branch"
37    BUILD_TARGET = "fake_target"
38    BUILD_ID = 12345
39    RESOURCE_ID = "avd-system.tar.gz"
40    LOCAL_DEST = "/fake/local/path"
41    DESTINATION_BUCKET = "fake_bucket"
42
43    def setUp(self):
44        """Set up test."""
45        super(AndroidBuildClientTest, self).setUp()
46        self.Patch(android_build_client.AndroidBuildClient,
47                   "InitResourceHandle")
48        self.client = android_build_client.AndroidBuildClient(mock.MagicMock())
49        self.client._service = mock.MagicMock()
50
51    # pylint: disable=no-member
52    def testDownloadArtifact(self):
53        """Test DownloadArtifact."""
54        # Create mocks.
55        mock_file = mock.MagicMock()
56        mock_file_io = mock.MagicMock()
57        mock_file_io.__enter__.return_value = mock_file
58        mock_downloader = mock.MagicMock()
59        mock_downloader.next_chunk = mock.MagicMock(
60            side_effect=[(mock.MagicMock(), False), (mock.MagicMock(), True)])
61        mock_api = mock.MagicMock()
62        self.Patch(io, "FileIO", return_value=mock_file_io)
63        self.Patch(
64            apiclient.http,
65            "MediaIoBaseDownload",
66            return_value=mock_downloader)
67        mock_resource = mock.MagicMock()
68        self.client._service.buildartifact = mock.MagicMock(
69            return_value=mock_resource)
70        mock_resource.get_media = mock.MagicMock(return_value=mock_api)
71        # Make the call to the api
72        self.client.DownloadArtifact(self.BUILD_TARGET, self.BUILD_ID,
73                                     self.RESOURCE_ID, self.LOCAL_DEST)
74        # Verify
75        mock_resource.get_media.assert_called_with(
76            buildId=self.BUILD_ID,
77            target=self.BUILD_TARGET,
78            attemptId="0",
79            resourceId=self.RESOURCE_ID)
80        io.FileIO.assert_called_with(self.LOCAL_DEST, mode="wb")
81        mock_call = mock.call(
82            mock_file,
83            mock_api,
84            chunksize=android_build_client.AndroidBuildClient.
85            DEFAULT_CHUNK_SIZE)
86        apiclient.http.MediaIoBaseDownload.assert_has_calls([mock_call])
87        self.assertEqual(mock_downloader.next_chunk.call_count, 2)
88
89    def testDownloadArtifactOSError(self):
90        """Test DownloadArtifact when OSError is raised."""
91        self.Patch(io, "FileIO", side_effect=OSError("fake OSError"))
92        self.assertRaises(errors.DriverError, self.client.DownloadArtifact,
93                          self.BUILD_TARGET, self.BUILD_ID, self.RESOURCE_ID,
94                          self.LOCAL_DEST)
95
96    def testCopyTo(self):
97        """Test CopyTo."""
98        mock_resource = mock.MagicMock()
99        self.client._service.buildartifact = mock.MagicMock(
100            return_value=mock_resource)
101        self.client.CopyTo(
102            build_target=self.BUILD_TARGET,
103            build_id=self.BUILD_ID,
104            artifact_name=self.RESOURCE_ID,
105            destination_bucket=self.DESTINATION_BUCKET,
106            destination_path=self.RESOURCE_ID)
107        mock_resource.copyTo.assert_called_once_with(
108            buildId=self.BUILD_ID,
109            target=self.BUILD_TARGET,
110            attemptId=self.client.DEFAULT_ATTEMPT_ID,
111            artifactName=self.RESOURCE_ID,
112            destinationBucket=self.DESTINATION_BUCKET,
113            destinationPath=self.RESOURCE_ID)
114
115    def testCopyToWithRetry(self):
116        """Test CopyTo with retry."""
117        self.Patch(time, "sleep")
118        mock_resource = mock.MagicMock()
119        mock_api_request = mock.MagicMock()
120        mock_resource.copyTo.return_value = mock_api_request
121        self.client._service.buildartifact.return_value = mock_resource
122        mock_api_request.execute.side_effect = errors.HttpError(503,
123                                                                "fake error")
124        self.assertRaises(
125            errors.HttpError,
126            self.client.CopyTo,
127            build_id=self.BUILD_ID,
128            build_target=self.BUILD_TARGET,
129            artifact_name=self.RESOURCE_ID,
130            destination_bucket=self.DESTINATION_BUCKET,
131            destination_path=self.RESOURCE_ID)
132        self.assertEqual(mock_api_request.execute.call_count, 6)
133
134    def testGetBranch(self):
135        """Test GetBuild."""
136        build_info = {"branch": "aosp-master"}
137        mock_api = mock.MagicMock()
138        mock_build = mock.MagicMock()
139        mock_build.get.return_value = mock_api
140        self.client._service.build = mock.MagicMock(return_value=mock_build)
141        mock_api.execute = mock.MagicMock(return_value=build_info)
142        branch = self.client.GetBranch(self.BUILD_TARGET, self.BUILD_ID)
143        mock_build.get.assert_called_once_with(
144            target=self.BUILD_TARGET,
145            buildId=self.BUILD_ID)
146        self.assertEqual(branch, build_info["branch"])
147
148    def testGetLKGB(self):
149        """Test GetLKGB."""
150        build_info = {"nextPageToken":"Test", "builds": [{"buildId": "3950000"}]}
151        mock_api = mock.MagicMock()
152        mock_build = mock.MagicMock()
153        mock_build.list.return_value = mock_api
154        self.client._service.build = mock.MagicMock(return_value=mock_build)
155        mock_api.execute = mock.MagicMock(return_value=build_info)
156        build_id = self.client.GetLKGB(self.BUILD_TARGET, self.BUILD_BRANCH)
157        mock_build.list.assert_called_once_with(
158            target=self.BUILD_TARGET,
159            branch=self.BUILD_BRANCH,
160            buildAttemptStatus=self.client.BUILD_STATUS_COMPLETE,
161            buildType=self.client.BUILD_TYPE_SUBMITTED,
162            maxResults=self.client.ONE_RESULT,
163            successful=self.client.BUILD_SUCCESSFUL)
164        self.assertEqual(build_id, build_info.get("builds")[0].get("buildId"))
165
166
167if __name__ == "__main__":
168    unittest.main()
169