Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ python:
env:
- TEST_SUITE=lint
- TEST_SUITE=functional OPENSHIFT_VERSION=latest
- TEST_SUITE=functional OPENSHIFT_VERSION=3.7
- TEST_SUITE=functional OPENSHIFT_VERSION=3.6
- TEST_SUITE=functional OPENSHIFT_VERSION=1.5
- TEST_SUITE=functional OPENSHIFT_VERSION=1.4
Expand Down
2 changes: 1 addition & 1 deletion openshift/ansiblegen/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def examples(self):
if os.path.exists(example_path):
logger.debug('parsing {}'.format(example_path))
yaml_examples = ruamel.yaml.load(open(example_path, 'r'), Loader=ruamel.yaml.RoundTripLoader)
for ex in yaml_examples:
for ex in yaml_examples['tasks']:
new_example = CommentedMap()
for key, value in ex.items():
if key == 'name':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v1_horizontal_pod_autoscaler.yml
---

tasks:
- create:
name: test-scaler
namespace: test
Expand Down
17 changes: 10 additions & 7 deletions openshift/ansiblegen/examples/k8s_v1_namespace.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# v1_namespace.yml
---
admin: yes

tasks:
- create:
name: k8s-namespace
name: Create a namespace

- patch:
name: k8s-namespace
labels:
Expand All @@ -13,7 +15,7 @@
annotations:
domain: namespace.com.acmecorp
name: Add labels and annotations

- patch:
name: k8s-namespace
labels:
Expand All @@ -24,11 +26,11 @@
domain: namespace.app.com.acmecorp
monitoring_group: '1'
name: Update labels and annotations

- remove:
name: k8s-namespace
task_name: Delete a namespace

- create:
name: search-namespace
labels:
Expand All @@ -37,14 +39,14 @@
annotations:
company: acme.com
name: Create a namespace

- patch:
name: search-namespace
labels:
app: search
type: web
task_name: Update labels

- replace:
name: search-namespace
labels:
Expand All @@ -53,7 +55,8 @@
annotations:
company: acme.com
name: Replace namespace

- remove:
name: search-namespace
name: Remove namespace

2 changes: 2 additions & 0 deletions openshift/ansiblegen/examples/k8s_v1_persistent_volume.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# v1_persistent_volume.yml
---

admin: yes
tasks:
- create:
name: mypv
capacity:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v1_persistent_volume_claim.yml
---

tasks:
- create:
name: mypvc
namespace: demo_project
Expand Down
7 changes: 4 additions & 3 deletions openshift/ansiblegen/examples/k8s_v1_service.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v1_service.yml
---

tasks:
- create:
name: myservice
namespace: k8s-project
Expand All @@ -26,7 +27,7 @@
- port: 8788
target_port: 8080
name: socket-port
type: NodePort
type: ClusterIP
name: Patch service

- create:
Expand Down Expand Up @@ -67,5 +68,5 @@
- port: 8080
target_port: 8080
name: http
type: ClusterIP
name: Repace service
type: NodePort
name: Repace service
1 change: 1 addition & 0 deletions openshift/ansiblegen/examples/k8s_v1beta1_replica_set.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v1beta1_replica_set.yml
---

tasks:
- create:
name: myreplicaset
namespace: test
Expand Down
4 changes: 4 additions & 0 deletions openshift/ansiblegen/examples/k8s_v1beta1_stateful_set.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# v1beta1_stateful_set.yml
---

version_limits:
min: '1.5'

tasks:
- create:
name: test
labels:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v1_deployment_config.yml
---

tasks:
- create:
name: frontend
namespace: k8s-project
Expand Down
3 changes: 2 additions & 1 deletion openshift/ansiblegen/examples/openshift_v1_project.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# v1_project.yml
---

admin: yes
tasks:
- create:
name: example
name: Create project
Expand All @@ -15,4 +17,3 @@
- remove:
name: example
name: Remove project

1 change: 1 addition & 0 deletions openshift/ansiblegen/examples/openshift_v1_route.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v1_route.yml
---

tasks:
- create:
name: myroute
namespace: k8s-project
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ norecursedirs =
docs
addopts =
--cov=openshift
--cov-report=term
--cov-report=html
--cov-append
142 changes: 40 additions & 102 deletions test/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import
from __future__ import print_function

import re
import copy
import io
import os
Expand Down Expand Up @@ -96,67 +97,30 @@ def admin_kubeconfig(openshift_container, tmpdir_factory):
return kubeconfig_file


@pytest.fixture(scope='module')
def k8s_ansible_helper(request, kubeconfig):
_, _, api_version, resource = request.module.__name__.split('_', 3)
auth = {}
if kubeconfig is not None:
auth = {
'kubeconfig': str(kubeconfig),
'host': 'https://localhost:8443',
'verify_ssl': False
}
helper = KubernetesAnsibleModuleHelper(api_version, resource, debug=True, reset_logfile=False, **auth)
helper.api_client.config.debug = True

return helper
def parse_test_name(name):
pieces = re.findall('[A-Z][a-z0-9]*', name)
api_version = pieces[1].lower()
resource = '_'.join(map(str.lower, pieces[2:]))
return api_version, resource


@pytest.fixture(scope='module')
def openshift_ansible_helper(request, kubeconfig):
_, _, api_version, resource = request.module.__name__.split('_', 3)
@pytest.fixture(scope='class')
def ansible_helper(request, kubeconfig, admin_kubeconfig):
api_version, resource = parse_test_name(request.node.name)
needs_admin = request.node.cls.tasks.get('admin')
config = admin_kubeconfig if needs_admin else kubeconfig
auth = {}
if kubeconfig is not None:
auth = {
'kubeconfig': str(kubeconfig),
'kubeconfig': str(config),
'host': 'https://localhost:8443',
'verify_ssl': False
}
helper = OpenShiftAnsibleModuleHelper(api_version, resource, debug=True, reset_logfile=False, **auth)
helper.api_client.config.debug = True

return helper
try:
helper = KubernetesAnsibleModuleHelper(api_version, resource, debug=True, reset_logfile=False, **auth)
except Exception:
helper = OpenShiftAnsibleModuleHelper(api_version, resource, debug=True, reset_logfile=False, **auth)


@pytest.fixture(scope='module')
def admin_k8s_ansible_helper(request, admin_kubeconfig):
_, _, api_version, resource = request.module.__name__.split('_', 3)
auth = {}
if admin_kubeconfig is not None:
auth = {
'kubeconfig': str(admin_kubeconfig),
'host': 'https://localhost:8443',
'verify_ssl': False
}
helper = KubernetesAnsibleModuleHelper(api_version, resource, **auth)
helper.enable_debug(to_file=False)
helper.api_client.config.debug = True

return helper


@pytest.fixture(scope='module')
def admin_openshift_ansible_helper(request, admin_kubeconfig):
_, _, api_version, resource = request.module.__name__.split('_', 3)
auth = {}
if admin_kubeconfig is not None:
auth = {
'kubeconfig': str(admin_kubeconfig),
'host': 'https://localhost:8443',
'verify_ssl': False
}
helper = OpenShiftAnsibleModuleHelper(api_version, resource, **auth)
helper.enable_debug(to_file=False)
helper.api_client.config.debug = True

return helper
Expand Down Expand Up @@ -192,7 +156,7 @@ def compare_func(ansible_helper, k8s_obj, parameters):
return compare_func


@pytest.fixture(scope='module')
@pytest.fixture(scope='class')
def namespace(kubeconfig):
name = "test-{}".format(uuid.uuid4())

Expand All @@ -214,14 +178,12 @@ def namespace(kubeconfig):


@pytest.fixture()
def object_name():
# v1.3 services cannot be longer than 24 characters long
# truncate at 23 to avoid a trailing '-'
name = 'test-{}'.format(uuid.uuid4())[:23]
return name
def object_name(request):
action = request.function.__name__.split('_')[1]
return '{}-{}'.format(action, uuid.uuid4())[:22].strip('-')


@pytest.fixture(scope='module')
@pytest.fixture(scope='class')
def project(kubeconfig):
name = "test-{}".format(uuid.uuid4())
auth = {}
Expand All @@ -247,21 +209,31 @@ def openshift_version():


@pytest.fixture(autouse=True)
def skip_by_version(request, openshift_version):
if request.node.get_marker('version_limit') and openshift_version:
def skip_empty(request):
api_version, resource = parse_test_name(request.node.cls._type)
action = request.function.__name__.split('_')[1]
tasks = list(filter(lambda x: x.get(action), request.node.cls.tasks['tasks']))
if not tasks and action not in ['get', 'remove']:
pytest.skip('No example provided to {} {}'.format(action, resource))

lowest_version = request.node.get_marker('version_limit').kwargs.get('lowest_version')
highest_version = request.node.get_marker('version_limit').kwargs.get('highest_version')
skip_latest = request.node.get_marker('version_limit').kwargs.get('skip_latest')

if openshift_version == 'latest' and skip_latest:
pytest.skip('This API is not supported in the latest openshift version')
@pytest.fixture(autouse=True)
def skip_by_version(request, openshift_version):
if request.node.cls.tasks.get('version_limits') and openshift_version:
lowest_version = request.node.cls.tasks['version_limits'].get('min')
highest_version = request.node.cls.tasks['version_limits'].get('max')
skip_latest = request.node.cls.tasks['version_limits'].get('latest')

too_low = lowest_version and parse_version(lowest_version) > parse_version(openshift_version)
too_high = highest_version and parse_version(highest_version) < parse_version(openshift_version)

if too_low or too_high:
pytest.skip('This API is not supported in openshift versions < {}'.format(openshift_version))
if openshift_version == 'latest':
if skip_latest:
pytest.skip('This API is not supported in the latest openshift version')
elif too_low:
pytest.skip('This API is not supported in openshift versions > {}. You are using version {}'.format(lowest_version, openshift_version))
elif too_high:
pytest.skip('This API is not supported in openshift versions < {}. You are using version {}'.format(highest_version, openshift_version))


def _get_id(argvalue):
Expand All @@ -275,37 +247,3 @@ def _get_id(argvalue):
elif argvalue.get('replace'):
op_type = 'replace'
return op_type + '_' + argvalue[op_type]['name'] + '_' + "{:0>3}".format(argvalue['seq'])


def pytest_generate_tests(metafunc):
_, api_version, resource = metafunc.module.__name__.split('_', 2)
yaml_name = api_version + '_' + resource + '.yml'
yaml_path = os.path.normpath(os.path.join(os.path.dirname(__file__),
'../../openshift/ansiblegen/examples', yaml_name))
if not os.path.exists(yaml_path):
raise Exception("ansible_data: Unable to locate {}".format(yaml_path))
with open(yaml_path, 'r') as f:
data = yaml.load(f)
seq = 0
for task in data:
seq += 1
task['seq'] = seq

if 'create_tasks' in metafunc.fixturenames:
tasks = [x for x in data if x.get('create')]
metafunc.parametrize("create_tasks", tasks, False, _get_id)
if 'patch_tasks' in metafunc.fixturenames:
tasks = [x for x in data if x.get('patch')]
metafunc.parametrize("patch_tasks", tasks, False, _get_id)
if 'remove_tasks' in metafunc.fixturenames:
tasks = [x for x in data if x.get('remove')]
metafunc.parametrize("remove_tasks", tasks, False, _get_id)
if 'replace_tasks' in metafunc.fixturenames:
tasks = [x for x in data if x.get('replace')]
metafunc.parametrize("replace_tasks", tasks, False, _get_id)
if 'namespaces' in metafunc.fixturenames:
tasks = [x for x in data if x.get('create') and x['create'].get('namespace')]
unique_namespaces = dict()
for task in tasks:
unique_namespaces[task['create']['namespace']] = None
metafunc.parametrize("namespaces", list(unique_namespaces.keys()))
Loading