vrovachev | bc2f5ce | 2017-05-22 19:37:24 +0400 | [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 | # under the License. |
| 14 | |
vrovachev | 99228d3 | 2017-06-08 19:46:10 +0400 | [diff] [blame] | 15 | from uuid import uuid4 |
| 16 | |
| 17 | from tcp_tests.helpers import ext |
vrovachev | bc2f5ce | 2017-05-22 19:37:24 +0400 | [diff] [blame] | 18 | from tcp_tests.managers.execute_commands import ExecuteCommandsMixin |
| 19 | |
vrovachev | 99228d3 | 2017-06-08 19:46:10 +0400 | [diff] [blame] | 20 | from devops.helpers import helpers |
| 21 | |
vrovachev | bc2f5ce | 2017-05-22 19:37:24 +0400 | [diff] [blame] | 22 | |
| 23 | class VirtletManager(ExecuteCommandsMixin): |
| 24 | """docstring for VirtletManager""" |
| 25 | |
| 26 | __config = None |
| 27 | __underlay = None |
| 28 | |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 29 | def __init__(self, config, underlay): |
vrovachev | bc2f5ce | 2017-05-22 19:37:24 +0400 | [diff] [blame] | 30 | self.__config = config |
| 31 | self.__underlay = underlay |
vrovachev | 99228d3 | 2017-06-08 19:46:10 +0400 | [diff] [blame] | 32 | self.virtlet_nodes = [ |
| 33 | i for i in self.__config.underlay.ssh |
vrovachev | 8a882d8 | 2017-06-21 12:56:19 +0400 | [diff] [blame^] | 34 | if ext.UNDERLAY_NODE_ROLES.k8s_virtlet in i['roles']] |
vrovachev | bc2f5ce | 2017-05-22 19:37:24 +0400 | [diff] [blame] | 35 | super(VirtletManager, self).__init__( |
| 36 | config=config, underlay=underlay) |
| 37 | |
| 38 | def install(self, commands): |
| 39 | self.execute_commands(commands, |
| 40 | label='Install Virtlet project') |
| 41 | self.__config.virtlet.virtlet_installed = True |
vrovachev | 99228d3 | 2017-06-08 19:46:10 +0400 | [diff] [blame] | 42 | |
| 43 | def run_vm(self, name=None): |
| 44 | if not name: |
| 45 | name = 'virtlet-vm-{}'.format(uuid4()) |
| 46 | virt_node = self.virtlet_nodes[0] |
| 47 | cmd = ( |
| 48 | "kubectl convert -f virtlet/examples/cirros-vm.yaml --local " |
| 49 | "-o json | jq '.metadata.name|=\"{}\"' | kubectl create -f -") |
| 50 | self.__underlay.check_call( |
| 51 | cmd.format(name), |
| 52 | node_name=virt_node['node_name']) |
| 53 | return name |
| 54 | |
| 55 | def get_vm_info(self, name, jsonpath="{.status.phase}", expected=None): |
| 56 | virt_node = self.virtlet_nodes[0] |
| 57 | cmd = "kubectl get po {} -n default".format(name) |
| 58 | if jsonpath: |
| 59 | cmd += " -o jsonpath={}".format(jsonpath) |
| 60 | return self.__underlay.check_call( |
| 61 | cmd, node_name=virt_node['node_name'], expected=expected) |
| 62 | |
| 63 | def wait_active_state(self, name, timeout=180): |
| 64 | helpers.wait( |
| 65 | lambda: self.get_vm_info(name)['stdout'][0] == 'Running', |
| 66 | timeout=timeout, |
| 67 | timeout_msg="VM {} didn't Running state in {} sec. " |
| 68 | "Current state: ".format( |
| 69 | name, timeout, self.get_vm_info(name)['stdout'][0])) |
| 70 | |
| 71 | def delete_vm(self, name, timeout=180): |
| 72 | virt_node = self.virtlet_nodes[0] |
| 73 | cmd = "kubectl delete po -n default {}".format(name) |
| 74 | self.__underlay.check_call(cmd, node_name=virt_node['node_name']) |
| 75 | |
| 76 | helpers.wait( |
| 77 | lambda: |
| 78 | "Error from server (NotFound):" in |
| 79 | " ".join(self.get_vm_info(name, expected=[0, 1])['stderr']), |
| 80 | timeout=timeout, |
| 81 | timeout_msg="VM {} didn't Running state in {} sec. " |
| 82 | "Current state: ".format( |
| 83 | name, timeout, self.get_vm_info(name)['stdout'][0])) |