xref: /aosp_15_r20/tools/acloud/create/create_common_test.py (revision 800a58d989c669b8eb8a71d8df53b1ba3d411444)
1*800a58d9SAndroid Build Coastguard Worker# Copyright 2018 - 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 create_common."""
15*800a58d9SAndroid Build Coastguard Worker
16*800a58d9SAndroid Build Coastguard Workerimport collections
17*800a58d9SAndroid Build Coastguard Workerimport os
18*800a58d9SAndroid Build Coastguard Workerimport shutil
19*800a58d9SAndroid Build Coastguard Workerimport tempfile
20*800a58d9SAndroid Build Coastguard Workerimport unittest
21*800a58d9SAndroid Build Coastguard Worker
22*800a58d9SAndroid Build Coastguard Workerfrom unittest import mock
23*800a58d9SAndroid Build Coastguard Worker
24*800a58d9SAndroid Build Coastguard Workerfrom acloud import errors
25*800a58d9SAndroid Build Coastguard Workerfrom acloud.create import create_common
26*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import android_build_client
27*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import auth
28*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import driver_test_lib
29*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import utils
30*800a58d9SAndroid Build Coastguard Worker
31*800a58d9SAndroid Build Coastguard Worker
32*800a58d9SAndroid Build Coastguard WorkerExtraFile = collections.namedtuple("ExtraFile", ["source", "target"])
33*800a58d9SAndroid Build Coastguard Worker
34*800a58d9SAndroid Build Coastguard Worker
35*800a58d9SAndroid Build Coastguard Workerclass FakeZipFile:
36*800a58d9SAndroid Build Coastguard Worker    """Fake implementation of ZipFile()"""
37*800a58d9SAndroid Build Coastguard Worker
38*800a58d9SAndroid Build Coastguard Worker    # pylint: disable=invalid-name,unused-argument,no-self-use
39*800a58d9SAndroid Build Coastguard Worker    def write(self, filename, arcname=None, compress_type=None):
40*800a58d9SAndroid Build Coastguard Worker        """Fake write method."""
41*800a58d9SAndroid Build Coastguard Worker        return
42*800a58d9SAndroid Build Coastguard Worker
43*800a58d9SAndroid Build Coastguard Worker    # pylint: disable=invalid-name,no-self-use
44*800a58d9SAndroid Build Coastguard Worker    def close(self):
45*800a58d9SAndroid Build Coastguard Worker        """Fake close method."""
46*800a58d9SAndroid Build Coastguard Worker        return
47*800a58d9SAndroid Build Coastguard Worker
48*800a58d9SAndroid Build Coastguard Worker
49*800a58d9SAndroid Build Coastguard Worker# pylint: disable=invalid-name,protected-access
50*800a58d9SAndroid Build Coastguard Workerclass CreateCommonTest(driver_test_lib.BaseDriverTest):
51*800a58d9SAndroid Build Coastguard Worker    """Test create_common functions."""
52*800a58d9SAndroid Build Coastguard Worker
53*800a58d9SAndroid Build Coastguard Worker    # pylint: disable=protected-access
54*800a58d9SAndroid Build Coastguard Worker    def testProcessHWPropertyWithInvalidArgs(self):
55*800a58d9SAndroid Build Coastguard Worker        """Test ParseKeyValuePairArgs with invalid args."""
56*800a58d9SAndroid Build Coastguard Worker        # Checking wrong property value.
57*800a58d9SAndroid Build Coastguard Worker        args_str = "cpu:3,disk:"
58*800a58d9SAndroid Build Coastguard Worker        with self.assertRaises(errors.MalformedDictStringError):
59*800a58d9SAndroid Build Coastguard Worker            create_common.ParseKeyValuePairArgs(args_str)
60*800a58d9SAndroid Build Coastguard Worker
61*800a58d9SAndroid Build Coastguard Worker        # Checking wrong property format.
62*800a58d9SAndroid Build Coastguard Worker        args_str = "cpu:3,disk"
63*800a58d9SAndroid Build Coastguard Worker        with self.assertRaises(errors.MalformedDictStringError):
64*800a58d9SAndroid Build Coastguard Worker            create_common.ParseKeyValuePairArgs(args_str)
65*800a58d9SAndroid Build Coastguard Worker
66*800a58d9SAndroid Build Coastguard Worker    def testParseHWPropertyStr(self):
67*800a58d9SAndroid Build Coastguard Worker        """Test ParseKeyValuePairArgs."""
68*800a58d9SAndroid Build Coastguard Worker        expected_dict = {"cpu": "2", "resolution": "1080x1920", "dpi": "240",
69*800a58d9SAndroid Build Coastguard Worker                         "memory": "4g", "disk": "4g"}
70*800a58d9SAndroid Build Coastguard Worker        args_str = "cpu:2,resolution:1080x1920,dpi:240,memory:4g,disk:4g"
71*800a58d9SAndroid Build Coastguard Worker        result_dict = create_common.ParseKeyValuePairArgs(args_str)
72*800a58d9SAndroid Build Coastguard Worker        self.assertTrue(expected_dict == result_dict)
73*800a58d9SAndroid Build Coastguard Worker
74*800a58d9SAndroid Build Coastguard Worker    def testGetNonEmptyEnvVars(self):
75*800a58d9SAndroid Build Coastguard Worker        """Test GetNonEmptyEnvVars."""
76*800a58d9SAndroid Build Coastguard Worker        with mock.patch.dict("acloud.internal.lib.utils.os.environ",
77*800a58d9SAndroid Build Coastguard Worker                             {"A": "", "B": "b"},
78*800a58d9SAndroid Build Coastguard Worker                             clear=True):
79*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(
80*800a58d9SAndroid Build Coastguard Worker                ["b"], create_common.GetNonEmptyEnvVars("A", "B", "C"))
81*800a58d9SAndroid Build Coastguard Worker
82*800a58d9SAndroid Build Coastguard Worker    def testParseExtraFilesArgs(self):
83*800a58d9SAndroid Build Coastguard Worker        """Test ParseExtraFilesArgs."""
84*800a58d9SAndroid Build Coastguard Worker        expected_result = [ExtraFile(source="local_path", target="gce_path")]
85*800a58d9SAndroid Build Coastguard Worker        files_info = ["local_path,gce_path"]
86*800a58d9SAndroid Build Coastguard Worker        self.assertEqual(expected_result,
87*800a58d9SAndroid Build Coastguard Worker                         create_common.ParseExtraFilesArgs(files_info))
88*800a58d9SAndroid Build Coastguard Worker
89*800a58d9SAndroid Build Coastguard Worker        # Test multiple files
90*800a58d9SAndroid Build Coastguard Worker        expected_result = [ExtraFile(source="local_path1", target="gce_path1"),
91*800a58d9SAndroid Build Coastguard Worker                           ExtraFile(source="local_path2", target="gce_path2")]
92*800a58d9SAndroid Build Coastguard Worker        files_info = ["local_path1,gce_path1",
93*800a58d9SAndroid Build Coastguard Worker                      "local_path2,gce_path2"]
94*800a58d9SAndroid Build Coastguard Worker        self.assertEqual(expected_result,
95*800a58d9SAndroid Build Coastguard Worker                         create_common.ParseExtraFilesArgs(files_info))
96*800a58d9SAndroid Build Coastguard Worker
97*800a58d9SAndroid Build Coastguard Worker        # Test wrong file info format.
98*800a58d9SAndroid Build Coastguard Worker        files_info = ["local_path"]
99*800a58d9SAndroid Build Coastguard Worker        with self.assertRaises(errors.MalformedDictStringError):
100*800a58d9SAndroid Build Coastguard Worker            create_common.ParseExtraFilesArgs(files_info)
101*800a58d9SAndroid Build Coastguard Worker
102*800a58d9SAndroid Build Coastguard Worker    def testGetCvdHostPackage(self):
103*800a58d9SAndroid Build Coastguard Worker        """test GetCvdHostPackage."""
104*800a58d9SAndroid Build Coastguard Worker        # Can't find the cvd host package
105*800a58d9SAndroid Build Coastguard Worker        with mock.patch("os.path.exists") as exists:
106*800a58d9SAndroid Build Coastguard Worker            exists.return_value = False
107*800a58d9SAndroid Build Coastguard Worker            self.assertRaises(
108*800a58d9SAndroid Build Coastguard Worker                errors.GetCvdLocalHostPackageError,
109*800a58d9SAndroid Build Coastguard Worker                create_common.GetCvdHostPackage)
110*800a58d9SAndroid Build Coastguard Worker
111*800a58d9SAndroid Build Coastguard Worker        self.Patch(os.environ, "get", return_value="/fake_dir2")
112*800a58d9SAndroid Build Coastguard Worker        self.Patch(utils, "GetDistDir", return_value="/fake_dir1")
113*800a58d9SAndroid Build Coastguard Worker        self.Patch(os.path, "exists",
114*800a58d9SAndroid Build Coastguard Worker                   side_effect=lambda path:
115*800a58d9SAndroid Build Coastguard Worker                       path == "/fake_dir1/cvd-host_package.tar.gz")
116*800a58d9SAndroid Build Coastguard Worker
117*800a58d9SAndroid Build Coastguard Worker        # Find cvd host in dist dir.
118*800a58d9SAndroid Build Coastguard Worker        self.assertEqual(
119*800a58d9SAndroid Build Coastguard Worker            create_common.GetCvdHostPackage(),
120*800a58d9SAndroid Build Coastguard Worker            "/fake_dir1/cvd-host_package.tar.gz")
121*800a58d9SAndroid Build Coastguard Worker
122*800a58d9SAndroid Build Coastguard Worker        # Find cvd host in host out dir.
123*800a58d9SAndroid Build Coastguard Worker        self.Patch(os.environ, "get", return_value="/fake_dir2")
124*800a58d9SAndroid Build Coastguard Worker        self.Patch(utils, "GetDistDir", return_value=None)
125*800a58d9SAndroid Build Coastguard Worker        with mock.patch("os.path.exists") as exists:
126*800a58d9SAndroid Build Coastguard Worker            exists.side_effect = lambda path: \
127*800a58d9SAndroid Build Coastguard Worker                path == "/fake_dir2/cvd-host_package.tar.gz"
128*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(
129*800a58d9SAndroid Build Coastguard Worker                create_common.GetCvdHostPackage(),
130*800a58d9SAndroid Build Coastguard Worker                "/fake_dir2/cvd-host_package.tar.gz")
131*800a58d9SAndroid Build Coastguard Worker        with mock.patch("os.path.exists") as exists:
132*800a58d9SAndroid Build Coastguard Worker            exists.side_effect = lambda path: \
133*800a58d9SAndroid Build Coastguard Worker                path == "/fake_dir2/cvd-host_package"
134*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(
135*800a58d9SAndroid Build Coastguard Worker                create_common.GetCvdHostPackage(),
136*800a58d9SAndroid Build Coastguard Worker                "/fake_dir2/cvd-host_package")
137*800a58d9SAndroid Build Coastguard Worker
138*800a58d9SAndroid Build Coastguard Worker        # Find cvd host in specified path.
139*800a58d9SAndroid Build Coastguard Worker        package_path = "/tool_dir/cvd-host_package.tar.gz"
140*800a58d9SAndroid Build Coastguard Worker        self.Patch(utils, "GetDistDir", return_value=None)
141*800a58d9SAndroid Build Coastguard Worker        with mock.patch("os.path.exists") as exists:
142*800a58d9SAndroid Build Coastguard Worker            exists.return_value = True
143*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(
144*800a58d9SAndroid Build Coastguard Worker                create_common.GetCvdHostPackage(package_path),
145*800a58d9SAndroid Build Coastguard Worker                "/tool_dir/cvd-host_package.tar.gz")
146*800a58d9SAndroid Build Coastguard Worker
147*800a58d9SAndroid Build Coastguard Worker    @mock.patch("acloud.create.create_common.os.path.isfile",
148*800a58d9SAndroid Build Coastguard Worker                side_effect=lambda path: path == "/dir/name")
149*800a58d9SAndroid Build Coastguard Worker    @mock.patch("acloud.create.create_common.os.path.isdir",
150*800a58d9SAndroid Build Coastguard Worker                side_effect=lambda path: path == "/dir")
151*800a58d9SAndroid Build Coastguard Worker    @mock.patch("acloud.create.create_common.os.listdir",
152*800a58d9SAndroid Build Coastguard Worker                return_value=["name", "name2"])
153*800a58d9SAndroid Build Coastguard Worker    def testFindLocalImage(self, _mock_listdir, _mock_isdir, _mock_isfile):
154*800a58d9SAndroid Build Coastguard Worker        """Test FindLocalImage."""
155*800a58d9SAndroid Build Coastguard Worker        self.assertEqual(
156*800a58d9SAndroid Build Coastguard Worker            "/dir/name",
157*800a58d9SAndroid Build Coastguard Worker            create_common.FindLocalImage("/test/../dir/name", "not_exist"))
158*800a58d9SAndroid Build Coastguard Worker
159*800a58d9SAndroid Build Coastguard Worker        self.assertEqual("/dir/name",
160*800a58d9SAndroid Build Coastguard Worker                         create_common.FindLocalImage("/dir/", "name"))
161*800a58d9SAndroid Build Coastguard Worker
162*800a58d9SAndroid Build Coastguard Worker
163*800a58d9SAndroid Build Coastguard Worker        self.assertIsNone(create_common.FindLocalImage("/dir", "not_exist",
164*800a58d9SAndroid Build Coastguard Worker                                                       raise_error=False))
165*800a58d9SAndroid Build Coastguard Worker        with self.assertRaises(errors.GetLocalImageError):
166*800a58d9SAndroid Build Coastguard Worker            create_common.FindLocalImage("/dir", "not_exist")
167*800a58d9SAndroid Build Coastguard Worker
168*800a58d9SAndroid Build Coastguard Worker        with self.assertRaises(errors.GetLocalImageError):
169*800a58d9SAndroid Build Coastguard Worker            create_common.FindLocalImage("/dir", "name.?", raise_error=False)
170*800a58d9SAndroid Build Coastguard Worker
171*800a58d9SAndroid Build Coastguard Worker    def testFindBootImage(self):
172*800a58d9SAndroid Build Coastguard Worker        """Test FindBootImage."""
173*800a58d9SAndroid Build Coastguard Worker        with tempfile.TemporaryDirectory() as temp_dir:
174*800a58d9SAndroid Build Coastguard Worker            with self.assertRaises(errors.GetLocalImageError):
175*800a58d9SAndroid Build Coastguard Worker                create_common.FindBootImage(temp_dir)
176*800a58d9SAndroid Build Coastguard Worker
177*800a58d9SAndroid Build Coastguard Worker            boot_image_path = os.path.join(temp_dir, "boot.img")
178*800a58d9SAndroid Build Coastguard Worker            self.CreateFile(boot_image_path, b"invalid")
179*800a58d9SAndroid Build Coastguard Worker            with self.assertRaises(errors.GetLocalImageError):
180*800a58d9SAndroid Build Coastguard Worker                create_common.FindBootImage(temp_dir)
181*800a58d9SAndroid Build Coastguard Worker            os.remove(boot_image_path)
182*800a58d9SAndroid Build Coastguard Worker
183*800a58d9SAndroid Build Coastguard Worker            boot_image_path = os.path.join(temp_dir, "boot.img")
184*800a58d9SAndroid Build Coastguard Worker            self.CreateFile(boot_image_path, b"ANDROID!")
185*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(boot_image_path,
186*800a58d9SAndroid Build Coastguard Worker                             create_common.FindBootImage(temp_dir))
187*800a58d9SAndroid Build Coastguard Worker            os.remove(boot_image_path)
188*800a58d9SAndroid Build Coastguard Worker
189*800a58d9SAndroid Build Coastguard Worker            boot_image_path = os.path.join(temp_dir, "boot-5.10.img")
190*800a58d9SAndroid Build Coastguard Worker            self.CreateFile(boot_image_path, b"ANDROID!")
191*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(boot_image_path,
192*800a58d9SAndroid Build Coastguard Worker                             create_common.FindBootImage(temp_dir))
193*800a58d9SAndroid Build Coastguard Worker
194*800a58d9SAndroid Build Coastguard Worker    def testFindSystemImages(self):
195*800a58d9SAndroid Build Coastguard Worker        """Test FindSystemImages."""
196*800a58d9SAndroid Build Coastguard Worker        with tempfile.TemporaryDirectory() as temp_dir:
197*800a58d9SAndroid Build Coastguard Worker            with self.assertRaises(errors.GetLocalImageError):
198*800a58d9SAndroid Build Coastguard Worker                create_common.FindSystemImages(temp_dir)
199*800a58d9SAndroid Build Coastguard Worker
200*800a58d9SAndroid Build Coastguard Worker            image_dir = os.path.join(temp_dir, "IMAGES")
201*800a58d9SAndroid Build Coastguard Worker            system_image_path = os.path.join(image_dir, "system.img")
202*800a58d9SAndroid Build Coastguard Worker            self.CreateFile(system_image_path)
203*800a58d9SAndroid Build Coastguard Worker            self.assertEqual((system_image_path, None, None),
204*800a58d9SAndroid Build Coastguard Worker                             create_common.FindSystemImages(temp_dir))
205*800a58d9SAndroid Build Coastguard Worker            self.assertEqual((system_image_path, None, None),
206*800a58d9SAndroid Build Coastguard Worker                             create_common.FindSystemImages(image_dir))
207*800a58d9SAndroid Build Coastguard Worker            self.assertEqual((system_image_path, None, None),
208*800a58d9SAndroid Build Coastguard Worker                             create_common.FindSystemImages(system_image_path))
209*800a58d9SAndroid Build Coastguard Worker
210*800a58d9SAndroid Build Coastguard Worker            system_ext_image_path = os.path.join(image_dir, "system_ext.img")
211*800a58d9SAndroid Build Coastguard Worker            self.CreateFile(system_ext_image_path)
212*800a58d9SAndroid Build Coastguard Worker            product_image_path = os.path.join(image_dir, "product.img")
213*800a58d9SAndroid Build Coastguard Worker            self.CreateFile(product_image_path)
214*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(
215*800a58d9SAndroid Build Coastguard Worker                (system_image_path, system_ext_image_path, product_image_path),
216*800a58d9SAndroid Build Coastguard Worker                create_common.FindSystemImages(temp_dir))
217*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(
218*800a58d9SAndroid Build Coastguard Worker                (system_image_path, system_ext_image_path, product_image_path),
219*800a58d9SAndroid Build Coastguard Worker                create_common.FindSystemImages(image_dir))
220*800a58d9SAndroid Build Coastguard Worker
221*800a58d9SAndroid Build Coastguard Worker
222*800a58d9SAndroid Build Coastguard Worker    def testFindVendorBootImage(self):
223*800a58d9SAndroid Build Coastguard Worker        """Test FindVendorBootImage."""
224*800a58d9SAndroid Build Coastguard Worker        with tempfile.TemporaryDirectory() as temp_dir:
225*800a58d9SAndroid Build Coastguard Worker            with self.assertRaises(errors.GetLocalImageError):
226*800a58d9SAndroid Build Coastguard Worker                create_common.FindVendorBootImage(temp_dir)
227*800a58d9SAndroid Build Coastguard Worker
228*800a58d9SAndroid Build Coastguard Worker            boot_image_path = os.path.join(temp_dir, "vendor_boot.img")
229*800a58d9SAndroid Build Coastguard Worker            self.CreateFile(boot_image_path)
230*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(boot_image_path,
231*800a58d9SAndroid Build Coastguard Worker                             create_common.FindVendorBootImage(boot_image_path))
232*800a58d9SAndroid Build Coastguard Worker            self.assertEqual(boot_image_path,
233*800a58d9SAndroid Build Coastguard Worker                             create_common.FindVendorBootImage(temp_dir))
234*800a58d9SAndroid Build Coastguard Worker
235*800a58d9SAndroid Build Coastguard Worker
236*800a58d9SAndroid Build Coastguard Worker    @mock.patch.object(utils, "Decompress")
237*800a58d9SAndroid Build Coastguard Worker    def testDownloadRemoteArtifact(self, mock_decompress):
238*800a58d9SAndroid Build Coastguard Worker        """Test Download cuttlefish package."""
239*800a58d9SAndroid Build Coastguard Worker        mock_build_client = mock.MagicMock()
240*800a58d9SAndroid Build Coastguard Worker        self.Patch(
241*800a58d9SAndroid Build Coastguard Worker            android_build_client,
242*800a58d9SAndroid Build Coastguard Worker            "AndroidBuildClient",
243*800a58d9SAndroid Build Coastguard Worker            return_value=mock_build_client)
244*800a58d9SAndroid Build Coastguard Worker        self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
245*800a58d9SAndroid Build Coastguard Worker        avd_spec = mock.MagicMock()
246*800a58d9SAndroid Build Coastguard Worker        avd_spec.cfg = mock.MagicMock()
247*800a58d9SAndroid Build Coastguard Worker        avd_spec.remote_image = {"build_target" : "aosp_cf_x86_64_phone-userdebug",
248*800a58d9SAndroid Build Coastguard Worker                                 "build_id": "1234"}
249*800a58d9SAndroid Build Coastguard Worker        build_id = "1234"
250*800a58d9SAndroid Build Coastguard Worker        build_target = "aosp_cf_x86_64_phone-userdebug"
251*800a58d9SAndroid Build Coastguard Worker        checkfile1 = "aosp_cf_x86_phone-img-1234.zip"
252*800a58d9SAndroid Build Coastguard Worker        checkfile2 = "cvd-host_package.tar.gz"
253*800a58d9SAndroid Build Coastguard Worker        extract_path = "/tmp/1234"
254*800a58d9SAndroid Build Coastguard Worker
255*800a58d9SAndroid Build Coastguard Worker        create_common.DownloadRemoteArtifact(
256*800a58d9SAndroid Build Coastguard Worker            avd_spec.cfg,
257*800a58d9SAndroid Build Coastguard Worker            avd_spec.remote_image["build_target"],
258*800a58d9SAndroid Build Coastguard Worker            avd_spec.remote_image["build_id"],
259*800a58d9SAndroid Build Coastguard Worker            checkfile1,
260*800a58d9SAndroid Build Coastguard Worker            extract_path,
261*800a58d9SAndroid Build Coastguard Worker            decompress=True)
262*800a58d9SAndroid Build Coastguard Worker
263*800a58d9SAndroid Build Coastguard Worker        self.assertEqual(mock_build_client.DownloadArtifact.call_count, 1)
264*800a58d9SAndroid Build Coastguard Worker        mock_build_client.DownloadArtifact.assert_called_once_with(
265*800a58d9SAndroid Build Coastguard Worker            build_target,
266*800a58d9SAndroid Build Coastguard Worker            build_id,
267*800a58d9SAndroid Build Coastguard Worker            checkfile1,
268*800a58d9SAndroid Build Coastguard Worker            "%s/%s" % (extract_path, checkfile1))
269*800a58d9SAndroid Build Coastguard Worker        self.assertEqual(mock_decompress.call_count, 1)
270*800a58d9SAndroid Build Coastguard Worker
271*800a58d9SAndroid Build Coastguard Worker        mock_decompress.call_count = 0
272*800a58d9SAndroid Build Coastguard Worker        mock_build_client.DownloadArtifact.call_count = 0
273*800a58d9SAndroid Build Coastguard Worker        create_common.DownloadRemoteArtifact(
274*800a58d9SAndroid Build Coastguard Worker            avd_spec.cfg,
275*800a58d9SAndroid Build Coastguard Worker            avd_spec.remote_image["build_target"],
276*800a58d9SAndroid Build Coastguard Worker            avd_spec.remote_image["build_id"],
277*800a58d9SAndroid Build Coastguard Worker            checkfile2,
278*800a58d9SAndroid Build Coastguard Worker            extract_path)
279*800a58d9SAndroid Build Coastguard Worker
280*800a58d9SAndroid Build Coastguard Worker        self.assertEqual(mock_build_client.DownloadArtifact.call_count, 1)
281*800a58d9SAndroid Build Coastguard Worker        mock_build_client.DownloadArtifact.assert_called_once_with(
282*800a58d9SAndroid Build Coastguard Worker            build_target,
283*800a58d9SAndroid Build Coastguard Worker            build_id,
284*800a58d9SAndroid Build Coastguard Worker            checkfile2,
285*800a58d9SAndroid Build Coastguard Worker            "%s/%s" % (extract_path, checkfile2))
286*800a58d9SAndroid Build Coastguard Worker        self.assertEqual(mock_decompress.call_count, 0)
287*800a58d9SAndroid Build Coastguard Worker
288*800a58d9SAndroid Build Coastguard Worker    def testPrepareLocalInstanceDir(self):
289*800a58d9SAndroid Build Coastguard Worker        """test PrepareLocalInstanceDir."""
290*800a58d9SAndroid Build Coastguard Worker        temp_dir = tempfile.mkdtemp()
291*800a58d9SAndroid Build Coastguard Worker        try:
292*800a58d9SAndroid Build Coastguard Worker            cvd_home_dir = os.path.join(temp_dir, "local-instance-1")
293*800a58d9SAndroid Build Coastguard Worker            mock_avd_spec = mock.Mock(local_instance_dir=None)
294*800a58d9SAndroid Build Coastguard Worker            create_common.PrepareLocalInstanceDir(cvd_home_dir, mock_avd_spec)
295*800a58d9SAndroid Build Coastguard Worker            self.assertTrue(os.path.isdir(cvd_home_dir) and
296*800a58d9SAndroid Build Coastguard Worker                            not os.path.islink(cvd_home_dir))
297*800a58d9SAndroid Build Coastguard Worker
298*800a58d9SAndroid Build Coastguard Worker            link_target_dir = os.path.join(temp_dir, "cvd_home")
299*800a58d9SAndroid Build Coastguard Worker            os.mkdir(link_target_dir)
300*800a58d9SAndroid Build Coastguard Worker            mock_avd_spec.local_instance_dir = link_target_dir
301*800a58d9SAndroid Build Coastguard Worker            create_common.PrepareLocalInstanceDir(cvd_home_dir, mock_avd_spec)
302*800a58d9SAndroid Build Coastguard Worker            self.assertTrue(os.path.islink(cvd_home_dir) and
303*800a58d9SAndroid Build Coastguard Worker                            os.path.samefile(cvd_home_dir, link_target_dir))
304*800a58d9SAndroid Build Coastguard Worker        finally:
305*800a58d9SAndroid Build Coastguard Worker            shutil.rmtree(temp_dir)
306*800a58d9SAndroid Build Coastguard Worker
307*800a58d9SAndroid Build Coastguard Worker
308*800a58d9SAndroid Build Coastguard Workerif __name__ == "__main__":
309*800a58d9SAndroid Build Coastguard Worker    unittest.main()
310