2 # Copyright (c) 2016 Samsung Electronics Co., Ltd
4 # Licensed under the Flora License, Version 1.1 (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
8 # http://floralicense.org/license/
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.
23 #from tic.dependency import analyze_dependency
24 from tic.parser.repo_parser import RepodataParser
25 from tic.repo import get_repodata_from_repos
27 current_milli_time = lambda: int(round(time.time() * 1000))
28 CWD = os.path.dirname(__file__) or '.'
29 TEST_REPODATA_LOC=os.path.join(CWD, 'dependency_fixtures')
30 DEFAULT_CACHEDIR='/var/tmp/tic-core/cached'
33 # return unittest.makeSuite(DependencyTest, ['testsun1', 'testTrue'])
34 return unittest.makeSuite(DependencyTest)
36 class DependencyTest(unittest.TestCase):
38 # test environment setup
39 self.repo_list = [{'name': 'local_base',
40 'url': 'file:/' + TEST_REPODATA_LOC + '/base'},
41 {'name': 'local_mobile',
42 'url': 'file:/' + TEST_REPODATA_LOC + '/mobile'}]
44 self.repodata_list = get_repodata_from_repos(self.repo_list, DEFAULT_CACHEDIR)
48 repo_parser = RepodataParser(self.repodata_list)
49 self.pkg_group = repo_parser.parse()
50 except Exception as e:
51 raise self.failureException
55 # clear environment after test
60 self.assertNotEqual(self.pkg_group, None)
61 self.assertNotEqual(self.pkg_group['pkg_list'], None)
62 self.assertNotEqual(self.pkg_group['pkg2id'], None)
63 self.assertEqual(len(self.pkg_group['pkg_list']), len(self.pkg_group['pkg2id']))
65 pkg_list = self.pkg_group['pkg_list']
66 pkg2id = self.pkg_group['pkg2id']
68 # Consistency checks between pkg_list and pkg2id
69 for pkg_id in range(len(pkg_list)):
70 pkg_info = pkg_list[pkg_id]
71 self.assertEqual(pkg_id, pkg2id.get(pkg_info['name']))
73 def test_dependency(self):
74 # 1st package install-depdency analysis
75 # analyze_dependency(self.pkg_group)
76 # print('time:', current_milli_time() - start_time)
77 pkg_list = self.pkg_group['pkg_list']
79 # 2nd package install-depdency analysis for test (using bruteforce)
80 test_repo_parser = RepodataParser(self.repodata_list)
81 test_pkg_group = test_repo_parser.parse()
82 test_pkg_list = test_pkg_group.get('pkg_list')
83 test_pkg2id = test_pkg_group.get('pkg2id')
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']]]
103 if pkg_info['name'] != test_pkg_info['name']:
104 raise self.failureException
107 if len(pkg_info['dependency']) != len(test_pkg_info['dependency']):
108 print('pkg_info_1:', pkg_info['dependency'])
109 print('pkg_info_2:', test_pkg_info['dependency'])
110 raise self.failureException
112 # dependency validation between pkg_info and test_pkg_info
113 for dep in test_pkg_info.get('dependency'):
114 if dep not in pkg_info['dependency']:
115 print(dep, 'does not exist')
116 raise self.failureException
118 def dep_dfs(self, count, pkg_id, pkg_list, visited):
119 dep_set = set([pkg_list[pkg_id]['name']])
121 if pkg_list[pkg_id].get('requires'):
122 for req in pkg_list[pkg_id].get('requires'):
123 req_id = req.get('id')
124 if req_id is not None:
125 if visited[req_id] < count:
126 visited[req_id] = count
127 dep_set.update(self.dep_dfs(count, req_id, pkg_list, visited))
130 #TODO: package doest not exist
131 #print('def_dfs::', req['name'], 'is not exist (in dep_analysis)')
135 if __name__ == "__main__":
136 #import sys;sys.argv = ['', 'Test.testName']