Skip to content

Commit

Permalink
Merge pull request #815 from mdujava/deployments
Browse files Browse the repository at this point in the history
Search for deployments first, use dc as fallback
  • Loading branch information
mdujava authored Mar 18, 2024
2 parents a869a0f + 2f8c6a4 commit 9c5ca28
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 32 deletions.
10 changes: 8 additions & 2 deletions testsuite/dynaconf_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def _guess_version(ocp, namespace):
"""Attempt to determine version from amp-system imagestream"""

version = None
# TODO: ImageStreams are no longer used by 3scale; https://github.com/3scale-qe/3scale-tests/issues/816
try:
version = ocp.image_stream_tag_from_trigger("dc/apicast-production")
Version(version)
Expand Down Expand Up @@ -99,8 +100,13 @@ def _guess_apicast_operator_version(ocp, settings):

def _apicast_image(ocp):
"""Find source of amp-apicast image"""
lookup = ocp.do_action("get", ["dc/apicast-production", "-o", "yaml"], parse_output=True)
return lookup.model.spec.template.spec.containers[0].image
# dc are replaced with deployments in 2.15-dev
try:
lookup = ocp.do_action("get", ["deployment/apicast-production", "-o", "yaml"], parse_output=True)
return lookup.model.spec.template.spec.containers[0].image
except OpenShiftPythonException:
lookup = ocp.do_action("get", ["dc/apicast-production", "-o", "yaml"], parse_output=True)
return lookup.model.spec.template.spec.containers[0].image


def _rhsso_password(server_url, token):
Expand Down
9 changes: 8 additions & 1 deletion testsuite/gateways/apicast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import Optional, List, Dict, Tuple
import logging

from openshift_client import OpenShiftPythonException

from threescale_api.resources import Service

from testsuite.capabilities import Capability
Expand Down Expand Up @@ -82,7 +84,12 @@ def setup_tls(self, secret_name, https_port):
@property
def deployment(self) -> Deployment:
"""Gateway deployment"""
return self.openshift.deployment(f"dc/{self.name}")
# dc are replaced with deployments in 2.15-dev
try:
self.openshift.do_action("get", [f"deployment/{self.name}"])
return self.openshift.deployment(f"deployment/{self.name}")
except OpenShiftPythonException:
return self.openshift.deployment(f"dc/{self.name}")

def _routename(self, service):
"""name of route for given service"""
Expand Down
9 changes: 8 additions & 1 deletion testsuite/gateways/apicast/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import time
from typing import Dict, Callable, Pattern, Any, Match, Union

from openshift_client import OpenShiftPythonException

from weakget import weakget

from testsuite import settings
Expand Down Expand Up @@ -153,7 +155,12 @@ def fits(openshift: OpenShiftClient):

@property
def deployment(self):
return self.openshift.deployment(f"deployment/apicast-{self.name}")
# dc are replaced with deployments in 2.15-dev
try:
self.openshift.do_action("get", [f"deployment/apicast-{self.name}"])
return self.openshift.deployment(f"deployment/apicast-{self.name}")
except OpenShiftPythonException:
return self.openshift.deployment(f"dc/apicast-{self.name}")

@property
def environ(self) -> Properties:
Expand Down
8 changes: 7 additions & 1 deletion testsuite/gateways/apicast/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ def __init__(self, staging: bool, openshift: "Optional[OpenShiftClient]" = None)
@property
def deployment(self):
"""Return deployment config name of this apicast"""
return self.openshift.deployment("dc/apicast-staging" if self.staging else "dc/apicast-production")
# dc are replaced with deployments in 2.15-dev
name = "apicast-staging" if self.staging else "apicast-production"
try:
self.openshift.do_action("get", [f"deployment/{name}"])
return self.openshift.deployment(f"deployment/{name}")
except OpenShiftPythonException:
return self.openshift.deployment(f"dc/{name}")

@property
def environ(self) -> Properties:
Expand Down
6 changes: 5 additions & 1 deletion testsuite/openshift/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ def wait_for(self, timeout: int = 90):
def get_pods(self):
"""Kubernetes Deployment doesnt have a nice universal way how to get the correct pods,
so this method relies on pods having the deployment label"""
return self.openshift.select_resource("pods", labels={"deployment": self.name})

def select_pod(apiobject):
return apiobject.get_label("deployment") == self.name

return self.openshift.select_resource("pods", narrow_function=select_pod)


class DeploymentConfig(Deployment):
Expand Down
10 changes: 8 additions & 2 deletions testsuite/openshift/scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import typing
from contextlib import contextmanager

from testsuite.openshift.deployments import DeploymentConfig
from openshift_client import OpenShiftPythonException

from testsuite.openshift.deployments import DeploymentConfig, KubernetesDeployment

if typing.TYPE_CHECKING:
from testsuite.openshift.client import OpenShiftClient
Expand Down Expand Up @@ -34,7 +36,11 @@ def _scale_component(self, deployment_name, replicas, wait_for_replicas=None):
if self.operator_deploy:
apimanager_func = getattr(self.apimanager, self.DEPLOYMENT_MAPPINGS[deployment_name])
return apimanager_func(replicas, wait_for_replicas=wait_for_replicas)
deployment = DeploymentConfig(self.client, f"dc/{deployment_name}")
try:
self.client.do_action("get", [f"deployment/{deployment_name}"])
deployment = KubernetesDeployment(self.client, f"deployment/{deployment_name}")
except OpenShiftPythonException:
deployment = DeploymentConfig(self.client, f"dc/{deployment_name}")
previous_replicas = deployment.get_replicas()
deployment.scale(replicas)
return previous_replicas
Expand Down
10 changes: 8 additions & 2 deletions testsuite/tests/apicast/parameters/test_modular_apicast.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import importlib_resources as resources
import pytest

from openshift_client import OpenShiftPythonException

from testsuite import rawobj
from testsuite.capabilities import Capability
from testsuite.gateways.apicast.operator import OperatorApicast
from testsuite.gateways.apicast.template import TemplateApicast
from testsuite.utils import blame
from testsuite.utils import blame, warn_and_skip

pytestmark = [
pytest.mark.required_capabilities(Capability.STANDARD_GATEWAY, Capability.CUSTOM_ENVIRONMENT),
Expand Down Expand Up @@ -61,7 +63,11 @@ def set_gateway_image(openshift, staging_gateway, request, testconfig):
github_template = resources.files("testsuite.resources.modular_apicast").joinpath("example_policy.yml")
copy_template = resources.files("testsuite.resources.modular_apicast").joinpath("example_policy_copy.yml")

amp_release = openshift().image_stream_tag_from_trigger("dc/apicast-production")
try:
amp_release = openshift().image_stream_tag_from_trigger("dc/apicast-production")
except OpenShiftPythonException:
warn_and_skip("ImageStream not found.")

project = openshift().project_name
build_name_github = blame(request, "apicast-example-policy-github")
build_name_copy = blame(request, "apicast-example-policy-copy")
Expand Down
10 changes: 8 additions & 2 deletions testsuite/tests/apicast/parameters/test_policy_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
from lxml import etree
from lxml.etree import XMLSyntaxError

from openshift_client import OpenShiftPythonException

from testsuite import rawobj
from testsuite.capabilities import Capability
from testsuite.gateways.apicast.operator import OperatorApicast
from testsuite.gateways.apicast.template import TemplateApicast
from testsuite.utils import blame
from testsuite.utils import blame, warn_and_skip

pytestmark = [
pytest.mark.required_capabilities(Capability.STANDARD_GATEWAY, Capability.CUSTOM_ENVIRONMENT),
Expand Down Expand Up @@ -58,7 +60,11 @@ def set_gateway_image(openshift, staging_gateway, request, testconfig):

template = resources.files("testsuite.resources.modular_apicast").joinpath("xml_policy.yml")

amp_release = openshift().image_stream_tag_from_trigger("dc/apicast-production")
try:
amp_release = openshift().image_stream_tag_from_trigger("dc/apicast-production")
except (OpenShiftPythonException, ValueError):
warn_and_skip("ImageStream not found.")

project = openshift().project_name
build_name = blame(request, "apicast-xml-policy")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import importlib_resources as resources
import pytest

from openshift_client import OpenShiftPythonException

from testsuite.capabilities import Capability
from testsuite.gateways import gateway
from testsuite.gateways.apicast.selfmanaged import SelfManagedApicast
from testsuite.utils import blame
from testsuite.utils import blame, warn_and_skip

pytestmark = [
pytest.mark.required_capabilities(Capability.STANDARD_GATEWAY, Capability.CUSTOM_ENVIRONMENT),
Expand All @@ -33,7 +35,11 @@ def set_gateway_image(openshift, staging_gateway, request, testconfig):

github_template = resources.files("testsuite.resources.modular_apicast").joinpath("example_policy.yml")

amp_release = openshift().image_stream_tag_from_trigger("dc/apicast-production")
try:
amp_release = openshift().image_stream_tag_from_trigger("dc/apicast-production")
except (OpenShiftPythonException, ValueError):
warn_and_skip("ImageStream not found.")

project = openshift().project_name
build_name_github = blame(request, "apicast-example-policy-github")

Expand Down
51 changes: 38 additions & 13 deletions testsuite/tests/images/test_images_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,36 @@

import pytest

from packaging.version import Version # noqa # pylint: disable=unused-import

from openshift_client import OpenShiftPythonException

from testsuite.gateways import gateway
from testsuite.gateways.apicast.operator import OperatorApicast
from testsuite.utils import blame
from testsuite import TESTED_VERSION # noqa # pylint: disable=unused-import


pytestmark = pytest.mark.nopersistence


@pytest.mark.parametrize(
("image", "image_stream", "deployment_configs"),
[
("threescale_system", "amp-system", ["system-app"]),
("threescale_backend", "amp-backend", ["backend-worker"]),
("threescale_zync", "amp-zync", ["zync"]),
("threescale_memcached", "system-memcached", ["system-memcache"]),
("threescale_searchd", "system-searchd", ["system-searchd"]),
("apicast", "amp-apicast", ["apicast-staging", "apicast-production"]),
],
)
def test_deployment_image(images, openshift, image, image_stream, deployment_configs):
PARAMETERS = [
("threescale_system", "amp-system", ["system-app"]),
("threescale_backend", "amp-backend", ["backend-worker"]),
("threescale_zync", "amp-zync", ["zync"]),
("threescale_memcached", "system-memcached", ["system-memcache"]),
("threescale_searchd", "system-searchd", ["system-searchd"]),
("apicast", "amp-apicast", ["apicast-staging", "apicast-production"]),
]

IS_PARAMETERS = [(image, image_stream) for image, _, image_stream in PARAMETERS]
COMPONENTS_PARAMETERS = [(image, deployment_configs) for image, deployment_configs, _ in PARAMETERS]


@pytest.mark.parametrize(("image", "image_stream"), IS_PARAMETERS)
# INFO: image streams are no longer used (starting with 2.15-dev)
@pytest.mark.skipif("TESTED_VERSION > Version('2.14')")
def test_imagesource_image(images, openshift, image, image_stream):
"""
Test:
- load expected images from settings
Expand All @@ -32,8 +43,22 @@ def test_deployment_image(images, openshift, image, image_stream, deployment_con
lookup = openshift.do_action("get", [f"is/{image_stream}", "-o", "yaml"], parse_output=True)
digest = lookup.model.spec.tags[-1]["from"].name.split(":")[-1]
assert digest == expected_image["manifest_digest"]


@pytest.mark.parametrize(("image", "deployment_configs"), COMPONENTS_PARAMETERS)
def test_deployment_image(images, openshift, image, deployment_configs):
"""
Test:
- load expected images from settings
- assert that expected image and image in deployment config are the same
"""
expected_image = images[image]
openshift = openshift()
for deployment_config in deployment_configs:
lookup = openshift.do_action("get", [f"dc/{deployment_config}", "-o", "yaml"], parse_output=True)
try:
lookup = openshift.do_action("get", [f"dc/{deployment_config}", "-o", "yaml"], parse_output=True)
except (StopIteration, OpenShiftPythonException):
lookup = openshift.do_action("get", [f"deployment/{deployment_config}", "-o", "yaml"], parse_output=True)
for container in lookup.model.spec.template.spec.containers:
digest = container.image.split(":")[-1]
assert digest == expected_image["resolved_images"].get(openshift.arch)
Expand Down
14 changes: 9 additions & 5 deletions testsuite/tests/prometheus/zync/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import pytest
from packaging.version import Version # noqa # pylint: disable=unused-import
from openshift_client import OpenShiftPythonException

from testsuite import TESTED_VERSION # noqa # pylint: disable=unused-import
from testsuite.configuration import openshift

pytestmark = [
pytest.mark.sandbag, # requires openshift
Expand All @@ -18,11 +18,15 @@
ANNOTATIONS = ["prometheus.io/port", "prometheus.io/scrape"]


@pytest.fixture(scope="module", params=["dc/zync", "dc/zync-que"])
def pod(request):
@pytest.fixture(scope="module", params=["zync", "zync-que"])
def pod(request, openshift):
"""Return zync pod object."""
pods = openshift().deployment(request.param).get_pods().objects()
pod = next(filter(lambda x: x.model.status.phase == "Running", pods))
try:
pods = openshift().deployment(f"deployment/{request.param}").get_pods().objects()
pod = next(filter(lambda x: x.model.status.phase == "Running", pods))
except (StopIteration, OpenShiftPythonException):
pods = openshift().deployment(f"dc/{request.param}").get_pods().objects()
pod = next(filter(lambda x: x.model.status.phase == "Running", pods))
return pod


Expand Down

0 comments on commit 9c5ca28

Please sign in to comment.