From a1cdd611a27d53a031435d580e25c46535b62ec7 Mon Sep 17 00:00:00 2001 From: Andrei Paplauski Date: Apr 02 2020 10:45:23 +0000 Subject: Add retry mechanism to Pulp class methods Now when Pulp methods will fail their requests, they will not exit immediately, but will retry queries for some time. + test for this mechanism Resolves: FACTORY-5883 --- diff --git a/freshmaker/pulp.py b/freshmaker/pulp.py index c111754..33d2759 100644 --- a/freshmaker/pulp.py +++ b/freshmaker/pulp.py @@ -24,6 +24,8 @@ import json import requests +from freshmaker.utils import retry + class Pulp(object): """Interface to Pulp""" @@ -50,6 +52,7 @@ class Pulp(object): r.raise_for_status() return r.json() + @retry(wait_on=requests.exceptions.RequestException) def get_content_set_by_repo_ids(self, repo_ids): """Get content_sets by repository IDs @@ -69,6 +72,7 @@ class Pulp(object): return [repo['notes']['content_set'] for repo in repos if 'content_set' in repo['notes']] + @retry(wait_on=requests.exceptions.RequestException) def get_docker_repository_name(self, cdn_repo): """ Getting docker repository name from pulp using cdn repo name. diff --git a/tests/test_pulp.py b/tests/test_pulp.py index ad25cb1..3643ce4 100644 --- a/tests/test_pulp.py +++ b/tests/test_pulp.py @@ -24,6 +24,7 @@ import json from unittest.mock import patch +from requests import exceptions from freshmaker.pulp import Pulp from tests import helpers @@ -180,3 +181,20 @@ class TestPulp(helpers.FreshmakerTestCase): auth=(self.username, self.password)) self.assertEqual(repo_name, "scl/foo-526") + + @patch('freshmaker.pulp.requests.post') + @patch('freshmaker.pulp.requests.get') + def test_retrying_calls(self, get, post): + get.side_effect = exceptions.HTTPError("Connection error: get") + post.side_effect = exceptions.HTTPError("Connection error: post") + + pulp = Pulp(self.server_url, username=self.username, + password=self.password) + + with self.assertRaises(exceptions.HTTPError): + pulp.get_docker_repository_name("test") + self.assertGreater(get.call_count, 1) + + with self.assertRaises(exceptions.HTTPError): + pulp.get_content_set_by_repo_ids(['test1', 'test2']) + self.assertGreater(post.call_count, 1)