From cb54d39b681196cca548f1abc9cdd6c51677634a Mon Sep 17 00:00:00 2001 From: Eric Ball Date: Thu, 20 Jun 2019 10:39:24 -0700 Subject: [PATCH] Use requests.put for Nexus 2+3 compatibility While both post and put methods work for deploying to Nexus 2, Nexus 3 is not compatible with post. Change to put for all deploy_nexus uploads. Change-Id: Ib07ba6b72b0c05afe226f7ce4c36f021535d4bba Signed-off-by: Eric Ball --- lftools/deploy.py | 38 ++++++++++++++++++++-- .../deploy-nexus-use-put-09e52050a869ac2d.yaml | 5 +++ tests/test_deploy.py | 14 ++++---- 3 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 releasenotes/notes/deploy-nexus-use-put-09e52050a869ac2d.yaml diff --git a/lftools/deploy.py b/lftools/deploy.py index 06ce721c..b962084b 100644 --- a/lftools/deploy.py +++ b/lftools/deploy.py @@ -134,6 +134,40 @@ def _request_post_file(url, file_to_upload, parameters=None): return resp +def _request_put_file(url, file_to_upload, parameters=None): + """Execute a request put, 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: + if parameters: + resp = requests.put(url, data=parameters, files=files) + else: + resp = requests.put(url, data=upload_file.read()) + 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'): + 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)) @@ -569,10 +603,10 @@ def deploy_nexus(nexus_repo_url, deploy_dir, snapshot=False): tests/fixtures/deploy/zip-test-files """ def _deploy_nexus_upload(file): - """Fix file path, and call _request_post_file.""" + """Fix file path, and call _request_put_file.""" nexus_url_with_file = '{}/{}'.format(_format_url(nexus_repo_url), file) log.info('Uploading {}'.format(file)) - _request_post_file(nexus_url_with_file, file) + _request_put_file(nexus_url_with_file, file) log.debug('Uploaded {}'.format(file)) file_list = [] diff --git a/releasenotes/notes/deploy-nexus-use-put-09e52050a869ac2d.yaml b/releasenotes/notes/deploy-nexus-use-put-09e52050a869ac2d.yaml new file mode 100644 index 00000000..a2fdf9cf --- /dev/null +++ b/releasenotes/notes/deploy-nexus-use-put-09e52050a869ac2d.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Use requests.put rather than requests.post for deploy_nexus in order to fix + Nexus 3 compatibility. This does not affect Nexus 2 compatibility. diff --git a/tests/test_deploy.py b/tests/test_deploy.py index d53b6bc6..586ecb10 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -654,7 +654,7 @@ def test_deploy_nexus_snapshot(datafiles, responses): """Test deploy_nexus with snapshot. This test will send a directory of files to deploy_nexus, which should - call requests.post once for every valid (=3) file. + call requests.put once for every valid (=3) file. There are two files that should not be uploaded. """ os.chdir(str(datafiles)) @@ -674,7 +674,7 @@ def test_deploy_nexus_snapshot(datafiles, responses): 'maven-metadata.xml.sha1'] for file in test_files: success_upload_url = '{}/{}'.format(nexus_url, file) - responses.add(responses.POST, success_upload_url, + responses.add(responses.PUT, success_upload_url, status=201) deploy_sys.deploy_nexus(nexus_url, deploy_dir, snapshot) @@ -686,7 +686,7 @@ def test_deploy_nexus_nosnapshot(datafiles, responses): """Test deploy_nexus with no snapshot. This test will send a directory of files to deploy_nexus, which should - call requests.post once for every valid (=3) file. + call requests.put once for every valid (=3) file. There are six files that should not be uploaded, and three that should. """ os.chdir(str(datafiles)) @@ -699,7 +699,7 @@ def test_deploy_nexus_nosnapshot(datafiles, responses): '4.0.3-SNAPSHOT/odlparent-lite-4.0.3-20181120.113136-1.pom.md5'] for file in test_files: success_upload_url = '{}/{}'.format(nexus_url, file) - responses.add(responses.POST, success_upload_url, + responses.add(responses.PUT, success_upload_url, status=201) deploy_sys.deploy_nexus(nexus_url, deploy_dir) @@ -707,8 +707,8 @@ def test_deploy_nexus_nosnapshot(datafiles, responses): @pytest.mark.datafiles( os.path.join(FIXTURE_DIR, 'deploy'), ) -def test_nexus_deploy_stage(datafiles, responses): - """Test nexus_deploy_stage.""" +def test_deploy_nexus_stage(datafiles, responses): + """Test deploy_nexus_stage.""" url='http://valid.deploy.stage' url_repo = 'service/local/staging/profiles' staging_profile_id='93fb68073c18' @@ -728,7 +728,7 @@ def test_nexus_deploy_stage(datafiles, responses): '4.0.3-SNAPSHOT/odlparent-lite-4.0.3-20181120.113136-1.pom.md5'] for file in test_files: success_upload_url = '{}/{}'.format(nexus_deploy_url, file) - responses.add(responses.POST, success_upload_url, + responses.add(responses.PUT, success_upload_url, status=201) #Setup for nexus_stage_repo_close -- 2.16.6