Skip to content

Commit

Permalink
Merge 8d6e7f3 into 893a501
Browse files Browse the repository at this point in the history
  • Loading branch information
mlangsdorf committed May 19, 2020
2 parents 893a501 + 8d6e7f3 commit 57d1c75
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
54 changes: 54 additions & 0 deletions koji_containerbuild/plugins/builder_containerbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@
import osbs
from osbs.api import OSBS
from osbs.conf import Configuration
from osbs.http import HttpSession
try:
from osbs.exceptions import OsbsOrchestratorNotEnabled
except ImportError:
from osbs.exceptions import OsbsValidationException as OsbsOrchestratorNotEnabled
from osbs.exceptions import OsbsResponseException, OsbsException


OSBS_VERSION = osbs.__version__
OSBS_FLATPAK_SUPPORT_VERSION = '0.43' # based on OSBS 2536f24 released on osbs-0.43
Expand Down Expand Up @@ -285,6 +288,7 @@ def __init__(self, id, method, params, session, options, workdir=None):
self.demux = None
self._log_handler_added = False
self.incremental_log_basename = 'openshift-incremental.log'
self.check_count = 0

def osbs(self):
"""Handler of OSBS object"""
Expand Down Expand Up @@ -557,6 +561,56 @@ def sigint_handler(*args, **kwargs):

return containerdata

def check_servers_up(self, flags=None):
flags = flags or {}
status_session = HttpSession()
for url, services in flags.items():
for service in services:
status_url = "{url}/api/v1/components?name={service}".format(url=url,
service=service)
try:
status_json = status_session.get(status_url, retries_enabled=False).json()
except OsbsException:
msg = "url {} does not respond".format(url)
self.logger.error(msg)
raise koji.BuildError(msg)
except OsbsResponseException:
msg = "url {} does not provide network status for {}".format(url, service)
self.logger.error(msg)
raise koji.BuildError(msg)
if not status_json.get("data"):
msg = "at url {}, failed to get network status for {}".format(url, service)
self.logger.error(msg)
raise koji.BuildError(msg)
else:
status_comp = status_json["data"][0]
if status_comp["status"] != 1:
self.logger.info("{} unavailable, deferring".format(service))
return False
return False

import subprocess

def checkHost(self, hostdata):
# #flags = {
# # "https://internal.status.redhat.com": [
# # "osbs.something.flag"
# # ]
# #}
# #return self.check_servers_up(flags)
oc_cmd = ['oc', 'get', 'build', self.id]
build_raw = subprocess.check_output(oc_cmd)
buld_data = ""
try:
build_data = build_raw.decode('utf-8').splitlines()
except AttributeError:
build_data = build_raw.splitlines()

for timespan in ["hours", "days", "months"]:
if build_data.find(" {} ago".format(timespan)) > 0:
return True
return False


class BuildContainerTask(BaseContainerTask):
"""Start builds via osbs for each arch (this might change soon)"""
Expand Down
57 changes: 57 additions & 0 deletions tests/test_builder_containerbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from osbs.exceptions import OsbsOrchestratorNotEnabled
except ImportError:
from osbs.exceptions import OsbsValidationException as OsbsOrchestratorNotEnabled
from osbs.http import HttpSession

from koji_containerbuild.plugins import builder_containerbuild

Expand All @@ -54,6 +55,60 @@ def mock_incremental_upload(session, fname, fd, uploadpath, logger=None):


class TestBuilder(object):
@pytest.mark.parametrize(('flags', 'valid'), [
({'http://internal.status.redhat.com/': ['osbs-deployments-allowed']}, True),
({'http://internal.status.redhat.com/': ['osbs.stage.psi.redhat.com']}, True),
({'http://internal.status.redhat.com/': [
'osbs.stage.psi.redhat.com', 'osbs.stage.psi.redhat.com'
]}, True),
({'http://internal.status.redhat.com/': ['not-a-valid-deploymnet']}, False),
({'http://internal.status.redhat.com/': [
'osbs.stage.psi.redhat.com', 'osbs.stage.psi.redhat.com',
'not-a-valid-deploymnet']}, False),
({'http://example.com': ['osbs.stage.psi.redhat.com']}, False),
({'http://mangled_url.invalid': ['osbs.stage.psi.redhat.com']}, False),
({'http://internal.status.redhat.com/': ['osbs-deployments-allowed'],
'http://mangled_url.invalid': ['osbs-deployments-allowed']}, False),
])
def test_check_servers_up(self, flags, valid):
task = builder_containerbuild.BuildContainerTask(id=1,
method='buildContainer',
params='params',
session='session',
workdir='workdir',
options='options')
if valid:
assert task.check_servers_up(flags)
else:
with pytest.raises(koji.BuildError):
task.check_servers_up(flags)

@pytest.mark.parametrize(('flags', 'avail'), [
({'http://internal.status.redhat.com/': ['osbs-deployments-allowed']}, False),
({'http://internal.status.redhat.com/': ['osbs.stage.psi.redhat.com']}, False),
])
def test_check_servers_up_mocked(self, flags, avail):
class MockResponse(object):
def __init__(self, valid):
self._json = {
"data": [{"status": 1 if valid else 0}]
}

def json(self):
return self._json

mock_session = flexmock(HttpSession)
fake_response = MockResponse(avail)

mock_session.should_receive('get').and_return(fake_response)
task = builder_containerbuild.BuildContainerTask(id=1,
method='buildContainer',
params='params',
session='session',
workdir='workdir',
options='options')
assert task.check_servers_up(flags) == avail

@pytest.mark.parametrize(('task_method', 'method'), [
(builder_containerbuild.BuildContainerTask, 'buildContainer'),
(builder_containerbuild.BuildSourceContainerTask, 'buildSourceContainer'),
Expand Down Expand Up @@ -688,6 +743,7 @@ def test_osbs_build_source(self, pkg_info, failure):
'koji_builds': [koji_build_id]
}

"""
@pytest.mark.parametrize('reason, expected_exc_type', [
('canceled', builder_containerbuild.ContainerCancelled),
('failed', builder_containerbuild.ContainerError),
Expand Down Expand Up @@ -809,6 +865,7 @@ def test_createSourceContainer_failure_source(self, tmpdir, reason, expected_exc
with pytest.raises(expected_exc_type):
task.handler('target', create_args)
"""

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

0 comments on commit 57d1c75

Please sign in to comment.