Skip to content

Commit

Permalink
add tests for build version and gitcommit (#958)
Browse files Browse the repository at this point in the history
- format conftest.py.
- add tests to verify if the build contains tags for version and gitcommit.
  • Loading branch information
vepatel committed May 19, 2020
1 parent 1740c64 commit eade506
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 29 deletions.
102 changes: 73 additions & 29 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import pytest

from kubernetes.config.kube_config import KUBE_CONFIG_DEFAULT_LOCATION
from settings import DEFAULT_IMAGE, DEFAULT_PULL_POLICY, DEFAULT_IC_TYPE, DEFAULT_SERVICE, DEFAULT_DEPLOYMENT_TYPE
from settings import (
DEFAULT_IMAGE,
DEFAULT_PULL_POLICY,
DEFAULT_IC_TYPE,
DEFAULT_SERVICE,
DEFAULT_DEPLOYMENT_TYPE,
)
from suite.resources_utils import get_first_pod_name


Expand All @@ -14,33 +20,63 @@ def pytest_addoption(parser) -> None:
:param parser: pytest parser
:return:
"""
parser.addoption("--context",
action="store", default="", help="The context to use in the kubeconfig file.")
parser.addoption("--image",
action="store", default=DEFAULT_IMAGE, help="The Ingress Controller image.")
parser.addoption("--image-pull-policy",
action="store", default=DEFAULT_PULL_POLICY, help="The pull policy of the Ingress Controller image.")
parser.addoption("--deployment-type",
action="store", default=DEFAULT_DEPLOYMENT_TYPE,
help="The type of the IC deployment: deployment or daemon-set.")
parser.addoption("--ic-type",
action="store", default=DEFAULT_IC_TYPE, help="The type of the Ingress Controller: nginx-ingress or nginx-ingress-plus.")
parser.addoption("--service",
action="store",
default=DEFAULT_SERVICE,
help="The type of the Ingress Controller service: nodeport or loadbalancer.")
parser.addoption("--node-ip", action="store", help="The public IP of a cluster node. Not required if you use the loadbalancer service (see --service argument).")
parser.addoption("--kubeconfig",
action="store",
default=os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION),
help="An absolute path to a kubeconfig file.")
parser.addoption("--show-ic-logs", action="store", default="no", help="Show IC logs in stdout on test failure")
parser.addoption(
"--context",
action="store",
default="",
help="The context to use in the kubeconfig file.",
)
parser.addoption(
"--image",
action="store",
default=DEFAULT_IMAGE,
help="The Ingress Controller image.",
)
parser.addoption(
"--image-pull-policy",
action="store",
default=DEFAULT_PULL_POLICY,
help="The pull policy of the Ingress Controller image.",
)
parser.addoption(
"--deployment-type",
action="store",
default=DEFAULT_DEPLOYMENT_TYPE,
help="The type of the IC deployment: deployment or daemon-set.",
)
parser.addoption(
"--ic-type",
action="store",
default=DEFAULT_IC_TYPE,
help="The type of the Ingress Controller: nginx-ingress or nginx-ingress-plus.",
)
parser.addoption(
"--service",
action="store",
default=DEFAULT_SERVICE,
help="The type of the Ingress Controller service: nodeport or loadbalancer.",
)
parser.addoption(
"--node-ip",
action="store",
help="The public IP of a cluster node. Not required if you use the loadbalancer service (see --service argument).",
)
parser.addoption(
"--kubeconfig",
action="store",
default=os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION),
help="An absolute path to a kubeconfig file.",
)
parser.addoption(
"--show-ic-logs",
action="store",
default="no",
help="Show IC logs in stdout on test failure",
)


# import fixtures into pytest global namespace
pytest_plugins = [
"suite.fixtures"
]
pytest_plugins = ["suite.fixtures"]


def pytest_collection_modifyitems(config, items) -> None:
Expand Down Expand Up @@ -79,9 +115,17 @@ def pytest_runtest_makereport(item) -> None:
rep = outcome.get_result()

# we only look at actual failing test calls, not setup/teardown
if rep.when == "call" and rep.failed and item.config.getoption("--show-ic-logs") == "yes":
pod_namespace = item.funcargs['ingress_controller_prerequisites'].namespace
pod_name = get_first_pod_name(item.funcargs['kube_apis'].v1, pod_namespace)
if (
rep.when == "call"
and rep.failed
and item.config.getoption("--show-ic-logs") == "yes"
):
pod_namespace = item.funcargs["ingress_controller_prerequisites"].namespace
pod_name = get_first_pod_name(item.funcargs["kube_apis"].v1, pod_namespace)
print("\n===================== IC Logs Start =====================")
print(item.funcargs['kube_apis'].v1.read_namespaced_pod_log(pod_name, pod_namespace))
print(
item.funcargs["kube_apis"].v1.read_namespaced_pod_log(
pod_name, pod_namespace
)
)
print("\n===================== IC Logs End =====================")
57 changes: 57 additions & 0 deletions tests/suite/test_build_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest, logging, io
from kubernetes.client.rest import ApiException
from suite.resources_utils import get_first_pod_name


@pytest.mark.smoke
class TestBuildVersion:
def test_build_version(
self, ingress_controller, kube_apis, ingress_controller_prerequisites
):
"""
Test Version tag of build i.e. 'Version=<VERSION>'
"""
_info = self.send_build_info(kube_apis, ingress_controller_prerequisites)
_version = _info[
_info.find("Version=") + len("Version=") : _info.rfind("GitCommit=")
]
logging.info(_version)
assert _version != " "

def test_build_gitcommit(
self, ingress_controller, kube_apis, ingress_controller_prerequisites
):
"""
Test Git Commit tag of build i.e. 'GitCommit=<GITCOMMIT>'
"""
_info = self.send_build_info(kube_apis, ingress_controller_prerequisites)
_commit = _info[_info.find("GitCommit=") :].lstrip().replace("GitCommit=", "")
logging.info(_commit)
assert _commit != ""

def send_build_info(self, kube_apis, ingress_controller_prerequisites) -> str:
"""
Helper function to get pod logs
"""
pod_name = get_first_pod_name(
kube_apis.v1, ingress_controller_prerequisites.namespace
)
try:
api_response = kube_apis.v1.read_namespaced_pod_log(
name=pod_name,
namespace=ingress_controller_prerequisites.namespace,
limit_bytes=200,
)
logging.info(api_response)
except ApiException as e:
logging.exception(f"Found exception in reading the logs: {e}")

br = io.StringIO(api_response)
_log = br.readline()
try:
_info = _log[_log.find("Version") :].strip()
logging.info(f"Version and GitCommit info: {_info}")
except Exception as e:
logging.exception(f"Tag labels not found")

return _info

0 comments on commit eade506

Please sign in to comment.