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
12 changes: 12 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[run]
branch = True
omit =
*/lib/python*/site-packages/*
*/lib/python*/*
/usr/*
setup.py
conftest.py
openshift/test/*

[html]
directory = cover
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ htmlcov/
nosetests.xml
coverage.xml
*,cover
cover/
.hypothesis/
venv/
.python-version
Expand Down
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
sudo: required

services:
- docker

cache:
- pip

language: python

python:
- "2.7"
- "3.5"

env:
- OPENSHIFT_VERSION=latest
- OPENSHIFT_VERSION=1.5
- OPENSHIFT_VERSION=1.4
- OPENSHIFT_VERSION=1.3

install:
- pip install tox-travis coveralls

script:
- tox

after_success:
- coveralls

before_cache:
- rm ~/.cache/pip/log/debug.log
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# openshift-restclient-python
# openshift-restclient-python

[![Build Status](https://travis-ci.org/openshift/openshift-restclient-python.svg?branch=master)](https://travis-ci.org/openshift/openshift-restclient-python)
[![Coverage Status](https://coveralls.io/repos/github/openshift/openshift-restclient-python/badge.svg?branch=master)](https://coveralls.io/github/openshift/openshift-restclient-python?branch=master)
7 changes: 7 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def pytest_addoption(parser):
parser.addoption("--openshift-version", action="store", default="latest",
help="Version of OpenShift to test against for functional tests")


def pytest_report_header(config):
return "OpenShift verison: {}".format(config.getoption('--openshift-version'))
5 changes: 3 additions & 2 deletions openshift/ansiblegen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import logging
import sys

from logging import config

from openshift import __version__
from openshift.helper import OpenShiftException

Expand All @@ -15,7 +17,6 @@

logger = logging.getLogger(__name__)

from logging import config

LOGGING = {
'version': 1,
Expand Down Expand Up @@ -94,7 +95,7 @@ def run_docstrings_cmd(**kwargs):
for model in models:
try:
strings = DocStrings(model=model, api_version=api_version)
except OpenShiftException as exc:
except OpenShiftException:
raise
print("DOCUMENTATION = '''")
print(strings.documentation)
Expand Down
8 changes: 4 additions & 4 deletions openshift/ansiblegen/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, model, api_version):
self.api_version = api_version
try:
self.helper = KubernetesObjectHelper(self.api_version, self.model)
except OpenShiftException as exc:
except OpenShiftException:
raise

@property
Expand All @@ -46,7 +46,7 @@ def documentation(self):
if not self.helper.base_model_name_snake.endswith('_list'):
# module allows CRUD operations on the object
doc_string['description'] = [
"Manage the lifecycle of a {} object. Supports check mode, and attempts to " \
"Manage the lifecycle of a {} object. Supports check mode, and attempts to "
"to be idempotent.".format(self.helper.base_model_name_snake)
]
else:
Expand Down Expand Up @@ -155,7 +155,7 @@ def __get_attributes(self, obj, doc_key=None):
sub_obj = None
try:
sub_obj = getattr(models, class_name)()
except:
except Exception:
pass
if sub_obj:
doc_key[attribute]['contains'] = CommentedMap()
Expand All @@ -172,7 +172,7 @@ def __get_attributes(self, obj, doc_key=None):
sub_obj = None
try:
sub_obj = getattr(models, class_name)()
except:
except Exception:
pass
if sub_obj:
doc_key[attribute]['contains'] = CommentedMap()
Expand Down
9 changes: 4 additions & 5 deletions openshift/ansiblegen/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import logging
import inspect
import os
import re
import shutil
import string_utils
import sys
Expand Down Expand Up @@ -89,7 +88,7 @@ def generate_modules(self):
logger.debug("success!")
except Exception as exc:
logger.debug("failed!!")
raise OpenShiftException(exc.message)
raise OpenShiftException(str(exc))

context = {
'documentation_string': documentation,
Expand All @@ -99,8 +98,8 @@ def generate_modules(self):
}
self.__jinja_render_to_temp('k8s_module.j2', module_name, temp_dir, **context)
if not self.suppress_stdout:
sys.stdout.write("\033[F") # cursor up 1 line
sys.stdout.write("\033[K") # clear to EOL
sys.stdout.write("\033[F") # cursor up 1 line
sys.stdout.write("\033[K") # clear to EOL
shutil.rmtree(temp_dir)
if not self.suppress_stdout:
print("Generated {} modules".format(len(self.models)))
Expand Down Expand Up @@ -133,6 +132,6 @@ def __create_output_path(self):
raise OpenShiftException("ERROR: expected {} to be a directory.".format(self.output_path))
else:
try:
os.makedirs(self.output_path, 0775)
os.makedirs(self.output_path, 0o775)
except os.error as exc:
raise OpenShiftException("ERROR: could not create {0} - {1}".format(self.output_path, str(exc)))
10 changes: 7 additions & 3 deletions openshift/helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def get_object(self, name, namespace=None):

def patch_object(self, name, namespace, k8s_obj):
# TODO: add a parameter for waiting until the object is ready
k8s_obj.status = None
empty_status = self.properties['status']['class']()
k8s_obj.status = empty_status
k8s_obj.metadata.resource_version = None
try:
patch_method = self.__lookup_method('patch', namespace)
Expand Down Expand Up @@ -458,7 +459,7 @@ def get_model(api_version, kind):
model_name = api_version.capitalize() + name
try:
model = getattr(client.models, model_name)
except Exception as exc:
except Exception:
raise OpenShiftException(
"Error: openshift.client.models.{} was not found. "
"Did you specify the correct Kind and API Version?".format(model_name)
Expand Down Expand Up @@ -586,7 +587,7 @@ def __log_argspec(self):
tmp_arg_spec.pop(key)
logger.debug(json.dumps(tmp_arg_spec, indent=4, sort_keys=True))

def __transform_properties(self, properties, prefix='', path=[], alternate_prefix=''):
def __transform_properties(self, properties, prefix='', path=None, alternate_prefix=''):
"""
Convert a list of properties to an argument_spec dictionary

Expand All @@ -596,6 +597,9 @@ def __transform_properties(self, properties, prefix='', path=[], alternate_prefi
:param alternate_prefix: a more minimal version of prefix
:return: dict
"""
if path is None:
path = []

args = {}
for prop, prop_attributes in properties.items():
if prop in ('api_version', 'status', 'kind'):
Expand Down
71 changes: 71 additions & 0 deletions openshift/test/functional/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import io
import tarfile
import time

import docker
import pytest
import requests

from six import StringIO

from kubernetes import config
from openshift.helper import KubernetesObjectHelper


@pytest.fixture(scope='session')
def openshift_container(request):
client = docker.from_env()
# TODO: bind to a random host port
image_name = 'openshift/origin:{}'.format(request.config.getoption('--openshift-version'))
container = client.containers.run(image_name, 'start master', detach=True,
ports={'8443/tcp': 8443})

try:
# Wait for the container to no longer be in the created state before
# continuing
while container.status == u'created':
time.sleep(0.2)
container = client.containers.get(container.id)

# Wait for the api server to be ready before continuing
# TODO: actually we end on a 200 response
for _ in range(10):
try:
resp = requests.head("https://localhost:8443/healthz/ready", verify=False)
if resp.status_code == 200:
break
except requests.RequestException:
pass

time.sleep(1)

# TODO: handle waiting for system policy to be fully configured better
time.sleep(1)

yield container
finally:
# Always remove the container
container.remove(force=True)


@pytest.fixture(scope='session')
def kubeconfig(openshift_container, tmpdir_factory):
# get_archive returns a stream of the tar archive containing the requested
# files/directories, so we need use BytesIO as an intermediate step.
tar_stream, _ = openshift_container.get_archive('/var/lib/origin/openshift.local.config/master/admin.kubeconfig')
tar_obj = tarfile.open(fileobj=io.BytesIO(tar_stream.read()))
kubeconfig_contents = tar_obj.extractfile('admin.kubeconfig').read()

kubeconfig_file = tmpdir_factory.mktemp('kubeconfig').join('admin.kubeconfig')
kubeconfig_file.write(kubeconfig_contents)
yield kubeconfig_file


@pytest.fixture()
def k8s_helper(request, kubeconfig):
print(request.module.__name__)
_,api_version,resource = request.module.__name__.split('_', 2)
k8s_helper = KubernetesObjectHelper(api_version, resource)
k8s_helper.set_client_config({'kubeconfig': str(kubeconfig)})
config.kube_config.configuration.host='https://127.0.0.1:8443'
yield k8s_helper
15 changes: 4 additions & 11 deletions openshift/test/functional/test_v1_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

from openshift.helper import KubernetesObjectHelper

pytestmark = pytest.mark.functional

@pytest.fixture(scope='module')
def k8s_helper():
k8s_helper = KubernetesObjectHelper('v1', 'namespace')
return k8s_helper


@pytest.fixture()
def namespace(k8s_helper):
Expand All @@ -24,7 +17,7 @@ def namespace(k8s_helper):

yield namespace

k8s_helper.delete_object(name)
k8s_helper.delete_object(name, None)


def test_namespace_patch(k8s_helper, namespace):
Expand All @@ -41,10 +34,10 @@ def test_namespace_exists(k8s_helper, namespace):
get_result = k8s_helper.get_object(namespace.metadata.name)
assert get_result is not None
assert get_result.metadata.name == namespace.metadata.name
assert get_result == namespace
assert get_result.metadata.uid == namespace.metadata.uid


def test_get_exists_not(k8s_helper):
name = "does-not-exist-{}".format(uuid.uuid4())
namespace = k8s_helper.get_object(name)
assert namespace is None
result = k8s_helper.get_object(name)
assert result is None
13 changes: 3 additions & 10 deletions openshift/test/functional/test_v1_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

from openshift.helper import KubernetesObjectHelper

pytestmark = pytest.mark.functional

@pytest.fixture(scope='module')
def k8s_helper():
k8s_helper = KubernetesObjectHelper('v1', 'project')
return k8s_helper


@pytest.fixture()
def project(k8s_helper):
Expand All @@ -24,10 +17,10 @@ def project(k8s_helper):

yield project

k8s_helper.delete_object(name)
k8s_helper.delete_object(name, None)


@pytest.mark.skip()
@pytest.mark.skip(reason="project fields are immutable")
def test_project_patch(k8s_helper, project):
ns_copy = copy.deepcopy(project)
ns_copy.metadata.labels = {'test-label': 'test-value'}
Expand All @@ -42,7 +35,7 @@ def test_project_exists(k8s_helper, project):
get_result = k8s_helper.get_object(project.metadata.name)
assert get_result is not None
assert get_result.metadata.name == project.metadata.name
assert get_result == project
assert get_result.metadata.uid == project.metadata.uid


def test_get_exists_not(k8s_helper):
Expand Down
9 changes: 1 addition & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
urllib3>=1.19
six>=1.10
kubernetes
certifi
python-dateutil
pyyaml
oauth2client
ipaddress
kubernetes >= 1.0.0b3
python-string-utils
ruamel.yaml
jinja2
11 changes: 11 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ max_line_length = 120
[yapf]
based_on_style = pep8
dedent_closing_brackets = True

[tool:pytest]
norecursedirs =
.*
__pycache__
cover
docs
addopts =
--cov=openshift
--cov-report=term
--cov-report=html
6 changes: 5 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
git+https://github.com/detiber/client-python@master
coverage
docker ~= 2.1.0
flake8
pytest
pytest-cov
7 changes: 0 additions & 7 deletions test_helper.py

This file was deleted.

Loading