Skip to content

Commit 5237722

Browse files
committed
adjustable namespace, create e2e tests
1 parent 1eb578d commit 5237722

File tree

7 files changed

+104
-4
lines changed

7 files changed

+104
-4
lines changed

examples/create_deployment_from_yaml.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
import yaml
1818

19-
from kubernetes import utils, config
19+
from kubernetes import client, utils, config
2020

2121

2222
def main():
2323
# Configs can be set in Configuration class directly or using helper
2424
# utility. If no argument provided, the config will be loaded from
2525
# default location.
2626
config.load_kube_config()
27-
k8s_client = utils.create_from_yaml("./nginx-deployment.yaml")
27+
k8s_client = utils.create_from_yaml("nginx-deployment.yaml")
28+
k8s_client
2829

2930
if __name__ == '__main__':
3031
main()

kubernetes/e2e_test/test_utils.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
import unittest
16+
17+
from kubernetes import utils, client
18+
from kubernetes.e2e_test import base
19+
20+
class TestUtils(unittest.TestCase):
21+
22+
@classmethod
23+
def setUpClass(cls):
24+
cls.config = base.get_e2e_configuration()
25+
26+
def test_app_yaml(self):
27+
k8s_client = utils.create_from_yaml("test_yaml/app.yaml")
28+
self.assertEqual("apps/v1",
29+
k8s_client.get_api_resources().group_version)
30+
deployments = k8s_client.get_namespaced_deployment(
31+
namespace="default").items
32+
deployment_name = deployments[0].metadata.name
33+
self.assertEqual("nginx-deployment-app", deployment_name)
34+
resp = k8s_client.delete_namespaced_deployment(
35+
name=deployment_name, namespace="default",
36+
body=client.V1DeleteOptions(
37+
propagation_policy="Foreground",
38+
grace_period_seconds=1))
39+
40+
def test_extension_yaml(self):
41+
k8s_client = utils.create_from_yaml("test_yaml/extension.yaml")
42+
self.assertEqual("extensions/v1beta1",
43+
k8s_client.get_api_resources().group_version)
44+
deployments = k8s_client.get_namespaced_deployment(
45+
namespace="default").items
46+
deployment_name = deployments[0].metadata.name
47+
self.assertEqual("nginx-deployment", deployment_name)
48+
resp = k8s_client.delete_namespaced_deployment(
49+
name=deployment_name, namespace="default",
50+
body=client.V1DeleteOptions(
51+
propagation_policy="Foreground",
52+
grace_period_seconds=1))
53+
54+
def test_core_pod_yaml(self):
55+
k8s_client = utils.create_from_yaml("test_yaml/core-pod.yaml")
56+
self.assertEqual("v1",
57+
k8s_client.get_api_resources().group_version)
58+
pods = k8s_client.list_namespaced_pod(namespace="default").items
59+
pod_name = pods[0].metadata.name
60+
self.assertEqual("myapp-pod", pod_name)
61+
resp = k8s_client.delete_namespaced_pod(
62+
name=pod_name, namespace="default",
63+
body=client.V1DeleteOptions(
64+
propagation_policy="Foreground",
65+
grace_period_seconds=1))
66+
67+
def test_core_service_yaml(self):
68+
k8s_client = utils.create_from_yaml("test_yaml/core-service.yaml")
69+
self.assertEqual("v1",
70+
k8s_client.get_api_resources().group_version)
71+
svcs = k8s_client.list_namespaced_service(namespace="default").items
72+
svc_name = svcs[1].metadata.name
73+
self.assertEqual("my-service", svc_name)
74+
resp = k8s_client.delete_namespaced_pod(
75+
name=svc_name, namespace="default",
76+
body=client.V1DeleteOptions(
77+
propagation_policy="Foreground",
78+
grace_period_seconds=1))

examples/nginx-deployment-app.yaml renamed to kubernetes/e2e_test/test_yaml/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: apps/v1
22
kind: Deployment
33
metadata:
4-
name: nginx-deployment
4+
name: nginx-deployment-app
55
labels:
66
app: nginx
77
spec:
File renamed without changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment
5+
spec:
6+
replicas: 3
7+
template:
8+
metadata:
9+
labels:
10+
app: nginx
11+
spec:
12+
containers:
13+
- name: nginx
14+
image: nginx:1.7.9
15+
ports:
16+
- containerPort: 80
17+

kubernetes/utils/create_from_yaml.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ def create_from_yaml(yaml_file):
2929
k8s_client = getattr(client, "%s%sApi" % (
3030
api_type.capitalize(), api_version.capitalize()))()
3131
action_type = dep["kind"]
32+
if "namespace" in dep["metadata"]:
33+
dep_namespace = dep["metadata"]["namespace"]
34+
else:
35+
dep_namespace = "default"
3236
resp = getattr(k8s_client, "create_namespaced_%s" % action_type.lower())(
33-
body=dep, namespace="default")
37+
body=dep, namespace=dep_namespace)
3438
print("Deployment created. status='%s'" % str(resp.status))
3539
return k8s_client
3640

0 commit comments

Comments
 (0)