Unverified Commit 796501af authored by efikalti's avatar efikalti Committed by Avraam Tsantekidis

modified files to match makis python api

parent 52036353
......@@ -9,6 +9,7 @@ from kamaki.clients import astakos, cyclades
from kamaki.clients import ClientError
from kamaki.clients.utils import https
from kamaki.cli.config import Config as KamakiConfig
from fokia.cluster_error_constants import *
# TODO: remove this and actually use ssl cert files
https.patch_ignore_ssl()
......@@ -46,6 +47,15 @@ class Provisioner:
logger.info("Initiating Cyclades client")
self.cyclades = cyclades.CycladesComputeClient(compute_url, auth_token)
# Create the network client
networkURL = self.astakos.get_endpoint_url(
cyclades.CycladesNetworkClient.service_type)
self.network_client = cyclades.CycladesNetworkClient(networkURL, auth_token)
# Constants
self.Bytes_to_GB = 1024*1024*1024
self.Bytes_to_MB = 1024*1024
def find_flavor(self, **kwargs):
"""
......@@ -110,6 +120,82 @@ class Provisioner:
raise ex
return okeanos_response
def get_quotas(self, **kwargs):
"""
Get the user quotas for the defined project.
:return: user quotas object
"""
return self.astakos.get_quotas()
def check_all_resources(self, quotas, **kwargs):
"""
Checks user's quota for every requested resource.
Returns True if everything available.
:param **kwargs: arguments
"""
project_id = self.find_project_id(**kwargs)['id']
# quotas = self.get_quotas()
# Check for VMs
pending_vm = quotas[project_id]['cyclades.vm']['pending']
limit_vm = quotas[project_id]['cyclades.vm']['limit']
usage_vm = quotas[project_id]['cyclades.vm']['usage']
available_vm = limit_vm - usage_vm - pending_vm
if available_vm < kwargs['cluster_size']:
msg = 'Cyclades VMs out of limit'
raise ClientError(msg, error_quotas_cluster_size)
return False
# Check for CPUs
pending_cpu = quotas[project_id]['cyclades.cpu']['pending']
limit_cpu = quotas[project_id]['cyclades.cpu']['limit']
usage_cpu = quotas[project_id]['cyclades.cpu']['usage']
available_cpu = limit_cpu - usage_cpu - pending_cpu
if available_cpu < kwargs['vcpus']:
msg = 'Cyclades cpu out of limit'
raise ClientError(msg, error_quotas_cpu)
return False
# Check for RAM
pending_ram = quotas[project_id]['cyclades.ram']['pending']
limit_ram = quotas[project_id]['cyclades.ram']['limit']
usage_ram = quotas[project_id]['cyclades.ram']['usage']
available_ram = (limit_ram - usage_ram - pending_ram) / self.Bytes_to_MB
if available_ram < kwargs['ram']:
msg = 'Cyclades ram out of limit'
raise ClientError(msg, error_quotas_ram)
return False
# Check for Disk space
pending_cd = quotas[project_id]['cyclades.ram']['pending']
limit_cd = quotas[project_id]['cyclades.disk']['limit']
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 < kwargs['disk']:
msg = 'Cyclades disk out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
return False
# Check for public IPs
list_float_ips = self.network_client.list_floatingips()
pending_ips = quotas[project_id]['cyclades.floating_ip']['pending']
limit_ips = quotas[project_id]['cyclades.floating_ip']['limit']
usage_ips = quotas[project_id]['cyclades.floating_ip']['usage']
available_ips = limit_ips - usage_ips - pending_ips
for d in list_float_ips:
if d['instance_id'] is None and d['port_id'] is None:
available_ips += 1
if available_ips < kwargs['ip_request']:
msg = 'Public IPs out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
return False
# Check for networks
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 < kwargs['network_request']:
msg = 'Private Network out of limit'
raise ClientError(msg, error_quotas_cyclades_disk)
return False
return True
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Okeanos VM provisioning")
......
File mode changed from 100644 to 100755
......@@ -124,6 +124,23 @@ test_projects = [{u'creation_date': u'2015-06-09T09:46:44.327826+00:00',
u'state': u'active',
u'system_project': False}]
test_quotas = { '6ff62e8e-0ce9-41f7-ad99-13a18ecada5f':
{'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}} }
def test_find_flavor():
with mock.patch('fokia.provisioner.astakos'), \
......@@ -140,6 +157,21 @@ def test_find_flavor():
name='tost', image_id=u'0035ac89-a86e-4108-93e8-93e294b74a3d', flavor_id=3,
project_id=u'6ff62e8e-0ce9-41f7-ad99-13a18ecada5f', networks=[], personality=[])
def test_check_all_resources():
with mock.patch('fokia.provisioner.astakos'), \
mock.patch('fokia.provisioner.KamakiConfig'), \
mock.patch('fokia.provisioner.cyclades'):
provisioner = Provisioner("lambda")
provisioner.astakos.get_projects.return_value = test_projects
provisioner.astakos.get_quotas.return_value = test_quotas
provisioner.check_all_resources(test_quotas, project_id=u'6ff62e8e-0ce9-41f7-ad99-13a18ecada5f',
slaves=2,
cluster_size=3,
vcpus=12,
ram=4096*3,
disk=180,
ip_request=1,
network_request=1)
if __name__ == "__main__":
test_find_flavor()
This diff is collapsed.
# -*- 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}} }
class MockCycladesNetClient():
""" support class for faking CycladesNetworkClient.list_floatingips """
def list_floatingips(self):
return [{'instance_id': '604863', 'port_id': '1743733'}, {'instance_id': None, 'port_id': None},
{'instance_id': '615302', 'port_id': '1773954'}]
# 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