blob: 3f15d8f61f448262661dca8453ab0b59b087d632 [file] [log] [blame]
Daniel Norman6d82fa32019-03-22 17:53:04 -07001#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import os.path
18
Daniel Normanfdb38812019-04-15 09:47:24 -070019import common
Daniel Norman6d82fa32019-03-22 17:53:04 -070020import test_utils
Daniel Normanfdb38812019-04-15 09:47:24 -070021from merge_target_files import (read_config_list, validate_config_lists,
22 default_system_item_list,
23 default_other_item_list,
Daniel Normana61cde02019-05-03 14:19:13 -070024 default_system_misc_info_keys, copy_items,
25 merge_dynamic_partition_info_dicts)
Daniel Norman6d82fa32019-03-22 17:53:04 -070026
27
28class MergeTargetFilesTest(test_utils.ReleaseToolsTestCase):
29
30 def setUp(self):
31 self.testdata_dir = test_utils.get_testdata_dir()
32
Daniel Normanfdb38812019-04-15 09:47:24 -070033 def test_copy_items_CopiesItemsMatchingPatterns(self):
34
35 def createEmptyFile(path):
36 if not os.path.exists(os.path.dirname(path)):
37 os.makedirs(os.path.dirname(path))
38 open(path, 'a').close()
39 return path
40
41 def createSymLink(source, dest):
42 os.symlink(source, dest)
43 return dest
44
45 def getRelPaths(start, filepaths):
46 return set(
47 os.path.relpath(path=filepath, start=start) for filepath in filepaths)
48
49 input_dir = common.MakeTempDir()
50 output_dir = common.MakeTempDir()
51 expected_copied_items = []
52 actual_copied_items = []
53 patterns = ['*.cpp', 'subdir/*.txt']
54
55 # Create various files that we expect to get copied because they
56 # match one of the patterns.
57 expected_copied_items.extend([
58 createEmptyFile(os.path.join(input_dir, 'a.cpp')),
59 createEmptyFile(os.path.join(input_dir, 'b.cpp')),
60 createEmptyFile(os.path.join(input_dir, 'subdir', 'c.txt')),
61 createEmptyFile(os.path.join(input_dir, 'subdir', 'd.txt')),
62 createEmptyFile(
63 os.path.join(input_dir, 'subdir', 'subsubdir', 'e.txt')),
64 createSymLink('a.cpp', os.path.join(input_dir, 'a_link.cpp')),
65 ])
66 # Create some more files that we expect to not get copied.
67 createEmptyFile(os.path.join(input_dir, 'a.h'))
68 createEmptyFile(os.path.join(input_dir, 'b.h'))
69 createEmptyFile(os.path.join(input_dir, 'subdir', 'subsubdir', 'f.gif'))
70 createSymLink('a.h', os.path.join(input_dir, 'a_link.h'))
71
72 # Copy items.
73 copy_items(input_dir, output_dir, patterns)
74
75 # Assert the actual copied items match the ones we expected.
76 for dirpath, _, filenames in os.walk(output_dir):
77 actual_copied_items.extend(
78 os.path.join(dirpath, filename) for filename in filenames)
79 self.assertEqual(
80 getRelPaths(output_dir, actual_copied_items),
81 getRelPaths(input_dir, expected_copied_items))
82 self.assertEqual(
83 os.readlink(os.path.join(output_dir, 'a_link.cpp')), 'a.cpp')
84
Daniel Norman6d82fa32019-03-22 17:53:04 -070085 def test_read_config_list(self):
86 system_item_list_file = os.path.join(self.testdata_dir,
87 'merge_config_system_item_list')
88 system_item_list = read_config_list(system_item_list_file)
89 expected_system_item_list = [
90 'META/apkcerts.txt',
91 'META/filesystem_config.txt',
92 'META/root_filesystem_config.txt',
93 'META/system_manifest.xml',
94 'META/system_matrix.xml',
95 'META/update_engine_config.txt',
96 'PRODUCT/*',
97 'ROOT/*',
98 'SYSTEM/*',
99 ]
100 self.assertItemsEqual(system_item_list, expected_system_item_list)
101
102 def test_validate_config_lists_ReturnsFalseIfMissingDefaultItem(self):
103 system_item_list = default_system_item_list[:]
104 system_item_list.remove('SYSTEM/*')
105 self.assertFalse(
106 validate_config_lists(system_item_list, default_system_misc_info_keys,
107 default_other_item_list))
108
109 def test_validate_config_lists_ReturnsTrueIfDefaultItemInDifferentList(self):
110 system_item_list = default_system_item_list[:]
111 system_item_list.remove('ROOT/*')
112 other_item_list = default_other_item_list[:]
113 other_item_list.append('ROOT/*')
114 self.assertTrue(
115 validate_config_lists(system_item_list, default_system_misc_info_keys,
116 other_item_list))
117
118 def test_validate_config_lists_ReturnsTrueIfExtraItem(self):
119 system_item_list = default_system_item_list[:]
120 system_item_list.append('MY_NEW_PARTITION/*')
121 self.assertTrue(
122 validate_config_lists(system_item_list, default_system_misc_info_keys,
123 default_other_item_list))
124
125 def test_validate_config_lists_ReturnsFalseIfBadSystemMiscInfoKeys(self):
126 for bad_key in ['dynamic_partition_list', 'super_partition_groups']:
127 system_misc_info_keys = default_system_misc_info_keys[:]
128 system_misc_info_keys.append(bad_key)
129 self.assertFalse(
130 validate_config_lists(default_system_item_list, system_misc_info_keys,
131 default_other_item_list))
Daniel Normana61cde02019-05-03 14:19:13 -0700132
133 def test_merge_dynamic_partition_info_dicts_ReturnsMergedDict(self):
134 system_dict = {
135 'super_partition_groups': 'group_a',
136 'dynamic_partition_list': 'system',
137 'super_group_a_list': 'system',
138 }
139 other_dict = {
140 'super_partition_groups': 'group_a group_b',
141 'dynamic_partition_list': 'vendor product',
142 'super_group_a_list': 'vendor',
143 'super_group_a_size': '1000',
144 'super_group_b_list': 'product',
145 'super_group_b_size': '2000',
146 }
147 merged_dict = merge_dynamic_partition_info_dicts(
148 system_dict=system_dict,
149 other_dict=other_dict,
150 size_prefix='super_',
151 size_suffix='_size',
152 list_prefix='super_',
153 list_suffix='_list')
154 expected_merged_dict = {
155 'super_partition_groups': 'group_a group_b',
156 'dynamic_partition_list': 'system vendor product',
157 'super_group_a_list': 'system vendor',
158 'super_group_a_size': '1000',
159 'super_group_b_list': 'product',
160 'super_group_b_size': '2000',
161 }
162 self.assertEqual(merged_dict, expected_merged_dict)