Skip to content

Commit

Permalink
Merge pull request #59 from grycap/devel
Browse files Browse the repository at this point in the history
Merge Devel
  • Loading branch information
micafer authored Jun 17, 2016
2 parents 34c29c7 + db054d2 commit bfd1b92
Show file tree
Hide file tree
Showing 26 changed files with 252 additions and 67 deletions.
2 changes: 1 addition & 1 deletion etc/im.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ USER_DB =
# Maximum number of simultaneous VM launch/delete operations
# In some old versions of python (prior to 2.7.5 or 3.3.2) it can produce an error
# See https://bugs.python.org/issue10015. In this case set this value to 1
MAX_SIMULTANEOUS_LAUNCHES = 1
MAX_SIMULTANEOUS_LAUNCHES = 5

# Max number of retries launching a VM (always > 0)
MAX_VM_FAILS = 1
Expand Down
185 changes: 185 additions & 0 deletions test/functional/test_im.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#! /usr/bin/env python
#
# IM - Infrastructure Manager
# Copyright (C) 2011 - GRyCAP - Universitat Politecnica de Valencia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import time
import logging
import unittest
import sys

from mock import Mock

sys.path.append("..")
sys.path.append(".")

from IM.config import Config
# To load the ThreadPool class
Config.MAX_SIMULTANEOUS_LAUNCHES = 2

from IM.VirtualMachine import VirtualMachine
from IM.InfrastructureManager import InfrastructureManager as IM
from IM.auth import Authentication
from radl.radl import RADL, system, deploy, Feature
from IM.CloudInfo import CloudInfo
from IM.connectors.CloudConnector import CloudConnector
from IM.SSH import SSH


def read_file_as_string(file_name):
tests_path = os.path.dirname(os.path.abspath(__file__))
abs_file_path = os.path.join(tests_path, file_name)
return open(abs_file_path, 'r').read()


class TestIM(unittest.TestCase):

def __init__(self, *args):
unittest.TestCase.__init__(self, *args)

def setUp(self):

IM._reinit()
# Patch save_data
IM.save_data = staticmethod(lambda *args: None)

ch = logging.StreamHandler(sys.stdout)
log = logging.getLogger('InfrastructureManager')
log.setLevel(logging.ERROR)
log.propagate = 0
log.addHandler(ch)
log = logging.getLogger('ConfManager')
log.setLevel(logging.DEBUG)
log.propagate = 0
log.addHandler(ch)

def tearDown(self):
IM.stop()

@staticmethod
def getAuth(im_users=[], vmrc_users=[], clouds=[]):
return Authentication([
{'id': 'im%s' % i, 'type': 'InfrastructureManager', 'username': 'user%s' % i,
'password': 'pass%s' % i} for i in im_users] + [
{'id': 'vmrc%s' % i, 'type': 'VMRC', 'username': 'vmrcuser%s' % i,
'password': 'pass%s' % i, 'host': 'hostname'} for i in vmrc_users] + [
{'id': 'cloud%s' % i, 'type': c, 'username': 'user%s' % i,
'password': 'pass%s' % i, 'host': 'http://server.com:80/path'} for c, i in clouds])

def register_cloudconnector(self, name, cloud_connector):
sys.modules['IM.connectors.' + name] = type('MyConnector', (object,),
{name + 'CloudConnector': cloud_connector})

def get_dummy_ssh(self, retry=False):
ssh = SSH("", "", "")
ssh.test_connectivity = Mock(return_value=True)
ssh.execute = Mock(return_value=("10", "", 0))
ssh.sftp_put_files = Mock(return_value=True)
ssh.sftp_mkdir = Mock(return_value=True)
ssh.sftp_put_dir = Mock(return_value=True)
ssh.sftp_put = Mock(return_value=True)
return ssh

def gen_launch_res(self, inf, radl, requested_radl, num_vm, auth_data):
res = []
for _ in range(num_vm):
cloud = CloudInfo()
cloud.type = "Dummy"
vm = VirtualMachine(inf, "1234", cloud, radl, requested_radl)
vm.get_ssh = Mock(side_effect=self.get_dummy_ssh)
vm.state = VirtualMachine.RUNNING
res.append((True, vm))
return res

def get_cloud_connector_mock(self, name="MyMock0"):
cloud = type(name, (CloudConnector, object), {})
cloud.launch = Mock(side_effect=self.gen_launch_res)
return cloud

def test_inf_lifecycle(self):
"""Test Infrastructure lifecycle"""
radl = """"
network publica (outbound = 'yes')
system front (
cpu.arch='x86_64' and
cpu.count>=1 and
memory.size>=512m and
net_interface.0.connection = 'publica' and
net_interface.0.ip = '10.0.0.1' and
disk.0.image.url = 'mock0://linux.for.ev.er' and
disk.0.os.credentials.username = 'ubuntu' and
disk.0.os.credentials.password = 'yoyoyo' and
disk.0.os.name = 'linux' and
disk.1.size=1GB and
disk.1.device='hdb' and
disk.1.fstype='ext4' and
disk.1.mount_path='/mnt/disk' and
disk.0.applications contains (name = 'ansible.modules.micafer.hadoop') and
disk.0.applications contains (name='gmetad') and
disk.0.applications contains (name='wget')
)
deploy front 1
"""

auth0 = self.getAuth([0], [], [("Mock", 0)])
IM._reinit()
Config.PLAYBOOK_RETRIES = 1
Config.CONTEXTUALIZATION_DIR = os.path.dirname(os.path.realpath(__file__)) + "/../../contextualization"
Config.CONFMAMAGER_CHECK_STATE_INTERVAL = 0.001
cloud0 = self.get_cloud_connector_mock("MyMock")
self.register_cloudconnector("Mock", cloud0)

infId = IM.CreateInfrastructure(str(radl), auth0)

time.sleep(5)

state = IM.GetInfrastructureState(infId, auth0)
self.assertEqual(state["state"], "unconfigured")

IM.infrastructure_list[infId].ansible_configured = True

IM.Reconfigure(infId, "", auth0)

time.sleep(2)

state = IM.GetInfrastructureState(infId, auth0)
self.assertEqual(state["state"], "running")

add_radl = RADL()
add_radl.add(system("s0", [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
Feature("disk.0.os.credentials.username", "=", "user"),
Feature("disk.0.os.credentials.password", "=", "pass")]))
add_radl.add(deploy("s0", 1))

vms = IM.AddResource(infId, str(add_radl), auth0)
self.assertEqual(vms, [1])

state = IM.GetVMProperty(infId, "1", "state", auth0)
self.assertEqual(state, "running")

contmsg = IM.GetVMContMsg(infId, "1", auth0)
self.assertEqual(contmsg, "")

cont = IM.RemoveResource(infId, ['1'], auth0)
self.assertEqual(cont, 1)

IM.DestroyInfrastructure(infId, auth0)

if __name__ == "__main__":
unittest.main()
19 changes: 10 additions & 9 deletions test/QuickTestIM.py → test/integration/QuickTestIM.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
from radl import radl_parse
from IM import __version__ as version

TESTS_PATH = os.path.dirname(os.path.realpath(__file__))
RADL_FILE = TESTS_PATH + '/files/quick-test.radl'
AUTH_FILE = TESTS_PATH + '/auth.dat'
HOSTNAME = "localhost"
TEST_PORT = 8899


def read_file_as_string(file_name):
tests_path = os.path.dirname(os.path.abspath(__file__))
abs_file_path = os.path.join(tests_path, file_name)
return open(abs_file_path, 'r').read()


class QuickTestIM(unittest.TestCase):

server = None
Expand All @@ -47,7 +50,9 @@ class QuickTestIM(unittest.TestCase):
def setUpClass(cls):
cls.server = xmlrpclib.ServerProxy(
"http://" + HOSTNAME + ":" + str(TEST_PORT), allow_none=True)
cls.auth_data = Authentication.read_auth_data(AUTH_FILE)
tests_path = os.path.dirname(os.path.realpath(__file__))
auth_file = tests_path + '/../auth.dat'
cls.auth_data = Authentication.read_auth_data(auth_file)
cls.inf_id = 0

@classmethod
Expand Down Expand Up @@ -122,11 +127,7 @@ def test_11_create(self):
"""
Test the CreateInfrastructure IM function
"""
f = open(RADL_FILE)
radl = ""
for line in f.readlines():
radl += line
f.close()
radl = read_file_as_string("../files/quick-test.radl")

(success, inf_id) = self.server.CreateInfrastructure(radl, self.auth_data)
self.assertTrue(
Expand Down
19 changes: 10 additions & 9 deletions test/TestIM.py → test/integration/TestIM.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
RADL_ADD_WIN = "network publica\nnetwork privada\nsystem windows\ndeploy windows 1 one"
RADL_ADD = "network publica\nnetwork privada\nsystem wn\ndeploy wn 1 one"
RADL_ADD_ERROR = "system wnno deploy wnno 1"
TESTS_PATH = os.path.dirname(os.path.realpath(__file__))
RADL_FILE = TESTS_PATH + '/files/test.radl'
AUTH_FILE = TESTS_PATH + '/auth.dat'
HOSTNAME = "localhost"
TEST_PORT = 8899


def read_file_as_string(file_name):
tests_path = os.path.dirname(os.path.abspath(__file__))
abs_file_path = os.path.join(tests_path, file_name)
return open(abs_file_path, 'r').read()


class TestIM(unittest.TestCase):

server = None
Expand All @@ -50,7 +53,9 @@ class TestIM(unittest.TestCase):
def setUpClass(cls):
cls.server = xmlrpclib.ServerProxy(
"http://" + HOSTNAME + ":" + str(TEST_PORT), allow_none=True)
cls.auth_data = Authentication.read_auth_data(AUTH_FILE)
tests_path = os.path.dirname(os.path.realpath(__file__))
auth_file = tests_path + '/../auth.dat'
cls.auth_data = Authentication.read_auth_data(auth_file)

@classmethod
def tearDownClass(cls):
Expand Down Expand Up @@ -129,11 +134,7 @@ def test_11_create(self):
"""
Test the CreateInfrastructure IM function
"""
f = open(RADL_FILE)
radl = ""
for line in f.readlines():
radl += line
f.close()
radl = read_file_as_string("../files/test.radl")

(success, inf_id) = self.server.CreateInfrastructure(radl, self.auth_data)
self.assertTrue(
Expand Down
22 changes: 8 additions & 14 deletions test/TestREST.py → test/integration/TestREST.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@
PID = None
RADL_ADD = "network publica\nsystem front\ndeploy front 1"
RADL_ADD_ERROR = "system wnno deploy wnno 1"
TESTS_PATH = os.path.dirname(os.path.realpath(__file__))
RADL_FILE = TESTS_PATH + '/files/test_simple.radl'
AUTH_FILE = TESTS_PATH + '/auth.dat'

HOSTNAME = "localhost"
TEST_PORT = 8800


def read_file_as_string(file_name):
tests_path = os.path.dirname(os.path.abspath(__file__))
abs_file_path = os.path.join(tests_path, file_name)
return open(abs_file_path, 'r').read()


class TestIM(unittest.TestCase):

server = None
Expand All @@ -51,11 +53,7 @@ class TestIM(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.server = httplib.HTTPConnection(HOSTNAME, TEST_PORT)
f = open(AUTH_FILE)
cls.auth_data = ""
for line in f.readlines():
cls.auth_data += line.strip() + "\\n"
f.close()
cls.auth_data = read_file_as_string('../auth.dat').replace("\n", "\\n")
cls.inf_id = "0"

@classmethod
Expand Down Expand Up @@ -161,11 +159,7 @@ def test_18_get_info_without_auth_data(self):
msg="Incorrect error message: " + str(resp.status))

def test_20_create(self):
f = open(RADL_FILE)
radl = ""
for line in f.readlines():
radl += line
f.close()
radl = read_file_as_string('../files/test_simple.radl')

self.server.request('POST', "/infrastructures", body=radl,
headers={'AUTHORIZATION': self.auth_data})
Expand Down
22 changes: 8 additions & 14 deletions test/TestREST_JSON.py → test/integration/TestREST_JSON.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@
PID = None
RADL_ADD = """[{"class":"network","reference":true,"id":"publica"},
{"class":"system","reference":true,"id":"front"},{"vm_number":1,"class":"deploy","system":"front"}]"""
TESTS_PATH = os.path.dirname(os.path.realpath(__file__))
RADL_FILE = TESTS_PATH + '/files/test_simple.json'
AUTH_FILE = TESTS_PATH + '/auth.dat'

HOSTNAME = "localhost"
TEST_PORT = 8800


def read_file_as_string(file_name):
tests_path = os.path.dirname(os.path.abspath(__file__))
abs_file_path = os.path.join(tests_path, file_name)
return open(abs_file_path, 'r').read()


class TestIM(unittest.TestCase):

server = None
Expand All @@ -50,11 +52,7 @@ class TestIM(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.server = httplib.HTTPConnection(HOSTNAME, TEST_PORT)
f = open(AUTH_FILE)
cls.auth_data = ""
for line in f.readlines():
cls.auth_data += line.strip() + "\\n"
f.close()
cls.auth_data = read_file_as_string('../auth.dat').replace("\n", "\\n")
cls.inf_id = "0"

@classmethod
Expand Down Expand Up @@ -117,11 +115,7 @@ def wait_inf_state(self, state, timeout, incorrect_states=[], vm_ids=None):
return all_ok

def test_20_create(self):
f = open(RADL_FILE)
radl = ""
for line in f.readlines():
radl += line
f.close()
radl = read_file_as_string('../files/test_simple.json')

self.server.request('POST', "/infrastructures", body=radl, headers={
'AUTHORIZATION': self.auth_data, 'Content-Type': 'application/json'})
Expand Down
Loading

0 comments on commit bfd1b92

Please sign in to comment.