blob: 717ba760d8d2045547a3fdef1fdc312706c009b2 [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
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]))