PyTest, test to cover VM creation a ovs nets

	- test_osm_01_create_vm -> add 2 host and create one vm with dhcp
	- test_osm_02_create_2_vm_ping_btw -> add 2 host and create two vm with dhcp and check ping between both vm.
	- test_osm_03_test_service_openvim -> restart a service and check net status

	- The test regresion launch the repo service and use the openvim CLI client available in the repo.

Change-Id: I3cb3c047fbd871f6d3a0c152dc6643a3248835d6
Signed-off-by: mirabal <leonardo.mirabal@altran.com>
diff --git a/test/lib/__init__.py b/test/lib/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/lib/__init__.py
diff --git a/test/lib/config_parser.py b/test/lib/config_parser.py
new file mode 100644
index 0000000..1981fce
--- /dev/null
+++ b/test/lib/config_parser.py
@@ -0,0 +1,15 @@
+import yaml
+
+
+def get_config(file):
+    """
+    Parse test config file
+    :param file: 
+    :return: 
+    """
+    with open(file, 'r') as stream:
+        try:
+            return yaml.load(stream)
+        except yaml.YAMLError as exc:
+            print(exc)
+
diff --git a/test/lib/ssh.py b/test/lib/ssh.py
new file mode 100644
index 0000000..5480483
--- /dev/null
+++ b/test/lib/ssh.py
@@ -0,0 +1,126 @@
+import subprocess
+import pexpect
+
+
+def execute_local(cmd):
+    """
+    Execute a command in locally    
+    :param cmd: command to be executed
+    :return: 
+    """
+
+    print "execute_local cmd = {}".format(cmd)
+    p = subprocess.Popen(['bash', "-c", cmd], stdout=subprocess.PIPE)
+    out = p.stdout.read()
+    print "execute_local result = {}".format(out)
+    return out
+
+
+def execute_namespace(vlan, cmd):
+    """
+    Execute a command inside a namespace created by openvim 
+    :param vlan: namespace id
+    :param cmd: command to be executed
+    :return: 
+    """
+
+    n_cmd = 'sudo ip netns exec {}-qrouter {} '.format(vlan, cmd)
+    return execute_local(n_cmd)
+
+
+def ping_ok(vlan_id, ip, retries=200):
+    """
+
+    :param vlan_id: namespace id
+    :param ip: vm ip to be pinged
+    :param retries: retries, by default 200
+    :return: 
+    """
+    print 'waiting for vm to be active vm with ip = {}'.format(ip)
+    for i in xrange(retries):
+        try:
+            subprocess.check_output(['bash', "-c", "sudo ip netns exec {}-qrouter ping -c 1 {}".format(vlan_id, ip)])
+            return True
+        except Exception, e:
+            pass
+    return False
+
+
+def ping_ok_btw_2_vms(vlan_id, ip_1, ip_2, retires=8):
+    """
+    Check net connectivity between to VM
+    :param vlan_id: namepsace id
+    :param ip_1: first vm ip
+    :param ip_2: second vm ip
+    :param retires: 
+    :return: 
+    """
+    for i in xrange(retires):
+
+        try:
+            ns_cmd = 'sudo ip netns exec {}-qrouter '.format(vlan_id)
+            cmd = ns_cmd + ' ssh -oStrictHostKeyChecking=no cirros@{} "ping -c 1 {}"'.format(ip_1, ip_2)
+            child = pexpect.spawn(cmd)
+            child.expect('.*assword*')
+            child.sendline('cubswin:)')
+            child.sendline('cubswin:)')
+
+            cmd = ns_cmd + ' ssh -oStrictHostKeyChecking=no  cirros@{} "ping -c 1 {}"'.format(ip_2, ip_1)
+            child = pexpect.spawn(cmd)
+            child.expect('.*assword*')
+            child.sendline('cubswin:)')
+            child.sendline('cubswin:)')
+
+        except EOFError as e:
+            if i == retires:
+                return False
+            pass
+
+    return True
+
+
+def copy_rsa_keys_into_vm(vlan_id, ip, rsa_key_path):
+    """
+    copy an RSA key given by the user to a vm 
+    :param vlan_id: 
+    :param ip: 
+    :param rsa_key_path: 
+    :return: 
+    """
+
+    try:
+        execute_local('sudo ssh-keygen -f "/root/.ssh/known_hosts" -R  {}'.format(ip))
+        cmd = 'sudo ip netns exec {}-qrouter ssh-copy-id -i {} ' \
+              '-oStrictHostKeyChecking=no -f cirros@{}'.format(vlan_id, rsa_key_path + '.pub', ip)
+
+        print 'copy_rsa_keys_into_vm = ' + cmd
+        child = pexpect.spawn(cmd)
+        child.expect('.*assword*')
+        child.sendline('cubswin:)')
+        child.sendline('cubswin:)')
+        return True
+    except EOFError as e:
+        return False
+
+
+def execute_check_output(vlan_id, cmd):
+    """
+    Execute a command inside a namespace and raise an expection in case of command fail
+    :param vlan_id: namepsace id
+    :param cmd: command
+    :return: 
+    """
+    try:
+        cmd = "sudo ip netns exec {}-qrouter  {}".format(vlan_id, cmd)
+        print "execute_check_output = {}".format(cmd)
+        subprocess.check_output(['bash', "-c", cmd])
+        return True
+    except Exception, e:
+        print "error execute_check_output"
+        return False
+
+
+
+
+
+
diff --git a/test/lib/test_utils.py b/test/lib/test_utils.py
new file mode 100644
index 0000000..bf09899
--- /dev/null
+++ b/test/lib/test_utils.py
@@ -0,0 +1,113 @@
+import yaml
+import re
+import os
+from string import Template
+from ssh import *
+
+
+def save_tmp_yaml(name, data):
+    """
+    Save a yaml file into a file to be declare in openvim
+    :param name: file name
+    :param data: 
+    :return: 
+    """
+    with open(name, "w") as text_file:
+        text_file.write(data)
+
+
+def delete_tmp_yaml(name):
+    """
+    Delete yaml form Filesystem
+    :param name: File name
+    :return: 
+    """
+    execute_local('rm {}'.format(name))
+
+
+def search_host_in_env_var():
+    """
+    Search for OPENVIM_TEST_HOST_X env var declare by pre_create_host fixture with the host id after creation. 
+    :return: All env vars founded
+    """
+    return search('OPENVIM_TEST_HOST_')
+
+
+def template_substitute(file_path, values):
+    """
+    Modify a Yaml template with values.
+    :param file_path: template file
+    :param values: values to be substituted
+    :return: a string with the file content modified
+    """
+    with open(file_path, 'r') as server_yaml:
+        template = Template(server_yaml.read())
+        server_yaml = template.safe_substitute(values)
+        return server_yaml
+
+
+def search(reg_ex):
+    """
+    Search for environment vars. 
+    :param reg_ex: regular expresion to be applied during the search
+    :return: return results
+    """
+    result = {}
+    for key in os.environ:
+        if reg_ex in key:
+            result[key] = os.environ[key]
+    return result
+
+
+def parse_uuid(data):
+    """
+    Parse an UUID value from a string given.
+    :param data: String to be evaluated
+    :return: the uuid value
+    """
+    match = re.compile(r'[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}', re.I).findall(data)
+    if match:
+        data = match[0].replace(' ', '')
+        data = data.replace('\n', '')
+        return data
+    else:
+        return []
+
+
+def get_config(data):
+    """
+    Parse test config file
+    :param data: config file path
+    :return: config dict 
+    """
+    with open(data, 'r') as stream:
+        try:
+            return yaml.load(stream)
+        except yaml.YAMLError as exc:
+            print(exc)
+
+
+def get_vm_ip(vm_id):
+    """
+    Parse vm id IP from openvim client.
+    :param vm_id: vm id
+    :return: IP value
+    """
+    openvim_path = os.path.join(os.environ['OPENVIM_ROOT_FOLDER'], 'openvim')
+    ip = execute_local("{} vm-list {} -vvv | grep ip_address:".format(openvim_path, vm_id))
+    ip = ip.replace('ip_address:', '')
+    ip = ip.replace(' ', '')
+    return ip
+
+
+def get_net_status(net_id):
+    """
+    Parse a net status from openvim client
+    :param net_id: network id
+    :return: 
+    """
+    openvim_path = os.path.join(os.environ['OPENVIM_ROOT_FOLDER'], 'openvim')
+    net_status = execute_local("{} net-list {} -vvv | grep status:".format(openvim_path, net_id))
+    net_status = net_status.replace('status:', '')
+    net_status = net_status.replace(' ', '')
+    return net_status