|
| 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) |
0 commit comments