Unverified Commit 5413fd4c authored by efikalti's avatar efikalti Committed by Avraam Tsantekidis

LAM-41 unit tests for resource checking methods

parent e94a6d97
......@@ -33,27 +33,27 @@ class Provisioner:
cloud_section = self.config._sections['cloud'].get(cloud_name)
if not cloud_section:
message = "Cloud '%s' was not found in you .kamakirc configuration file. " \
"Currently you have availablie in your configuration these clouds: %s"
"Currently you have available in your configuration these clouds: %s"
raise KeyError(message % (cloud_name, self.config._sections['cloud'].keys()))
# Get the authentication url and token
auth_url, auth_token = cloud_section['url'], cloud_section['token']
self.auth_url, self.auth_token = cloud_section['url'], cloud_section['token']
# Create the astakos client
self.astakos = astakos.AstakosClient(auth_url, auth_token)
self.astakos = astakos.AstakosClient(self.auth_url, self.auth_token)
# Create the cyclades client
computeURL = self.astakos.get_endpoint_url(
cyclades.CycladesComputeClient.service_type)
self.cyclades = cyclades.CycladesComputeClient(computeURL, auth_token)
self.cyclades = cyclades.CycladesComputeClient(computeURL, self.auth_token)
# Create the identity_client
self.identity_client = astakos.AstakosClient(auth_url, auth_token)
self.identity_client = astakos.AstakosClient(self.auth_url, self.auth_token)
# Create the network client
networkURL = self.identity_client.get_endpoint_url(
cyclades.CycladesNetworkClient.service_type)
self.network_client = cyclades.CycladesNetworkClient(networkURL, auth_token)
self.network_client = cyclades.CycladesNetworkClient(networkURL, self.auth_token)
# Constants
self.Bytes_to_GB = 1024*1024*1024
......@@ -229,8 +229,7 @@ class Provisioner:
usage_vm = quotas[project_id]['cyclades.vm']['usage']
available_vm = limit_vm - usage_vm - pending_vm
if available_vm < cluster_size:
msg = 'Cyclades VMs out of limit'
raise ClientError(msg, error_quotas_cluster_size)
return False
else:
return True
......@@ -248,8 +247,7 @@ class Provisioner:
usage_cpu = quotas[project_id]['cyclades.cpu']['usage']
available_cpu = limit_cpu - usage_cpu - pending_cpu
if available_cpu < cpu_request:
msg = 'Cyclades cpu out of limit'
raise ClientError(msg, error_quotas_cpu)
return False
else:
return True
......@@ -268,8 +266,7 @@ class Provisioner:
usage_ram = quotas[project_id]['cyclades.ram']['usage']
available_ram = (limit_ram - usage_ram - pending_ram) / self.Bytes_to_MB
if available_ram < ram_request:
msg = 'Cyclades ram out of limit'
raise ClientError(msg, error_quotas_ram)
return False
else:
return True
......@@ -288,8 +285,7 @@ class Provisioner:
usage_cd = quotas[project_id]['cyclades.disk']['usage']
available_cyclades_disk_GB = (limit_cd - usage_cd - pending_cd) / self.Bytes_to_GB
if available_cyclades_disk_GB < disk_request:
msg = 'Cyclades disk out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
return False
else:
return True
......@@ -310,8 +306,7 @@ class Provisioner:
if d['instance_id'] is None and d['port_id'] is None:
available_ips += 1
if available_ips < ip_request:
msg = 'Public IPs out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
return False
else:
return True
......@@ -321,13 +316,12 @@ class Provisioner:
Subtracts the number of networks used and pending from the max allowed
number of networks
"""
pending_net = quotas[project_id]['cyclades.network.private']['pending']
limit_net = quotas[project_id]['cyclades.network.private']['limit']
usage_net = quotas[project_id]['cyclades.network.private']['usage']
pending_net = quotas[project_id]['cyclades.network.private']['project_pending']
limit_net = quotas[project_id]['cyclades.network.private']['project_limit']
usage_net = quotas[project_id]['cyclades.network.private']['project_usage']
available_networks = limit_net - usage_net - pending_net
if available_networks < 1:
msg = 'Private Network out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
if available_networks < network_request:
return False
else:
return True
......@@ -340,42 +334,36 @@ class Provisioner:
project_id = self.find_project_id(**kwargs)['id']
quotas = self.get_quotas()
# Check for VMs
try:
self.check_cluster_size_quotas(quotas, project_id, kwargs.get("cluster_size"))
except ClientError as ex:
raise ex
return False
# Check for CPUs
try:
self.check_cpu_quotas(quotas, project_id, kwargs.get("cpu_request"))
except ClientError as ex:
raise ex
return False
# Check for RAM
try:
self.check_ram_quotas(quotas, project_id, kwargs.get("ram_request"))
except ClientError as ex:
raise ex
return False
# Check for Disk space
try:
self.check_disk_quotas(quotas, project_id, kwargs.get("disk_request"))
except ClientError as ex:
raise ex
return False
# Check for public IPs
try:
self.check_ip_quotas(quotas, project_id, kwargs.get("ip_request"))
except ClientError as ex:
raise ex
return False
# Check for networks
try:
self.check_network_quotas(quotas, project_id, kwargs.get("network_request"))
except ClientError as ex:
raise ex
return False
return True
if self.check_cluster_size_quotas(quotas, project_id, kwargs.get("cluster_size")):
# Check for CPUs
if self.check_cpu_quotas(quotas, project_id, kwargs.get("cpu_request")):
# Check for RAM
if self.check_ram_quotas(quotas, project_id, kwargs.get("ram_request")):
# Check for Disk space
if self.check_disk_quotas(quotas, project_id, kwargs.get("disk_request")):
# Check for public IPs
if self.check_ip_quotas(quotas, project_id, kwargs.get("ip_request")):
# Check for networks
if self.check_network_quotas(quotas, project_id, kwargs.get("network_request")):
return True
else:
msg = 'Private Network out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
else:
msg = 'Public IPs out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
else:
msg = 'Cyclades disk out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
else:
msg = 'Cyclades ram out of limit'
raise ClientError(msg, error_quotas_ram)
else:
msg = 'Cyclades cpu out of limit'
raise ClientError(msg, error_quotas_cpu)
else:
msg = 'Cyclades VMs out of limit'
raise ClientError(msg, error_quotas_cluster_size)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Okeanos VM provisioning")
......
# -*- coding: utf-8 -*-
""" Functionality related to unit tests with un-managed resources mocked. """
# setup testing framework
from unittest import TestCase, main, expectedFailure, skip
from mock import patch
import sys
import os
from os.path import join, dirname, abspath
from mock import patch
from ConfigParser import RawConfigParser, NoSectionError
sys.path.append(join(dirname(abspath(__file__)), '../'))
# import objects we aim to test
import provision
import cluster_error_constants
class MockAstakos():
""" support class for faking AstakosClient.get_quotas """
def get_quotas(self, *args):
return { 'test_project_id':
{'cyclades.disk':
{'project_limit': 1288490188800, 'project_pending': 0, 'project_usage': 64424509440, 'usage': 0, 'limit': 322122547200, 'pending': 0},
'cyclades.vm':
{'project_limit': 60, 'project_pending': 0, 'project_usage': 2, 'usage': 0, 'limit': 15, 'pending': 0},
'pithos.diskspace':
{'project_limit': 429496729600, 'project_pending': 0, 'project_usage': 0, 'usage': 0, 'limit': 107374182400, 'pending': 0},
'cyclades.ram':
{'project_limit': 128849018880, 'project_pending': 0, 'project_usage': 12884901888, 'usage': 0, 'limit': 32212254720, 'pending': 0},
'cyclades.cpu':
{'project_limit': 120, 'project_pending': 0, 'project_usage': 12, 'usage': 0, 'limit': 30, 'pending': 0},
'cyclades.floating_ip':
{'project_limit': 10, 'project_pending': 0, 'project_usage': 6, 'usage': 3, 'limit': 4, 'pending': 0},
'cyclades.network.private':
{'project_limit': 10, 'project_pending': 0, 'project_usage': 7, 'usage': 0, 'limit': 4, 'pending': 0},
'astakos.pending_app':
{'project_limit': 0, 'project_pending': 0, 'project_usage': 0, 'usage': 0, 'limit': 0, 'pending': 0}} }
# replace unmanaged calls with fakes
#@patch('provision.Cluster.create_vpn', mock_create_vpn)
class TestCreateCluster(TestCase):
""" Test cases with separate un-managed resources mocked. """
# initialize objects common to all tests in this test case#
def setUp(self):
cloud_name = '~okeanos'
self.provisioner = provision.Provisioner(cloud_name=cloud_name)
self.astakos = MockAstakos()
self.quotas = self.astakos.get_quotas()
self.opts = {
'name': 'Test', 'cluster_size': 2, 'cpu_master': 2, 'cluster_size_ex': 100, 'cpu_master_ex': 100,
'ram_master': 4096, 'disk_master': 5, 'cpu_slaves': 2, 'cpu_slaves_ex': 100, 'disk_master_ex': 10000,
'ram_slaves': 2048, 'disk_slaves': 5, 'disk_slaves_ex': 10000, 'token': self.provisioner.auth_token,
'ram_master_ex': 100000, 'ram_slaves_ex': 100000,
'cluster_size_ex': 1000,
'vpn': 1, 'vpn_ex': 20, 'ips': 1, 'ips_ex': 100,
'os_choice': 'debian',
'auth_url': self.provisioner.auth_url,
'cloud_name':cloud_name,
'project_id': 'test_project_id',
'project_name': 'test_project'}
""" Test Resource Checking methods """
def test_check_vm_quotas_sufficient(self):
# arrange
expected = True # success
# act
returned = self.provisioner.check_cluster_size_quotas(self.quotas, self.opts['project_id'], self.opts['cluster_size'])
# assert
self.assertEqual(expected, returned)
def test_check_vm_quotas_exceeds(self):
# arrange
expected = False # success
# act
returned = self.provisioner.check_cluster_size_quotas(self.quotas, self.opts['project_id'], self.opts['cluster_size_ex'])
# assert
self.assertEqual(expected, returned)
def test_check_cpu_quotas_sufficient(self):
# arrange
cpus = self.opts['cpu_master'] + self.opts['cpu_slaves']
expected = True # success
# act
returned = self.provisioner.check_cpu_quotas(self.quotas, self.opts['project_id'], cpus)
# assert
self.assertEqual(expected, returned)
def test_check_cpu_quotas_exceeds(self):
# arrange
cpus = self.opts['cpu_master_ex'] + self.opts['cpu_slaves_ex']
expected = False # success
# act
returned = self.provisioner.check_cpu_quotas(self.quotas, self.opts['project_id'], cpus)
# assert
self.assertEqual(expected, returned)
def test_check_ram_quotas_sufficient(self):
# arrange
ram = self.opts['ram_master'] + self.opts['ram_slaves']
expected = True # success
# act
returned = self.provisioner.check_ram_quotas(self.quotas, self.opts['project_id'], ram)
# assert
self.assertEqual(expected, returned)
def test_check_ram_quotas_exceeds(self):
# arrange
ram = self.opts['ram_master_ex'] + self.opts['ram_slaves_ex']
expected = False # success
# act
returned = self.provisioner.check_ram_quotas(self.quotas, self.opts['project_id'], ram)
# assert
self.assertEqual(expected, returned)
def test_check_disk_quotas_sufficient(self):
# arrange
disk = self.opts['disk_master'] + self.opts['disk_slaves']
expected = True # success
# act
returned = self.provisioner.check_disk_quotas(self.quotas, self.opts['project_id'], disk)
# assert
self.assertEqual(expected, returned)
def test_check_disk_quotas_exceeds(self):
# arrange
disk = self.opts['disk_master_ex'] + self.opts['disk_slaves_ex']
expected = False # success
# act
returned = self.provisioner.check_disk_quotas(self.quotas, self.opts['project_id'], disk)
# assert
self.assertEqual(expected, returned)
def test_check_ip_quotas_sufficient(self):
# arrange
expected = True # success
# act
returned = self.provisioner.check_ip_quotas(self.quotas, self.opts['project_id'], self.opts['ips'])
# assert
self.assertEqual(expected, returned)
def test_check_ip_quotas_exceeds(self):
# arrange
expected = False # success
# act
returned = self.provisioner.check_ip_quotas(self.quotas, self.opts['project_id'], self.opts['ips_ex'])
# assert
self.assertEqual(expected, returned)
def test_check_network_quotas_sufficient(self):
# arrange
expected = True # success
# act
returned = self.provisioner.check_network_quotas(self.quotas, self.opts['project_id'], self.opts['vpn'])
# assert
self.assertEqual(expected, returned)
def test_check_network_quotas_exceeds(self):
# arrange
expected = False # success
# act
returned = self.provisioner.check_network_quotas(self.quotas, self.opts['project_id'], self.opts['vpn_ex'])
# assert
self.assertEqual(expected, returned)
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment