Skip to content

Commit

Permalink
Merge pull request #15 from fiaas/add-configmap-model
Browse files Browse the repository at this point in the history
Add ConfigMap model
  • Loading branch information
oyvindio committed Sep 11, 2017
2 parents 60e302d + ed3bd10 commit e1edac4
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 45 deletions.
13 changes: 13 additions & 0 deletions k8s/models/configmap.py
@@ -0,0 +1,13 @@
from __future__ import absolute_import

from .common import ObjectMeta
from ..base import Model
from ..fields import Field


class ConfigMap(Model):
class Meta:
url_template = "/api/v1/namespaces/{namespace}/configmaps/{name}"

metadata = Field(ObjectMeta)
data = Field(dict)
31 changes: 31 additions & 0 deletions tests/k8s/conftest.py
Expand Up @@ -4,6 +4,7 @@
import logging

import pytest
import mock

from k8s import config

Expand All @@ -27,3 +28,33 @@ def k8s_config(monkeypatch):
monkeypatch.setattr(config, "api_server", "https://10.0.0.1")
monkeypatch.setattr(config, "api_token", "password")
monkeypatch.setattr(config, "verify_ssl", False)


@pytest.fixture
def post():
with mock.patch('k8s.client.Client.post') as m:
yield m


@pytest.fixture
def put():
with mock.patch('k8s.client.Client.put') as m:
yield m


@pytest.fixture
def get():
with mock.patch('k8s.client.Client.get') as m:
yield m


@pytest.fixture
def delete():
with mock.patch('k8s.client.Client.delete') as m:
yield m


@pytest.fixture
def api_get():
with mock.patch('k8s.base.ApiMixIn.get') as m:
yield m
80 changes: 80 additions & 0 deletions tests/k8s/test_configmap.py
@@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding: utf-8

import mock
import pytest

from k8s.client import NotFound
from k8s.models.common import ObjectMeta
from k8s.models.configmap import ConfigMap

NAME = "my-name"
NAMESPACE = "my-namespace"


@pytest.mark.usefixtures("k8s_config")
class TestConfigMap(object):
def test_created_if_not_exists(self, post, api_get):
api_get.side_effect = NotFound()
configmap = _create_default_configmap()
call_params = configmap.as_dict()

assert configmap._new
configmap.save()
assert not configmap._new

pytest.helpers.assert_any_call(post, _uri(NAMESPACE), call_params)

def test_updated_if_exists(self, get, put):
mock_response = _create_mock_response()
get.return_value = mock_response
configmap = _create_default_configmap()

from_api = ConfigMap.get_or_create(metadata=configmap.metadata, data=configmap.data)
assert not from_api._new
assert from_api.data == {"foo": "bar"}

from_api.data = {"baz": "quux"}
call_params = from_api.as_dict()

from_api.save()
pytest.helpers.assert_any_call(put, _uri(NAMESPACE, NAME), call_params)

def test_deleted(self, delete):
ConfigMap.delete(NAME, namespace=NAMESPACE)
pytest.helpers.assert_any_call(delete, _uri(NAMESPACE, NAME))


def _create_mock_response():
mock_response = mock.Mock()
mock_response.json.return_value = {
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"creationTimestamp": "2017-09-08T13:37:00Z",
"generation": 1,
"labels": {
"test": "true"
},
"name": NAME,
"namespace": NAMESPACE,
"resourceVersion": "42",
"selfLink": _uri(NAMESPACE, NAME),
"uid": "d8f1ba26-b182-11e6-a364-fa163ea2a9c4"
},
"data": {
"foo": "bar",
},
}
return mock_response


def _create_default_configmap():
object_meta = ObjectMeta(name=NAME, namespace=NAMESPACE, labels={"test": "true"})
data = {"foo": "bar"}
configmap = ConfigMap(metadata=object_meta, data=data)
return configmap


def _uri(namespace, name=""):
return "/api/v1/namespaces/{namespace}/configmaps/{name}".format(name=name, namespace=namespace)
25 changes: 0 additions & 25 deletions tests/k8s/test_deployer.py
Expand Up @@ -17,31 +17,6 @@

@pytest.mark.usefixtures("k8s_config")
class TestDeployer(object):
@pytest.fixture
def post(self):
with mock.patch('k8s.client.Client.post') as m:
yield m

@pytest.fixture
def put(self):
with mock.patch('k8s.client.Client.put') as m:
yield m

@pytest.fixture
def get(self):
with mock.patch('k8s.client.Client.get') as m:
yield m

@pytest.fixture
def delete(self):
with mock.patch('k8s.client.Client.delete') as m:
yield m

@pytest.fixture
def api_get(self):
with mock.patch('k8s.base.ApiMixIn.get') as m:
yield m

def test_create_blank_deployment(self):
object_meta = ObjectMeta(name=NAME, namespace=NAMESPACE)
deployment = Deployment(metadata=object_meta)
Expand Down
20 changes: 0 additions & 20 deletions tests/k8s/test_ingress.py
Expand Up @@ -14,26 +14,6 @@

@pytest.mark.usefixtures("k8s_config")
class TestIngress(object):
@pytest.fixture
def post(self):
with mock.patch('k8s.client.Client.post') as m:
yield m

@pytest.fixture
def put(self):
with mock.patch('k8s.client.Client.put') as m:
yield m

@pytest.fixture
def get(self):
with mock.patch('k8s.client.Client.get') as m:
yield m

@pytest.fixture
def api_get(self):
with mock.patch('k8s.base.ApiMixIn.get') as m:
yield m

def test_create_blank(self):
object_meta = ObjectMeta(name=NAME, namespace=NAMESPACE, labels={"test": "true"})
ingress = Ingress(metadata=object_meta)
Expand Down

0 comments on commit e1edac4

Please sign in to comment.