Merge lp:~danilo/linaro-license-protection/bug-1084013 into lp:~linaro-automation/linaro-license-protection/trunk

Proposed by Данило Шеган
Status: Merged
Approved by: James Tunnicliffe
Approved revision: 153
Merged at revision: 151
Proposed branch: lp:~danilo/linaro-license-protection/bug-1084013
Merge into: lp:~linaro-automation/linaro-license-protection/trunk
Diff against target: 260 lines (+95/-62)
5 files modified
license_protected_downloads/buildinfo.py (+2/-5)
license_protected_downloads/tests/__init__.py (+13/-7)
license_protected_downloads/tests/helpers.py (+42/-0)
license_protected_downloads/tests/test_buildinfo.py (+17/-0)
license_protected_downloads/tests/test_views.py (+21/-50)
To merge this branch: bzr merge lp:~danilo/linaro-license-protection/bug-1084013
Reviewer Review Type Date Requested Status
James Tunnicliffe (community) Approve
Review via email: mp+136639@code.launchpad.net

Description of the change

This ensures files like MD5SUM (all uppercase) are still recognized in BUILD-INFO patterns: problem was that we used to lowercase the key, but then didn't match against it.

I don't see any reason to try to match the globs in a case-sensitive manner, so I am instead keeping the file patterns as they are.

This branch introduces tests on both the level of BUILD-INFO parsing, and an integration test to ensure we can access a file with all uppercase filename.

Some minor cleanups are done along the way:
 - temporary_directory helper has been moved into tests/helpers.py since it's used by both test_buildinfo.py and test_views.py now
 - a few imports have been reordered, and I extract the shared test-case setup in test_views.py

To post a comment you must log in.
Revision history for this message
Данило Шеган (danilo) wrote :

pep8 is even stricter in quantal, I'll work on fixing that in the follow-up branch.

Revision history for this message
James Tunnicliffe (dooferlad) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'license_protected_downloads/buildinfo.py'
--- license_protected_downloads/buildinfo.py 2012-07-11 11:56:10 +0000
+++ license_protected_downloads/buildinfo.py 2012-11-28 12:32:21 +0000
@@ -40,7 +40,6 @@
40 os.path.join(cls.get_search_path(path), "BUILD-INFO.txt"))40 os.path.join(cls.get_search_path(path), "BUILD-INFO.txt"))
4141
42 def _set(self, key, value):42 def _set(self, key, value):
43 key = key.lower()
44 if key in self.build_info_array[self.index]:43 if key in self.build_info_array[self.index]:
45 # A repeated key indicates we have found another chunk of44 # A repeated key indicates we have found another chunk of
46 # build-info45 # build-info
@@ -175,9 +174,7 @@
175 self.file_info_array.pop(index)174 self.file_info_array.pop(index)
176175
177if __name__ == "__main__":176if __name__ == "__main__":
178 bi = BuildInfo("/var/www/build-info/origen-blob.txt")177 import sys
179# print bi.build_info_array178 bi = BuildInfo(sys.argv[1])
180# print "file_info_array: %s" % bi.file_info_array
181# print bi.getFormatVersion()
182 for field in bi.fields_defined:179 for field in bi.fields_defined:
183 print field + " = " + str(bi.get(field))180 print field + " = " + str(bi.get(field))
184181
=== modified file 'license_protected_downloads/tests/__init__.py'
--- license_protected_downloads/tests/__init__.py 2012-11-07 18:13:07 +0000
+++ license_protected_downloads/tests/__init__.py 2012-11-28 12:32:21 +0000
@@ -1,8 +1,12 @@
1from license_protected_downloads.tests.test_buildinfo import BuildInfoTests1from license_protected_downloads.tests.test_buildinfo import (
2 BuildInfoTests,
3 FileNameMatchingTests,
4 )
2from license_protected_downloads.tests.test_models import LicenseTestCase5from license_protected_downloads.tests.test_models import LicenseTestCase
3from license_protected_downloads.tests.test_pep8 import TestPep86from license_protected_downloads.tests.test_pep8 import TestPep8
4from license_protected_downloads.tests.test_pyflakes import TestPyflakes7from license_protected_downloads.tests.test_pyflakes import TestPyflakes
5from license_protected_downloads.tests.test_views import (8from license_protected_downloads.tests.test_views import (
9 FileViewTests,
6 HowtoViewTests,10 HowtoViewTests,
7 ViewTests,11 ViewTests,
8 )12 )
@@ -14,13 +18,15 @@
1418
15#starts the test suite19#starts the test suite
16__test__ = {20__test__ = {
21 'BuildInfoTests': BuildInfoTests,
22 'FileNameMatchingTests': FileNameMatchingTests,
23 'FileViewTests': FileViewTests,
24 'HowtoViewTests': HowtoViewTests,
17 'LicenseTestCase': LicenseTestCase,25 'LicenseTestCase': LicenseTestCase,
18 'ViewTests': ViewTests,26 'RenderTextFilesTests': RenderTextFilesTests,
19 'HowtoViewTests': HowtoViewTests,27 'SetsuperuserCommandTest': SetsuperuserCommandTest,
20 'BuildInfoTests': BuildInfoTests,28 'TestOpenIDAuth': TestOpenIDAuth,
21 'TestPep8': TestPep8,29 'TestPep8': TestPep8,
22 'TestPyflakes': TestPyflakes,30 'TestPyflakes': TestPyflakes,
23 'TestOpenIDAuth': TestOpenIDAuth,31 'ViewTests': ViewTests,
24 'SetsuperuserCommandTest': SetsuperuserCommandTest,
25 'RenderTextFilesTests': RenderTextFilesTests,
26}32}
2733
=== added file 'license_protected_downloads/tests/helpers.py'
--- license_protected_downloads/tests/helpers.py 1970-01-01 00:00:00 +0000
+++ license_protected_downloads/tests/helpers.py 2012-11-28 12:32:21 +0000
@@ -0,0 +1,42 @@
1# Copyright (c)2012 Linaro.
2# Test helpers.
3
4import os
5import shutil
6import tempfile
7
8
9class temporary_directory(object):
10 """Creates a context manager for a temporary directory."""
11
12 def __enter__(self):
13 self.root = tempfile.mkdtemp()
14 return self
15
16 def __exit__(self, *args):
17 shutil.rmtree(self.root)
18
19 def make_file(self, name, data=None, with_buildinfo=False):
20 """Creates a file in this temporary directory."""
21 full_path = os.path.join(self.root, name)
22 dir_name = os.path.dirname(full_path)
23 try:
24 os.makedirs(dir_name)
25 except os.error:
26 pass
27 if with_buildinfo:
28 buildinfo_name = os.path.join(dir_name, 'BUILD-INFO.txt')
29 base_name = os.path.basename(full_path)
30 with open(buildinfo_name, 'w') as buildinfo_file:
31 buildinfo_file.write(
32 'Format-Version: 0.1\n\n'
33 'Files-Pattern: %s\n'
34 'License-Type: open\n' % base_name)
35 target = open(full_path, "w")
36 if data is None:
37 return target
38 else:
39 target.write(data)
40 target.close()
41 return full_path
42
043
=== modified file 'license_protected_downloads/tests/test_buildinfo.py'
--- license_protected_downloads/tests/test_buildinfo.py 2012-07-20 12:26:22 +0000
+++ license_protected_downloads/tests/test_buildinfo.py 2012-11-28 12:32:21 +0000
@@ -4,6 +4,7 @@
4import unittest4import unittest
5from license_protected_downloads.buildinfo import BuildInfo5from license_protected_downloads.buildinfo import BuildInfo
6from license_protected_downloads.buildinfo import IncorrectDataFormatException6from license_protected_downloads.buildinfo import IncorrectDataFormatException
7from license_protected_downloads.tests.helpers import temporary_directory
78
8THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))9THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
910
@@ -275,5 +276,21 @@
275 self.assertEquals(build_info.file_info_array, [{}])276 self.assertEquals(build_info.file_info_array, [{}])
276277
277278
279class FileNameMatchingTests(unittest.TestCase):
280 def test_buildinfo_simple_filename(self):
281 with temporary_directory() as serve_root:
282 sample_file = serve_root.make_file("MD5SUM", data="blah")
283 serve_root.make_file(
284 "BUILD-INFO.txt",
285 data=(
286 "Format-Version: 2.0\n\n"
287 "Files-Pattern: MD5SUM\n"
288 "License-Type: open\n"
289 ))
290 build_info = BuildInfo(sample_file)
291 file_info = build_info.getInfoForFile()
292 self.assertEqual('open', file_info[0]['license-type'])
293
294
278if __name__ == '__main__':295if __name__ == '__main__':
279 unittest.main()296 unittest.main()
280297
=== modified file 'license_protected_downloads/tests/test_views.py'
--- license_protected_downloads/tests/test_views.py 2012-11-21 17:57:53 +0000
+++ license_protected_downloads/tests/test_views.py 2012-11-28 12:32:21 +0000
@@ -4,60 +4,25 @@
4from django.test import Client, TestCase4from django.test import Client, TestCase
5import hashlib5import hashlib
6import os6import os
7import tempfile
7import unittest8import unittest
8import urlparse9import urlparse
9import shutil
10import tempfile
1110
12from license_protected_downloads import bzr_version11from license_protected_downloads import bzr_version
13from license_protected_downloads.buildinfo import BuildInfo12from license_protected_downloads.buildinfo import BuildInfo
13from license_protected_downloads.config import INTERNAL_HOSTS
14from license_protected_downloads.tests.helpers import temporary_directory
14from license_protected_downloads.views import _insert_license_into_db15from license_protected_downloads.views import _insert_license_into_db
16from license_protected_downloads.views import _process_include_tags
15from license_protected_downloads.views import _sizeof_fmt17from license_protected_downloads.views import _sizeof_fmt
16from license_protected_downloads.views import _process_include_tags
17from license_protected_downloads.views import is_same_parent_dir18from license_protected_downloads.views import is_same_parent_dir
18from license_protected_downloads.config import INTERNAL_HOSTS
1919
2020
21THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))21THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
22TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")22TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")
2323
2424
25class temporary_directory(object):25class BaseServeViewTest(TestCase):
26 """Creates a context manager for a temporary directory."""
27
28 def __enter__(self):
29 self.root = tempfile.mkdtemp()
30 return self
31
32 def __exit__(self, *args):
33 shutil.rmtree(self.root)
34
35 def make_file(self, name, data=None, with_buildinfo=False):
36 """Creates a file in this temporary directory."""
37 full_path = os.path.join(self.root, name)
38 dir_name = os.path.dirname(full_path)
39 try:
40 os.makedirs(dir_name)
41 except os.error:
42 pass
43 if with_buildinfo:
44 buildinfo_name = os.path.join(dir_name, 'BUILD-INFO.txt')
45 base_name = os.path.basename(full_path)
46 with open(buildinfo_name, 'w') as buildinfo_file:
47 buildinfo_file.write(
48 'Format-Version: 0.1\n\n'
49 'Files-Pattern: %s\n'
50 'License-Type: open\n' % base_name)
51 target = open(full_path, "w")
52 if data is None:
53 return target
54 else:
55 target.write(data)
56 target.close()
57 return full_path
58
59
60class ViewTests(TestCase):
61 def setUp(self):26 def setUp(self):
62 self.client = Client()27 self.client = Client()
63 self.old_served_paths = settings.SERVED_PATHS28 self.old_served_paths = settings.SERVED_PATHS
@@ -67,6 +32,7 @@
67 def tearDown(self):32 def tearDown(self):
68 settings.SERVED_PATHS = self.old_served_paths33 settings.SERVED_PATHS = self.old_served_paths
6934
35class ViewTests(BaseServeViewTest):
70 def test_license_directly(self):36 def test_license_directly(self):
71 response = self.client.get('/licenses/license.html', follow=True)37 response = self.client.get('/licenses/license.html', follow=True)
72 self.assertEqual(response.status_code, 200)38 self.assertEqual(response.status_code, 200)
@@ -674,16 +640,7 @@
674 self.assertFalse(is_same_parent_dir(TESTSERVER_ROOT, fname))640 self.assertFalse(is_same_parent_dir(TESTSERVER_ROOT, fname))
675641
676642
677class HowtoViewTests(TestCase):643class HowtoViewTests(BaseServeViewTest):
678 def setUp(self):
679 self.client = Client()
680 self.old_served_paths = settings.SERVED_PATHS
681 settings.SERVED_PATHS = [os.path.join(THIS_DIRECTORY,
682 "testserver_root")]
683
684 def tearDown(self):
685 settings.SERVED_PATHS = self.old_served_paths
686
687 def test_no_howtos(self):644 def test_no_howtos(self):
688 with temporary_directory() as serve_root:645 with temporary_directory() as serve_root:
689 settings.SERVED_PATHS = [serve_root.root]646 settings.SERVED_PATHS = [serve_root.root]
@@ -754,5 +711,19 @@
754 self.assertContains(response, 'HowTo Test')711 self.assertContains(response, 'HowTo Test')
755712
756713
714class FileViewTests(BaseServeViewTest):
715 def test_static_file(self):
716 with temporary_directory() as serve_root:
717 settings.SERVED_PATHS = [serve_root.root]
718 serve_root.make_file("MD5SUM")
719 serve_root.make_file(
720 "BUILD-INFO.txt",
721 data=("Format-Version: 2.0\n\n"
722 "Files-Pattern: MD5SUM\n"
723 "License-Type: open\n"))
724 response = self.client.get('/MD5SUM')
725 self.assertEqual(response.status_code, 200)
726
727
757if __name__ == '__main__':728if __name__ == '__main__':
758 unittest.main()729 unittest.main()

Subscribers

People subscribed via source and target branches