diff --git a/.circleci/config.yml b/.circleci/config.yml index 48ea3cef..b49b00b4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -11,7 +11,8 @@ jobs: steps: - checkout - python/load-cache - - python/install-deps + - python/install-deps: + dependency-file: requirements-dev.txt - python/save-cache - python/test lint-and-coverage: @@ -20,7 +21,8 @@ jobs: PYTHONPATH=./src steps: - checkout - - python/install-deps + - python/install-deps: + dependency-file: requirements-dev.txt - run: name: Linter command: | diff --git a/.gitignore b/.gitignore index a204aabf..10872ad9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .venv +.idea .vscode .coverage *.pyc diff --git a/CHANGELOG.md b/CHANGELOG.md index a0a42b93..b31be34a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## In progress +### Changed +- Refactored setup.py & requirements, moved library scope to GLOBAL, sperated exceptions [#101](https://github.com/devopsspiral/KubeLibrary/pull/101) by [@MarcinMaciaszek](https://github.com/MarcinMaciaszek) ## [0.6.0] - 2021-11-30 ### Changed - Helpers and keywords unification [#75](https://github.com/devopsspiral/KubeLibrary/pull/75) by [@m-wcislo](https://github.com/m-wcislo) diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 00000000..d0249f7f --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,5 @@ +-r requirements.txt +mock +flake8 +coverage +robotframework-requests diff --git a/requirements.txt b/requirements.txt index 335e09d3..4c9881f4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,4 @@ -kubernetes +google-auth==1.21.3 +kubernetes>=10.0.1 robotframework>=3.2.2 urllib3-mock>=0.3.3 -# for development -mock -flake8 -coverage -# for example testcases -robotframework-requests diff --git a/setup.py b/setup.py index 042ec5b1..a1afae9b 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,21 @@ -import setuptools +from pkg_resources import parse_requirements +from pathlib import Path +from setuptools import setup + +exec(open("src/KubeLibrary/version.py").read()) with open("README.md", "r") as fh: long_description = fh.read() -setuptools.setup( +with Path("requirements.txt").open() as requirements: + install_requires = [ + str(requirement) + for requirement in parse_requirements(requirements) + ] + +setup( name="robotframework-kubelibrary", - version="0.6.0", + version=version, author="Michał Wcisło", author_email="mwcislo999@gmail.com", description="Kubernetes library for Robot Framework", @@ -24,10 +34,5 @@ keywords="robotframework testing test automation kubernetes", python_requires='>=3.6', package_dir={'': 'src'}, - install_requires=[ - 'google-auth==1.21.3', - 'kubernetes>=10.0.1', - 'robotframework>=3.2.2', - 'urllib3-mock>=0.3.3' - ], + install_requires=install_requires, ) diff --git a/src/KubeLibrary/KubeLibrary.py b/src/KubeLibrary/KubeLibrary.py index 3430b480..190ef416 100755 --- a/src/KubeLibrary/KubeLibrary.py +++ b/src/KubeLibrary/KubeLibrary.py @@ -2,31 +2,28 @@ import re import ssl import urllib3 + from kubernetes import client, config, dynamic from robot.api import logger +from robot.api.deco import library from string import digits, ascii_lowercase from random import choices +from KubeLibrary.exceptions import BearerTokenWithPrefixException +from KubeLibrary.version import version + # supressing SSL warnings when using self-signed certs urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) -class BearerTokenWithPrefixException(Exception): - - ROBOT_SUPPRESS_NAME = True - - def __init__(self): - super().__init__("Unnecessary 'Bearer ' prefix in token") - pass - - class DynamicClient(dynamic.DynamicClient): @property def api_client(self): return self.client -class KubeLibrary(object): +@library(scope="GLOBAL", version=version, auto_keywords=True) +class KubeLibrary: """KubeLibrary is a Robot Framework test library for Kubernetes. The approach taken by this library is to provide easy to access kubernetes objects representation that can @@ -693,7 +690,8 @@ def filter_endpoints_names(self, endpoints): """ return self.filter_names(endpoints.items) - def filter_pods_containers_by_name(self, pods, name_pattern): + @staticmethod + def filter_pods_containers_by_name(pods, name_pattern): """Filters pods containers by name for given list of pods. Returns lists of containers (flattens). @@ -709,7 +707,8 @@ def filter_pods_containers_by_name(self, pods, name_pattern): containers.append(container) return containers - def filter_containers_images(self, containers): + @staticmethod + def filter_containers_images(containers): """Filters container images for given lists of containers. Returns list of images. @@ -719,7 +718,8 @@ def filter_containers_images(self, containers): """ return [container.image for container in containers] - def filter_containers_resources(self, containers): + @staticmethod + def filter_containers_resources(containers): """Filters container resources for given lists of containers. Returns list of resources. @@ -729,7 +729,8 @@ def filter_containers_resources(self, containers): """ return [container.resources for container in containers] - def filter_pods_containers_statuses_by_name(self, pods, name_pattern): + @staticmethod + def filter_pods_containers_statuses_by_name(pods, name_pattern): """Filters pods containers statuses by container name for given list of pods. Returns lists of containers statuses. @@ -767,7 +768,8 @@ def get_pod_status_in_namespace(self, name, namespace): ret = self.v1.read_namespaced_pod_status(name, namespace) return ret.status.phase - def assert_pod_has_labels(self, pod, labels_json): + @staticmethod + def assert_pod_has_labels(pod, labels_json): """Assert pod has labels. Returns True/False @@ -792,7 +794,8 @@ def assert_pod_has_labels(self, pod, labels_json): logger.error(f'Failed parsing Pod Labels JSON:{labels_json}') return False - def assert_pod_has_annotations(self, pod, annotations_json): + @staticmethod + def assert_pod_has_annotations(pod, annotations_json): """Assert pod has annotations. Returns True/False @@ -817,7 +820,8 @@ def assert_pod_has_annotations(self, pod, annotations_json): logger.error(f'Failed parsing Pod Annotations JSON:{annotations_json}') return False - def assert_container_has_env_vars(self, container, env_vars_json): + @staticmethod + def assert_container_has_env_vars(container, env_vars_json): """Assert container has env vars. Returns True/False diff --git a/src/KubeLibrary/__init__.py b/src/KubeLibrary/__init__.py index 1a439663..a6772ef4 100644 --- a/src/KubeLibrary/__init__.py +++ b/src/KubeLibrary/__init__.py @@ -1 +1 @@ -from .KubeLibrary import KubeLibrary, BearerTokenWithPrefixException # noqa: F401 +from .KubeLibrary import KubeLibrary # noqa: F401 diff --git a/src/KubeLibrary/exceptions.py b/src/KubeLibrary/exceptions.py new file mode 100644 index 00000000..ba464377 --- /dev/null +++ b/src/KubeLibrary/exceptions.py @@ -0,0 +1,7 @@ +class BearerTokenWithPrefixException(Exception): + + ROBOT_SUPPRESS_NAME = True + + def __init__(self): + super().__init__("Unnecessary 'Bearer ' prefix in token") + pass diff --git a/src/KubeLibrary/version.py b/src/KubeLibrary/version.py new file mode 100644 index 00000000..aaf05b32 --- /dev/null +++ b/src/KubeLibrary/version.py @@ -0,0 +1 @@ +version = "0.6.0" diff --git a/test/test_KubeLibrary.py b/test/test_KubeLibrary.py index 7af0c90c..780fe29c 100644 --- a/test/test_KubeLibrary.py +++ b/test/test_KubeLibrary.py @@ -3,7 +3,8 @@ import re import ssl import unittest -from KubeLibrary import KubeLibrary, BearerTokenWithPrefixException +from KubeLibrary import KubeLibrary +from KubeLibrary.exceptions import BearerTokenWithPrefixException from kubernetes.config.config_exception import ConfigException from urllib3_mock import Responses diff --git a/testcases/Dockerfile b/testcases/Dockerfile index a0f506a9..a7d83a03 100644 --- a/testcases/Dockerfile +++ b/testcases/Dockerfile @@ -1,5 +1,5 @@ FROM python:3.8.1-alpine -COPY setup.py README.md ./ +COPY setup.py README.md requirements.txt ./ COPY src ./src COPY testcases ./testcases RUN pip install robotframework-requests \