[TIC-CORE] change the license from apache 2.0 to flora 1.1
[archive/20170607/tools/tic-core.git] / test / test_dependency.py
1 #!/usr/bin/python
2 # Copyright (c) 2016 Samsung Electronics Co., Ltd
3 #
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
7 #
8 #     http://floralicense.org/license/
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 # Contributors:
17 # - S-Core Co., Ltd
18
19 import os
20 import time
21 import unittest
22
23 #from tic.dependency import analyze_dependency
24 from tic.parser.repo_parser import RepodataParser
25 from tic.repo import get_repodata_from_repos
26
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'
31
32 def suite():
33     # return unittest.makeSuite(DependencyTest, ['testsun1', 'testTrue'])
34     return unittest.makeSuite(DependencyTest)
35
36 class DependencyTest(unittest.TestCase):
37     def setUp(self):
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'}]
43         
44         self.repodata_list = get_repodata_from_repos(self.repo_list, DEFAULT_CACHEDIR)
45         
46         self.pkg_group = None
47         try:
48             repo_parser = RepodataParser(self.repodata_list)
49             self.pkg_group = repo_parser.parse()
50         except Exception as e:
51             raise self.failureException
52
53
54     def tearDown(self):
55         # clear environment after test 
56         del self.repo_list
57         del self.pkg_group
58
59     def test_parse(self):
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']))
64         
65         pkg_list = self.pkg_group['pkg_list']
66         pkg2id = self.pkg_group['pkg2id']
67         
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']))
72     
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']
78         
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')
84         
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             # package check 
103             if pkg_info['name'] != test_pkg_info['name']:
104                 raise self.failureException
105             
106             # dependency count 
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
111             
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
117     
118     def dep_dfs(self, count, pkg_id, pkg_list, visited):
119         dep_set = set([pkg_list[pkg_id]['name']])
120         
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))
128                 else:
129                     pass
130                     #TODO: package doest not exist
131                     #print('def_dfs::', req['name'], 'is not exist (in dep_analysis)')
132         
133         return dep_set
134
135 if __name__ == "__main__":
136     #import sys;sys.argv = ['', 'Test.testName']
137     unittest.main()