diff options
Diffstat (limited to 'sfc/lib')
-rw-r--r-- | sfc/lib/config.py | 37 | ||||
-rw-r--r-- | sfc/lib/utils.py | 154 |
2 files changed, 83 insertions, 108 deletions
diff --git a/sfc/lib/config.py b/sfc/lib/config.py index e9fcd582..f5a07013 100644 --- a/sfc/lib/config.py +++ b/sfc/lib/config.py @@ -34,22 +34,31 @@ class CommonConfig(object): self.functest_results_dir = os.path.join( CONST.dir_results, "odl-sfc") self.config_file = os.path.join(self.sfc_test_dir, "config.yaml") - self.fuel_master_ip = ft_utils.get_parameter_from_yaml( - "defaults.fuel_master_ip", self.config_file) - self.fuel_master_uname = ft_utils.get_parameter_from_yaml( - "defaults.fuel_master_uname", self.config_file) - self.fuel_master_passwd = ft_utils.get_parameter_from_yaml( - "defaults.fuel_master_passwd", self.config_file) - self.fuel_proxy = { - 'ip': self.fuel_master_ip, - 'username': self.fuel_master_uname, - 'password': self.fuel_master_passwd - } + self.installer_type = ft_utils.get_parameter_from_yaml( + "defaults.installer.type", self.config_file) + self.installer_ip = ft_utils.get_parameter_from_yaml( + "defaults.installer.ip", self.config_file) + self.installer_user = ft_utils.get_parameter_from_yaml( + "defaults.installer.user", self.config_file) + + try: + self.installer_password = ft_utils.get_parameter_from_yaml( + "defaults.installer.password", self.config_file) + except: + self.installer_password = None + try: - self.fuel_environment = ft_utils.get_parameter_from_yaml( - "defaults.fuel_environment", self.config_file) + self.installer_key_file = ft_utils.get_parameter_from_yaml( + "defaults.installer.key_file", self.config_file) except: - self.fuel_environment = None + self.installer_key_file = None + + try: + self.installer_cluster = ft_utils.get_parameter_from_yaml( + "defaults.installer.cluster", self.config_file) + except: + self.installer_cluster = None + self.flavor = ft_utils.get_parameter_from_yaml( "defaults.flavor", self.config_file) self.ram_size_in_mb = ft_utils.get_parameter_from_yaml( diff --git a/sfc/lib/utils.py b/sfc/lib/utils.py index 02782765..4289ee0a 100644 --- a/sfc/lib/utils.py +++ b/sfc/lib/utils.py @@ -8,17 +8,16 @@ # http://www.apache.org/licenses/LICENSE-2.0 # -import json import os import re import subprocess import time +import yaml import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils -import opnfv.utils.SSHUtils as ssh_utils -import sfc.lib.config as sfc_config +import functest.utils.openstack_tacker as os_tacker logger = ft_logger.Logger("sfc_test_utils").getLogger() @@ -28,46 +27,23 @@ FUNCTEST_RESULTS_DIR = os.path.join("home", "opnfv", def run_cmd(cmd): - """run given command locally and return commands output if success""" + """ + Run given command locally + Return a tuple with the return code, stdout, and stderr of the command + """ pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (output, errors) = pipe.communicate() + output, errors = pipe.communicate() logger.debug("running [%s] returns: <%s> - %s " "" % (cmd, pipe.returncode, output)) - if output: - output = output.strip() + if pipe.returncode != 0 or len(errors) > 0: logger.error('FAILED to execute {0}'.format(cmd)) logger.error(errors) - return None - - return output - - -def run_cmd_on_controller(cmd): - """run given command on OpenStack controller""" - ip_controllers = get_openstack_node_ips("controller") - if not ip_controllers: - return None - - ssh_cmd = "ssh %s %s %s" % (SSH_OPTIONS, ip_controllers[0], cmd) - return run_cmd_on_fm(ssh_cmd) - -def run_cmd_on_compute(cmd, ip_compute): - """run given command on OpenStack Compute node""" - ssh_cmd = "ssh %s %s %s" % (SSH_OPTIONS, ip_compute, cmd) - return run_cmd_on_fm(ssh_cmd) - - -def run_cmd_on_fm(cmd, username="root", passwd="r00tme"): - """run given command on Fuel Master""" - ip = os.environ.get("INSTALLER_IP") - ssh_cmd = "sshpass -p %s ssh %s %s@%s %s" % ( - passwd, SSH_OPTIONS, username, ip, cmd) - return run_cmd(ssh_cmd) + return pipe.returncode, output.strip(), errors.strip() def run_cmd_remote(ip, cmd, username="root", passwd="opnfv"): @@ -78,25 +54,7 @@ def run_cmd_remote(ip, cmd, username="root", passwd="opnfv"): return run_cmd(ssh_cmd) -def get_openstack_node_ips(role): - """Get OpenStack Nodes IP Address""" - fuel_env = sfc_config.CommonConfig().fuel_environment - if fuel_env is not None: - cmd = "fuel2 node list -f json -e %s" % fuel_env - else: - cmd = "fuel2 node list -f json" - - nodes = run_cmd_on_fm(cmd) - ips = [] - nodes = json.loads(nodes) - for node in nodes: - if role in node["roles"]: - ips.append(node["ip"]) - - return ips - - -def configure_iptables(): +def configure_iptables(controller_nodes): """Configures IPTABLES on OpenStack Controller""" iptable_cmds = ["iptables -P INPUT ACCEPT", "iptables -t nat -P INPUT ACCEPT", @@ -105,7 +63,8 @@ def configure_iptables(): for cmd in iptable_cmds: logger.info("Configuring %s on contoller" % cmd) - run_cmd_on_controller(cmd) + for controller in controller_nodes: + controller.run_cmd(cmd) def download_image(url, image_path): @@ -119,6 +78,22 @@ def download_image(url, image_path): logger.info("Using old image") +def create_vnf_in_av_zone(tacker_client, vnf_name, vnfd_name, av_zone=None): + param_file = os.path.join(os.getcwd(), + 'vnfd-templates', + 'test-vnfd-default-params.yaml') + if av_zone is not None: + param_file = os.path.join('/tmp', 'param_{0}.yaml'.format(av_zone)) + data = {'zone': av_zone} + with open(param_file) as f: + yaml.dump(data, f) + + os_tacker.create_vnf(tacker_client, + vnf_name, + vnfd_name=vnfd_name, + param_file=param_file) + + def setup_neutron(neutron_client, net, subnet, router, subnet_cidr): n_dict = os_utils.create_network_full(neutron_client, net, @@ -207,7 +182,7 @@ def ping(remote, pkt_cnt=1, iface=None, retries=100, timeout=None): cmd = ping_cmd + '|' + grep_cmd while retries > 0: - output = run_cmd(cmd) + _, output, _ = run_cmd(cmd) if not output: return False @@ -257,7 +232,7 @@ def start_http_server(ip): cmd = "\'python -m SimpleHTTPServer 80" cmd = cmd + " > /dev/null 2>&1 &\'" run_cmd_remote(ip, cmd) - output = run_cmd_remote(ip, "ps aux|grep SimpleHTTPServer") + _, output, _ = run_cmd_remote(ip, "ps aux | grep SimpleHTTPServer") if not output: logger.error("Failed to start http server") return False @@ -281,32 +256,28 @@ def vxlan_tool_stop(sf): run_cmd_remote(sf, cmd) -def netcat(s_ip, c_ip, port="80", timeout=5): - """Run netcat on a give machine, Can be VM""" - cmd = "nc -zv " - cmd = cmd + " -w %s %s %s" % (timeout, s_ip, port) - cmd = cmd + " 2>&1" - output = run_cmd_remote(c_ip, cmd) +def netcat(source_ip, destination_ip, port, timeout=5): + """ + SSH into source_ip, and check the connectivity from there to destination_ip + on the specified port, using the netcat command. + Returns 0 on successful execution, != 0 on failure + """ + cmd = "nc -zv -w %s %s %s 2>&1" % (timeout, destination_ip, port) + rc, output, _ = run_cmd_remote(source_ip, cmd) + logger.info("Running netcat from [%s] - connecting to [%s] on port [%s]" % + (source_ip, destination_ip, port)) logger.info("%s" % output) - return output + return rc -def is_ssh_blocked(srv_prv_ip, client_ip): - res = netcat(srv_prv_ip, client_ip, port="22") - match = re.search("nc:.*timed out:.*", res, re.M) - if match: - return True +def is_ssh_blocked(source_ip, destination_ip): + rc = netcat(source_ip, destination_ip, port="22") + return rc != 0 - return False - - -def is_http_blocked(srv_prv_ip, client_ip): - res = netcat(srv_prv_ip, client_ip, port="80") - match = re.search(".* 80 port.* succeeded!", res, re.M) - if match: - return False - return True +def is_http_blocked(source_ip, destination_ip): + rc = netcat(source_ip, destination_ip, port="80") + return rc != 0 def capture_ovs_logs(ovs_logger, controller_clients, compute_clients, error): @@ -317,13 +288,8 @@ def capture_ovs_logs(ovs_logger, controller_clients, compute_clients, error): timestamp=timestamp) -def get_ssh_clients(role, proxy): - clients = [] - for ip in get_openstack_node_ips(role): - s_client = ssh_utils.get_ssh_client(ip, 'root', proxy=proxy) - clients.append(s_client) - - return clients +def get_ssh_clients(nodes): + return [n.ssh_client for n in nodes] def check_ssh(ips, retries=100): @@ -332,7 +298,8 @@ def check_ssh(ips, retries=100): logger.info("Checking SSH connectivity to the SFs with ips %s" % str(ips)) while retries and not all(check): for index, ip in enumerate(ips): - check[index] = run_cmd_remote(ip, "exit") + rc, _, _ = run_cmd_remote(ip, "exit") + check[index] = True if rc == 0 else False if all(check): logger.info("SSH connectivity to the SFs established") @@ -396,15 +363,14 @@ def wait_for_classification_rules(ovs_logger, compute_clients, timeout=200): logger.info("classification rules updated") -def setup_compute_node(cidr): +def setup_compute_node(cidr, compute_nodes): logger.info("bringing up br-int iface") - # TODO: Get only the compute nodes which belong to the env. - ip_computes = get_openstack_node_ips("compute") - for ip_compute in ip_computes: - run_cmd_on_compute("ifconfig br-int up", ip_compute) - if not run_cmd_on_compute( - "ip route|grep -o %s || true" % cidr, ip_compute): - logger.info("adding route %s in %s" % (cidr, ip_compute)) - run_cmd_on_compute("ip route add %s dev br-int" % cidr, ip_compute) + grep_cidr_routes = ("ip route | grep -o {0} || true".format(cidr)).strip() + add_cidr = "ip route add {0} dev br-int".format(cidr) + for compute in compute_nodes: + compute.run_cmd("ifconfig br-int up") + if not compute.run_cmd(grep_cidr_routes): + logger.info("adding route %s in %s" % (cidr, compute.ip)) + compute.run_cmd(add_cidr) else: - logger.info("route %s exists" % cidr) + logger.info("route %s already exists" % cidr) |