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 | |
Victor Ryzhenkin | 42cfc31 | 2017-06-13 12:51:39 +0400 | [diff] [blame^] | 43 | def run_vm(self, name=None, yaml_path='virtlet/examples/cirros-vm.yaml'): |
vrovachev | 99228d3 | 2017-06-08 19:46:10 +0400 | [diff] [blame] | 44 | if not name: |
| 45 | name = 'virtlet-vm-{}'.format(uuid4()) |
| 46 | virt_node = self.virtlet_nodes[0] |
| 47 | cmd = ( |
Victor Ryzhenkin | 42cfc31 | 2017-06-13 12:51:39 +0400 | [diff] [blame^] | 48 | "kubectl convert -f {0} --local " |
| 49 | "-o json | jq '.metadata.name|=\"{1}\"' | kubectl create -f -") |
vrovachev | 99228d3 | 2017-06-08 19:46:10 +0400 | [diff] [blame] | 50 | self.__underlay.check_call( |
Victor Ryzhenkin | 42cfc31 | 2017-06-13 12:51:39 +0400 | [diff] [blame^] | 51 | cmd.format(name, yaml_path), |
vrovachev | 99228d3 | 2017-06-08 19:46:10 +0400 | [diff] [blame] | 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])) |
Victor Ryzhenkin | 42cfc31 | 2017-06-13 12:51:39 +0400 | [diff] [blame^] | 84 | |
| 85 | def adjust_cirros_resources( |
| 86 | self, cpu=2, memory='256', |
| 87 | target_yaml='virtlet/examples/cirros-vm-exp.yaml'): |
| 88 | virt_node = self.virtlet_nodes[0] |
| 89 | # We will need to change params in case of example change |
| 90 | cmd = ("cd ~/virtlet/examples && " |
| 91 | "cp cirros-vm.yaml {2} && " |
| 92 | "sed -r 's/^(\s*)(VirtletVCPUCount\s*:\s*\"1\"\s*$)/ " |
| 93 | "\1VirtletVCPUCount: \"{0}\"/' {2} && " |
| 94 | "sed -r 's/^(\s*)(memory\s*:\s*128Mi\s*$)/\1memory: " |
| 95 | "{1}Mi/' {2}".format(cpu, memory, target_yaml)) |
| 96 | self.__underlay.check_call(cmd, node_name=virt_node['node_name']) |
| 97 | |
| 98 | def get_domain_name(self, vm_name): |
| 99 | virt_node = self.virtlet_nodes[0] |
| 100 | cmd = ("cd ~/virtlet/examples && " |
| 101 | "./virsh.sh list | grep -i {0} " |
| 102 | "| awk {{'print $2'}}".format(vm_name)) |
| 103 | result = self.__underlay.check_call(cmd, |
| 104 | node_name=virt_node['node_name']) |
| 105 | return result['stdout'].strip() |
| 106 | |
| 107 | def get_vm_cpu_count(self, domain_name): |
| 108 | virt_node = self.virtlet_nodes[0] |
| 109 | cmd = ("cd ~/virtlet/examples && " |
| 110 | "./virsh.sh dumpxml {0} | " |
| 111 | "grep 'cpu' | grep -o '[[:digit:]]*'".format(domain_name)) |
| 112 | result = self.__underlay.check_call(cmd, |
| 113 | node_name=virt_node['node_name']) |
| 114 | return int(result['stdout'].strip()) |
| 115 | |
| 116 | def get_vm_memory_count(self, domain_name): |
| 117 | virt_node = self.virtlet_nodes[0] |
| 118 | cmd = ("cd ~/virtlet/examples && " |
| 119 | "./virsh.sh dumpxml {0} | " |
| 120 | "grep 'memory unit' | " |
| 121 | "grep -o '[[:digit:]]*'".format(domain_name)) |
| 122 | result = self.__underlay.check_call(cmd, |
| 123 | node_name=virt_node['node_name']) |
| 124 | return int(result['stdout'].strip()) |