Refactor deploy_nexus_stage to Python 88/13488/6
authorBengt Thuree <bthuree@linuxfoundation.org>
Tue, 20 Nov 2018 13:02:20 +0000 (00:02 +1100)
committerBengt Thuree <bthuree@linuxfoundation.org>
Thu, 22 Nov 2018 06:05:49 +0000 (17:05 +1100)
    This is part of the work to convert the existing SHELL
    scripts to Python scripts.

    * deploy nexus stage

Issue: RELENG-1380
Change-Id: I63e5546fb7455253d14effd638e65d45c36487cb
Signed-off-by: Bengt Thuree <bthuree@linuxfoundation.org>
lftools/cli/deploy.py
lftools/deploy.py
releasenotes/notes/deploy_nexus_stage-e5f6f3e068f88ca4.yaml [new file with mode: 0644]
tests/test_deploy.py

index e9b93a4..59625b1 100644 (file)
@@ -259,8 +259,9 @@ def nexus_stage(ctx, nexus_url, staging_profile_id, deploy_dir):
     This script takes a local Maven repository and deploys it to a Nexus
     staging repository as defined by the staging-profile-id.
     """
-    status = subprocess.call(['deploy', 'nexus-stage', nexus_url, staging_profile_id, deploy_dir])
-    sys.exit(status)
+    deploy_sys.deploy_nexus_stage(nexus_url,
+                                  staging_profile_id,
+                                  deploy_dir)
 
 
 @click.command(name='nexus-stage-repo-close')
index 59404b9..0861d15 100644 (file)
@@ -535,3 +535,36 @@ def deploy_nexus(nexus_repo_url, deploy_dir, snapshot=False):
     pool.join()
 
     os.chdir(previous_dir)
+
+
+def deploy_nexus_stage(nexus_url, staging_profile_id, deploy_dir):
+    """Deploy Maven artifacts to Nexus staging repo.
+
+    Parameters:
+    nexus_url:          URL to Nexus server. (Ex: https://nexus.example.org)
+    staging_profile_id: The staging profile id as defined in Nexus for the
+                        staging repo.
+    deploy_dir:         The directory to deploy. (Ex: /tmp/m2repo)
+
+    Sample:
+        lftools deploy nexus-stage http://192.168.1.26:8081/nexus 4e6f95cd2344 /tmp/slask
+            Deploying Maven artifacts to staging repo...
+            Staging repository aaf-1005 created.
+            /tmp/slask ~/LF/work/lftools-dev/lftools/shell
+            Uploading fstab
+            Uploading passwd
+            ~/LF/work/lftools-dev/lftools/shell
+            Completed uploading files to aaf-1005.
+    """
+    staging_repo_id = nexus_stage_repo_create(nexus_url, staging_profile_id)
+    log.info("Staging repository {} created.".format(staging_repo_id))
+
+    deploy_nexus_url = '{0}/service/local/staging/deployByRepositoryId/{1}'.format(
+        _format_url(nexus_url),
+        staging_repo_id)
+
+    log.debug("Nexus URL = {}".format(_format_url(deploy_nexus_url)))
+    deploy_nexus(deploy_nexus_url, deploy_dir)
+
+    nexus_stage_repo_close(nexus_url, staging_profile_id, staging_repo_id)
+    log.info("Completed uploading files to {}".format(staging_repo_id))
diff --git a/releasenotes/notes/deploy_nexus_stage-e5f6f3e068f88ca4.yaml b/releasenotes/notes/deploy_nexus_stage-e5f6f3e068f88ca4.yaml
new file mode 100644 (file)
index 0000000..ce03b52
--- /dev/null
@@ -0,0 +1,14 @@
+---
+features:
+  - |
+    Refactored deploy_nexus_stage function
+    from shell/deploy to pure Python to be more portable with Windows systems.
+    Also added a number of unit tests to cover all executable branches of the
+    code.
+
+deprecations:
+  - |
+    shell/deploy script's deploy_nexus_stage
+    function is now deprecated and will be removed in a future release.
+
+
index a0f2e34..5bef101 100644 (file)
@@ -599,3 +599,38 @@ def test_deploy_nexus_nosnapshot(datafiles, responses):
         responses.add(responses.POST, success_upload_url,
                       status=201)
     deploy_sys.deploy_nexus(nexus_url, deploy_dir)
+
+
+@pytest.mark.datafiles(
+    os.path.join(FIXTURE_DIR, 'deploy'),
+    )
+def test_nexus_deploy_stage(datafiles, responses):
+    """Test nexus_deploy_stage."""
+    url='http://valid.deploy.stage'
+    url_repo = 'service/local/staging/profiles'
+    staging_profile_id='93fb68073c18'
+    repo_id='test1-1030'
+
+    #Setup for nexus_stage_repo_create
+    xml_created = "<stagedRepositoryId>{}</stagedRepositoryId>".format(repo_id)
+    responses.add(responses.POST, '{}/{}/{}/start'.format(url, url_repo, staging_profile_id),
+                  body=xml_created, status=201)
+
+    #Setup for deploy_nexus with no snapshot
+    os.chdir(str(datafiles))
+    nexus_deploy_url = '{}/service/local/staging/deployByRepositoryId/{}'.format(url, repo_id)
+    deploy_dir = 'm2repo'
+    test_files = ['4.0.3-SNAPSHOT/odlparent-lite-4.0.3-20181120.113136-1.pom',
+                  '4.0.3-SNAPSHOT/odlparent-lite-4.0.3-20181120.113136-1.pom.sha1',
+                  '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,
+                      status=201)
+
+    #Setup for nexus_stage_repo_close
+    responses.add(responses.POST, '{}/{}/{}/finish'.format(url, url_repo, staging_profile_id),
+                  body=None, status=201)
+
+    #Execute test, should not return anything for successful run.
+    deploy_sys.deploy_nexus_stage (url, staging_profile_id, deploy_dir)