-
Notifications
You must be signed in to change notification settings - Fork 24
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 #22 from fiaas/configmap-and-secret-improvements
Add support for envFrom and optional configmaps and secrets
- Loading branch information
Showing
6 changed files
with
141 additions
and
134 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
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 |
---|---|---|
@@ -1,45 +1,129 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from pprint import pformat | ||
|
||
import mock | ||
import pytest | ||
from k8s.models.common import ObjectMeta | ||
from k8s.models.pod import Pod, ContainerPort, Container, LocalObjectReference, PodSpec, Volume, VolumeMount, SecretVolumeSource | ||
from util import get_vcr | ||
|
||
vcr = get_vcr(__file__) | ||
from k8s.client import NotFound | ||
from k8s.models.common import ObjectMeta | ||
from k8s.models.pod import Pod, ContainerPort, Container, LocalObjectReference, PodSpec, Volume, VolumeMount, \ | ||
SecretVolumeSource | ||
|
||
NAME = "my-name" | ||
NAMESPACE = "my-namespace" | ||
POD_URI = Pod._meta.url_template.format(name="", namespace=NAMESPACE) | ||
|
||
|
||
@pytest.mark.usefixtures("logger", "k8s_config") | ||
class TestPod(object): | ||
@vcr.use_cassette() | ||
def teardown(self): | ||
for name, namespace in ((NAME, "default"), (NAME, NAMESPACE)): | ||
try: | ||
Pod.delete(name, namespace) | ||
except: | ||
pass | ||
|
||
@vcr.use_cassette() | ||
def test_lifecycle(self, logger): | ||
object_meta = ObjectMeta(name=NAME, namespace=NAMESPACE, labels={"test": "true", "app": NAME}) | ||
container_port = ContainerPort(name="http5000", containerPort=5000) | ||
secrets_volume_mounts = [VolumeMount(name=NAME, readOnly=True, mountPath="/var/run/secrets/kubernetes.io/kubernetes-secrets")] | ||
secret_volumes = [Volume(name=NAME, secret=SecretVolumeSource(secretName=NAME))] | ||
container = Container(name="container", image="dummy_image", ports=[container_port], volumeMounts=secrets_volume_mounts) | ||
image_pull_secret = LocalObjectReference(name="image_pull_secret") | ||
pod_spec = PodSpec(containers=[container], imagePullSecrets=[image_pull_secret], | ||
volumes=secret_volumes, serviceAccountName="default") | ||
first = Pod(metadata=object_meta, spec=pod_spec) | ||
logger.debug(pformat(first.as_dict())) | ||
first.save() | ||
|
||
pods = Pod.find(NAME, NAMESPACE) | ||
assert len(pods) == 1 | ||
second = pods[0] | ||
assert first.metadata.name == second.metadata.name | ||
assert first.metadata.namespace == second.metadata.namespace | ||
def test_create_blank_pod(self): | ||
pod = _create_pod() | ||
assert pod.metadata.name == NAME | ||
assert pod.as_dict()["metadata"]["name"] == NAME | ||
|
||
def test_pod_created_if_not_exists(self, post, api_get): | ||
api_get.side_effect = NotFound() | ||
pod = _create_pod() | ||
call_params = pod.as_dict() | ||
assert pod._new | ||
pod.save() | ||
assert not pod._new | ||
pytest.helpers.assert_any_call(post, POD_URI, call_params) | ||
|
||
def test_get_or_create_pod_not_new(self, put, get): | ||
mock_response = mock.Mock() | ||
mock_response.json.return_value = { | ||
'apiVersion': 'v1', | ||
'kind': 'Pod', | ||
'metadata': { | ||
'creationTimestamp': '2017-10-03T10:36:20Z', | ||
'labels': { | ||
'app': 'my-name', 'test': 'true' | ||
}, | ||
'name': 'my-name', | ||
'namespace': 'my-namespace', | ||
'resourceVersion': '852', | ||
'selfLink': '/api/v1/namespaces/my-namespace/pods/my-name', | ||
'uid': 'b1e35ab5-a826-11e7-ba76-0800273598c9' | ||
}, | ||
'spec': { | ||
'containers': [{ | ||
'image': 'dummy_image', | ||
'imagePullPolicy': 'IfNotPresent', | ||
'name': 'container', | ||
'ports': [{ | ||
'containerPort': 5000, | ||
'name': 'http5000', | ||
'protocol': 'TCP' | ||
}], | ||
'resources': {}, | ||
'volumeMounts': [{ | ||
'mountPath': '/var/run/secrets/kubernetes.io/kubernetes-secrets', | ||
'name': 'my-name', | ||
'readOnly': True | ||
}, { | ||
'mountPath': '/var/run/secrets/kubernetes.io/serviceaccount', | ||
'name': 'default-token-0g73b', | ||
'readOnly': True | ||
}] | ||
}], | ||
'imagePullSecrets': [{ | ||
'name': 'image_pull_secret' | ||
}], | ||
'restartPolicy': 'Always', | ||
'serviceAccount': 'default', | ||
'serviceAccountName': 'default', | ||
'terminationGracePeriodSeconds': 30, | ||
'volumes': [{ | ||
'name': 'my-name', | ||
'secret': { | ||
'defaultMode': 420, | ||
'secretName': 'my-name' | ||
}}, { | ||
'name': 'default-token-0g73b', | ||
'secret': { | ||
'defaultMode': 420, | ||
'secretName': 'default-token-0g73b' | ||
}}] | ||
}, | ||
'status': { | ||
'conditions': [{ | ||
'lastProbeTime': None, | ||
'lastTransitionTime': '2017-10-03T10:36:20Z', | ||
'status': 'True', | ||
'type': 'PodScheduled' | ||
}], | ||
'phase': 'Pending', | ||
'qosClass': 'BestEffort' | ||
} | ||
} | ||
get.return_value = mock_response | ||
pod = Pod.get(name=NAME, namespace=NAMESPACE) | ||
assert not pod._new | ||
assert pod.metadata.name == NAME | ||
assert pod.spec.containers[0].ports[0].name == "http5000" | ||
call_params = pod.as_dict() | ||
pod.save() | ||
pytest.helpers.assert_any_call(put, POD_URI + NAME, call_params) | ||
|
||
def test_pod_deleted(self, delete): | ||
Pod.delete(NAME, NAMESPACE) | ||
|
||
# call delete with service_name | ||
pytest.helpers.assert_any_call(delete, POD_URI + NAME) | ||
|
||
|
||
def _create_pod(): | ||
object_meta = ObjectMeta(name=NAME, namespace=NAMESPACE, labels={"test": "true", "app": NAME}) | ||
container_port = ContainerPort(name="http5000", containerPort=5000) | ||
secrets_volume_mounts = [ | ||
VolumeMount(name=NAME, readOnly=True, mountPath="/var/run/secrets/kubernetes.io/kubernetes-secrets")] | ||
secret_volumes = [Volume(name=NAME, secret=SecretVolumeSource(secretName=NAME))] | ||
container = Container(name="container", image="dummy_image", ports=[container_port], | ||
volumeMounts=secrets_volume_mounts) | ||
image_pull_secret = LocalObjectReference(name="image_pull_secret") | ||
pod_spec = PodSpec(containers=[container], imagePullSecrets=[image_pull_secret], | ||
volumes=secret_volumes, serviceAccountName="default") | ||
first = Pod(metadata=object_meta, spec=pod_spec) | ||
return first |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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