Merge ~ines-almeida/launchpad:fix-infer-pro-enable-function into launchpad:master

Proposed by Ines Almeida
Status: Merged
Approved by: Colin Watson
Approved revision: ee5dea5be0cad62984aeccbb65a42237e89b364a
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~ines-almeida/launchpad:fix-infer-pro-enable-function
Merge into: launchpad:master
Diff against target: 140 lines (+62/-4)
4 files modified
lib/lp/code/model/branchhosting.py (+7/-1)
lib/lp/code/model/tests/test_branchhosting.py (+12/-3)
lib/lp/snappy/model/snap.py (+2/-0)
lib/lp/snappy/tests/test_snap.py (+41/-0)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+454815@code.launchpad.net

Commit message

Catch revision ID not well formatted exception in inferProEnable

Description of the change

Currently, the garbo job to populate the pro-enable value is failing in production with a `ValueError: Revision ID '<revision_id>' is not well-formed.`

Here we are catching that error and setting those to False by default.

Also took the chance to add more unit tests.

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) :
review: Needs Fixing
Revision history for this message
Ines Almeida (ines-almeida) wrote :

Sounds good, updated

Revision history for this message
Colin Watson (cjwatson) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/code/model/branchhosting.py b/lib/lp/code/model/branchhosting.py
2index 365a6c8..3be5857 100644
3--- a/lib/lp/code/model/branchhosting.py
4+++ b/lib/lp/code/model/branchhosting.py
5@@ -31,6 +31,10 @@ class RequestExceptionWrapper(requests.RequestException):
6 """A non-requests exception that occurred during a request."""
7
8
9+class InvalidRevisionException(Exception):
10+ """An exception thrown when a revision ID is not valid"""
11+
12+
13 @implementer(IBranchHostingClient)
14 class BranchHostingClient:
15 """A client for the Bazaar Loggerhead API."""
16@@ -94,7 +98,9 @@ class BranchHostingClient:
17 attacks.
18 """
19 if rev is not None and "/" in rev:
20- raise ValueError("Revision ID '%s' is not well-formed." % rev)
21+ raise InvalidRevisionException(
22+ "Revision ID '%s' is not well-formed." % rev
23+ )
24
25 def getDiff(
26 self, branch_id, new, old=None, context_lines=None, logger=None
27diff --git a/lib/lp/code/model/tests/test_branchhosting.py b/lib/lp/code/model/tests/test_branchhosting.py
28index 05931b2..76e7aa2 100644
29--- a/lib/lp/code/model/tests/test_branchhosting.py
30+++ b/lib/lp/code/model/tests/test_branchhosting.py
31@@ -19,6 +19,7 @@ from zope.security.proxy import removeSecurityProxy
32
33 from lp.code.errors import BranchFileNotFound, BranchHostingFault
34 from lp.code.interfaces.branchhosting import IBranchHostingClient
35+from lp.code.model.branchhosting import InvalidRevisionException
36 from lp.services.job.interfaces.job import IRunnableJob, JobStatus
37 from lp.services.job.model.job import Job
38 from lp.services.job.runner import BaseRunnableJob, JobRunner
39@@ -90,10 +91,14 @@ class TestBranchHostingClient(TestCase):
40 self.assertRequest("+branch-id/123/diff/2/1?context_lines=4")
41
42 def test_getDiff_bad_old_revision(self):
43- self.assertRaises(ValueError, self.client.getDiff, 123, "x/y", "1")
44+ self.assertRaises(
45+ InvalidRevisionException, self.client.getDiff, 123, "x/y", "1"
46+ )
47
48 def test_getDiff_bad_new_revision(self):
49- self.assertRaises(ValueError, self.client.getDiff, 123, "1", "x/y")
50+ self.assertRaises(
51+ InvalidRevisionException, self.client.getDiff, 123, "1", "x/y"
52+ )
53
54 def test_getDiff_failure(self):
55 with self.mockRequests("GET", status=400):
56@@ -144,7 +149,11 @@ class TestBranchHostingClient(TestCase):
57
58 def test_getBlob_bad_revision(self):
59 self.assertRaises(
60- ValueError, self.client.getBlob, 123, "file-name", rev="x/y"
61+ InvalidRevisionException,
62+ self.client.getBlob,
63+ 123,
64+ "file-name",
65+ rev="x/y",
66 )
67
68 def test_getBlob_failure(self):
69diff --git a/lib/lp/snappy/model/snap.py b/lib/lp/snappy/model/snap.py
70index 92dd911..c47ec1e 100644
71--- a/lib/lp/snappy/model/snap.py
72+++ b/lib/lp/snappy/model/snap.py
73@@ -78,6 +78,7 @@ from lp.code.interfaces.gitrepository import (
74 )
75 from lp.code.model.branch import Branch
76 from lp.code.model.branchcollection import GenericBranchCollection
77+from lp.code.model.branchhosting import InvalidRevisionException
78 from lp.code.model.branchnamespace import (
79 BRANCH_POLICY_ALLOWED_TYPES,
80 BRANCH_POLICY_REQUIRED_GRANTS,
81@@ -1671,6 +1672,7 @@ class SnapSet:
82 MissingSnapcraftYaml,
83 CannotFetchSnapcraftYaml,
84 CannotParseSnapcraftYaml,
85+ InvalidRevisionException,
86 ):
87 pass
88 else:
89diff --git a/lib/lp/snappy/tests/test_snap.py b/lib/lp/snappy/tests/test_snap.py
90index 0e6a31e..3cc6d7f 100644
91--- a/lib/lp/snappy/tests/test_snap.py
92+++ b/lib/lp/snappy/tests/test_snap.py
93@@ -3696,6 +3696,47 @@ class TestSnapProcessors(TestCaseWithFactory):
94 self.assertFalse(inferProEnable(refs[6])) # Snap w/out snapcraft.yaml
95 self.assertFalse(inferProEnable(None)) # Snap w/out ref or branch
96
97+ def test_inferProEnable_branches(self):
98+ """With a branch as a context, inferProEnable returns the inferred
99+ values for pro-enable as expected (see test description above)
100+ """
101+ branches = [
102+ removeSecurityProxy(self.factory.makeBranch()) for _ in range(7)
103+ ]
104+ blobs = {
105+ branch.id: blob
106+ for branch, blob in (
107+ (branches[0], b"name: test-snap\n"),
108+ (branches[1], b"name: test-snap\nbase: core\n"),
109+ (branches[2], b"name: test-snap\nbase: core18\n"),
110+ (branches[3], b"name: test-snap\nbuild-base: devel\n"),
111+ (branches[4], b"name: core\ntype: base\n"),
112+ (branches[5], b"name: core18\ntype: base\n"),
113+ )
114+ }
115+ self.useFixture(
116+ BranchHostingFixture()
117+ ).getBlob = lambda branch_id, *args, **kwargs: blobs.get(branch_id)
118+
119+ inferProEnable = getUtility(ISnapSet).inferProEnable
120+ self.assertTrue(inferProEnable(branches[0])) # Snap w no base
121+ self.assertTrue(inferProEnable(branches[1])) # Snap w 'core' base
122+ self.assertFalse(inferProEnable(branches[2])) # Snap w 'core18' base
123+ self.assertFalse(inferProEnable(branches[3])) # Snap w only build-base
124+ self.assertTrue(inferProEnable(branches[4])) # 'core' snap itself
125+ self.assertFalse(inferProEnable(branches[5])) # 'core18' snap itself
126+ self.assertFalse(inferProEnable(branches[6])) # w/out snapcraft.yaml
127+ self.assertFalse(inferProEnable(None)) # w/out ref or branch
128+
129+ def test_inferProEnable_bad_revision(self):
130+ """A branch with an invalid revision ID (or an invalid last_scanned_id)
131+ will have its pro-enable value set to False.
132+ """
133+ branch = self.factory.makeBranch()
134+ branch.last_scanned_id = "bad/revision"
135+ inferProEnable = getUtility(ISnapSet).inferProEnable
136+ self.assertFalse(inferProEnable(removeSecurityProxy(branch)))
137+
138
139 class TestSnapWebservice(TestCaseWithFactory):
140 layer = LaunchpadFunctionalLayer

Subscribers

People subscribed via source and target branches

to status/vote changes: