Handle archives is a file instead of dir. 30/15830/6
authorBengt Thuree <bthuree@linuxfoundation.org>
Thu, 13 Jun 2019 00:51:56 +0000 (10:51 +1000)
committerBengt Thuree <bthuree@linuxfoundation.org>
Thu, 13 Jun 2019 07:53:27 +0000 (09:53 +0200)
This fix raises an Exception, and exists lftools with (1),
if there is any issues with the Archive directory (missing,
a file instead of directory, or something else)

Change-Id: I3102bd2bd06d14a2e420aada64d76d280221db49
Signed-off-by: Bengt Thuree <bthuree@linuxfoundation.org>
Signed-off-by: Anil Belur <abelur@linuxfoundation.org>
lftools/cli/deploy.py
lftools/deploy.py
releasenotes/notes/Fix-copy_archives-error-when-archive-is-file-14e7a4e12775b887.yaml [new file with mode: 0644]
tests/fixtures/deploy/workspace-archivesfile/abc.txt [new file with mode: 0644]
tests/fixtures/deploy/workspace-archivesfile/archives [new file with mode: 0644]
tests/fixtures/deploy/workspace-archivesfile/dependencies.log [new file with mode: 0644]
tests/fixtures/deploy/workspace-noarchives/abc.txt [new file with mode: 0644]
tests/fixtures/deploy/workspace-noarchives/dependencies.log [new file with mode: 0644]
tests/test_deploy.py

index 59625b1..e134a43 100644 (file)
@@ -64,6 +64,8 @@ def archives(ctx, nexus_url, nexus_path, workspace, pattern):
     except HTTPError as e:
         log.error(str(e))
         sys.exit(1)
+    except OSError as e:
+        deploy_sys._log_error_and_exit(str(e))
 
     log.info('Archives upload complete.')
 
index 93a77a7..0797338 100644 (file)
@@ -168,16 +168,22 @@ def copy_archives(workspace, pattern=None):
     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)
+        if os.path.isfile(archives_dir):
+            log.error('Archives {} is a file, not a directory.'.format(archives_dir))
+            raise OSError(errno.ENOENT, 'Not a directory', archives_dir)
+        else:
+            log.debug('Archives 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.error(e)
+                    raise OSError(errno.EPERM, 'Could not move to', archives_dir)
     else:
-        log.debug('Archives file/dir {} does not exist.'.format(archives_dir))
+        log.error('Archives dir {} does not exist.'.format(archives_dir))
+        raise OSError(errno.ENOENT, 'Missing directory', archives_dir)
 
     if pattern is None:
         return
diff --git a/releasenotes/notes/Fix-copy_archives-error-when-archive-is-file-14e7a4e12775b887.yaml b/releasenotes/notes/Fix-copy_archives-error-when-archive-is-file-14e7a4e12775b887.yaml
new file mode 100644 (file)
index 0000000..17a840a
--- /dev/null
@@ -0,0 +1,12 @@
+---
+fixes:
+  - |
+    There is a possibility that there exists a file called Archives, and if so, there will be an OSError crash
+        02:15:01   File "/home/jenkins/.local/lib/python2.7/site-packages/lftools/deploy.py", line 236, in deploy_archives
+        02:15:01     copy_archives(workspace, pattern)
+        02:15:01   File "/home/jenkins/.local/lib/python2.7/site-packages/lftools/deploy.py", line 170, in copy_archives
+        02:15:01     for file_or_dir in os.listdir(archives_dir):
+        02:15:01 OSError: [Errno 20] Not a directory: '/w/workspace/autorelease-update-validate-jobs-fluorine/archives'
+
+    This fix raises an Exception, and exists lftools with (1), if there is any issues with the Archive directory
+      (missing, a file instead of directory, or something else)
diff --git a/tests/fixtures/deploy/workspace-archivesfile/abc.txt b/tests/fixtures/deploy/workspace-archivesfile/abc.txt
new file mode 100644 (file)
index 0000000..b0883f3
--- /dev/null
@@ -0,0 +1 @@
+abcdefghijklmnopqrstuvwxyz
diff --git a/tests/fixtures/deploy/workspace-archivesfile/archives b/tests/fixtures/deploy/workspace-archivesfile/archives
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/fixtures/deploy/workspace-archivesfile/dependencies.log b/tests/fixtures/deploy/workspace-archivesfile/dependencies.log
new file mode 100644 (file)
index 0000000..80ccd5e
--- /dev/null
@@ -0,0 +1,17 @@
+aaa:controller,infrautils,mdsal,odlparent,yangtools
+bgpcep:controller,infrautils,mdsal,netconf,odlparent,yangtools
+controller:mdsal,odlparent,yangtools
+coe:controller,mdsal,netconf,odlparent
+daexim:controller,infrautils,mdsal,netconf,odlparent,yangtools
+genius:controller,daexim,infrautils,mdsal,netconf,odlparent,openflowplugin,ovsdb,serviceutils,yangtools
+infrautils:odlparent
+integration/distribution:aaa,integration,odlparent
+lispflowmapping:controller,mdsal,netconf,neutron,odlparent
+mdsal:odlparent,yangtools
+netconf:aaa,controller,mdsal,odlparent,yangtools
+netvirt:aaa,coe,controller,genius,infrautils,mdsal,netconf,neutron,odlparent,openflowplugin,ovsdb,serviceutils,sfc,yangtools
+neutron:aaa,controller,infrautils,mdsal,netconf,odlparent,ovsdb,yangtools
+openflowplugin:controller,infrautils,mdsal,netconf,odlparent,serviceutils,yangtools
+ovsdb:aaa,controller,infrautils,mdsal,netconf,odlparent,yangtools
+serviceutils:controller,infrautils,mdsal,odlparent,yangtools
+sfc:aaa,controller,genius,infrautils,lispflowmapping,mdsal,netconf,odlparent,openflowplugin,ovsdb,serviceutils
diff --git a/tests/fixtures/deploy/workspace-noarchives/abc.txt b/tests/fixtures/deploy/workspace-noarchives/abc.txt
new file mode 100644 (file)
index 0000000..b0883f3
--- /dev/null
@@ -0,0 +1 @@
+abcdefghijklmnopqrstuvwxyz
diff --git a/tests/fixtures/deploy/workspace-noarchives/dependencies.log b/tests/fixtures/deploy/workspace-noarchives/dependencies.log
new file mode 100644 (file)
index 0000000..80ccd5e
--- /dev/null
@@ -0,0 +1,17 @@
+aaa:controller,infrautils,mdsal,odlparent,yangtools
+bgpcep:controller,infrautils,mdsal,netconf,odlparent,yangtools
+controller:mdsal,odlparent,yangtools
+coe:controller,mdsal,netconf,odlparent
+daexim:controller,infrautils,mdsal,netconf,odlparent,yangtools
+genius:controller,daexim,infrautils,mdsal,netconf,odlparent,openflowplugin,ovsdb,serviceutils,yangtools
+infrautils:odlparent
+integration/distribution:aaa,integration,odlparent
+lispflowmapping:controller,mdsal,netconf,neutron,odlparent
+mdsal:odlparent,yangtools
+netconf:aaa,controller,mdsal,odlparent,yangtools
+netvirt:aaa,coe,controller,genius,infrautils,mdsal,netconf,neutron,odlparent,openflowplugin,ovsdb,serviceutils,sfc,yangtools
+neutron:aaa,controller,infrautils,mdsal,netconf,odlparent,ovsdb,yangtools
+openflowplugin:controller,infrautils,mdsal,netconf,odlparent,serviceutils,yangtools
+ovsdb:aaa,controller,infrautils,mdsal,netconf,odlparent,yangtools
+serviceutils:controller,infrautils,mdsal,odlparent,yangtools
+sfc:aaa,controller,genius,infrautils,lispflowmapping,mdsal,netconf,odlparent,openflowplugin,ovsdb,serviceutils
index 3334f8b..453f6e4 100644 (file)
@@ -130,20 +130,27 @@ def test_deploy_archive(cli_runner, datafiles, responses):
 @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."""
+def test_deploy_archive2(datafiles):
+    """Test deploy_archives() command when archives dir is missing."""
     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
+    with pytest.raises(OSError) as excinfo:
+         deploy_sys.copy_archives(workspace_dir)
+    assert workspace_dir in str(excinfo.value)
+
+
+@pytest.mark.datafiles(
+    os.path.join(FIXTURE_DIR, 'deploy'),
+    )
+def test_deploy_archive3(datafiles):
+    """Test deploy_archives() command when archives dir is a file instead of a dir."""
+    os.chdir(str(datafiles))
+    workspace_dir = os.path.join(str(datafiles), 'workspace-archivesfile')
+
+    with pytest.raises(OSError) as excinfo:
+         deploy_sys.copy_archives(workspace_dir)
+    assert workspace_dir in str(excinfo.value)
 
 
 @pytest.mark.datafiles(