Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from fiaas/add-configmap-model
Add ConfigMap model
- Loading branch information
Showing
5 changed files
with
124 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters