3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 """vm controll module"""
17 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
18 from functest.opnfv_tests.vnf.router.vnf_controller.command_generator import (
20 from functest.opnfv_tests.vnf.router.vnf_controller.ssh_client import (
24 class VmController(object):
25 """vm controll class"""
27 logger = logging.getLogger(__name__)
29 def __init__(self, util_info):
30 self.logger.debug("initialize vm controller")
31 self.command_gen = CommandGenerator()
32 credentials = util_info["credentials"]
35 self.util.set_credentials(credentials["username"],
36 credentials["password"],
37 credentials["auth_url"],
38 credentials["tenant_name"])
40 with open(self.util.test_env_config_yaml) as file_fd:
41 test_env_config_yaml = yaml.safe_load(file_fd)
44 self.reboot_wait = test_env_config_yaml.get("general").get(
46 self.command_wait = test_env_config_yaml.get("general").get(
48 self.ssh_connect_timeout = test_env_config_yaml.get("general").get(
49 "ssh_connect_timeout")
50 self.ssh_connect_retry_count = test_env_config_yaml.get("general").get(
51 "ssh_connect_retry_count")
53 def command_gen_from_template(self, command_file_path, cmd_input_param):
54 (command_file_dir, command_file_name) = os.path.split(
56 template = self.command_gen.load_template(command_file_dir,
58 return self.command_gen.command_create(template,
61 def config_vm(self, vm_info, test_cmd_file_path,
62 cmd_input_param, prompt_file_path):
63 ssh = self.connect_ssh_and_config_vm(vm_info,
74 def connect_ssh_and_config_vm(self, vm_info, test_cmd_file_path,
75 cmd_input_param, prompt_file_path):
78 if "key_path" in vm_info:
79 key_filename = vm_info["key_path"]
81 ssh = SshClient(ip_address=vm_info["floating_ip"],
83 password=vm_info["pass"],
84 key_filename=key_filename)
86 result = ssh.connect(self.ssh_connect_timeout,
87 self.ssh_connect_retry_count)
89 self.logger.debug("try to vm reboot.")
90 self.util.reboot_vm(vm_info["vnf_name"])
91 time.sleep(self.reboot_wait)
92 result = ssh.connect(self.ssh_connect_timeout,
93 self.ssh_connect_retry_count)
97 (result, _) = self.command_create_and_execute(
108 def command_create_and_execute(self, ssh, test_cmd_file_path,
109 cmd_input_param, prompt_file_path):
110 prompt_file = open(prompt_file_path,
112 prompt = yaml.safe_load(prompt_file)
114 config_mode_prompt = prompt["config_mode"]
116 commands = self.command_gen_from_template(test_cmd_file_path,
118 return self.command_list_execute(ssh,
122 def command_list_execute(self, ssh, command_list, prompt):
124 for command in command_list:
125 self.logger.debug("Command : " + command)
126 (res, res_data) = self.command_execute(ssh,
129 self.logger.debug("Response : " + res_data)
130 res_data_list.append(res_data)
132 return res, res_data_list
134 time.sleep(self.command_wait)
136 return True, res_data_list
138 def command_execute(self, ssh, command, prompt):
139 res_data = ssh.send(command, prompt)
141 self.logger.info("retry send command : " + command)
142 res_data = ssh.send(command,
144 if not ssh.error_check(res_data):
145 return False, res_data
147 return True, res_data