Skip to content

Commit

Permalink
Reorganize testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
pehala committed Dec 6, 2023
1 parent 7788a6e commit 0d1a4f3
Show file tree
Hide file tree
Showing 86 changed files with 357 additions and 352 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Module containing classes for working with TLS certificates"""
import datetime
import dataclasses
import datetime
import json
import shutil
import subprocess
from functools import cached_property
from importlib import resources
from typing import Optional, List, Dict, Any, Tuple, Collection, Union
from typing import Optional, List, Dict, Collection, Union

from cryptography import x509

Expand Down
40 changes: 35 additions & 5 deletions testsuite/objects/gateway.py → testsuite/gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Module containing Proxy related stuff"""
from abc import abstractmethod, ABC
"""Classes related to Gateways"""
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import TYPE_CHECKING, Optional, Any
from typing import Any, Optional, TYPE_CHECKING

from . import LifecycleObject, asdict
from ..certificates import Certificate
from httpx import Client

from testsuite.certificates import Certificate
from testsuite.lifecycle import LifecycleObject
from testsuite.utils import asdict

if TYPE_CHECKING:
from testsuite.openshift.client import OpenShiftClient
Expand Down Expand Up @@ -103,3 +106,30 @@ def add_backend(self, backend: "Httpbin", prefix):
@abstractmethod
def remove_all_backend(self):
"""Sets match for a specific backend"""


class Hostname(ABC):
"""
Abstraction layer on top of externally exposed hostname
Simplified: Does not have any equal Kubernetes object. It is a hostname you can send HTTP request to
"""

@abstractmethod
def client(self, **kwargs) -> Client:
"""Return Httpx client for the requests to this backend"""

@property
@abstractmethod
def hostname(self) -> str:
"""Returns full hostname in string form associated with this object"""


class Exposer:
"""Exposes hostnames to be accessible from outside"""

@abstractmethod
def expose_hostname(self, name, gateway: Gateway) -> Hostname:
"""
Exposes hostname, so it is accessible from outside
Actual hostname is generated from "name" and is returned in a form of a Hostname object
"""
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import openshift as oc

from testsuite.certificates import Certificate
from testsuite.objects.gateway import Gateway
from testsuite.gateway import Gateway
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects.envoy.config import EnvoyConfig
from testsuite.gateway.envoy.config import EnvoyConfig


class Envoy(Gateway): # pylint: disable=too-many-instance-attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import yaml

from testsuite.openshift.httpbin import Httpbin
from testsuite.openshift.objects import modify
from testsuite.openshift.objects.config_map import ConfigMap
from testsuite.openshift import modify
from testsuite.openshift.config_map import ConfigMap


BASE_CONFIG = """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""GatewayRoute implementation for pure Envoy Deployment"""
from testsuite.objects.gateway import GatewayRoute, Gateway
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.httpbin import Httpbin
from testsuite.openshift.objects.auth_config import AuthConfig
import typing

from testsuite.gateway import Gateway, GatewayRoute

if typing.TYPE_CHECKING:
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.httpbin import Httpbin
from testsuite.policy.authorization.auth_config import AuthConfig


class EnvoyVirtualRoute(GatewayRoute):
Expand Down Expand Up @@ -47,7 +51,7 @@ def remove_all_hostnames(self):
for auth_config in self.auth_configs:
auth_config.remove_all_hosts()

def add_auth_config(self, auth_config: AuthConfig):
def add_auth_config(self, auth_config: "AuthConfig"):
"""Adds AuthConfig to this virtual route"""
self.auth_configs.append(auth_config)
for hostname in self.hostnames:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from . import Envoy, EnvoyConfig

if TYPE_CHECKING:
from ...client import OpenShiftClient
from testsuite.openshift.client import OpenShiftClient

TLS_TRANSPORT = """
name: envoy.transport_sockets.tls
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Wristband Envoy"""
import yaml

from testsuite.openshift.objects.envoy import Envoy, EnvoyConfig
from testsuite.gateway.envoy import Envoy, EnvoyConfig


class WristbandEnvoy(Envoy):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import openshift as oc

from testsuite.certificates import Certificate
from testsuite.objects.gateway import Gateway
from testsuite.gateway import Gateway
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject
from testsuite.openshift import OpenShiftObject


class KuadrantGateway(OpenShiftObject, Gateway):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

from testsuite.certificates import Certificate
from testsuite.httpx import KuadrantClient
from testsuite.objects import LifecycleObject
from testsuite.objects.gateway import Gateway
from testsuite.objects.hostname import Exposer, Hostname
from testsuite.openshift.objects.route import OpenshiftRoute
from testsuite.lifecycle import LifecycleObject
from testsuite.gateway import Gateway, Hostname, Exposer
from testsuite.openshift.route import OpenshiftRoute


class OpenShiftExposer(Exposer, LifecycleObject):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from httpx import Client

from testsuite.httpx import KuadrantClient
from testsuite.objects.gateway import GatewayRoute, Gateway
from testsuite.gateway import Gateway, GatewayRoute
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import modify, OpenShiftObject
from testsuite.openshift import OpenShiftObject, modify

if typing.TYPE_CHECKING:
from testsuite.openshift.httpbin import Httpbin
Expand Down
2 changes: 1 addition & 1 deletion testsuite/httpx/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpx import Auth, Request, URL, Response

from testsuite.oidc.rhsso import User
from testsuite.openshift.objects.api_key import APIKey
from testsuite.openshift.api_key import APIKey
from testsuite.oidc import Token

TokenType = Union[Token, Callable[[], Token]]
Expand Down
16 changes: 16 additions & 0 deletions testsuite/lifecycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Classes related to lifecycle management"""
import abc


class LifecycleObject(abc.ABC):
"""Any objects which has its lifecycle controlled by create() and delete() methods"""

@abc.abstractmethod
def commit(self):
"""Commits resource.
if there is some reconciliation needed, the method should wait until it is all reconciled"""

@abc.abstractmethod
def delete(self):
"""Removes resource,
if there is some reconciliation needed, the method should wait until it is all reconciled"""
33 changes: 0 additions & 33 deletions testsuite/objects/hostname.py

This file was deleted.

2 changes: 1 addition & 1 deletion testsuite/oidc/rhsso/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from keycloak import KeycloakOpenID, KeycloakAdmin, KeycloakPostError

from testsuite.oidc import OIDCProvider, Token
from testsuite.objects import LifecycleObject
from testsuite.lifecycle import LifecycleObject
from .objects import Realm, Client, User


Expand Down
54 changes: 54 additions & 0 deletions testsuite/openshift/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""OpenShift common objects"""
import functools

from openshift import APIObject, timeout

from testsuite.lifecycle import LifecycleObject


class OpenShiftObject(APIObject, LifecycleObject):
"""Custom APIObjects which tracks if the object was already committed to the server or not"""

def __init__(self, dict_to_model=None, string_to_model=None, context=None):
super().__init__(dict_to_model, string_to_model, context)
self.committed = False

def commit(self):
"""
Creates object on the server and returns created entity.
It will be the same class but attributes might differ, due to server adding/rejecting some of them.
"""
self.create(["--save-config=true"])
self.committed = True
return self.refresh()

def delete(self, ignore_not_found=True, cmd_args=None):
"""Deletes the resource, by default ignored not found"""
with timeout(30):
deleted = super().delete(ignore_not_found, cmd_args)
self.committed = False
return deleted


def modify(func):
"""Wraps method of a subclass of OpenShiftObject to use modify_and_apply when the object
is already committed to the server, or run it normally if it isn't.
All methods modifying the target object in any way should be decorated by this"""

def _custom_partial(func, *args, **kwargs):
"""Custom partial function which makes sure that self is always assigned correctly"""

def _func(self):
func(self, *args, **kwargs)

return _func

@functools.wraps(func)
def _wrap(self, *args, **kwargs):
if self.committed:
result, _ = self.modify_and_apply(_custom_partial(func, *args, **kwargs))
assert result.status
else:
func(self, *args, **kwargs)

return _wrap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import base64
from functools import cached_property

from testsuite.objects import Selector
from testsuite.policy.authorization import Selector
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject, modify
from testsuite.openshift import OpenShiftObject, modify


class APIKey(OpenShiftObject):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
"""Authorino CR object"""
import abc
from typing import Any, Dict, List

import openshift
from openshift import selector

from testsuite.objects import Authorino
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject
from testsuite.openshift import OpenShiftObject
from testsuite.lifecycle import LifecycleObject


class Authorino(LifecycleObject):
"""Authorino interface"""

@abc.abstractmethod
def wait_for_ready(self):
"""True, if after some waiting the Authorino is ready"""

@property
@abc.abstractmethod
def metrics_service(self):
"""Authorino metrics service name"""

@property
@abc.abstractmethod
def authorization_url(self):
"""Authorization URL that can be plugged into envoy"""

@property
@abc.abstractmethod
def oidc_url(self):
"""Authorino oidc url"""


class AuthorinoCR(OpenShiftObject, Authorino):
Expand Down Expand Up @@ -78,3 +102,34 @@ def authorization_url(self):
def oidc_url(self):
"""Return authorino oidc endpoint"""
return f"{self.name()}-authorino-oidc.{self.namespace()}.svc.cluster.local"


class PreexistingAuthorino(Authorino):
"""Authorino which is already deployed prior to the testrun"""

def __init__(self, authorization_url, oidc_url, metrics_service) -> None:
super().__init__()
self._authorization_url = authorization_url
self._oidc_url = oidc_url
self._metrics_service = metrics_service

def wait_for_ready(self):
return True

@property
def metrics_service(self):
return self._metrics_service

@property
def authorization_url(self):
return self._authorization_url

@property
def oidc_url(self):
return self._oidc_url

def commit(self):
return

def delete(self):
return
4 changes: 2 additions & 2 deletions testsuite/openshift/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import openshift as oc
from openshift import Context, Selector, OpenShiftPythonException

from testsuite.openshift.objects.route import OpenshiftRoute
from testsuite.openshift.objects.secret import Secret
from .route import OpenshiftRoute
from .secret import Secret


class ServiceTypes(enum.Enum):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Config map"""
from testsuite.openshift.objects import OpenShiftObject
from testsuite.openshift import OpenShiftObject


class ConfigMap(OpenShiftObject):
Expand Down
Loading

0 comments on commit 0d1a4f3

Please sign in to comment.