← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~twom/launchpad:pagetests-access-temporrary into launchpad:master

 

Tom Wardill has proposed merging ~twom/launchpad:pagetests-access-temporrary into launchpad:master.

Commit message:
Move to a temporary pagetests-access.log

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~twom/launchpad/+git/launchpad/+merge/387543

Avoid race conditions in writing to the file and reading it back within the same test.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~twom/launchpad:pagetests-access-temporrary into launchpad:master.
diff --git a/lib/lp/app/stories/basics/xx-pagetest-logging.txt b/lib/lp/app/stories/basics/xx-pagetest-logging.txt
index c140d45..b700a8b 100644
--- a/lib/lp/app/stories/basics/xx-pagetest-logging.txt
+++ b/lib/lp/app/stories/basics/xx-pagetest-logging.txt
@@ -5,7 +5,7 @@ pagetests-access.log file.
 
     >>> browser.open('http://launchpad.test/')
 
-    >>> log = open('logs/pagetests-access.log').read()
+    >>> log = open(page_log_location).read()
     >>> print(log.strip().split('\n')[-1])
     127.0.0.88 - ... "launchpad.test" [...] "GET / HTTP/1.1" 200 ...
     "Anonymous" "RootObject:index.html" "" "Python-urllib/..."
diff --git a/lib/lp/oci/browser/ocirecipe.py b/lib/lp/oci/browser/ocirecipe.py
index 356ac1c..9d3adf1 100644
--- a/lib/lp/oci/browser/ocirecipe.py
+++ b/lib/lp/oci/browser/ocirecipe.py
@@ -322,6 +322,7 @@ class IOCIRecipeEditSchema(Interface):
         "build_file",
         "build_daily",
         "require_virtualized",
+        "allow_internet",
         ])
 
 
@@ -434,7 +435,7 @@ class OCIRecipeAdminView(BaseOCIRecipeEditView):
 
     page_title = "Administer"
 
-    field_names = ("require_virtualized",)
+    field_names = ("require_virtualized", "allow_internet")
 
 
 class OCIRecipeEditView(BaseOCIRecipeEditView, EnableProcessorsMixin):
diff --git a/lib/lp/oci/browser/tests/test_ocirecipe.py b/lib/lp/oci/browser/tests/test_ocirecipe.py
index e25d463..a1b86ba 100644
--- a/lib/lp/oci/browser/tests/test_ocirecipe.py
+++ b/lib/lp/oci/browser/tests/test_ocirecipe.py
@@ -280,14 +280,17 @@ class TestOCIRecipeAdminView(BaseTestOCIRecipeView):
         login_person(self.person)
         recipe = self.factory.makeOCIRecipe(registrant=self.person)
         self.assertTrue(recipe.require_virtualized)
+        self.assertTrue(recipe.allow_internet)
 
         browser = self.getViewBrowser(recipe, user=commercial_admin)
         browser.getLink("Administer OCI recipe").click()
         browser.getControl("Require virtualized builders").selected = False
+        browser.getControl("Allow external network access").selected = False
         browser.getControl("Update OCI recipe").click()
 
         login_person(self.person)
         self.assertFalse(recipe.require_virtualized)
+        self.assertFalse(recipe.allow_internet)
 
     def test_admin_recipe_sets_date_last_modified(self):
         # Administering an OCI recipe sets the date_last_modified property.
diff --git a/lib/lp/oci/interfaces/ocirecipe.py b/lib/lp/oci/interfaces/ocirecipe.py
index 64c5387..37f74a2 100644
--- a/lib/lp/oci/interfaces/ocirecipe.py
+++ b/lib/lp/oci/interfaces/ocirecipe.py
@@ -408,6 +408,13 @@ class IOCIRecipeAdminAttributes(Interface):
         value_type=Reference(schema=IProcessor),
         readonly=False))
 
+    allow_internet = exported(Bool(
+        title=_("Allow external network access"),
+        required=True, readonly=False,
+        description=_(
+            "Allow access to external network resources via a proxy.  "
+            "Resources hosted on Launchpad itself are always allowed.")))
+
 
 @exported_as_webservice_entry(
     publish_web_link=True, as_of="devel", singular_name="oci_recipe")
@@ -421,7 +428,8 @@ class IOCIRecipeSet(Interface):
 
     def new(name, registrant, owner, oci_project, git_ref, build_file,
             description=None, official=False, require_virtualized=True,
-            build_daily=False, processors=None, date_created=DEFAULT):
+            build_daily=False, processors=None, date_created=DEFAULT,
+            allow_internet=True):
         """Create an IOCIRecipe."""
 
     def exists(owner, oci_project, name):
diff --git a/lib/lp/oci/model/ocirecipe.py b/lib/lp/oci/model/ocirecipe.py
index 38e5a9a..45691c5 100644
--- a/lib/lp/oci/model/ocirecipe.py
+++ b/lib/lp/oci/model/ocirecipe.py
@@ -145,11 +145,14 @@ class OCIRecipe(Storm, WebhookTargetMixin):
     require_virtualized = Bool(name="require_virtualized", default=True,
                                allow_none=False)
 
+    allow_internet = Bool(name='allow_internet', allow_none=False)
+
     build_daily = Bool(name="build_daily", default=False)
 
     def __init__(self, name, registrant, owner, oci_project, git_ref,
                  description=None, official=False, require_virtualized=True,
-                 build_file=None, build_daily=False, date_created=DEFAULT):
+                 build_file=None, build_daily=False, date_created=DEFAULT,
+                 allow_internet=True):
         if not getFeatureFlag(OCI_RECIPE_ALLOW_CREATE):
             raise OCIRecipeFeatureDisabled()
         super(OCIRecipe, self).__init__()
@@ -165,6 +168,7 @@ class OCIRecipe(Storm, WebhookTargetMixin):
         self.date_created = date_created
         self.date_last_modified = date_created
         self.git_ref = git_ref
+        self.allow_internet = allow_internet
 
     def __repr__(self):
         return "<OCIRecipe ~%s/%s/+oci/%s/+recipe/%s>" % (
@@ -502,7 +506,8 @@ class OCIRecipeSet:
 
     def new(self, name, registrant, owner, oci_project, git_ref, build_file,
             description=None, official=False, require_virtualized=True,
-            build_daily=False, processors=None, date_created=DEFAULT):
+            build_daily=False, processors=None, date_created=DEFAULT,
+            allow_internet=True):
         """See `IOCIRecipeSet`."""
         if not registrant.inTeam(owner):
             if owner.is_team:
@@ -524,7 +529,7 @@ class OCIRecipeSet:
         oci_recipe = OCIRecipe(
             name, registrant, owner, oci_project, git_ref, description,
             official, require_virtualized, build_file, build_daily,
-            date_created)
+            date_created, allow_internet)
         store.add(oci_recipe)
 
         if processors is None:
diff --git a/lib/lp/oci/model/ocirecipebuildbehaviour.py b/lib/lp/oci/model/ocirecipebuildbehaviour.py
index 8b5f5e0..fce6dd3 100644
--- a/lib/lp/oci/model/ocirecipebuildbehaviour.py
+++ b/lib/lp/oci/model/ocirecipebuildbehaviour.py
@@ -85,7 +85,7 @@ class OCIRecipeBuildBehaviour(SnapProxyMixin, BuildFarmJobBehaviourBase):
         build = self.build
         args = yield super(OCIRecipeBuildBehaviour, self).extraBuildArgs(
             logger=logger)
-        yield self.addProxyArgs(args)
+        yield self.addProxyArgs(args, build.recipe.allow_internet)
         # XXX twom 2020-02-17 This may need to be more complex, and involve
         # distribution name.
         args["name"] = build.recipe.name
diff --git a/lib/lp/oci/tests/test_ocirecipe.py b/lib/lp/oci/tests/test_ocirecipe.py
index 1d07913..880c351 100644
--- a/lib/lp/oci/tests/test_ocirecipe.py
+++ b/lib/lp/oci/tests/test_ocirecipe.py
@@ -806,6 +806,7 @@ class TestOCIRecipeSet(TestCaseWithFactory):
         self.assertEqual(target.official, False)
         self.assertEqual(target.require_virtualized, False)
         self.assertEqual(target.git_ref, git_ref)
+        self.assertTrue(target.allow_internet)
 
     def test_already_exists(self):
         owner = self.factory.makePerson()
diff --git a/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py b/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
index f621037..750347e 100644
--- a/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
+++ b/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
@@ -106,13 +106,14 @@ class MakeOCIBuildMixin:
         build.queueBuild()
         return build
 
-    def makeJob(self, git_ref, recipe=None, build=None):
+    def makeJob(self, git_ref, recipe=None, build=None, **kwargs):
         """Create a sample `IOCIRecipeBuildBehaviour`."""
         if build is None:
             if recipe is None:
-                build = self.factory.makeOCIRecipeBuild()
+                build = self.factory.makeOCIRecipeBuild(**kwargs)
             else:
-                build = self.factory.makeOCIRecipeBuild(recipe=recipe)
+                build = self.factory.makeOCIRecipeBuild(
+                    recipe=recipe, **kwargs)
         build.recipe.git_ref = git_ref
 
         job = IBuildFarmJobBehaviour(build)
@@ -330,7 +331,7 @@ class TestAsyncOCIRecipeBuildBehaviour(MakeOCIBuildMixin, TestCaseWithFactory):
     def test_dispatchBuildToSlave_prefers_lxd(self):
         self.pushConfig("snappy", builder_proxy_host=None)
         [ref] = self.factory.makeGitRefs()
-        job = self.makeJob(git_ref=ref)
+        job = self.makeJob(git_ref=ref, allow_internet=False)
         builder = MockBuilder()
         builder.processor = job.build.processor
         slave = OkSlave()
@@ -349,7 +350,7 @@ class TestAsyncOCIRecipeBuildBehaviour(MakeOCIBuildMixin, TestCaseWithFactory):
     def test_dispatchBuildToSlave_falls_back_to_chroot(self):
         self.pushConfig("snappy", builder_proxy_host=None)
         [ref] = self.factory.makeGitRefs()
-        job = self.makeJob(git_ref=ref)
+        job = self.makeJob(git_ref=ref, allow_internet=False)
         builder = MockBuilder()
         builder.processor = job.build.processor
         slave = OkSlave()
@@ -401,6 +402,17 @@ class TestAsyncOCIRecipeBuildBehaviour(MakeOCIBuildMixin, TestCaseWithFactory):
         # for Distro series
         self.assertEqual(distroseries.name, slave.call_log[1][5]['series'])
 
+    @defer.inlineCallbacks
+    def test_extraBuildArgs_disallow_internet(self):
+        # If external network access is not allowed for the OCI Recipe,
+        # extraBuildArgs does not dispatch a proxy token.
+        [ref] = self.factory.makeGitRefs()
+        job = self.makeJob(git_ref=ref, allow_internet=False)
+        with dbuser(config.builddmaster.dbuser):
+            args = yield job.extraBuildArgs()
+        self.assertNotIn("proxy_url", args)
+        self.assertNotIn("revocation_endpoint", args)
+
 
 class TestHandleStatusForOCIRecipeBuild(WithScenarios,
                                         MakeOCIBuildMixin,
diff --git a/lib/lp/testing/factory.py b/lib/lp/testing/factory.py
index b57632d..6c6c9cb 100644
--- a/lib/lp/testing/factory.py
+++ b/lib/lp/testing/factory.py
@@ -4987,7 +4987,8 @@ class BareLaunchpadObjectFactory(ObjectFactory):
     def makeOCIRecipe(self, name=None, registrant=None, owner=None,
                       oci_project=None, git_ref=None, description=None,
                       official=False, require_virtualized=True,
-                      build_file=None, date_created=DEFAULT):
+                      build_file=None, date_created=DEFAULT,
+                      allow_internet=True):
         """Make a new OCIRecipe."""
         if name is None:
             name = self.getUniqueString(u"oci-recipe-name")
@@ -5013,7 +5014,8 @@ class BareLaunchpadObjectFactory(ObjectFactory):
             description=description,
             official=official,
             require_virtualized=require_virtualized,
-            date_created=date_created)
+            date_created=date_created,
+            allow_internet=allow_internet)
 
     def makeOCIRecipeArch(self, recipe=None, processor=None):
         """Make a new OCIRecipeArch."""
@@ -5026,7 +5028,7 @@ class BareLaunchpadObjectFactory(ObjectFactory):
     def makeOCIRecipeBuild(self, requester=None, registrant=None, recipe=None,
                            distro_arch_series=None, date_created=DEFAULT,
                            status=BuildStatus.NEEDSBUILD, builder=None,
-                           duration=None):
+                           duration=None, **kwargs):
         """Make a new OCIRecipeBuild."""
         if requester is None:
             requester = self.makePerson()
@@ -5047,7 +5049,7 @@ class BareLaunchpadObjectFactory(ObjectFactory):
             if registrant is None:
                 registrant = requester
             recipe = self.makeOCIRecipe(
-                registrant=registrant, oci_project=oci_project)
+                registrant=registrant, oci_project=oci_project, **kwargs)
         oci_build = getUtility(IOCIRecipeBuildSet).new(
             requester, recipe, distro_arch_series, date_created)
         if duration is not None:
diff --git a/lib/lp/testing/layers.py b/lib/lp/testing/layers.py
index fa89018..d93e88f 100644
--- a/lib/lp/testing/layers.py
+++ b/lib/lp/testing/layers.py
@@ -1648,6 +1648,8 @@ class PageTestLayer(LaunchpadFunctionalLayer, BingServiceLayer):
     """Environment for page tests.
     """
 
+    log_location = None
+
     @classmethod
     @profiled
     def setUp(cls):
@@ -1655,7 +1657,8 @@ class PageTestLayer(LaunchpadFunctionalLayer, BingServiceLayer):
             PageTestLayer.profiler = Profile()
         else:
             PageTestLayer.profiler = None
-        file_handler = logging.FileHandler('logs/pagetests-access.log', 'w')
+        cls.log_location = tempfile.NamedTemporaryFile().name
+        file_handler = logging.FileHandler(cls.log_location, 'w')
         file_handler.setFormatter(logging.Formatter())
         logger = PythonLogger('pagetests-access')
         logger.logger.addHandler(file_handler)
diff --git a/lib/lp/testing/pages.py b/lib/lp/testing/pages.py
index 77a4059..949dae6 100644
--- a/lib/lp/testing/pages.py
+++ b/lib/lp/testing/pages.py
@@ -903,6 +903,7 @@ def setUpGlobs(test, future=False):
     test.globs['print_self_link_of_entries'] = print_self_link_of_entries
     test.globs['print_tag_with_id'] = print_tag_with_id
     test.globs['PageTestLayer'] = PageTestLayer
+    test.globs['page_log_location'] = PageTestLayer.log_location
     test.globs['stop'] = stop
     test.globs['six'] = six