Use requests.put for Nexus 2+3 compatibility 60/15960/2
authorEric Ball <eball@linuxfoundation.org>
Thu, 20 Jun 2019 17:39:24 +0000 (10:39 -0700)
committerEric Ball <eball@linuxfoundation.org>
Thu, 27 Jun 2019 21:02:41 +0000 (14:02 -0700)
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 <eball@linuxfoundation.org>
lftools/deploy.py
releasenotes/notes/deploy-nexus-use-put-09e52050a869ac2d.yaml [new file with mode: 0644]
tests/test_deploy.py

index 06ce721..b962084 100644 (file)
@@ -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 (file)
index 0000000..a2fdf9c
--- /dev/null
@@ -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.
index d53b6bc..586ecb1 100644 (file)
@@ -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