Verified Commit 04123204 authored by Peter Stanko's avatar Peter Stanko
Browse files

Status endpoint refactor

parent bd65921b
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
import logging
from typing import Optional

from requests import Response

@@ -127,6 +128,11 @@ class Defaults(object):
            return 'Object'
        return self._instance_klass.__name__

    def _make_json(self, response: Response) -> Optional[dict]:
        if response is None:
            return None
        return response.json() if len(response.content) > 0 else None

    def _make_instance(self, response: Response, should_log=True, is_list=False,
                       instance_klass=None):
        """Creates instance
@@ -138,10 +144,10 @@ class Defaults(object):
        Returns: New instance wrapper

        """
        if response is None:
        data = self._make_json(response)
        if data is None:
            return None
        instance_klass = instance_klass or self._instance_klass
        data: dict = response.json() if len(response.content) > 0 else None
        if instance_klass is None:
            return data
        instance = instance_klass(self, data) if not is_list \
+7 −13
Original line number Diff line number Diff line
@@ -2,27 +2,21 @@ from kontr_api.resources.default import Default, Defaults
from kontr_api.resources.secrets import Secrets


class Statuses(Defaults):
    def __init__(self, parent):
        super().__init__(parent, instance_klass=Status)

    def url(self):
        return "{self.url}/status"


class Workers(Defaults):
    def __init__(self, parent):
        super().__init__(parent, instance_klass=Worker)


class Status(Default):
    pass
    def status(self, wid):
        url = self._get_resource_url(wid) + "/status"
        self.log.debug(f"[READ] Worker status ({url})")
        response = self.rest.get(url)
        return self._make_json(response)


class Worker(Default):
    @property
    def status(self) -> Statuses:
        return Statuses(self)
    def status(self):
        return self.client.status(self.entity_id)

    @property
    def secrets(self) -> Secrets:
+0 −1
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@ extra_requirements = {
          'dev': [
              'pytest',
              'coverage',
              'pytest-cov',
              'mock',
              ],
          'docs': ['sphinx']
+1 −1
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ class EntitiesCreator(object):
    def get_worker_config(self, **kwargs) -> dict:
        uname = unique_name('test-worker')
        config = dict(name=uname,
                      url=f"https://{uname}.example.com",
                      url=f"http://localhost:8080",
                      tags="docker gcc")

        config.update(kwargs)
+9 −3
Original line number Diff line number Diff line
@@ -25,13 +25,13 @@ def test_worker_has_been_created(entity_client, ent_creator):
    ent_creator.entities_stack.append(created)


def test_worker_should_be_listed(entity_client, entity_testing: User):
def test_worker_should_be_listed(entity_client, entity_testing: Worker):
    workers_list = entity_client.list()
    assert len(workers_list) > 0
    assert entity_testing in workers_list


def test_worker_should_be_updated(client, entity_testing: User):
def test_worker_should_be_updated(client, entity_testing: Worker):
    old_tags = entity_testing['tags']
    new_tags = old_tags + " valgrind"
    entity_testing['tags'] = new_tags
@@ -39,10 +39,16 @@ def test_worker_should_be_updated(client, entity_testing: User):
    assert entity_testing['tags'] == new_tags


def test_worker_should_be_deleted(entity_client, entity_testing: User):
def test_worker_should_be_deleted(entity_client, entity_testing: Worker):
    assert entity_testing in entity_client.list()
    entity_testing.delete()
    assert entity_testing not in entity_client.list()


def test_worker_should_have_status(entity_client, entity_testing: Worker):
    status = entity_testing.status
    assert status