Fix OSError in lftools deploy archives 66/15066/9
authorBengt Thuree <bthuree@linuxfoundation.org>
Tue, 2 Apr 2019 00:04:03 +0000 (17:04 -0700)
committerBengt Thuree <bthuree@linuxfoundation.org>
Tue, 2 Apr 2019 23:32:07 +0000 (16:32 -0700)
Adding a check if dir/file exists before executing for loop.

Change-Id: Id887b67ba3c2bea4e4978c6f5ce21bf499ed3d4f
Signed-off-by: Bengt Thuree <bthuree@linuxfoundation.org>
lftools/deploy.py
releasenotes/notes/lftools-deploy-HandleMissingArchiveDir-415ac62d2a45303f.yaml [new file with mode: 0644]
tests/test_deploy.py

index 094119a..6266590 100644 (file)
@@ -163,13 +163,19 @@ def copy_archives(workspace, pattern=None):
 
     log.debug('Copying files from {} with pattern \'{}\' to {}.'.format(
         workspace, pattern, dest_dir))
-    for file_or_dir in os.listdir(archives_dir):
-        f = os.path.join(archives_dir, file_or_dir)
-        try:
-            log.debug('Moving {}'.format(f))
-            shutil.move(f, dest_dir)
-        except shutil.Error as e:
-            log.warn(e)
+    log.debug('archives_dir = {}'.format(archives_dir))
+
+    if os.path.exists(archives_dir):
+        log.debug('Archives file/dir {} does exist.'.format(archives_dir))
+        for file_or_dir in os.listdir(archives_dir):
+            f = os.path.join(archives_dir, file_or_dir)
+            try:
+                log.debug('Moving {}'.format(f))
+                shutil.move(f, dest_dir)
+            except shutil.Error as e:
+                log.warn(e)
+    else:
+        log.debug('Archives file/dir {} does not exist.'.format(archives_dir))
 
     if pattern is None:
         return
diff --git a/releasenotes/notes/lftools-deploy-HandleMissingArchiveDir-415ac62d2a45303f.yaml b/releasenotes/notes/lftools-deploy-HandleMissingArchiveDir-415ac62d2a45303f.yaml
new file mode 100644 (file)
index 0000000..8293cb2
--- /dev/null
@@ -0,0 +1,17 @@
+fixes:
+  - |
+    Fixes an OSError exception that is not handled, in the lftools command:
+
+    lftools deploy archives
+
+    The code resides in the copy_archives function in deploy.py file.
+
+    This exception is caused by a missing archives directory, which a for loop
+    expects to be there.
+    The fix is simply to verify if archives file/directory exists, and if it does
+    then perform the for loop.
+
+    12:07:36   File "/home/jenkins/.local/lib/python2.7/site-packages/lftools/deploy.py", line 166, in copy_archives
+    12:07:36     for file_or_dir in os.listdir(archives_dir):
+    12:07:36 OSError: [Errno 2] No such file or directory: '/w/workspace/music-mdbc-master-verify-java/archives'
+
index 5bef101..3334f8b 100644 (file)
@@ -126,6 +126,26 @@ def test_deploy_archive(cli_runner, datafiles, responses):
         obj={})
     assert result.exit_code == 1
 
+
+@pytest.mark.datafiles(
+    os.path.join(FIXTURE_DIR, 'deploy'),
+    )
+def test_deploy_archive2(cli_runner, datafiles, responses):
+    """Test deploy_archives() command for expected upload cases."""
+    os.chdir(str(datafiles))
+    workspace_dir = os.path.join(str(datafiles), 'workspace-noarchives')
+
+    # Test successful upload
+    url = 'https://nexus.example.org/service/local/repositories/logs/content-compressed'
+    responses.add(responses.POST, '{}/test/path/abc'.format(url),
+                  json=None, status=201)
+    result = cli_runner.invoke(
+        cli.cli,
+        ['--debug', 'deploy', 'archives', 'https://nexus.example.org', 'test/path/abc', workspace_dir],
+        obj={})
+    assert result.exit_code == 0
+
+
 @pytest.mark.datafiles(
     os.path.join(FIXTURE_DIR, 'deploy'),
     )