[TIC-CORE] Initial Import
[archive/20170607/tools/tic-core.git] / test / test_dependency.py
1 #!/usr/bin/python
2 # Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3 #
4 # Contact: 
5 # @author Chulwoo Shin <cw1.shin@samsung.com>
6
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
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19 # Contributors:
20 # - S-Core Co., Ltd
21
22 import os
23 import time
24 import unittest
25
26 from tic.dependency import analyze_dependency
27 from tic.parser.repo_parser import RepodataParser
28
29
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')
33
34 def suite():
35     # return unittest.makeSuite(DependencyTest, ['testsun1', 'testTrue'])
36     return unittest.makeSuite(DependencyTest)
37     
38 class DependencyTest(unittest.TestCase):
39     def setUp(self):
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')]
43         self.pkg_group = None
44         try:
45             repo_parser = RepodataParser(self.repo_list)
46             self.pkg_group = repo_parser.parse()
47         except Exception as e:
48             raise self.failureException
49         
50         
51
52     def tearDown(self):
53         # clear environment after test 
54         del self.repo_list
55         del self.pkg_group
56
57     def test_parse(self):
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']))
62         
63         pkg_list = self.pkg_group['pkg_list']
64         pkg2id = self.pkg_group['pkg2id']
65         
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']))
70         
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']
77         
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')
83         
84         start_time = current_milli_time()
85         count = 1;
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)
91             count += 1
92         # print('time:', current_milli_time() - start_time)
93         
94         # compares the result of 1st and 2nd
95         if len(pkg_list) != len(test_pkg_list):
96             raise self.failureException
97         
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']]]
101             
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
108             
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
113                     
114     def dep_dfs(self, count, pkg_id, pkg_list, visited):
115         dep_set = set([pkg_list[pkg_id]['name']])
116         
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))
124                 else:
125                     pass
126                     #TODO: package doest not exist
127                     #print('def_dfs::', req['name'], 'is not exist (in dep_analysis)')
128         
129         return dep_set
130
131 if __name__ == "__main__":
132     #import sys;sys.argv = ['', 'Test.testName']
133     unittest.main()