From: Bengt Thuree Date: Thu, 13 Jun 2019 00:51:56 +0000 (+1000) Subject: Handle archives is a file instead of dir. X-Git-Tag: v0.25.0~2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F30%2F15830%2F6;p=releng%2Flftools.git Handle archives is a file instead of dir. 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 Signed-off-by: Anil Belur --- diff --git a/lftools/cli/deploy.py b/lftools/cli/deploy.py index 59625b1e..e134a430 100644 --- a/lftools/cli/deploy.py +++ b/lftools/cli/deploy.py @@ -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.') diff --git a/lftools/deploy.py b/lftools/deploy.py index 93a77a74..0797338e 100644 --- a/lftools/deploy.py +++ b/lftools/deploy.py @@ -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 index 00000000..17a840ae --- /dev/null +++ b/releasenotes/notes/Fix-copy_archives-error-when-archive-is-file-14e7a4e12775b887.yaml @@ -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 index 00000000..b0883f38 --- /dev/null +++ b/tests/fixtures/deploy/workspace-archivesfile/abc.txt @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxyz diff --git a/tests/fixtures/deploy/workspace-archivesfile/archives b/tests/fixtures/deploy/workspace-archivesfile/archives new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/deploy/workspace-archivesfile/dependencies.log b/tests/fixtures/deploy/workspace-archivesfile/dependencies.log new file mode 100644 index 00000000..80ccd5ed --- /dev/null +++ b/tests/fixtures/deploy/workspace-archivesfile/dependencies.log @@ -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 index 00000000..b0883f38 --- /dev/null +++ b/tests/fixtures/deploy/workspace-noarchives/abc.txt @@ -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 index 00000000..80ccd5ed --- /dev/null +++ b/tests/fixtures/deploy/workspace-noarchives/dependencies.log @@ -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/test_deploy.py b/tests/test_deploy.py index 3334f8b8..453f6e4e 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -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(