def wait_for_syncplan_tasks(repo_backend_id=None, timeout=10, repo_name=None): """Search the pulp tasks and identify repositories sync tasks with specified name or backend_identifier :param repo_backend_id: The Backend ID for the repository to identify the repo in Pulp environment :param timeout: Value to decided how long to check for the Sync task :param repo_name: If repo_backend_id can not be passed, pass the repo_name """ if repo_name: repo_backend_id = (entities.Repository().search(query={ 'search': f'name="{repo_name}"', 'per_page': '1000' })[0].backend_identifier) # Fetch the Pulp password pulp_pass = ssh.command( 'grep "^default_password" /etc/pulp/server.conf | awk \'{print $2}\'' ).stdout.splitlines()[0] # Set the Timeout value timeup = time.time() + int(timeout) * 60 # Search Filter to filter out the task based on backend-id and sync action filtered_req = { 'criteria': { 'filters': { 'tags': { '$in': [f"pulp:repository:{repo_backend_id}"] }, 'task_type': { '$in': ["pulp.server.managers.repo.sync.sync"] }, } } } while True: if time.time() > timeup: raise entities.APIResponseError( f'Pulp task with repo_id {repo_backend_id} not found') # Send request to pulp API to get the task info req = request( 'POST', f'{get_url()}/pulp/api/v2/tasks/search/', verify=False, auth=('admin', f'{pulp_pass}'), headers={'content-type': 'application/json'}, data=filtered_req, ) # Check Status code of response if req.status_code != 200: raise entities.APIResponseError( f'Pulp task with repo_id {repo_backend_id} not found') # Check content of response # It is '[]' string for empty content when backend_identifier is wrong if len(req.content) > 2: if req.json()[0].get('state') in ['finished']: return True elif req.json()[0].get('error'): raise AssertionError( f"Pulp task with repo_id {repo_backend_id} error or not found: " f"'{req.json().get('error')}'") time.sleep(2)
def test_positive_add_subscription(self): """Try to bulk add a subscription to members of a host collection. :id: c4ec5727-eb25-452e-a91f-87cafb16666b :steps: 1. Create a new or use an existing subscription 2. Add the subscription to the members of the host collection :expectedresults: The subscription was added to hosts in the host collection :CaseImportance: Critical """ # Create an activate key for this test act_key = entities.ActivationKey(content_view=self.content_view, environment=self.lce, organization=self.org).create() # this command creates a host collection and "appends", makes available, to the AK act_key.host_collection.append( entities.HostCollection(organization=self.org).create()) # Move HC from Add tab to List tab on AK view act_key = act_key.update(['host_collection']) # Create a product to be bulk added to members of Host Collection product = entities.Product(organization=self.org).create() prod_name = product.name product_subscription = entities.Subscription().search( query={'search': f'name={prod_name}'})[0] with VirtualMachine(distro=DISTRO_RHEL7) as client1, VirtualMachine( distro=DISTRO_RHEL7) as client2: for client in [client1, client2]: client.install_katello_ca() client.register_contenthost(self.org.label, act_key.name) # Read host_collection back from Satellite to get host_ids host_collection = act_key.host_collection[0].read() host_ids = [host.id for host in host_collection.host] # Call nailgun to make the API PUT to members of Host Collection entities.Host().bulk_add_subscriptions( data={ "organization_id": self.org.id, "included": { "ids": host_ids }, "subscriptions": [{ "id": product_subscription.id, "quantity": 1 }], }) # Get the subscriptions from hosts for host_id in host_ids: req = request( 'GET', '{0}/api/v2/hosts/{1}/subscriptions'.format( settings.server.get_url(), host_id), verify=False, auth=settings.server.get_credentials(), headers={'content-type': 'application/json'}, ) assert prod_name in req.text, 'Subscription not applied HC members'
def test_client_request(self): """Test :func:`nailgun.client.request`. Make the same assertions as :meth:`tests.test_client.ClientTestCase.test_clients`. """ with mock.patch.object(requests, "request") as requests_request: requests_request.return_value = self.mock_response self.assertIs(client.request("foo", self.bogus_url), self.mock_response) requests_request.assert_called_once_with( "foo", self.bogus_url, headers={"content-type": "application/json"} )
def test_client_request(self): """Test :func:`nailgun.client.request`. Make the same assertions as :meth:`tests.test_client.ClientTestCase.test_clients`. """ with mock.patch.object(requests, 'request') as requests_request: requests_request.return_value = self.mock_response self.assertIs( client.request('foo', self.bogus_url), self.mock_response, ) requests_request.assert_called_once_with( 'foo', self.bogus_url, headers={'content-type': 'application/json'})
def wait_for_syncplan_tasks(repo_backend_id=None, timeout=10, repo_name=None): """Search the pulp tasks and identify repositories sync tasks with specified name or backend_identifier :param repo_backend_id: The Backend ID for the repository to identify the repo in Pulp environment :param timeout: Value to decided how long to check for the Sync task :param repo_name: If repo_backend_id can not be passed, pass the repo_name """ if repo_name: repo_backend_id = entities.Repository().search(query={ 'search': 'name="{0}"'.format(repo_name), 'per_page': 1000, })[0].backend_identifier # Fetch the Pulp password pulp_pass = ssh.command( 'grep "^default_password" /etc/pulp/server.conf |' ' awk \'{print $2}\'' ).stdout[0] # Set the Timeout value timeup = time.time() + int(timeout) * 60 # Search Filter to filter out the task based on backend-id and sync action filtered_req = { 'criteria': { 'filters': { 'tags': { '$in': [ "pulp:repository:{0}".format(repo_backend_id)] }, 'task_type': { '$in': ["pulp.server.managers.repo.sync.sync"] } } } } while True: if time.time() > timeup: raise entities.APIResponseError( 'Pulp task with repo_id {0} not found'.format(repo_backend_id) ) # Send request to pulp API to get the task info req = request('POST', '{0}/pulp/api/v2/tasks/search/'.format( settings.server.get_url()), verify=False, auth=('admin', '{0}'.format(pulp_pass)), headers={'content-type': 'application/json'}, data=filtered_req) # Check Status code of response if req.status_code != 200: raise entities.APIResponseError( 'Pulp task with repo_id {0} not found'.format(repo_backend_id)) # Check content of response # It is '[]' string for empty content when backend_identifier is wrong if len(req.content) > 2: if req.json()[0].get('state') in ['finished']: return True elif req.json()[0].get('error'): raise AssertionError( "Pulp task with repo_id {0} errored or not " "found: '{1}'".format(repo_backend_id, req.json().get('error') ) ) time.sleep(2)