Skip to content

Commit

Permalink
Added small improvements. (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinMaciaszek authored Dec 14, 2021
1 parent f97084a commit fb41933
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 38 deletions.
6 changes: 4 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -20,7 +21,8 @@ jobs:
PYTHONPATH=./src
steps:
- checkout
- python/install-deps
- python/install-deps:
dependency-file: requirements-dev.txt
- run:
name: Linter
command: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.venv
.idea
.vscode
.coverage
*.pyc
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-r requirements.txt
mock
flake8
coverage
robotframework-requests
9 changes: 2 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
23 changes: 14 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
description="Kubernetes library for Robot Framework",
Expand All @@ -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,
)
38 changes: 21 additions & 17 deletions src/KubeLibrary/KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/KubeLibrary/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .KubeLibrary import KubeLibrary, BearerTokenWithPrefixException # noqa: F401
from .KubeLibrary import KubeLibrary # noqa: F401
7 changes: 7 additions & 0 deletions src/KubeLibrary/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BearerTokenWithPrefixException(Exception):

ROBOT_SUPPRESS_NAME = True

def __init__(self):
super().__init__("Unnecessary 'Bearer ' prefix in token")
pass
1 change: 1 addition & 0 deletions src/KubeLibrary/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = "0.6.0"
3 changes: 2 additions & 1 deletion test/test_KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion testcases/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down

0 comments on commit fb41933

Please sign in to comment.