Skip to content

Commit

Permalink
Use objects instead of template for HTTPBin
Browse files Browse the repository at this point in the history
  • Loading branch information
pehala committed Jan 4, 2024
1 parent 247ebf3 commit 61b3683
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 71 deletions.
51 changes: 33 additions & 18 deletions testsuite/openshift/httpbin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Module for Httpbin backend classes"""
"""Httpbin related classes"""
from functools import cached_property
from importlib import resources

from testsuite.lifecycle import LifecycleObject
from testsuite.gateway import Referencable
from testsuite.openshift import Selector
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.deployment import Deployment
from testsuite.openshift.service import Service


class Httpbin(LifecycleObject, Referencable):
"""Httpbin deployed in OpenShift through template"""
"""Httpbin deployed in OpenShift"""

def __init__(self, openshift: OpenShiftClient, name, label, replicas=1) -> None:
super().__init__()
Expand All @@ -17,7 +19,8 @@ def __init__(self, openshift: OpenShiftClient, name, label, replicas=1) -> None:
self.label = label
self.replicas = replicas

self.httpbin_objects = None
self.deployment = None
self.service = None

@property
def reference(self):
Expand All @@ -29,27 +32,39 @@ def url(self):
return f"{self.name}.{self.openshift.project}.svc.cluster.local"

def commit(self):
self.httpbin_objects = self.openshift.new_app(
resources.files("testsuite.resources").joinpath("httpbin.yaml"),
{"NAME": self.name, "LABEL": self.label, "REPLICAS": self.replicas},
match_labels = {"app": self.label, "deployment": self.name}
self.deployment = Deployment.create_instance(
self.openshift,
self.name,
container_name="httpbin",
image="quay.io/jsmadis/httpbin:latest",
ports={"api": 8080},
selector=Selector(matchLabels=match_labels),
labels={"app": self.label},
)
self.deployment.commit()

with self.openshift.context:
assert self.openshift.is_ready(self.httpbin_objects.narrow("deployment")), "Httpbin wasn't ready in time"
self.service = Service.create_instance(
self.openshift,
self.name,
selector=match_labels,
ports=[{"name": "http", "port": 8080, "targetPort": "api"}],
)
self.service.commit()

def delete(self):
with self.openshift.context:
if self.httpbin_objects:
self.httpbin_objects.delete()
self.httpbin_objects = None
assert self.openshift.is_ready(self.deployment.self_selector()), "Httpbin wasn't ready in time"

@cached_property
def service(self):
"""Service associated with httpbin"""
def delete(self):
with self.openshift.context:
return self.httpbin_objects.narrow("service").object()
if self.service:
self.service.delete()
self.service = None
if self.deployment:
self.deployment.delete()
self.deployment = None

@cached_property
def port(self):
"""Service port that httpbin listens on"""
return self.service.model.spec.ports[0].get("port")
return self.service.get_port("http").port
53 changes: 0 additions & 53 deletions testsuite/resources/httpbin.yaml

This file was deleted.

0 comments on commit 61b3683

Please sign in to comment.