Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: native_client_sdk/src/build_tools/tests/test_sdktools_commands.py

Issue 11571032: [NaCl SDK] cleanup python unittests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/env python
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.
5
6 import os
7 import sys
8 import re
9 import tarfile
10 import tempfile
11 import unittest
12 from test_sdktools import SdkToolsTestCase
13
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')
17
18 sys.path.extend([BUILD_TOOLS_DIR, TOOLS_DIR])
19 import manifest_util
20 import oshelpers
21
22
23 class TestCommands(SdkToolsTestCase):
24 def setUp(self):
25 self.SetupDefault()
26
27 def _AddDummyBundle(self, manifest, bundle_name):
28 bundle = manifest_util.Bundle(bundle_name)
29 bundle.revision = 1337
30 bundle.version = 23
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)
38
39 # Need to get the bundle from the manifest -- it doesn't use the one we
40 # gave it.
41 return manifest.GetBundle(bundle_name)
42
43 def _MakeDummyArchive(self, bundle_name):
44 temp_dir = tempfile.mkdtemp(prefix='archive')
45 try:
46 dummy_path = os.path.join(temp_dir, 'dummy.txt')
47 with open(dummy_path, 'w') as stream:
48 stream.write('Dummy stuff for %s' % (bundle_name,))
49
50 # Build the tarfile directly into the server's directory.
51 tar_path = os.path.join(self.basedir, bundle_name + '.tar.bz2')
52 tarstream = tarfile.open(tar_path, 'w:bz2')
53 try:
54 tarstream.add(dummy_path, os.path.join(bundle_name, 'dummy.txt'))
55 finally:
56 tarstream.close()
57
58 with open(tar_path, 'rb') as archive_stream:
59 sha1, size = manifest_util.DownloadAndComputeHash(archive_stream)
60
61 archive = manifest_util.Archive(manifest_util.GetHostOS())
62 archive.url = self.server.GetURL(os.path.basename(tar_path))
63 archive.size = size
64 archive.checksum = sha1
65 return archive
66 finally:
67 oshelpers.Remove(['-rf', temp_dir])
68
69 def testInfoBasic(self):
70 """The info command should display information about the given bundle."""
71 self._WriteManifest()
72 output = self._Run(['info', 'sdk_tools'])
73 # Make sure basic information is there
74 bundle = self.manifest.GetBundle('sdk_tools')
75 archive = bundle.GetHostOSArchive();
76 self.assertTrue(bundle.name in output)
77 self.assertTrue(bundle.description in output)
78 self.assertTrue(str(bundle.revision) in output)
79 self.assertTrue(str(archive.size) in output)
80 self.assertTrue(archive.checksum in output)
81 self.assertTrue(bundle.stability in output)
82
83 def testInfoUnknownBundle(self):
84 """The info command should notify the user of unknown bundles."""
85 self._WriteManifest()
86 bogus_bundle = 'foobar'
87 output = self._Run(['info', bogus_bundle])
88 self.assertTrue(re.search(r'[uU]nknown', output))
89 self.assertTrue(bogus_bundle in output)
90
91 def testInfoMultipleBundles(self):
92 """The info command should support listing multiple bundles."""
93 self._AddDummyBundle(self.manifest, 'pepper_23')
94 self._AddDummyBundle(self.manifest, 'pepper_24')
95 self._WriteManifest()
96 output = self._Run(['info', 'pepper_23', 'pepper_24'])
97 self.assertTrue('pepper_23' in output)
98 self.assertTrue('pepper_24' in output)
99 self.assertFalse(re.search(r'[uU]nknown', output))
100
101 def testListBasic(self):
102 """The list command should display basic information about remote
103 bundles."""
104 self._WriteManifest()
105 output = self._Run(['list'])
106 self.assertTrue(re.search('I.*?sdk_tools.*?stable', output, re.MULTILINE))
107 # This line is important (it's used by the updater to determine if the
108 # sdk_tools bundle needs to be updated), so let's be explicit.
109 self.assertTrue('All installed bundles are up-to-date.')
110
111 def testListMultiple(self):
112 """The list command should display multiple bundles."""
113 self._AddDummyBundle(self.manifest, 'pepper_23')
114 self._WriteManifest()
115 output = self._Run(['list'])
116 # Added pepper_23 to the remote manifest not the local manifest, so it
117 # shouldn't be installed.
118 self.assertTrue(re.search('^[^I]*pepper_23', output, re.MULTILINE))
119 self.assertTrue('sdk_tools' in output)
120
121 def testListWithRevision(self):
122 """The list command should display the revision, if desired."""
123 self._AddDummyBundle(self.manifest, 'pepper_23')
124 self._WriteManifest()
125 output = self._Run(['list', '-r'])
126 self.assertTrue(re.search('pepper_23.*?r1337', output))
127
128 def testListWithUpdatedRevision(self):
129 """The list command should display when there is an update available."""
130 p23bundle = self._AddDummyBundle(self.manifest, 'pepper_23')
131 self._WriteCacheManifest(self.manifest)
132 # Modify the remote manifest to have a newer revision.
133 p23bundle.revision += 1
134 self._WriteManifest()
135 output = self._Run(['list', '-r'])
136 # We should see a display like this: I* pepper_23 (r1337 -> r1338)
137 # The star indicates the bundle has an update.
138 self.assertTrue(re.search('I\*\s+pepper_23.*?r1337.*?r1338', output))
139
140 def testListLocalVersionNotOnRemote(self):
141 """The list command should tell the user if they have a bundle installed
142 that doesn't exist in the remote manifest."""
143 self._WriteManifest()
144 p23bundle = self._AddDummyBundle(self.manifest, 'pepper_23')
145 self._WriteCacheManifest(self.manifest)
146 output = self._Run(['list', '-r'])
147 message = 'Bundles installed locally that are not available remotely:'
148 message_loc = output.find(message)
149 self.assertNotEqual(message_loc, -1)
150 # Make sure pepper_23 is listed after the message above.
151 self.assertTrue('pepper_23' in output[message_loc:])
152
153 def testSources(self):
154 """The sources command should allow adding/listing/removing of sources.
155 When a source is added, it will provide an additional set of bundles."""
156 other_manifest = manifest_util.SDKManifest()
157 self._AddDummyBundle(other_manifest, 'naclmono_23')
158 with open(os.path.join(self.basedir, 'source.json'), 'w') as stream:
159 stream.write(other_manifest.GetDataAsString())
160
161 source_json_url = self.server.GetURL('source.json')
162 self._WriteManifest()
163 output = self._Run(['sources', '--list'])
164 self.assertTrue('No external sources installed.' in output)
165 output = self._Run(['sources', '--add', source_json_url])
166 output = self._Run(['sources', '--list'])
167 self.assertTrue(source_json_url in output)
168
169 # Should be able to get info about that bundle.
170 output = self._Run(['info', 'naclmono_23'])
171 self.assertTrue('Unknown bundle' not in output)
172
173 self._Run(['sources', '--remove', source_json_url])
174 output = self._Run(['sources', '--list'])
175 self.assertTrue('No external sources installed.' in output)
176
177 def testUpdateBasic(self):
178 """The update command should install the contents of a bundle to the SDK."""
179 self._AddDummyBundle(self.manifest, 'pepper_23')
180 self._WriteManifest()
181 output = self._Run(['update', 'pepper_23'])
182 self.assertTrue(os.path.exists(
183 os.path.join(self.basedir, 'nacl_sdk', 'pepper_23', 'dummy.txt')))
184
185 def testUpdateInCacheButDirectoryRemoved(self):
186 """The update command should update if the bundle directory does not exist,
187 even if the bundle is already in the cache manifest."""
188 self._AddDummyBundle(self.manifest, 'pepper_23')
189 self._WriteCacheManifest(self.manifest)
190 self._WriteManifest()
191 output = self._Run(['update', 'pepper_23'])
192 self.assertTrue(os.path.exists(
193 os.path.join(self.basedir, 'nacl_sdk', 'pepper_23', 'dummy.txt')))
194
195 def testUpdateNoNewVersion(self):
196 """The update command should do nothing if the bundle is already up-to-date.
197 """
198 self._AddDummyBundle(self.manifest, 'pepper_23')
199 self._WriteManifest()
200 self._Run(['update', 'pepper_23'])
201 output = self._Run(['update', 'pepper_23'])
202 self.assertTrue('is already up-to-date.' in output)
203
204 def testUpdateWithNewVersion(self):
205 """The update command should update to a new version if it exists."""
206 bundle = self._AddDummyBundle(self.manifest, 'pepper_23')
207 self._WriteManifest()
208 self._Run(['update', 'pepper_23'])
209
210 bundle.revision += 1
211 self._WriteManifest()
212 output = self._Run(['update', 'pepper_23'])
213 self.assertTrue('already exists, but has an update available' in output)
214
215 # Now update using --force.
216 output = self._Run(['update', 'pepper_23', '--force'])
217 self.assertTrue('Updating bundle' in output)
218
219 def testUpdateUnknownBundles(self):
220 """The update command should ignore unknown bundles and notify the user."""
221 self._WriteManifest()
222 output = self._Run(['update', 'foobar'])
223 self.assertTrue('unknown bundle' in output)
224
225
226 if __name__ == '__main__':
227 sys.exit(unittest.main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698