• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright (C) 2018 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
18import os
19import shutil
20import tempfile
21import unittest
22import zipfile
23
24from host_controller import common
25from host_controller.build import build_provider
26
27try:
28    from unittest import mock
29except ImportError:
30    import mock
31
32
33class BuildProviderTest(unittest.TestCase):
34    """Tests for build_provider.
35
36    Attributes:
37        _build_provider: The BuildProvider object under test.
38        _temp_dir: The path to the temporary directory for test files.
39    """
40
41    def setUp(self):
42        """Creates temporary directory."""
43        self._build_provider = build_provider.BuildProvider()
44        self._temp_dir = tempfile.mkdtemp()
45
46    def tearDown(self):
47        """Deletes temporary directory."""
48        shutil.rmtree(self._temp_dir)
49
50    def _CreateFile(self, name):
51        """Creates an empty file as test data.
52
53        Args:
54            name: string, the name of the file.
55
56        Returns:
57            string, the path to the file.
58        """
59        path = os.path.join(self._temp_dir, name)
60        with open(path, "w"):
61            pass
62        return path
63
64    def _CreateZip(self, name, *paths):
65        """Creates a zip file containing empty files.
66
67        Args:
68            name: string, the name of the zip file.
69            *paths: strings, the file paths to create in the zip file.
70
71        Returns:
72            string, the path to the zip file.
73        """
74        empty_path = self._CreateFile("empty")
75        zip_path = os.path.join(self._temp_dir, name)
76        with zipfile.ZipFile(zip_path, "w") as zip_file:
77            for path in paths:
78                zip_file.write(empty_path, path)
79        return zip_path
80
81    def _CreateVtsPackage(self):
82        """Creates an android-vts.zip containing vts-tradefed.
83
84        Returns:
85            string, the path to the zip file.
86        """
87        return self._CreateZip(
88            "android-vts.zip",
89            os.path.join("android-vts", "tools", "vts-tradefed"))
90
91    def _CreateDeviceImageZip(self):
92        """Creates a zip containing common device images.
93
94        Returns:
95            string, the path to the zip file.
96        """
97        return self._CreateZip(
98            "img.zip", "system.img", "vendor.img", "boot.img")
99
100    def _CreateProdConfig(self):
101        """Creates a zip containing config files.
102
103        Returns:
104            string, the path to the zip file.
105        """
106        return self._CreateZip(
107            "vti-global-config-prod.zip", os.path.join("test", "prod.config"))
108
109    def testSetTestSuitePackage(self):
110        """Tests setting a VTS package."""
111        vts_path = self._CreateVtsPackage()
112        self._build_provider.SetTestSuitePackage("vts", vts_path)
113
114        self.assertTrue(
115            os.path.exists(self._build_provider.GetTestSuitePackage("vts")))
116
117    def testSetDeviceImageZip(self):
118        """Tests setting a device image zip."""
119        img_path = self._CreateDeviceImageZip()
120        self._build_provider.SetDeviceImageZip(img_path)
121
122        self.assertEqual(
123            img_path,
124            self._build_provider.GetDeviceImage(common.FULL_ZIPFILE))
125
126    def testSetConfigPackage(self):
127        """Tests setting a config package."""
128        config_path = self._CreateProdConfig()
129        self._build_provider.SetConfigPackage("prod", config_path)
130
131        self.assertTrue(
132            os.path.exists(self._build_provider.GetConfigPackage("prod")))
133
134    def testSetFetchedDirectory(self):
135        """Tests setting all files in a directory."""
136        self._CreateVtsPackage()
137        self._CreateProdConfig()
138        img_zip = self._CreateDeviceImageZip()
139        img_file = self._CreateFile("userdata.img")
140        txt_file = self._CreateFile("additional.txt")
141        self._build_provider.SetFetchedDirectory(self._temp_dir)
142
143        self.assertDictContainsSubset(
144            {common.FULL_ZIPFILE: img_zip, "userdata.img": img_file},
145            self._build_provider.GetDeviceImage())
146        self.assertTrue(
147            os.path.exists(self._build_provider.GetTestSuitePackage("vts")))
148        self.assertTrue(
149            os.path.exists(self._build_provider.GetConfigPackage("prod")))
150        self.assertDictContainsSubset(
151            {"additional.txt": txt_file},
152            self._build_provider.GetAdditionalFile())
153
154
155if __name__ == "__main__":
156    unittest.main()
157