##############################################################################
"""Library of functions for deploying artifacts to Nexus."""
+import errno
import gzip
import logging
import os
return resp
+def _request_post_file(url, file_to_upload):
+ """Execute a request post, return the resp."""
+ resp = {}
+ try:
+ upload_file = open(file_to_upload, 'rb')
+ except FileNotFoundError:
+ raise FileNotFoundError(
+ errno.ENOENT, os.strerror(errno.ENOENT), file_to_upload)
+
+ files = {'file': upload_file}
+ try:
+ resp = requests.post(url, files=files)
+ except requests.exceptions.MissingSchema:
+ raise requests.HTTPError("Not valid URL: {}".format(url))
+ except requests.exceptions.ConnectionError:
+ raise requests.HTTPError("Could not connect to URL: {}".format(url))
+ except requests.exceptions.InvalidURL:
+ raise requests.HTTPError("Invalid URL: {}".format(url))
+
+ if resp.status_code == 400:
+ raise requests.HTTPError("Repository is read only")
+ elif resp.status_code == 404:
+ raise requests.HTTPError("Did not find repository.")
+
+ if not str(resp.status_code).startswith('20'):
+ if zipfile.is_zipfile(file_to_upload):
+ raise requests.HTTPError("Failed to upload to Nexus with status code: {}.\n{}\n{}".format(
+ resp.status_code, resp.text, zipfile.ZipFile(file_to_upload).infolist()))
+ else:
+ raise requests.HTTPError("Failed to upload to Nexus with status code: {}.\n{}\n{}\{}".format(
+ resp.status_code, resp.text, file_to_upload))
+
+ return resp
+
+
def _get_node_from_xml(xml_data, tag_name):
"""Extract tag data from xml data."""
log.debug('xml={}'.format(xml_data))
Maven Ex: org/opendaylight/odlparent
Site Ex: org.opendaylight.odlparent
zip_file: The zip to deploy. (Ex: /tmp/artifacts.zip)
+
+ Sample:
+ lftools deploy nexus-zip \
+ 192.168.1.26:8081/nexus \
+ snapshots \
+ tst_path \
+ tests/fixtures/deploy/zip-test-files/test.zip
"""
url = '{}/service/local/repositories/{}/content-compressed/{}'.format(
_format_url(nexus_url),
deploy_sys._request_post(test_url, xml_doc, headers)
assert 'missing_schema' in str(excinfo.value)
+def test__request_post_file(responses, mocker):
+ """Test _request_post_file."""
+
+ zip_file='zip-test-files/test.zip'
+ resp = {}
+ test_url='http://connection.error.test'
+ exception = requests.exceptions.ConnectionError(test_url)
+ responses.add(responses.POST, test_url, body=exception)
+ with pytest.raises(requests.HTTPError) as excinfo:
+ resp = deploy_sys._request_post_file(test_url, zip_file)
+ assert 'Could not connect to URL' in str(excinfo.value)
+
+ test_url='http://invalid.url.test:8081'
+ exception = requests.exceptions.InvalidURL(test_url)
+ responses.add(responses.POST, test_url, body=exception)
+ with pytest.raises(requests.HTTPError) as excinfo:
+ resp = deploy_sys._request_post_file(test_url, zip_file)
+ assert 'Invalid URL' in str(excinfo.value)
+
+ test_url='http://missing.schema.test:8081'
+ exception = requests.exceptions.MissingSchema(test_url)
+ responses.add(responses.POST, test_url, body=exception)
+ with pytest.raises(requests.HTTPError) as excinfo:
+ resp = deploy_sys._request_post_file(test_url, zip_file)
+ assert 'Not valid URL' in str(excinfo.value)
+
+ test_url='http://repository.read.only:8081'
+ responses.add(responses.POST, test_url, body=None, status=400)
+ with pytest.raises(requests.HTTPError) as excinfo:
+ resp = deploy_sys._request_post_file(test_url, zip_file)
+ assert 'Repository is read only' in str(excinfo.value)
+
+ test_url='http://repository.not.found:8081'
+ responses.add(responses.POST, test_url, body=None, status=404)
+ with pytest.raises(requests.HTTPError) as excinfo:
+ resp = deploy_sys._request_post_file(test_url, zip_file)
+ assert 'Did not find repository' in str(excinfo.value)
+
+ test_url='http://other.upload.error:8081'
+ responses.add(responses.POST, test_url, body=None, status=500)
+ with pytest.raises(requests.HTTPError) as excinfo:
+ resp = deploy_sys._request_post_file(test_url, zip_file)
+ assert 'Failed to upload to Nexus with status code' in str(excinfo.value)
+
def test_nexus_stage_repo_close(responses, mocker):
"""Test nexus_stage_repo_close."""