Skip to content

Commit a36ecd2

Browse files
committed
1 parent 7c3fb18 commit a36ecd2

5 files changed

Lines changed: 327 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import asyncio
14+
import http.client
15+
import os
16+
import unittest
17+
18+
from kubernetes_asyncio.client.configuration import Configuration
19+
from kubernetes_asyncio.config import kube_config
20+
21+
DEFAULT_E2E_HOST = '127.0.0.1'
22+
23+
24+
def get_e2e_configuration():
25+
config = Configuration()
26+
config.host = None
27+
if os.path.exists(
28+
os.path.expanduser(kube_config.KUBE_CONFIG_DEFAULT_LOCATION)):
29+
loop = asyncio.new_event_loop()
30+
asyncio.set_event_loop(loop)
31+
loop.run_until_complete(kube_config.load_kube_config(client_configuration=config))
32+
else:
33+
print('Unable to load config from %s' %
34+
kube_config.KUBE_CONFIG_DEFAULT_LOCATION)
35+
for proto, host, port in [('https', DEFAULT_E2E_HOST, '8443'),
36+
('http', DEFAULT_E2E_HOST, '8080')]:
37+
try:
38+
print('Testing:', proto, host, port)
39+
http.client.HTTPConnection(host, port).request('GET', '/')
40+
config.host = "{}://{}:{}".format(proto, host, port)
41+
config.verify_ssl = False
42+
break
43+
except ConnectionRefusedError:
44+
pass
45+
46+
if config.host is None:
47+
raise unittest.SkipTest('Unable to find a running Kubernetes instance')
48+
print('Running test against : %s' % config.host)
49+
config.assert_hostname = False
50+
return config
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 os
16+
import uuid
17+
from unittest import IsolatedAsyncioTestCase
18+
19+
import yaml
20+
21+
from kubernetes_asyncio import utils
22+
from kubernetes_asyncio.client import api_client
23+
from kubernetes_asyncio.client.api import apps_v1_api
24+
from kubernetes_asyncio.client.models import v1_delete_options
25+
from kubernetes_asyncio.e2e_test import base
26+
27+
28+
class TestClientApi(IsolatedAsyncioTestCase):
29+
30+
@classmethod
31+
def setUpClass(cls):
32+
cls.config = base.get_e2e_configuration()
33+
34+
async def test_create_deployment(self):
35+
client = api_client.ApiClient(configuration=self.config)
36+
api = apps_v1_api.AppsV1Api(client)
37+
name = 'nginx-deployment-' + str(uuid.uuid4())
38+
deployment = '''apiVersion: apps/v1
39+
kind: Deployment
40+
metadata:
41+
name: %s
42+
spec:
43+
replicas: 3
44+
selector:
45+
matchLabels:
46+
app: nginx
47+
template:
48+
metadata:
49+
labels:
50+
app: nginx
51+
spec:
52+
containers:
53+
- name: nginx
54+
image: nginx:1.7.9
55+
ports:
56+
- containerPort: 80
57+
'''
58+
resp = await api.create_namespaced_deployment(
59+
body=yaml.safe_load(deployment % name),
60+
namespace="default")
61+
resp = await api.read_namespaced_deployment(name, 'default')
62+
self.assertIsNotNone(resp)
63+
64+
options = v1_delete_options.V1DeleteOptions()
65+
resp = await api.delete_namespaced_deployment(name, 'default', body=options)
66+
67+
async def test_create_deployment_from_yaml_file(self):
68+
client = api_client.ApiClient(configuration=self.config)
69+
api = apps_v1_api.AppsV1Api(client)
70+
name = 'nginx-deployment-' + str(uuid.uuid4())
71+
tempfile = 'temp.yaml'
72+
deployment = '''apiVersion: apps/v1
73+
kind: Deployment
74+
metadata:
75+
name: %s
76+
spec:
77+
replicas: 3
78+
selector:
79+
matchLabels:
80+
app: nginx
81+
template:
82+
metadata:
83+
labels:
84+
app: nginx
85+
spec:
86+
containers:
87+
- name: nginx
88+
image: nginx:1.7.9
89+
ports:
90+
- containerPort: 80
91+
'''
92+
with open(tempfile, 'w') as f:
93+
f.write(deployment % name)
94+
resp = await utils.create_from_yaml(client, tempfile)
95+
os.remove(tempfile)
96+
resp = await api.read_namespaced_deployment(name, 'default')
97+
self.assertIsNotNone(resp)
98+
99+
options = v1_delete_options.V1DeleteOptions()
100+
resp = await api.delete_namespaced_deployment(name, 'default', body=options)
101+
102+
async def test_create_daemonset(self):
103+
client = api_client.ApiClient(configuration=self.config)
104+
api = apps_v1_api.AppsV1Api(client)
105+
name = 'nginx-app-' + str(uuid.uuid4())
106+
daemonset = {
107+
'apiVersion': 'apps/v1',
108+
'kind': 'DaemonSet',
109+
'metadata': {
110+
'labels': {'app': 'nginx'},
111+
'name': name,
112+
},
113+
'spec': {
114+
'selector': {
115+
'matchLabels': {'app': 'nginx'}
116+
},
117+
'template': {
118+
'metadata': {
119+
'labels': {'app': 'nginx'},
120+
'name': name},
121+
'spec': {
122+
'containers': [
123+
{'name': 'nginx-app',
124+
'image': 'nginx:1.10'},
125+
],
126+
},
127+
},
128+
'updateStrategy': {
129+
'type': 'RollingUpdate',
130+
},
131+
}
132+
}
133+
resp = await api.create_namespaced_daemon_set('default', body=daemonset)
134+
resp = await api.read_namespaced_daemon_set(name, 'default')
135+
self.assertIsNotNone(resp)
136+
137+
options = v1_delete_options.V1DeleteOptions()
138+
resp = await api.delete_namespaced_daemon_set(name, 'default', body=options)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 uuid
16+
from unittest import IsolatedAsyncioTestCase
17+
18+
from kubernetes_asyncio.client import api_client
19+
from kubernetes_asyncio.client.api import core_v1_api
20+
from kubernetes_asyncio.e2e_test import base
21+
22+
23+
class TestApplyPatch(IsolatedAsyncioTestCase):
24+
25+
@classmethod
26+
def setUpClass(cls):
27+
cls.config = base.get_e2e_configuration()
28+
29+
async def test_apply_patch(self):
30+
client = api_client.ApiClient(configuration=self.config)
31+
api = core_v1_api.CoreV1Api(client)
32+
33+
name = "cm-test" + str(uuid.uuid4())
34+
manifest = dict(
35+
apiVersion="v1",
36+
kind="ConfigMap",
37+
metadata=dict(
38+
namespace="default",
39+
name=name,
40+
),
41+
data={"hello": "world!"},
42+
)
43+
44+
resp = await api.patch_namespaced_config_map(
45+
_content_type="application/apply-patch+yaml",
46+
field_manager="test",
47+
body=manifest,
48+
name=name,
49+
namespace="default",
50+
)
51+
self.assertEqual(name, resp.metadata.name)
52+
53+
cm = await api.read_namespaced_config_map(name=name, namespace="default")
54+
self.assertEqual(name, cm.metadata.name)
55+
56+
# strategic merge patch for object
57+
cm.data["new"] = "value"
58+
resp = await api.patch_namespaced_config_map(
59+
field_manager="test",
60+
body=cm,
61+
name=name,
62+
namespace="default",
63+
)
64+
self.assertEqual(resp.data, {'hello': 'world!', 'new': 'value'})
65+
66+
resp = await api.delete_namespaced_config_map(
67+
name=name, body={}, namespace="default"
68+
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 uuid
16+
from unittest import IsolatedAsyncioTestCase
17+
18+
from kubernetes_asyncio.client import api_client
19+
from kubernetes_asyncio.client.api import batch_v1_api
20+
from kubernetes_asyncio.e2e_test import base
21+
22+
23+
class TestClientBatch(IsolatedAsyncioTestCase):
24+
25+
@classmethod
26+
def setUpClass(cls):
27+
cls.config = base.get_e2e_configuration()
28+
29+
async def test_job_apis(self):
30+
client = api_client.ApiClient(configuration=self.config)
31+
api = batch_v1_api.BatchV1Api(client)
32+
33+
name = 'test-job-' + str(uuid.uuid4())
34+
job_manifest = {
35+
'kind': 'Job',
36+
'spec': {
37+
'template':
38+
{'spec':
39+
{'containers': [
40+
{'image': 'busybox',
41+
'name': name,
42+
'command': ["sh", "-c", "sleep 5"]
43+
}],
44+
'restartPolicy': 'Never'},
45+
'metadata': {'name': name}}},
46+
'apiVersion': 'batch/v1',
47+
'metadata': {'name': name}}
48+
49+
resp = await api.create_namespaced_job(
50+
body=job_manifest, namespace='default')
51+
self.assertEqual(name, resp.metadata.name)
52+
53+
resp = await api.read_namespaced_job(
54+
name=name, namespace='default')
55+
self.assertEqual(name, resp.metadata.name)
56+
57+
resp = await api.delete_namespaced_job(
58+
name=name, body={}, namespace='default')

0 commit comments

Comments
 (0)