• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 create_common."""
15
16import os
17import tempfile
18import time
19import unittest
20import zipfile
21
22from acloud import errors
23from acloud.create import create_common
24from acloud.internal import constants
25from acloud.internal.lib import driver_test_lib
26
27
28
29class FakeZipFile(object):
30    """Fake implementation of ZipFile()"""
31
32    # pylint: disable=invalid-name,unused-argument,no-self-use
33    def write(self, filename, arcname=None, compress_type=None):
34        """Fake write method."""
35        return
36
37    # pylint: disable=invalid-name,no-self-use
38    def close(self):
39        """Fake close method."""
40        return
41
42
43# pylint: disable=invalid-name,protected-access
44class CreateCommonTest(driver_test_lib.BaseDriverTest):
45    """Test create_common functions."""
46
47    # pylint: disable=protected-access
48    def testProcessHWPropertyWithInvalidArgs(self):
49        """Test ParseHWPropertyArgs with invalid args."""
50        # Checking wrong property value.
51        args_str = "cpu:3,disk:"
52        with self.assertRaises(errors.MalformedDictStringError):
53            create_common.ParseHWPropertyArgs(args_str)
54
55        # Checking wrong property format.
56        args_str = "cpu:3,disk"
57        with self.assertRaises(errors.MalformedDictStringError):
58            create_common.ParseHWPropertyArgs(args_str)
59
60    def testParseHWPropertyStr(self):
61        """Test ParseHWPropertyArgs."""
62        expected_dict = {"cpu": "2", "resolution": "1080x1920", "dpi": "240",
63                         "memory": "4g", "disk": "4g"}
64        args_str = "cpu:2,resolution:1080x1920,dpi:240,memory:4g,disk:4g"
65        result_dict = create_common.ParseHWPropertyArgs(args_str)
66        self.assertTrue(expected_dict == result_dict)
67
68    def testZipCFImageFiles(self):
69        """Test ZipCFImageFiles."""
70        # Should raise error if zip file already exists
71        fake_image_path = "/fake_image_dir/"
72        self.Patch(os.path, "exists", return_value=True)
73        self.Patch(os, "makedirs")
74        self.assertRaises(errors.ZipImageError,
75                          create_common.ZipCFImageFiles,
76                          fake_image_path)
77
78        # Test should get archive name by timestamp if zip file does not exist.
79        self.Patch(zipfile, "ZipFile", return_value=FakeZipFile())
80        self.Patch(os.path, "exists", return_value=False)
81        self.Patch(os.environ, "get", return_value="fake_build_target")
82        self.Patch(time, "time", return_value=12345)
83        self.Patch(tempfile, "gettempdir", return_value="/fake_temp")
84        self.assertEqual(create_common.ZipCFImageFiles(fake_image_path),
85                         "/fake_temp/%s/fake_build_target-local-12345.zip" %
86                         constants.TEMP_ARTIFACTS_FOLDER)
87
88
89if __name__ == "__main__":
90    unittest.main()
91