From 6e56b68e075a75ca1c139129dfd032de06e0afda Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Thu, 10 Dec 2020 12:38:14 +0000 Subject: [PATCH] CI tests Change-Id: I6650a0a58b4e6c8932f5e20ae7d29d948c765112 --- .gitignore | 3 ++ .gitlab-ci.yml | 2 +- Makefile | 30 ++++++++++++ docs/Makefile | 23 --------- docs/make.bat | 35 -------------- docs/source/conf.py | 75 ------------------------------ docs/source/index.rst | 19 -------- local-cluster/hack/cluster-down.sh | 8 ++++ local-cluster/hack/cluster-up.sh | 11 +++++ resources/virtual_machine.py | 8 +++- tests/__init__.py | 0 tests/manifests/vm.yaml | 38 +++++++++++++++ tests/test_resources.py | 35 ++++++++++++++ tests/utils.py | 45 ++++++++++++++++++ tox.ini | 18 +++++-- 15 files changed, 192 insertions(+), 158 deletions(-) create mode 100644 Makefile delete mode 100644 docs/Makefile delete mode 100644 docs/make.bat delete mode 100644 docs/source/conf.py delete mode 100644 docs/source/index.rst create mode 100755 local-cluster/hack/cluster-down.sh create mode 100755 local-cluster/hack/cluster-up.sh create mode 100644 tests/__init__.py create mode 100644 tests/manifests/vm.yaml create mode 100644 tests/test_resources.py create mode 100644 tests/utils.py diff --git a/.gitignore b/.gitignore index 4c8bb8895e..e9ef8640fb 100644 --- a/.gitignore +++ b/.gitignore @@ -115,3 +115,6 @@ docs/source/rst # IntelliJ project folder .idea/ + +# General +local-cluster/_hco/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 436d387385..5358e3cd66 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,4 +7,4 @@ before_script: test: script: - - tox + - tox -e code-check diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..085d441ef7 --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +# Local cluster preparations +CLUSTER_DIR := local-cluster/_hco +export KUBEVIRT_PROVIDER ?= k8s-1.18 +export KUBEVIRT_NUM_NODES ?= 2 +export KUBEVIRT_NUM_SECONDARY_NICS ?= 4 +export KUBEVIRT_WITH_CNAO=true + +# Helper scripts +HACK_DIR := local-cluster/hack +CLUSTER_UP := $(HACK_DIR)/cluster-up.sh +CLUSTER_DOWN := $(HACK_DIR)/cluster-down.sh + +# If not specified otherwise, local cluster's KUBECONFIG will be used +export KUBECONFIG ?= $(CLUSTER_DIR)/_kubevirtci/_ci-configs/$(KUBEVIRT_PROVIDER)/.kubeconfig + +all: check + +cluster-up: $(CLUSTER_UP) + $(CLUSTER_UP) + +cluster-down: $(CLUSTER_DOWN) + $(CLUSTER_DOWN) + +check: cluster-down cluster-up + tox + +.PHONY: \ + cluster-down \ + cluster-up \ + check diff --git a/docs/Makefile b/docs/Makefile deleted file mode 100644 index 1ecdfb8454..0000000000 --- a/docs/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line, and also -# from the environment for the first two. -SPHINXOPTS ?= -SPHINXBUILD ?= sphinx-build -SOURCEDIR = source -BUILDDIR = build -SPHINXAPIDOC = sphinx-apidoc -RESOURCES = ../resources - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXAPIDOC) -T -f -o "$(SOURCEDIR)/rst" "$(RESOURCES)" - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/make.bat b/docs/make.bat deleted file mode 100644 index 6247f7e231..0000000000 --- a/docs/make.bat +++ /dev/null @@ -1,35 +0,0 @@ -@ECHO OFF - -pushd %~dp0 - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set SOURCEDIR=source -set BUILDDIR=build - -if "%1" == "" goto help - -%SPHINXBUILD% >NUL 2>NUL -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% -goto end - -:help -%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% - -:end -popd diff --git a/docs/source/conf.py b/docs/source/conf.py deleted file mode 100644 index 01dcb7a440..0000000000 --- a/docs/source/conf.py +++ /dev/null @@ -1,75 +0,0 @@ -# Configuration file for the Sphinx documentation builder. -# -# This file only contains a selection of the most common options. For a full -# list see the documentation: -# https://www.sphinx-doc.org/en/master/usage/configuration.html - -# -- Path setup -------------------------------------------------------------- - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# -import os -import sys - -import sphinx_bootstrap_theme - - -# -- Project information ----------------------------------------------------- - -project = "OCP Python Wrapper" -copyright = "2020 OCP Python Wrapper" # noqa: A001 -author = "myakove" - -sys.path.insert(0, os.path.abspath("../..")) -sys.path.insert(0, os.path.abspath(".")) -# -- General configuration --------------------------------------------------- - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. - -extensions = [ - "sphinx.ext.autodoc", - "sphinx.ext.intersphinx", - "sphinx.ext.ifconfig", - "sphinx.ext.viewcode", - "sphinx.ext.napoleon", - "sphinx.ext.todo", -] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ["_templates"] - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This pattern also affects html_static_path and html_extra_path. -exclude_patterns = [] - - -# -- Options for HTML output ------------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# -# html_theme = 'alabaster' - -html_theme = "bootstrap" -html_theme_path = sphinx_bootstrap_theme.get_html_theme_path() -html_theme_options = { - # HTML navbar class (Default: "navbar") to attach to
. - # For black navbar, do "navbar navbar-inverse" - "navbar_class": "navbar navbar-inverse", - # Fix navigation bar to top of page? - # Values: "true" (default) or "false" - "navbar_fixed_top": "true", - # Location of link to source. - # Options are "nav" (default), "footer". - "source_link_position": "nav", -} - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ["_static"] diff --git a/docs/source/index.rst b/docs/source/index.rst deleted file mode 100644 index f2ab0836ec..0000000000 --- a/docs/source/index.rst +++ /dev/null @@ -1,19 +0,0 @@ -.. cnv-tests documentation master file, created by - sphinx-quickstart on Thu Oct 31 11:05:15 2019. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Welcome to OCP Python Wrapper documentation! -===================================== - -.. toctree:: - :maxdepth: 10 - - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` diff --git a/local-cluster/hack/cluster-down.sh b/local-cluster/hack/cluster-down.sh new file mode 100755 index 0000000000..c2ea9e0fff --- /dev/null +++ b/local-cluster/hack/cluster-down.sh @@ -0,0 +1,8 @@ +#!/bin/bash -e + +script_dir=$(dirname "$(readlink -f "$0")") +hco_dir=local-cluster/_hco + +pushd $hco_dir +make cluster-down +popd diff --git a/local-cluster/hack/cluster-up.sh b/local-cluster/hack/cluster-up.sh new file mode 100755 index 0000000000..0ccfbd3384 --- /dev/null +++ b/local-cluster/hack/cluster-up.sh @@ -0,0 +1,11 @@ +#!/bin/bash -e + +script_dir=$(dirname "$(readlink -f "$0")") +hco_dir=local-cluster/_hco + +rm -rf $hco_dir +git clone https://github.com/kubevirt/hyperconverged-cluster-operator.git $hco_dir +pushd $hco_dir +make cluster-up +make cluster-sync +popd diff --git a/resources/virtual_machine.py b/resources/virtual_machine.py index 54b80d3c88..900fbf3326 100644 --- a/resources/virtual_machine.py +++ b/resources/virtual_machine.py @@ -29,10 +29,13 @@ class RunStrategy: ALWAYS = "Always" RERUNONFAILURE = "RerunOnFailure" - def __init__(self, name, namespace, client=None, teardown=True): + api_group = NamespacedResource.ApiGroup.KUBEVIRT_IO + + def __init__(self, name, namespace, client=None, body=None, teardown=True): super().__init__( name=name, namespace=namespace, client=client, teardown=teardown ) + self.body = body @property def _subresource_api_url(self): @@ -49,7 +52,8 @@ def api_request(self, method, action, **params): def to_dict(self): res = super().to_dict() - res["spec"] = {"template": {"spec": {}}} + body_spec = self.body.get("spec") + res["spec"] = body_spec or {"template": {"spec": {}}} return res def start(self, timeout=TIMEOUT, wait=False): diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/manifests/vm.yaml b/tests/manifests/vm.yaml new file mode 100644 index 0000000000..6337d4c5f7 --- /dev/null +++ b/tests/manifests/vm.yaml @@ -0,0 +1,38 @@ +apiVersion: kubevirt.io/v1alpha3 +kind: VirtualMachine +metadata: + creationTimestamp: null + labels: + kubevirt.io/vm: {{ name }} + name: {{ name }} +spec: + running: false + template: + metadata: + creationTimestamp: null + spec: + domain: + cpu: + cores: 1 + devices: + disks: + - disk: + bus: virtio + name: containerdisk + interfaces: + - masquerade: {} + name: default + machine: + type: "" + resources: + requests: + memory: 1024Mi + networks: + - name: default + pod: {} + terminationGracePeriodSeconds: 30 + volumes: + - containerDisk: + image: quay.io/openshift-cnv/qe-cnv-tests-fedora:33 + name: containerdisk +status: {} diff --git a/tests/test_resources.py b/tests/test_resources.py new file mode 100644 index 0000000000..4eacb1d942 --- /dev/null +++ b/tests/test_resources.py @@ -0,0 +1,35 @@ +import kubernetes +import pytest +from openshift.dynamic import DynamicClient +from resources.namespace import Namespace +from resources.pod import Pod +from resources.virtual_machine import VirtualMachine +from tests.utils import generate_yaml_from_template + + +@pytest.fixture(scope="session") +def client(): + return DynamicClient(client=kubernetes.config.new_client_from_config()) + + +@pytest.fixture(scope="session") +def namespace(): + with Namespace(name="namespace-for-tests") as ns: + yield ns + + +def test_get(client): + Pod.get(dyn_client=client) + + +def test_create(): + with Namespace(name="test-namespace"): + pass + + +def test_vm(namespace): + name = "test-vm" + with VirtualMachine( + name=name, namespace=namespace.name, body=generate_yaml_from_template(name=name) + ): + pass diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000000..c34d0e643c --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,45 @@ +import re + +import jinja2 +import yaml + + +class MissingTemplateVariables(Exception): + def __init__(self, var, template): + self.var = var + self.template = template + + def __str__(self): + return f"Missing variables {self.var} for template {self.template}" + + +def generate_yaml_from_template(**kwargs): + """ + Generate JSON from yaml file_ + + Keyword Args: + name (str): + image (str): + + Returns: + dict: Generated from template file + + Raises: + MissingTemplateVariables: If not all template variables exists + + Examples: + generate_yaml_from_template(file_='path/to/file/name', name='vm-name-1') + """ + file_ = "tests/manifests/vm.yaml" + with open(file_, "r") as stream: + data = stream.read() + + # Find all template variables + template_vars = [i.split()[1] for i in re.findall(r"{{ .* }}", data)] + for var in template_vars: + if var not in kwargs.keys(): + raise MissingTemplateVariables(var=var, template=file_) + + template = jinja2.Template(source=data) + out = template.render(**kwargs) + return yaml.safe_load(stream=out) diff --git a/tox.ini b/tox.ini index 1c6a150761..da8ff6c3cf 100644 --- a/tox.ini +++ b/tox.ini @@ -1,12 +1,24 @@ [tox] -envlist=code-check -skipsdist=True +envlist = code-check,tests +skipsdist = True [flake8] [testenv:code-check] basepython = python3 setenv = PYTHONPATH = {toxinidir} -deps= +deps = pre-commit commands = pre-commit run --all-files + +[testenv:tests] +basepython = python3 +setenv = + PYTHONPATH = {toxinidir} +passenv = + KUBECONFIG +deps = + pytest +commands = + pip install . + pytest tests