2 # Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5 # @author Chulwoo Shin <cw1.shin@samsung.com>
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
26 from tic.dependency import analyze_dependency
27 from tic.parser.repo_parser import RepodataParser
30 current_milli_time = lambda: int(round(time.time() * 1000))
31 CWD = os.path.dirname(__file__) or '.'
32 TEST_REPODATA_LOC = os.path.join(CWD, 'dependency_fixtures')
35 # return unittest.makeSuite(DependencyTest, ['testsun1', 'testTrue'])
36 return unittest.makeSuite(DependencyTest)
38 class DependencyTest(unittest.TestCase):
40 # test environment setup
41 self.repo_list = [os.path.join(TEST_REPODATA_LOC, 'repodata/base-primary.xml'),
42 os.path.join(TEST_REPODATA_LOC, 'repodata/mobile-primary.xml')]
45 repo_parser = RepodataParser(self.repo_list)
46 self.pkg_group = repo_parser.parse()
47 except Exception as e:
48 raise self.failureException
53 # clear environment after test
58 self.assertNotEqual(self.pkg_group, None)
59 self.assertNotEqual(self.pkg_group['pkg_list'], None)
60 self.assertNotEqual(self.pkg_group['pkg2id'], None)
61 self.assertEqual(len(self.pkg_group['pkg_list']), len(self.pkg_group['pkg2id']))
63 pkg_list = self.pkg_group['pkg_list']
64 pkg2id = self.pkg_group['pkg2id']
66 # Consistency checks between pkg_list and pkg2id
67 for pkg_id in range(len(pkg_list)):
68 pkg_info = pkg_list[pkg_id]
69 self.assertEqual(pkg_id, pkg2id.get(pkg_info['name']))
71 def test_dependency(self):
72 # 1st package install-depdency analysis
73 start_time = current_milli_time()
74 analyze_dependency(self.pkg_group)
75 # print('time:', current_milli_time() - start_time)
76 pkg_list = self.pkg_group['pkg_list']
78 # 2nd package install-depdency analysis for test (using bruteforce)
79 test_repo_parser = RepodataParser(self.repo_list)
80 test_pkg_group = test_repo_parser.parse()
81 test_pkg_list = test_pkg_group.get('pkg_list')
82 test_pkg2id = test_pkg_group.get('pkg2id')
84 start_time = current_milli_time()
86 visited = [0]*len(test_pkg_list)
87 for pkg_id in range(len(test_pkg_list)):
88 visited[pkg_id] = count
89 dep_set = self.dep_dfs(count, pkg_id, test_pkg_list, visited)
90 test_pkg_list[pkg_id]['dependency'] = list(dep_set)
92 # print('time:', current_milli_time() - start_time)
94 # compares the result of 1st and 2nd
95 if len(pkg_list) != len(test_pkg_list):
96 raise self.failureException
98 for pkg_id in range(len(pkg_list)):
99 pkg_info = pkg_list[pkg_id]
100 test_pkg_info = test_pkg_list[test_pkg2id[pkg_info['name']]]
102 if pkg_info['name'] != test_pkg_info['name']:
103 raise self.failureException
104 elif len(pkg_info['dependency']) != len(test_pkg_info['dependency']):
105 print('pkg_info_1:', pkg_info['dependency'])
106 print('pkg_info_2:', test_pkg_info['dependency'])
107 raise self.failureException
109 for dep in test_pkg_info.get('dependency'):
110 if dep not in pkg_info['dependency']:
111 print(dep, 'does not exist')
112 raise self.failureException
114 def dep_dfs(self, count, pkg_id, pkg_list, visited):
115 dep_set = set([pkg_list[pkg_id]['name']])
117 if pkg_list[pkg_id].get('requires'):
118 for req in pkg_list[pkg_id].get('requires'):
119 req_id = req.get('id')
120 if req_id is not None:
121 if visited[req_id] < count:
122 visited[req_id] = count
123 dep_set.update(self.dep_dfs(count, req_id, pkg_list, visited))
126 #TODO: package doest not exist
127 #print('def_dfs::', req['name'], 'is not exist (in dep_analysis)')
131 if __name__ == "__main__":
132 #import sys;sys.argv = ['', 'Test.testName']