2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
12 from sdktools_test
import SdkToolsTestCase
14 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
15 BUILD_TOOLS_DIR
= os
.path
.dirname(SCRIPT_DIR
)
16 TOOLS_DIR
= os
.path
.join(os
.path
.dirname(BUILD_TOOLS_DIR
), 'tools')
18 sys
.path
.extend([BUILD_TOOLS_DIR
, TOOLS_DIR
])
23 class TestCommands(SdkToolsTestCase
):
27 def _AddDummyBundle(self
, manifest
, bundle_name
):
28 bundle
= manifest_util
.Bundle(bundle_name
)
29 bundle
.revision
= 1337
31 bundle
.description
= bundle_name
32 bundle
.stability
= 'beta'
33 bundle
.recommended
= 'no'
34 bundle
.repath
= bundle_name
35 archive
= self
._MakeDummyArchive
(bundle_name
)
36 bundle
.AddArchive(archive
)
37 manifest
.SetBundle(bundle
)
39 # Need to get the bundle from the manifest -- it doesn't use the one we
41 return manifest
.GetBundle(bundle_name
)
43 def _MakeDummyArchive(self
, bundle_name
, tarname
=None, filename
='dummy.txt'):
44 tarname
= (tarname
or bundle_name
) + '.tar.bz2'
45 temp_dir
= tempfile
.mkdtemp(prefix
='archive')
47 dummy_path
= os
.path
.join(temp_dir
, filename
)
48 with
open(dummy_path
, 'w') as stream
:
49 stream
.write('Dummy stuff for %s' % (bundle_name
,))
51 # Build the tarfile directly into the server's directory.
52 tar_path
= os
.path
.join(self
.basedir
, tarname
)
53 tarstream
= tarfile
.open(tar_path
, 'w:bz2')
55 tarstream
.add(dummy_path
, os
.path
.join(bundle_name
, filename
))
59 with
open(tar_path
, 'rb') as archive_stream
:
60 sha1
, size
= manifest_util
.DownloadAndComputeHash(archive_stream
)
62 archive
= manifest_util
.Archive(manifest_util
.GetHostOS())
63 archive
.url
= self
.server
.GetURL(os
.path
.basename(tar_path
))
65 archive
.checksum
= sha1
68 oshelpers
.Remove(['-rf', temp_dir
])
70 def testInfoBasic(self
):
71 """The info command should display information about the given bundle."""
73 output
= self
._Run
(['info', 'sdk_tools'])
74 # Make sure basic information is there
75 bundle
= self
.manifest
.GetBundle('sdk_tools')
76 archive
= bundle
.GetHostOSArchive();
77 self
.assertTrue(bundle
.name
in output
)
78 self
.assertTrue(bundle
.description
in output
)
79 self
.assertTrue(str(bundle
.revision
) in output
)
80 self
.assertTrue(str(archive
.size
) in output
)
81 self
.assertTrue(archive
.checksum
in output
)
82 self
.assertTrue(bundle
.stability
in output
)
84 def testInfoUnknownBundle(self
):
85 """The info command should notify the user of unknown bundles."""
87 bogus_bundle
= 'foobar'
88 output
= self
._Run
(['info', bogus_bundle
])
89 self
.assertTrue(re
.search(r
'[uU]nknown', output
))
90 self
.assertTrue(bogus_bundle
in output
)
92 def testInfoMultipleBundles(self
):
93 """The info command should support listing multiple bundles."""
94 self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
95 self
._AddDummyBundle
(self
.manifest
, 'pepper_24')
97 output
= self
._Run
(['info', 'pepper_23', 'pepper_24'])
98 self
.assertTrue('pepper_23' in output
)
99 self
.assertTrue('pepper_24' in output
)
100 self
.assertFalse(re
.search(r
'[uU]nknown', output
))
102 def testListBasic(self
):
103 """The list command should display basic information about remote
105 self
._WriteManifest
()
106 output
= self
._Run
(['list'])
107 self
.assertTrue(re
.search('I.*?sdk_tools.*?stable', output
, re
.MULTILINE
))
108 # This line is important (it's used by the updater to determine if the
109 # sdk_tools bundle needs to be updated), so let's be explicit.
110 self
.assertTrue('All installed bundles are up-to-date.')
112 def testListMultiple(self
):
113 """The list command should display multiple bundles."""
114 self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
115 self
._WriteManifest
()
116 output
= self
._Run
(['list'])
117 # Added pepper_23 to the remote manifest not the local manifest, so it
118 # shouldn't be installed.
119 self
.assertTrue(re
.search('^[^I]*pepper_23', output
, re
.MULTILINE
))
120 self
.assertTrue('sdk_tools' in output
)
122 def testListWithRevision(self
):
123 """The list command should display the revision, if desired."""
124 self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
125 self
._WriteManifest
()
126 output
= self
._Run
(['list', '-r'])
127 self
.assertTrue(re
.search('pepper_23.*?r1337', output
))
129 def testListWithUpdatedRevision(self
):
130 """The list command should display when there is an update available."""
131 p23bundle
= self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
132 self
._WriteCacheManifest
(self
.manifest
)
133 # Modify the remote manifest to have a newer revision.
134 p23bundle
.revision
+= 1
135 self
._WriteManifest
()
136 output
= self
._Run
(['list', '-r'])
137 # We should see a display like this: I* pepper_23 (r1337 -> r1338)
138 # The star indicates the bundle has an update.
139 self
.assertTrue(re
.search('I\*\s+pepper_23.*?r1337.*?r1338', output
))
141 def testListLocalVersionNotOnRemote(self
):
142 """The list command should tell the user if they have a bundle installed
143 that doesn't exist in the remote manifest."""
144 self
._WriteManifest
()
145 p23bundle
= self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
146 self
._WriteCacheManifest
(self
.manifest
)
147 output
= self
._Run
(['list', '-r'])
148 message
= 'Bundles installed locally that are not available remotely:'
149 message_loc
= output
.find(message
)
150 self
.assertNotEqual(message_loc
, -1)
151 # Make sure pepper_23 is listed after the message above.
152 self
.assertTrue('pepper_23' in output
[message_loc
:])
154 def testSources(self
):
155 """The sources command should allow adding/listing/removing of sources.
156 When a source is added, it will provide an additional set of bundles."""
157 other_manifest
= manifest_util
.SDKManifest()
158 self
._AddDummyBundle
(other_manifest
, 'naclmono_23')
159 with
open(os
.path
.join(self
.basedir
, 'source.json'), 'w') as stream
:
160 stream
.write(other_manifest
.GetDataAsString())
162 source_json_url
= self
.server
.GetURL('source.json')
163 self
._WriteManifest
()
164 output
= self
._Run
(['sources', '--list'])
165 self
.assertTrue('No external sources installed.' in output
)
166 output
= self
._Run
(['sources', '--add', source_json_url
])
167 output
= self
._Run
(['sources', '--list'])
168 self
.assertTrue(source_json_url
in output
)
170 # Should be able to get info about that bundle.
171 output
= self
._Run
(['info', 'naclmono_23'])
172 self
.assertTrue('Unknown bundle' not in output
)
174 self
._Run
(['sources', '--remove', source_json_url
])
175 output
= self
._Run
(['sources', '--list'])
176 self
.assertTrue('No external sources installed.' in output
)
178 def testUpdateBasic(self
):
179 """The update command should install the contents of a bundle to the SDK."""
180 self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
181 self
._WriteManifest
()
182 output
= self
._Run
(['update', 'pepper_23'])
183 self
.assertTrue(os
.path
.exists(
184 os
.path
.join(self
.basedir
, 'nacl_sdk', 'pepper_23', 'dummy.txt')))
186 def testUpdateInCacheButDirectoryRemoved(self
):
187 """The update command should update if the bundle directory does not exist,
188 even if the bundle is already in the cache manifest."""
189 self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
190 self
._WriteCacheManifest
(self
.manifest
)
191 self
._WriteManifest
()
192 output
= self
._Run
(['update', 'pepper_23'])
193 self
.assertTrue(os
.path
.exists(
194 os
.path
.join(self
.basedir
, 'nacl_sdk', 'pepper_23', 'dummy.txt')))
196 def testUpdateNoNewVersion(self
):
197 """The update command should do nothing if the bundle is already up-to-date.
199 self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
200 self
._WriteManifest
()
201 self
._Run
(['update', 'pepper_23'])
202 output
= self
._Run
(['update', 'pepper_23'])
203 self
.assertTrue('is already up-to-date.' in output
)
205 def testUpdateWithNewVersion(self
):
206 """The update command should update to a new version if it exists."""
207 bundle
= self
._AddDummyBundle
(self
.manifest
, 'pepper_23')
208 self
._WriteManifest
()
209 self
._Run
(['update', 'pepper_23'])
212 self
._WriteManifest
()
213 output
= self
._Run
(['update', 'pepper_23'])
214 self
.assertTrue('already exists, but has an update available' in output
)
216 # Now update using --force.
217 output
= self
._Run
(['update', 'pepper_23', '--force'])
218 self
.assertTrue('Updating bundle' in output
)
220 def testUpdateUnknownBundles(self
):
221 """The update command should ignore unknown bundles and notify the user."""
222 self
._WriteManifest
()
223 output
= self
._Run
(['update', 'foobar'])
224 self
.assertTrue('unknown bundle' in output
)
226 def testUpdateRecommended(self
):
227 """The update command should update only recommended bundles when run
230 bundle
= self
._AddDummyBundle
(self
.manifest
, 'pepper_26')
231 bundle
.recommended
= 'yes'
232 self
._WriteManifest
()
233 output
= self
._Run
(['update'])
234 self
.assertTrue(os
.path
.exists(
235 os
.path
.join(self
.basedir
, 'nacl_sdk', 'pepper_26', 'dummy.txt')))
237 def testUpdateCanary(self
):
238 """The update command should create the correct directory name for repath'd
241 bundle
= self
._AddDummyBundle
(self
.manifest
, 'pepper_26')
242 bundle
.name
= 'pepper_canary'
243 self
._WriteManifest
()
244 output
= self
._Run
(['update'])
245 self
.assertTrue(os
.path
.exists(
246 os
.path
.join(self
.basedir
, 'nacl_sdk', 'pepper_canary', 'dummy.txt')))
248 def testUpdateMultiArchive(self
):
249 """The update command should include download/untar multiple archives
250 specified in the bundle.
252 bundle
= self
._AddDummyBundle
(self
.manifest
, 'pepper_26')
253 archive2
= self
._MakeDummyArchive
('pepper_26', tarname
='pepper_26_more',
254 filename
='dummy2.txt')
255 archive2
.host_os
= 'all'
256 bundle
.AddArchive(archive2
)
257 self
._WriteManifest
()
258 output
= self
._Run
(['update'])
259 self
.assertTrue(os
.path
.exists(
260 os
.path
.join(self
.basedir
, 'nacl_sdk', 'pepper_26', 'dummy.txt')))
261 self
.assertTrue(os
.path
.exists(
262 os
.path
.join(self
.basedir
, 'nacl_sdk', 'pepper_26', 'dummy2.txt')))
265 if __name__
== '__main__':