Adding a check if dir/file exists before executing for loop.
Change-Id: Id887b67ba3c2bea4e4978c6f5ce21bf499ed3d4f
Signed-off-by: Bengt Thuree <bthuree@linuxfoundation.org>
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
--- /dev/null
+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'
+
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'),
)