Skip to content

Commit

Permalink
CI tests
Browse files Browse the repository at this point in the history
Change-Id: I6650a0a58b4e6c8932f5e20ae7d29d948c765112
  • Loading branch information
myakove authored and rnetser committed Dec 10, 2020
1 parent f5e8063 commit 6e56b68
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,6 @@ docs/source/rst

# IntelliJ project folder
.idea/

# General
local-cluster/_hco/
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ before_script:

test:
script:
- tox
- tox -e code-check
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
23 changes: 0 additions & 23 deletions docs/Makefile

This file was deleted.

35 changes: 0 additions & 35 deletions docs/make.bat

This file was deleted.

75 changes: 0 additions & 75 deletions docs/source/conf.py

This file was deleted.

19 changes: 0 additions & 19 deletions docs/source/index.rst

This file was deleted.

8 changes: 8 additions & 0 deletions local-cluster/hack/cluster-down.sh
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions local-cluster/hack/cluster-up.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions resources/virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
Empty file added tests/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions tests/manifests/vm.yaml
Original file line number Diff line number Diff line change
@@ -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: {}
35 changes: 35 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 15 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6e56b68

Please sign in to comment.