Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 1 | # Copyright 2017 Mirantis, Inc. |
| 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 | |
| 14 | from devops.helpers import helpers |
| 15 | |
| 16 | from tcp_tests.managers.k8s.base import K8sBaseResource |
| 17 | from tcp_tests.managers.k8s.base import K8sBaseManager |
| 18 | |
| 19 | |
| 20 | class K8sPod(K8sBaseResource): |
| 21 | """docstring for K8sPod""" |
| 22 | |
| 23 | def __repr__(self): |
| 24 | return "<K8sPod: %s>" % self.name |
| 25 | |
| 26 | @property |
| 27 | def name(self): |
| 28 | return self.metadata.name |
| 29 | |
| 30 | @property |
| 31 | def phase(self): |
| 32 | return self.status.phase |
| 33 | |
| 34 | @property |
| 35 | def namespace(self): |
| 36 | return self.metadata.namespace |
| 37 | |
| 38 | def wait_phase(self, phase, timeout=60, interval=5): |
| 39 | """Wait phase of pod_name from namespace while timeout |
| 40 | |
| 41 | :param list or str: phase |
| 42 | :param int: timeout |
| 43 | |
| 44 | :rtype: None |
| 45 | """ |
| 46 | if isinstance(phase, str): |
| 47 | phase = [phase] |
| 48 | |
| 49 | def check(): |
| 50 | self._add_details(self._manager.get(name=self.name, |
| 51 | namespace=self.namespace)) |
| 52 | return self.phase in phase |
| 53 | |
| 54 | helpers.wait(check, timeout=timeout, interval=interval, |
| 55 | timeout_msg='Timeout waiting({timeout}s), pod {pod_name} ' |
| 56 | 'is not in "{phase}" phase'.format( |
| 57 | timeout=timeout, |
| 58 | pod_name=self.name, |
| 59 | phase=phase)) |
| 60 | |
| 61 | def wait_running(self, timeout=60, interval=5): |
| 62 | self.wait_phase(['Running'], timeout=timeout, interval=interval) |
| 63 | |
| 64 | |
| 65 | class K8sPodManager(K8sBaseManager): |
| 66 | """docstring for ClassName""" |
| 67 | |
| 68 | resource_class = K8sPod |
| 69 | |
| 70 | def _get(self, name, namespace=None, **kwargs): |
| 71 | namespace = namespace or self.namespace |
| 72 | return self.api.read_namespaced_pod( |
| 73 | name=name, namespace=namespace, **kwargs) |
| 74 | |
| 75 | def _list(self, namespace=None, **kwargs): |
| 76 | namespace = namespace or self.namespace |
| 77 | return self.api.list_namespaced_pod(namespace=namespace, **kwargs) |
| 78 | |
| 79 | def _create(self, body, namespace=None, **kwargs): |
| 80 | namespace = namespace or self.namespace |
| 81 | return self.api.create_namespaced_pod( |
| 82 | body=body, namespace=namespace, **kwargs) |
| 83 | |
| 84 | def _replace(self, body, name, namespace=None, **kwargs): |
| 85 | namespace = namespace or self.namespace |
| 86 | return self.api.replace_namespaced_pod( |
| 87 | body=body, name=name, namespace=namespace, **kwargs) |
| 88 | |
| 89 | def _delete(self, body, name, namespace=None, **kwargs): |
| 90 | namespace = namespace or self.namespace |
| 91 | # NOTE: the following two lines should be deleted after |
| 92 | # serialization is fixed in python-k8sclient |
| 93 | if isinstance(body, self.resource_class): |
| 94 | body = body.swagger_types |
| 95 | return self.api.delete_namespaced_pod( |
| 96 | body=body, name=name, namespace=namespace, **kwargs) |
| 97 | |
| 98 | def _deletecollection(self, namespace=None, **kwargs): |
| 99 | namespace = namespace or self.namespace |
| 100 | return self.api.deletecollection_namespaced_pod( |
| 101 | namespace=namespace, **kwargs) |
| 102 | |
| 103 | def full_list(self, *args, **kwargs): |
| 104 | lst = self._full_list(*args, **kwargs) |
| 105 | return [self.resource_class(self, item) for item in lst.items] |
| 106 | |
| 107 | def _full_list(self, **kwargs): |
| 108 | return self.api.list_pod(**kwargs) |