From: Bengt Thuree Date: Tue, 2 Apr 2019 00:04:03 +0000 (-0700) Subject: Fix OSError in lftools deploy archives X-Git-Tag: v0.22.2^2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F66%2F15066%2F9;p=releng%2Flftools.git Fix OSError in lftools deploy archives Adding a check if dir/file exists before executing for loop. Change-Id: Id887b67ba3c2bea4e4978c6f5ce21bf499ed3d4f Signed-off-by: Bengt Thuree --- diff --git a/lftools/deploy.py b/lftools/deploy.py index 094119ad..62665906 100644 --- a/lftools/deploy.py +++ b/lftools/deploy.py @@ -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 index 00000000..8293cb21 --- /dev/null +++ b/releasenotes/notes/lftools-deploy-HandleMissingArchiveDir-415ac62d2a45303f.yaml @@ -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' + diff --git a/tests/test_deploy.py b/tests/test_deploy.py index 5bef101e..3334f8b8 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -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'), )