blob: f2a05edac30833a7c7ef4830a51a4fb11a4b0b9d [file] [log] [blame]
vrovachevbc2f5ce2017-05-22 19:37:24 +04001# 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
vrovachev99228d32017-06-08 19:46:10 +040015from uuid import uuid4
16
17from tcp_tests.helpers import ext
vrovachevbc2f5ce2017-05-22 19:37:24 +040018from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
19
vrovachev99228d32017-06-08 19:46:10 +040020from devops.helpers import helpers
21
vrovachevbc2f5ce2017-05-22 19:37:24 +040022
23class VirtletManager(ExecuteCommandsMixin):
24 """docstring for VirtletManager"""
25
26 __config = None
27 __underlay = None
28
Artem Panchenko0594cd72017-06-12 13:25:26 +030029 def __init__(self, config, underlay):
vrovachevbc2f5ce2017-05-22 19:37:24 +040030 self.__config = config
31 self.__underlay = underlay
vrovachev99228d32017-06-08 19:46:10 +040032 self.virtlet_nodes = [
33 i for i in self.__config.underlay.ssh
vrovachev8a882d82017-06-21 12:56:19 +040034 if ext.UNDERLAY_NODE_ROLES.k8s_virtlet in i['roles']]
vrovachevbc2f5ce2017-05-22 19:37:24 +040035 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
vrovachev99228d32017-06-08 19:46:10 +040042
Victor Ryzhenkin42cfc312017-06-13 12:51:39 +040043 def run_vm(self, name=None, yaml_path='virtlet/examples/cirros-vm.yaml'):
vrovachev99228d32017-06-08 19:46:10 +040044 if not name:
45 name = 'virtlet-vm-{}'.format(uuid4())
46 virt_node = self.virtlet_nodes[0]
47 cmd = (
Victor Ryzhenkin42cfc312017-06-13 12:51:39 +040048 "kubectl convert -f {0} --local "
49 "-o json | jq '.metadata.name|=\"{1}\"' | kubectl create -f -")
vrovachev99228d32017-06-08 19:46:10 +040050 self.__underlay.check_call(
Victor Ryzhenkin42cfc312017-06-13 12:51:39 +040051 cmd.format(name, yaml_path),
vrovachev99228d32017-06-08 19:46:10 +040052 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 Ryzhenkin42cfc312017-06-13 12:51:39 +040084
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]
Victor Ryzhenkin6609aaf2017-06-21 13:09:56 +0400100 cmd = ("~/virtlet/examples/virsh.sh list --name | "
101 "grep -i {0} ".format(vm_name))
Victor Ryzhenkin42cfc312017-06-13 12:51:39 +0400102 result = self.__underlay.check_call(cmd,
103 node_name=virt_node['node_name'])
104 return result['stdout'].strip()
105
106 def get_vm_cpu_count(self, domain_name):
107 virt_node = self.virtlet_nodes[0]
Victor Ryzhenkin6609aaf2017-06-21 13:09:56 +0400108 cmd = ("~/virtlet/examples/virsh.sh dumpxml {0} | "
Victor Ryzhenkin42cfc312017-06-13 12:51:39 +0400109 "grep 'cpu' | grep -o '[[:digit:]]*'".format(domain_name))
110 result = self.__underlay.check_call(cmd,
111 node_name=virt_node['node_name'])
112 return int(result['stdout'].strip())
113
114 def get_vm_memory_count(self, domain_name):
115 virt_node = self.virtlet_nodes[0]
Victor Ryzhenkin6609aaf2017-06-21 13:09:56 +0400116 cmd = ("~/virtlet/examples/virsh.sh dumpxml {0} | "
Victor Ryzhenkin42cfc312017-06-13 12:51:39 +0400117 "grep 'memory unit' | "
118 "grep -o '[[:digit:]]*'".format(domain_name))
119 result = self.__underlay.check_call(cmd,
120 node_name=virt_node['node_name'])
121 return int(result['stdout'].strip())
Victor Ryzhenkin6609aaf2017-06-21 13:09:56 +0400122
123 def get_domain_id(self, domain_name):
124 virt_node = self.virtlet_nodes[0]
125 cmd = ("virsh dumpxml {} | grep id=\' | "
126 "grep -o [[:digit:]]*".format(domain_name))
127 result = self.__underlay.check_call(cmd,
128 node_name=virt_node['node_name'])
129 return int(result['stdout'].strip())
130
131 def list_vm_volumes(self, domain_name):
132 virt_node = self.virtlet_nodes[0]
133 domain_id = self.get_domain_id(domain_name)
134 cmd = ("~/virtlet/examples/virsh.sh domblklist {} | "
135 "tail -n +3 | awk {{'print $2'}}".format(domain_id))
136 result = self.__underlay.check_call(cmd,
137 node_name=virt_node['node_name'])
138 return result['stdout'].strip()