Skip to content

Commit

Permalink
Merge 3aa098b into d28d3d7
Browse files Browse the repository at this point in the history
  • Loading branch information
chmeliik committed Apr 17, 2019
2 parents d28d3d7 + 3aa098b commit 8bc52b1
Showing 1 changed file with 196 additions and 1 deletion.
197 changes: 196 additions & 1 deletion tests/test_kcb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import osbs
import os
import os.path
import signal
import koji
from koji_containerbuild.plugins import builder_containerbuild
from osbs.exceptions import OsbsValidationException
Expand Down Expand Up @@ -49,13 +50,15 @@ def test_resultdir(self, resdir):
workdir=resdir)
assert cct.resultdir() == '%s/osbslogs' % resdir

def test_osbs(self):
@pytest.mark.parametrize('scratch', [False, True])
def test_osbs(self, scratch):
cct = builder_containerbuild.BuildContainerTask(id=1,
method='buildContainer',
params='params',
session='session',
options='options',
workdir='workdir')
cct.opts['scratch'] = scratch
assert type(cct.osbs()) is osbs.api.OSBS

@pytest.mark.parametrize("repos", [{'repo1': 'test1'}, {'repo2': 'test2'}])
Expand All @@ -72,6 +75,117 @@ def test_get_repositories(self, repos):
repositories.extend(repo)
assert set(cct._get_repositories(response)) ^ set(repositories) == set([])

def _check_logfiles(self, log_entries, logs_dir, orchestrator):
# check that all the log entries for a build are where they are supposed to be
def check_orchestrator_logs():
def check_meta_entry(filename):
source_file = os.path.join(koji.pathinfo.work(), filename)
target_file = os.path.join(logs_dir, filename)

with open(source_file) as s, open(target_file) as t:
assert s.read() == t.read()

log_contents = {}
for entry in log_entries:
platform = entry.platform or 'orchestrator'

if platform == builder_containerbuild.METADATA_TAG:
check_meta_entry(entry.line)
else:
log_contents[platform] = '{old}{new}\n'.format(
old=log_contents.get(platform, ''),
new=entry.line
)

for platform, logs_content in log_contents.items():
logfile_path = os.path.join(logs_dir, platform + '.log')
with open(logfile_path) as log_file:
assert log_file.read() == logs_content

def check_non_orchestrator_logs():
logs_content = ''.join(line + '\n' for line in log_entries)
logfile_path = os.path.join(logs_dir, 'openshift-incremental.log')
with open(logfile_path) as log_file:
assert log_file.read() == logs_content

if orchestrator:
check_orchestrator_logs()
else:
check_non_orchestrator_logs()

@pytest.mark.parametrize('orchestrator', [False, True])
@pytest.mark.parametrize('get_logs_exc', [None, Exception('error')])
@pytest.mark.parametrize('build_not_finished', [False, True])
def test_write_logs(self, tmpdir, orchestrator, get_logs_exc, build_not_finished):
cct = builder_containerbuild.BuildContainerTask(id=1,
method='buildContainer',
params='params',
session='session',
options='options',
workdir='workdir',
demux=orchestrator)
if orchestrator:
get_logs_fname = 'get_orchestrator_build_logs'
log_entries = [LogEntry(None, 'line 1'),
LogEntry(None, 'line 2'),
LogEntry('x86_64', 'line 1'),
LogEntry('x86_64', 'line 2'),
LogEntry(builder_containerbuild.METADATA_TAG, 'x.log')]

koji_tmpdir = tmpdir.mkdir('koji')
koji_tmpdir.join('x.log').write('line 1\n'
'line 2\n')

(flexmock(koji.pathinfo)
.should_receive('work')
.and_return(str(koji_tmpdir)))
else:
get_logs_fname = 'get_build_logs'
log_entries = ['line 1',
'line 2']

build_response = flexmock(status=42)
(build_response
.should_receive('is_running')
.and_return(build_not_finished))
(build_response
.should_receive('is_pending')
.and_return(build_not_finished))

cct._osbs = flexmock()
(cct._osbs
.should_receive('get_build')
.and_return(build_response))

should_receive = cct._osbs.should_receive(get_logs_fname)
if get_logs_exc:
should_receive.and_raise(get_logs_exc)
else:
should_receive.and_return(log_entries)

if get_logs_exc:
exc_type = builder_containerbuild.ContainerError
exc_msg = 'Exception while waiting for {what}: {why}'.format(
what='orchestrator build logs' if orchestrator else 'build logs',
why=get_logs_exc
)
elif build_not_finished:
exc_type = builder_containerbuild.ContainerError
exc_msg = ('Build log finished but build still has not finished: {}.'
.format(build_response.status))
else:
exc_type = exc_msg = None

if exc_type is not None:
with pytest.raises(exc_type) as exc_info:
cct._write_incremental_logs('id', str(tmpdir))
assert str(exc_info.value) == exc_msg
else:
cct._write_incremental_logs('id', str(tmpdir))

if get_logs_exc is None:
self._check_logfiles(log_entries, str(tmpdir), orchestrator)

def _mock_session(self, last_event_id, koji_task_id, pkg_info=USE_DEFAULT_PKG_INFO):
if pkg_info == USE_DEFAULT_PKG_INFO:
pkg_info = {'blocked': False}
Expand Down Expand Up @@ -233,6 +347,29 @@ def _mock_git_source(self):
src = git_uri + '#' + git_ref
return {'git_uri': git_uri, 'git_ref': git_ref, 'src': src}

def test_checkLabels_missing_labels(self, tmpdir):
cct = builder_containerbuild.BuildContainerTask(id=1,
method='buildContainer',
params='params',
session='session',
options='options',
workdir='workdir')

dockerfile_content = 'FROM fedora\n'
missing_labels = ['com.redhat.component (or BZComponent)',
'version (or Version)']
folder_info = self._mock_folders(str(tmpdir),
dockerfile_content=dockerfile_content)
(flexmock(cct)
.should_receive('fetchDockerfile')
.and_return(folder_info['dockerfile_path']))

with pytest.raises(koji.BuildError) as exc_info:
cct.checkLabels('src', 'build-tag')

err_msg = str(exc_info.value)
assert all(label in err_msg for label in missing_labels)

@pytest.mark.parametrize(('pkg_info', 'failure'), (
(None, 'not in list for tag'),
({'blocked': True}, 'is blocked for'),
Expand Down Expand Up @@ -291,6 +428,64 @@ def test_osbs_build(self, tmpdir, pkg_info, failure, orchestrator):
'koji_builds': [koji_build_id]
}

@pytest.mark.parametrize('reason, expected_exc_type', [
('canceled', builder_containerbuild.ContainerCancelled),
('failed', builder_containerbuild.ContainerError),
])
def test_createContainer_failure(self, tmpdir, reason, expected_exc_type):
koji_task_id = 123
last_event_id = 456
koji_build_id = 999

session = self._mock_session(last_event_id, koji_task_id)
folders_info = self._mock_folders(str(tmpdir))
src = self._mock_git_source()
options = flexmock(allowed_scms='pkgs.example.com:/*:no')

task = builder_containerbuild.BuildContainerTask(id=koji_task_id,
method='buildContainer',
params='params',
session=session,
options=options,
workdir='workdir')

(flexmock(task)
.should_receive('fetchDockerfile')
.with_args(src['src'], 'build-tag')
.and_return(folders_info['dockerfile_path']))

task._osbs = self._mock_osbs(koji_build_id=koji_build_id,
src=src,
koji_task_id=koji_task_id)

build_finished_response = flexmock(status=500, json={})
(build_finished_response
.should_receive('is_succeeded')
.and_return(False))
(build_finished_response
.should_receive('is_cancelled')
.and_return(reason == 'canceled'))
(build_finished_response
.should_receive('is_failed')
.and_return(reason == 'failed'))

(task._osbs
.should_receive('wait_for_build_to_finish')
.and_return(build_finished_response))

if reason == 'canceled':
task._osbs.wait_for_build_to_get_scheduled = \
lambda build_id: os.kill(os.getpid(), signal.SIGINT)
(task._osbs
.should_receive('cancel_build')
.once())
(session
.should_receive('cancelTask')
.once())

with pytest.raises(expected_exc_type):
task.handler(src['src'], 'target')

def test_get_build_target_failed(self, tmpdir):
koji_task_id = 123
last_event_id = 456
Expand Down

0 comments on commit 8bc52b1

Please sign in to comment.