From 4b726ae71f9467682e430577c2f9b7b93cdc264b Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Sat, 2 Jul 2022 15:35:23 -0700 Subject: Add branch cache fetch failure tests These two tests exercise the LookupError code paths in the branch cache a bit more. In particular, we now have a test that verifies that a failure in fetching will result in a retry later. Change-Id: Ia83b0233ccc31f018a61093d02fd1ce68547f9c0 --- tests/unit/test_connection.py | 49 +++++++++++++++++++++++++++++++++++++++++++ tests/unit/test_zk.py | 12 +++++++++++ 2 files changed, 61 insertions(+) diff --git a/tests/unit/test_connection.py b/tests/unit/test_connection.py index ab781252e..b4c155240 100644 --- a/tests/unit/test_connection.py +++ b/tests/unit/test_connection.py @@ -849,3 +849,52 @@ class TestElasticsearchConnection(AnsibleZuulTestCase): # Check if there is a secret leak self.assertFalse('test_secret' in build_doc['job_vars']) + + +class TestConnectionsBranchCache(ZuulTestCase): + config_file = "zuul-gerrit-github.conf" + tenant_config_file = 'config/multi-driver/main.yaml' + + def test_branch_cache_fetch_error(self): + # Test that a fetch error stores the right value in the branch cache + tenant = self.scheds.first.sched.abide.tenants.get('tenant-one') + connection = self.scheds.first.connections.connections['github'] + source = connection.source + project = source.getProject('org/project1') + + # Patch the fetch method so that it fails + orig = connection._fetchProjectBranches + + def fail(*args, **kw): + raise Exception("Unable to fetch branches") + self.patch(connection, '_fetchProjectBranches', fail) + + # Clear the branch cache so we start with nothing + connection.clearBranchCache() + + # Verify that we raise an error when we try to get branches + # for a missing project + self.assertRaises( + Exception, + lambda: connection.getProjectBranches(project, tenant)) + # This should happen again (ie, we should retry since we don't + # have an entry) + self.assertRaises( + Exception, + lambda: connection.getProjectBranches(project, tenant)) + + # Restore the normal fetch method and verify that the cache + # works as expected + self.patch(connection, '_fetchProjectBranches', orig) + branches = connection.getProjectBranches(project, tenant) + self.assertEqual(['master'], branches) + + # Ensure that the empty list of branches is valid and is not + # seen as an error + newproject = source.getProject('org/newproject') + connection.addProject(newproject) + tpc = zuul.model.TenantProjectConfig(newproject) + tpc.exclude_unprotected_branches = True + tenant.addUntrustedProject(tpc) + branches = connection.getProjectBranches(newproject, tenant) + self.assertEqual([], branches) diff --git a/tests/unit/test_zk.py b/tests/unit/test_zk.py index e45629bef..eadf5c855 100644 --- a/tests/unit/test_zk.py +++ b/tests/unit/test_zk.py @@ -1947,6 +1947,18 @@ class TestBranchCache(ZooKeeperBaseTestCase): data1['project1']['all'] ) + def test_branch_cache_lookup_error(self): + # Test that a missing branch cache entry results in a LookupError + conn = DummyConnection() + cache = BranchCache(self.zk_client, conn, self.component_registry) + self.assertRaises( + LookupError, + lambda: cache.getProjectBranches('project1', True) + ) + self.assertIsNone( + cache.getProjectBranches('project1', True, default=None) + ) + class TestConfigurationErrorList(ZooKeeperBaseTestCase): def test_config_error_list(self): -- cgit v1.2.1