1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7import dependency_manager
8
9class DependencyInfoTest(unittest.TestCase):
10  def testInitRequiredInfo(self):
11    # Must have a dependency, platform and file_path.
12    self.assertRaises(ValueError, dependency_manager.DependencyInfo,
13                      None, None, None)
14    self.assertRaises(ValueError, dependency_manager.DependencyInfo,
15                      'dep', None, None)
16    self.assertRaises(ValueError, dependency_manager.DependencyInfo,
17                      None, 'plat', None)
18    self.assertRaises(ValueError, dependency_manager.DependencyInfo,
19                      None, None, 'config_path')
20    # Empty DependencyInfo.
21    empty_di = dependency_manager.DependencyInfo('dep', 'plat', 'config_path')
22    self.assertEqual('dep', empty_di.dependency)
23    self.assertEqual('plat', empty_di.platform)
24    self.assertEqual(['config_path'], empty_di.config_paths)
25    self.assertFalse(empty_di.has_local_path_info)
26    self.assertFalse(empty_di.has_cloud_storage_info)
27
28  def testInitLocalPaths(self):
29    local_path_info = dependency_manager.LocalPathInfo(['path0', 'path1'])
30    dep_info = dependency_manager.DependencyInfo(
31        'dep', 'platform', 'config_path', local_path_info
32        )
33    self.assertEqual('dep', dep_info.dependency)
34    self.assertEqual('platform', dep_info.platform)
35    self.assertEqual(['config_path'], dep_info.config_paths)
36    self.assertEqual(local_path_info, dep_info._local_path_info)
37    self.assertFalse(dep_info.has_cloud_storage_info)
38
39  def testInitCloudStorageInfo(self):
40    cs_info = dependency_manager.CloudStorageInfo(
41        'cs_bucket', 'cs_hash', 'dowload_path', 'cs_remote_path')
42    dep_info = dependency_manager.DependencyInfo(
43        'dep', 'platform', 'config_path', cloud_storage_info=cs_info)
44    self.assertEqual('dep', dep_info.dependency)
45    self.assertEqual('platform', dep_info.platform)
46    self.assertEqual(['config_path'], dep_info.config_paths)
47    self.assertFalse(dep_info.has_local_path_info)
48    self.assertTrue(dep_info.has_cloud_storage_info)
49    self.assertEqual(cs_info, dep_info._cloud_storage_info)
50
51  def testInitAllInfo(self):
52    cs_info = dependency_manager.CloudStorageInfo(
53        'cs_bucket', 'cs_hash', 'dowload_path', 'cs_remote_path')
54    dep_info = dependency_manager.DependencyInfo(
55        'dep', 'platform', 'config_path', cloud_storage_info=cs_info)
56    self.assertEqual('dep', dep_info.dependency)
57    self.assertEqual('platform', dep_info.platform)
58    self.assertEqual(['config_path'], dep_info.config_paths)
59    self.assertFalse(dep_info.has_local_path_info)
60    self.assertTrue(dep_info.has_cloud_storage_info)
61
62
63  def testUpdateRequiredArgsConflicts(self):
64    lp_info = dependency_manager.LocalPathInfo(['path0', 'path2'])
65    dep_info1 = dependency_manager.DependencyInfo(
66        'dep1', 'platform1', 'config_path1', local_path_info=lp_info)
67    dep_info2 = dependency_manager.DependencyInfo(
68        'dep1', 'platform2', 'config_path2', local_path_info=lp_info)
69    dep_info3 = dependency_manager.DependencyInfo(
70        'dep2', 'platform1', 'config_path3', local_path_info=lp_info)
71    self.assertRaises(ValueError, dep_info1.Update, dep_info2)
72    self.assertRaises(ValueError, dep_info1.Update, dep_info3)
73    self.assertRaises(ValueError, dep_info3.Update, dep_info2)
74
75  def testUpdateMinimumCloudStorageInfo(self):
76    dep_info1 = dependency_manager.DependencyInfo(
77        'dep1', 'platform1', 'config_path1')
78
79    cs_info2 = dependency_manager.CloudStorageInfo(
80        cs_bucket='cs_bucket2', cs_hash='cs_hash2',
81        download_path='download_path2', cs_remote_path='cs_remote_path2')
82    dep_info2 = dependency_manager.DependencyInfo(
83        'dep1', 'platform1', 'config_path2', cloud_storage_info=cs_info2)
84
85    dep_info3 = dependency_manager.DependencyInfo(
86        'dep1', 'platform1', 'config_path3')
87
88    cs_info4 = dependency_manager.CloudStorageInfo(
89        cs_bucket='cs_bucket4', cs_hash='cs_hash4',
90        download_path='download_path4', cs_remote_path='cs_remote_path4')
91    dep_info4 = dependency_manager.DependencyInfo(
92        'dep1', 'platform1', 'config_path4', cloud_storage_info=cs_info4)
93
94    self.assertEqual('dep1', dep_info1.dependency)
95    self.assertEqual('platform1', dep_info1.platform)
96    self.assertEqual(['config_path1'], dep_info1.config_paths)
97
98    dep_info1.Update(dep_info2)
99    self.assertFalse(dep_info1.has_local_path_info)
100    self.assertEqual('dep1', dep_info1.dependency)
101    self.assertEqual('platform1', dep_info1.platform)
102    self.assertEqual(['config_path1', 'config_path2'], dep_info1.config_paths)
103
104    cs_info = dep_info1._cloud_storage_info
105    self.assertEqual(cs_info, cs_info2)
106    self.assertEqual('cs_bucket2', cs_info._cs_bucket)
107    self.assertEqual('cs_hash2', cs_info._cs_hash)
108    self.assertEqual('download_path2', cs_info._download_path)
109    self.assertEqual('cs_remote_path2', cs_info._cs_remote_path)
110
111    dep_info1.Update(dep_info3)
112    self.assertEqual('dep1', dep_info1.dependency)
113    self.assertEqual('platform1', dep_info1.platform)
114    self.assertEqual(['config_path1', 'config_path2', 'config_path3'],
115                     dep_info1.config_paths)
116    self.assertFalse(dep_info1.has_local_path_info)
117    cs_info = dep_info1._cloud_storage_info
118    self.assertEqual(cs_info, cs_info2)
119    self.assertEqual('cs_bucket2', cs_info._cs_bucket)
120    self.assertEqual('cs_hash2', cs_info._cs_hash)
121    self.assertEqual('download_path2', cs_info._download_path)
122    self.assertEqual('cs_remote_path2', cs_info._cs_remote_path)
123
124    self.assertRaises(ValueError, dep_info1.Update, dep_info4)
125
126  def testUpdateMaxCloudStorageInfo(self):
127    dep_info1 = dependency_manager.DependencyInfo(
128        'dep1', 'platform1', 'config_path1')
129
130    zip_info2 = dependency_manager.ArchiveInfo(
131        'archive_path2', 'unzip_path2', 'path_withing_archive2')
132    cs_info2 = dependency_manager.CloudStorageInfo(
133        'cs_bucket2', 'cs_hash2', 'download_path2', 'cs_remote_path2',
134        version_in_cs='2.1.1', archive_info=zip_info2)
135    dep_info2 = dependency_manager.DependencyInfo(
136        'dep1', 'platform1', 'config_path2', cloud_storage_info=cs_info2)
137
138    dep_info3 = dependency_manager.DependencyInfo(
139        'dep1', 'platform1', 'config_path3')
140
141    zip_info4 = dependency_manager.ArchiveInfo(
142        'archive_path4', 'unzip_path4', 'path_withing_archive4')
143    cs_info4 = dependency_manager.CloudStorageInfo(
144        'cs_bucket4', 'cs_hash4', 'download_path4', 'cs_remote_path4',
145        version_in_cs='4.2.1', archive_info=zip_info4)
146    dep_info4 = dependency_manager.DependencyInfo(
147        'dep1', 'platform1', 'config_path4', cloud_storage_info=cs_info4)
148
149    self.assertEqual('dep1', dep_info1.dependency)
150    self.assertEqual('platform1', dep_info1.platform)
151    self.assertEqual(['config_path1'], dep_info1.config_paths)
152
153    dep_info1.Update(dep_info2)
154    self.assertFalse(dep_info1.has_local_path_info)
155    self.assertEqual('dep1', dep_info1.dependency)
156    self.assertEqual('platform1', dep_info1.platform)
157    self.assertEqual(['config_path1', 'config_path2'], dep_info1.config_paths)
158
159    cs_info = dep_info1._cloud_storage_info
160    self.assertEqual(cs_info, cs_info2)
161    self.assertEqual('cs_bucket2', cs_info._cs_bucket)
162    self.assertEqual('cs_hash2', cs_info._cs_hash)
163    self.assertEqual('download_path2', cs_info._download_path)
164    self.assertEqual('cs_remote_path2', cs_info._cs_remote_path)
165    self.assertEqual('cs_remote_path2', cs_info._cs_remote_path)
166
167    dep_info1.Update(dep_info3)
168    self.assertEqual('dep1', dep_info1.dependency)
169    self.assertEqual('platform1', dep_info1.platform)
170    self.assertEqual(['config_path1', 'config_path2', 'config_path3'],
171                     dep_info1.config_paths)
172    self.assertFalse(dep_info1.has_local_path_info)
173    cs_info = dep_info1._cloud_storage_info
174    self.assertEqual(cs_info, cs_info2)
175    self.assertEqual('cs_bucket2', cs_info._cs_bucket)
176    self.assertEqual('cs_hash2', cs_info._cs_hash)
177    self.assertEqual('download_path2', cs_info._download_path)
178    self.assertEqual('cs_remote_path2', cs_info._cs_remote_path)
179
180    self.assertRaises(ValueError, dep_info1.Update, dep_info4)
181
182  def testUpdateAllInfo(self):
183    lp_info1 = dependency_manager.LocalPathInfo(['path1'])
184    dep_info1 = dependency_manager.DependencyInfo(
185        'dep1', 'platform1', 'config_path1', local_path_info=lp_info1)
186    cs_info2 = dependency_manager.CloudStorageInfo(
187        cs_bucket='cs_bucket2', cs_hash='cs_hash2',
188        download_path='download_path2', cs_remote_path='cs_remote_path2')
189    lp_info2 = dependency_manager.LocalPathInfo(['path2'])
190    dep_info2 = dependency_manager.DependencyInfo(
191        'dep1', 'platform1', 'config_path2', local_path_info=lp_info2,
192        cloud_storage_info=cs_info2)
193    lp_info3 = dependency_manager.LocalPathInfo(['path3'])
194    dep_info3 = dependency_manager.DependencyInfo(
195        'dep1', 'platform1', 'config_path3', local_path_info=lp_info3)
196    lp_info4 = dependency_manager.LocalPathInfo(['path4'])
197    cs_info4 = dependency_manager.CloudStorageInfo(
198        cs_bucket='cs_bucket4', cs_hash='cs_hash4',
199        download_path='download_path4', cs_remote_path='cs_remote_path4')
200    dep_info4 = dependency_manager.DependencyInfo(
201        'dep1', 'platform1', 'config_path4', local_path_info=lp_info4,
202        cloud_storage_info=cs_info4)
203
204    self.assertTrue(dep_info1._local_path_info.IsPathInLocalPaths('path1'))
205    self.assertFalse(dep_info1._local_path_info.IsPathInLocalPaths('path2'))
206    self.assertFalse(dep_info1._local_path_info.IsPathInLocalPaths('path3'))
207    self.assertFalse(dep_info1._local_path_info.IsPathInLocalPaths('path4'))
208
209    dep_info1.Update(dep_info2)
210    cs_info = dep_info1._cloud_storage_info
211    self.assertEqual(cs_info, cs_info2)
212    self.assertEqual('cs_bucket2', cs_info._cs_bucket)
213    self.assertEqual('cs_hash2', cs_info._cs_hash)
214    self.assertEqual('download_path2', cs_info._download_path)
215    self.assertEqual('cs_remote_path2', cs_info._cs_remote_path)
216    self.assertTrue(dep_info1._local_path_info.IsPathInLocalPaths('path1'))
217    self.assertTrue(dep_info1._local_path_info.IsPathInLocalPaths('path2'))
218    self.assertFalse(dep_info1._local_path_info.IsPathInLocalPaths('path3'))
219    self.assertFalse(dep_info1._local_path_info.IsPathInLocalPaths('path4'))
220
221    dep_info1.Update(dep_info3)
222    cs_info = dep_info1._cloud_storage_info
223    self.assertEqual(cs_info, cs_info2)
224    self.assertEqual('cs_bucket2', cs_info._cs_bucket)
225    self.assertEqual('cs_hash2', cs_info._cs_hash)
226    self.assertEqual('download_path2', cs_info._download_path)
227    self.assertEqual('cs_remote_path2', cs_info._cs_remote_path)
228    self.assertTrue(dep_info1._local_path_info.IsPathInLocalPaths('path1'))
229    self.assertTrue(dep_info1._local_path_info.IsPathInLocalPaths('path2'))
230    self.assertTrue(dep_info1._local_path_info.IsPathInLocalPaths('path3'))
231    self.assertFalse(dep_info1._local_path_info.IsPathInLocalPaths('path4'))
232
233    self.assertRaises(ValueError, dep_info1.Update, dep_info4)
234
235