forked from google/ota-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_target_lib.py
206 lines (189 loc) · 7.83 KB
/
test_target_lib.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright 2021 Google LLC
#
# 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.
import unittest
from unittest.mock import patch, mock_open, Mock, MagicMock
from target_lib import BuildInfo, TargetLib
import zipfile
import os
import sqlite3
from tempfile import NamedTemporaryFile
class CreateTestBuild():
def __init__(self, include_build_prop=True, include_ab_partitions=True):
self.file = NamedTemporaryFile(dir='test/')
self.test_path = self.file.name
with zipfile.ZipFile(self.file, mode='w') as package:
if include_build_prop:
package.write('test/test_build.prop', 'SYSTEM/build.prop')
if include_ab_partitions:
package.write('test/test_ab_partitions.txt',
'META/ab_partitions.txt')
self.test_info = {
'file_name': self.test_path.split('/')[-1],
'path': self.test_path,
'time': 1628698830,
'build_id': 'AOSP.MASTER',
'build_version': '7392671',
'build_flavor': 'aosp_cf_x86_64_phone-userdebug',
'partitions': ['system', 'vendor'],
}
if not include_build_prop:
self.test_info['build_id'] = ''
self.test_info['build_version'] = ''
self.test_info['build_flavor'] = ''
if not include_ab_partitions:
self.test_info['partitions'] = []
def clean(self):
self.file.close()
class TestBuildInfo(unittest.TestCase):
def setUp(self):
"""
Create a virtual Android build, which only have build.prop
and ab_partitions.txt
"""
self.test_build = CreateTestBuild()
self.build_info = BuildInfo(
self.test_build.test_info['file_name'],
self.test_build.test_info['path'],
self.test_build.test_info['time']
)
self.build_info.analyse_buildprop()
def tearDown(self):
self.test_build.clean()
def test_analyse_buildprop(self):
# Test if the build.prop and ab_partitions are not empty
for key, value in self.test_build.test_info.items():
self.assertEqual(value, self.build_info.__dict__[key],
'The ' + key + ' is not parsed correctly.'
)
# Test if the ab_partitions is empty
test_build_no_partitions = CreateTestBuild(include_ab_partitions=False)
build_info = BuildInfo(
test_build_no_partitions.test_info['file_name'],
test_build_no_partitions.test_info['path'],
test_build_no_partitions.test_info['time']
)
build_info.analyse_buildprop()
self.assertEqual(build_info.partitions,
test_build_no_partitions.test_info['partitions'],
'The partition list is not empty if ab_partitions is not provided.'
)
test_build_no_partitions.clean()
def test_to_sql_form_dict(self):
sql_dict = self.build_info.to_sql_form_dict()
for key, value in self.test_build.test_info.items():
if key != 'partitions':
self.assertEqual(value, sql_dict[key],
'The ' + key + ' is not parsed correctly.'
)
else:
self.assertEqual(','.join(value), sql_dict[key],
'The partition list is not coverted to sql form properly.'
)
def test_to_dict(self):
ordinary_dict = self.build_info.to_dict()
for key, value in self.test_build.test_info.items():
self.assertEqual(value, ordinary_dict[key],
'The ' + key + ' is not parsed correctly.'
)
class TestTargetLib(unittest.TestCase):
def setUp(self):
self.test_path = 'test/test_target_lib.db'
self.tearDown()
self.target_build = TargetLib(path=self.test_path)
def tearDown(self):
if os.path.isfile(self.test_path):
os.remove(self.test_path)
def test_init(self):
# Test the database is created successfully
self.assertTrue(os.path.isfile(self.test_path))
test_columns = [
{'name': 'FileName','type':'TEXT'},
{'name': 'Path','type':'TEXT'},
{'name': 'BuildID','type':'TEXT'},
{'name': 'BuildVersion','type':'TEXT'},
{'name': 'BuildFlavor','type':'TEXT'},
{'name': 'Partitions','type':'TEXT'},
{'name': 'UploadTime','type':'INTEGER'},
]
connect = sqlite3.connect(self.test_path)
cursor = connect.cursor()
cursor.execute("PRAGMA table_info(Builds)")
columns = cursor.fetchall()
for column in test_columns:
column_found = list(filter(lambda x: x[1]==column['name'], columns))
self.assertEqual(len(column_found), 1,
'The column ' + column['name'] + ' is not found in database'
)
self.assertEqual(column_found[0][2], column['type'],
'The column ' + column['name'] + ' has a wrong type'
)
def test_new_build(self):
test_build = CreateTestBuild()
self.target_build.new_build(
filename=test_build.test_info['file_name'],
path=test_build.test_path
)
connect = sqlite3.connect(self.test_path)
cursor = connect.cursor()
cursor.execute("SELECT * FROM BUILDS")
entries = cursor.fetchall()
self.assertEqual(len(entries), 1,
'The test build cannot be added into the database.'
)
test_build.clean()
def test_get_builds(self):
test_build = CreateTestBuild()
# time.time() has to be mocked, otherwise it will be the current time
mock_time = Mock(return_value=test_build.test_info['time'])
with patch('time.time', mock_time):
self.target_build.new_build(
filename=test_build.test_info['file_name'],
path=test_build.test_path
)
# build_list is read and parsed from the database
# build_info is directly parsed from the package
# Test if the read/write database process changes the data entry
build_list = self.target_build.get_builds()
build_info = BuildInfo(
test_build.test_info['file_name'],
test_build.test_info['path'],
test_build.test_info['time']
)
build_info.analyse_buildprop()
self.assertEqual(build_list[0], build_info,
'The list of build info cannot be extracted from database.'
)
test_build.clean()
def test_get_build_by_path(self):
test_build = CreateTestBuild()
# time.time() has to be mocked, otherwise it will be the current time
mock_time = Mock(return_value=test_build.test_info['time'])
with patch('time.time', mock_time):
self.target_build.new_build(
filename=test_build.test_info['file_name'],
path=test_build.test_path
)
build = self.target_build.get_build_by_path(test_build.test_info['path'])
build_info = BuildInfo(
test_build.test_info['file_name'],
test_build.test_info['path'],
test_build.test_info['time']
)
build_info.analyse_buildprop()
self.assertEqual(build, build_info,
'Build info cannot be extracted by path.'
)
test_build.clean()
if __name__ == '__main__':
unittest.main()