diff options
author | James E. Blair <jim@acmegating.com> | 2022-07-02 15:35:23 -0700 |
---|---|---|
committer | Simon Westphahl <simon.westphahl@bmw.de> | 2022-07-04 11:35:36 +0200 |
commit | 4b726ae71f9467682e430577c2f9b7b93cdc264b (patch) | |
tree | 2fda46eb387c410a79f03b4b11b027ec29098be1 | |
parent | c98f14025aa4f72334eab437401ceb2a960e6915 (diff) | |
download | zuul-4b726ae71f9467682e430577c2f9b7b93cdc264b.tar.gz |
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
-rw-r--r-- | tests/unit/test_connection.py | 49 | ||||
-rw-r--r-- | tests/unit/test_zk.py | 12 |
2 files changed, 61 insertions, 0 deletions
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): |