Skip to content

Commit

Permalink
Add test for configmap
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindio committed Sep 8, 2017
1 parent 52fa5b3 commit 271dca1
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tests/k8s/test_configmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/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 TestIngress(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)

0 comments on commit 271dca1

Please sign in to comment.