Merge ~jslarraz/review-tools:add-container-exceptions-and-tests-framework into review-tools:master

Proposed by Jorge Sancho Larraz
Status: Merged
Merged at revision: d02c70de67d1ee0b90672eea8bd2fda11e2cc72e
Proposed branch: ~jslarraz/review-tools:add-container-exceptions-and-tests-framework
Merge into: review-tools:master
Diff against target: 180 lines (+76/-12)
5 files modified
reviewtools/containers/base_container.py (+26/-9)
reviewtools/sr_common.py (+5/-2)
reviewtools/sr_lint.py (+6/-1)
reviewtools/tests/containers/__init__.py (+0/-0)
reviewtools/tests/containers/test_base_container.py (+39/-0)
Reviewer Review Type Date Requested Status
Nishit Majithia Approve
Review via email: mp+466372@code.launchpad.net

Commit message

Add container exception and tests framework

To post a comment you must log in.
Revision history for this message
Nishit Majithia (0xnishit) wrote :

thanks for adding tests and handling error more standardize
lgtm!!

review: Approve
Revision history for this message
Alex Murray (alexmurray) wrote :

Thanks for this - one comment though, normally exceptions have a class or type - so I would expect this to be called 'InvalidContainerExtension' say rather than just 'ContainerException' - or perhaps 'BaseContainerException' - just wondering whether you think the name should be changed?

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reviewtools/containers/base_container.py b/reviewtools/containers/base_container.py
2index a3f5f4a..a9e0f41 100644
3--- a/reviewtools/containers/base_container.py
4+++ b/reviewtools/containers/base_container.py
5@@ -3,7 +3,25 @@ import shutil
6 import magic
7 from tempfile import mkdtemp
8
9-from reviewtools.common import MKDTEMP_PREFIX, MKDTEMP_DIR, error, debug, unpack_pkg, recursive_rm, open_file_read
10+from reviewtools.common import (
11+ MKDTEMP_PREFIX,
12+ MKDTEMP_DIR,
13+ debug,
14+ unpack_pkg,
15+ recursive_rm,
16+ open_file_read,
17+)
18+
19+
20+class ContainerException(Exception):
21+ """This class represents exceptions"""
22+
23+ def __init__(self, value):
24+ self.value = value
25+
26+ def __str__(self):
27+ return str(self.value)
28+
29
30 class BaseContainer:
31 """
32@@ -31,14 +49,14 @@ class BaseContainer:
33 self.tmp_dir = mkdtemp(prefix=MKDTEMP_PREFIX, dir=MKDTEMP_DIR)
34 if os.path.isfile(fn):
35 if os.path.basename(fn).count(".") == 0:
36- error("container filename must include an extension")
37+ raise ContainerException("container filename must include an extension")
38 self.filename = os.path.join(
39 self.tmp_dir,
40 os.path.basename(fn),
41 )
42 shutil.copyfile(fn, self.filename)
43 else:
44- error("Could not find '%s'" % fn)
45+ raise ContainerException("Could not find '%s'" % fn)
46
47 self.unpack_dir = ".".join(self.filename.split(".")[:-1])
48 unpack_pkg(fn, dest=self.unpack_dir)
49@@ -62,11 +80,11 @@ class BaseContainer:
50 def _extract_file(self, fn):
51 """Extract file"""
52 if not fn.startswith("/"):
53- error("_extract_file() expects absolute path")
54+ raise ContainerException("_extract_file() expects absolute path")
55 rel = os.path.relpath(fn, self.unpack_dir)
56
57 if not os.path.isfile(fn):
58- error("Could not find '%s'" % rel)
59+ raise ContainerException("Could not find '%s'" % rel)
60 return open_file_read(fn)
61
62 def get_files_list(self):
63@@ -91,11 +109,10 @@ class BaseContainer:
64 continue
65
66 if (
67- res in self.magic_binary_file_descriptions
68- and not i.endswith(".mo") # check is not a message catalog
69- and i not in bin_files
70+ res in self.magic_binary_file_descriptions
71+ and not i.endswith(".mo") # check is not a message catalog
72+ and i not in bin_files
73 ):
74 bin_files.append(i)
75
76 return bin_files
77-
78diff --git a/reviewtools/sr_common.py b/reviewtools/sr_common.py
79index ca8960f..9bdb556 100644
80--- a/reviewtools/sr_common.py
81+++ b/reviewtools/sr_common.py
82@@ -30,7 +30,7 @@ from reviewtools.common import (
83 unsquashfs_lln_parse,
84 verify_type,
85 )
86-from reviewtools.containers.base_container import BaseContainer
87+from reviewtools.containers.base_container import BaseContainer, ContainerException
88 from reviewtools.overrides import interfaces_attribs_addons
89
90
91@@ -417,7 +417,10 @@ class SnapReview(ReviewBase):
92 return
93 ReviewBase.__init__(self, review_type, overrides=overrides)
94
95- self.snap = BaseContainer(fn)
96+ try:
97+ self.snap = BaseContainer(fn)
98+ except ContainerException as e:
99+ error(str(e))
100 # TODO: update code to directly use self.snap.xxx instead of assigning here
101 self.pkg_filename = self.snap.filename
102 self.unpack_dir = self.snap.unpack_dir
103diff --git a/reviewtools/sr_lint.py b/reviewtools/sr_lint.py
104index 40990ad..518e9a2 100644
105--- a/reviewtools/sr_lint.py
106+++ b/reviewtools/sr_lint.py
107@@ -16,9 +16,11 @@
108
109 from __future__ import print_function
110 from reviewtools.schemas.schema_validator import validate
111+from reviewtools.containers.base_container import ContainerException
112 from reviewtools.sr_common import SnapReview
113 from reviewtools.common import (
114 find_external_symlinks,
115+ error,
116 )
117 from reviewtools.overrides import (
118 redflagged_snap_types_overrides,
119@@ -1979,7 +1981,10 @@ class SnapReviewLint(SnapReview):
120 else:
121 appnames.append("%s.%s" % (self.snap_yaml["name"], app))
122
123- fh = self.snap._extract_file(fn)
124+ try:
125+ fh = self.snap._extract_file(fn)
126+ except ContainerException as e:
127+ error(str(e))
128 lines = fh.readlines()
129 fh.close()
130
131diff --git a/reviewtools/tests/containers/__init__.py b/reviewtools/tests/containers/__init__.py
132new file mode 100644
133index 0000000..e69de29
134--- /dev/null
135+++ b/reviewtools/tests/containers/__init__.py
136diff --git a/reviewtools/tests/containers/test_base_container.py b/reviewtools/tests/containers/test_base_container.py
137new file mode 100644
138index 0000000..4d0563f
139--- /dev/null
140+++ b/reviewtools/tests/containers/test_base_container.py
141@@ -0,0 +1,39 @@
142+import os
143+import shutil
144+import unittest
145+from tempfile import mkdtemp
146+from reviewtools.containers.base_container import BaseContainer, ContainerException
147+from reviewtools.tests.utils import make_snap2
148+
149+
150+class TestBaseContainer(unittest.TestCase):
151+ """Tests for reviewtools.containers.base_container.BaseContainer"""
152+
153+ @classmethod
154+ def setUpClass(cls):
155+ cls.tmp_dir = mkdtemp()
156+ cls.fn = make_snap2(
157+ output_dir=cls.tmp_dir,
158+ )
159+
160+ @classmethod
161+ def tearDownClass(cls):
162+ if os.path.exists(cls.tmp_dir):
163+ shutil.rmtree(cls.tmp_dir)
164+
165+ # Test class initialization
166+ def test_initialization_container__happy(self):
167+ try:
168+ BaseContainer(self.fn)
169+ except Exception:
170+ self.fail(
171+ "An unexpected error occurred during BaseContainer initialization with container file"
172+ )
173+
174+ def test_initialization__missing_file(self):
175+ with self.assertRaises(ContainerException) as context:
176+ BaseContainer("/non/existing/path")
177+ self.assertTrue(
178+ "does not exists or has incorrect permissions"
179+ in context.exception.value
180+ )

Subscribers

People subscribed via source and target branches