Extracted contracts_primitive from contracts_extra to avoid problems with imports, added tests for long values returned by libvirt

This commit is contained in:
Anton Beloglazov 2015-03-26 12:00:10 +11:00
parent 620425f10d
commit fa687ec0e0
24 changed files with 103 additions and 14 deletions

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import os import os

View File

@ -15,9 +15,6 @@
from contracts import new_contract from contracts import new_contract
new_contract('long', lambda x: isinstance(x, (int, long)))
new_contract('function', lambda x: hasattr(x, '__call__'))
import collections import collections
new_contract('deque', collections.deque) new_contract('deque', collections.deque)

View File

@ -0,0 +1,19 @@
# Copyright 2012 Anton Beloglazov
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from contracts import new_contract
new_contract('long', lambda x: isinstance(x, (int, long)))
new_contract('function', lambda x: hasattr(x, '__call__'))

View File

@ -13,7 +13,7 @@
# limitations under the License. # limitations under the License.
from contracts import contract from contracts import contract
from neat.contracts_extra import * from neat.contracts_primitive import *
import datetime import datetime
from sqlalchemy import * from sqlalchemy import *

View File

@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
from sqlalchemy import * from sqlalchemy import *

View File

@ -20,6 +20,7 @@ of the database size.
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import datetime import datetime

View File

@ -68,6 +68,7 @@ the beginning of the global manager's execution.
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import bottle import bottle

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import logging import logging

View File

@ -91,6 +91,7 @@ invoked, the component performs the following steps:
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import os import os

View File

@ -101,6 +101,7 @@ local manager performs the following steps:
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import requests import requests

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import nlp import nlp

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import neat.locals.overload.mhod.multisize_estimation as estimation import neat.locals.overload.mhod.multisize_estimation as estimation

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import logging import logging

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
from itertools import islice from itertools import islice

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import operator import operator

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import logging import logging
@ -41,7 +42,7 @@ def otf_factory(time_step, migration_time, params):
migration_time_normalized = float(migration_time) / time_step migration_time_normalized = float(migration_time) / time_step
def otf_wrapper(utilization, state=None): def otf_wrapper(utilization, state=None):
if state is None or state == {}: if state is None or state == {}:
state = {'overload': 0, state = {'overload': 0,
'total': 0} 'total': 0}
return otf(params['otf'], return otf(params['otf'],
params['threshold'], params['threshold'],
@ -49,9 +50,9 @@ def otf_factory(time_step, migration_time, params):
migration_time_normalized, migration_time_normalized,
utilization, utilization,
state) state)
return otf_wrapper return otf_wrapper
@contract @contract
def otf(otf, threshold, limit, migration_time, utilization, state): def otf(otf, threshold, limit, migration_time, utilization, state):
@ -89,10 +90,10 @@ def otf(otf, threshold, limit, migration_time, utilization, state):
log.debug('OTF total steps:' + str(state['total'])) log.debug('OTF total steps:' + str(state['total']))
log.debug('OTF:' + str(float(state['overload']) / state['total'])) log.debug('OTF:' + str(float(state['overload']) / state['total']))
log.debug('OTF migration time:' + str(migration_time)) log.debug('OTF migration time:' + str(migration_time))
log.debug('OTF + migration time:' + log.debug('OTF + migration time:' +
str((migration_time + state['overload']) / \ str((migration_time + state['overload']) / \
(migration_time + state['total']))) (migration_time + state['total'])))
log.debug('OTF decision:' + log.debug('OTF decision:' +
str(overload and (migration_time + state['overload']) / \ str(overload and (migration_time + state['overload']) / \
(migration_time + state['total']) >= otf)) (migration_time + state['total']) >= otf))

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
from numpy import median from numpy import median

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import logging import logging

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
import logging import logging

View File

@ -16,6 +16,7 @@
""" """
from contracts import contract from contracts import contract
from neat.contracts_primitive import *
from neat.contracts_extra import * from neat.contracts_extra import *
from random import choice from random import choice

View File

@ -560,6 +560,19 @@ class Collector(TestCase):
assert collector.get_host_characteristics(connection) == \ assert collector.get_host_characteristics(connection) == \
(cores * mhz, ram) (cores * mhz, ram)
@qc(10)
def get_host_characteristics_long(
ram=int_(min=1, max=4000),
cores=int_(min=1, max=8),
mhz=int_(min=1, max=3000)
):
with MockTransaction:
connection = libvirt.virConnect()
expect(connection).getInfo().and_return(
['x86_64', long(ram), cores, mhz, 1, 1, 4, 2]).once()
assert collector.get_host_characteristics(connection) == \
(cores * mhz, long(ram))
@qc(1) @qc(1)
def log_host_overload(): def log_host_overload():
db = db_utils.init_db('sqlite:///:memory:') db = db_utils.init_db('sqlite:///:memory:')

View File

@ -141,11 +141,26 @@ class LocalManager(TestCase):
with MockTransaction: with MockTransaction:
def mock_get_max_ram(vir_connection, uuid): def mock_get_max_ram(vir_connection, uuid):
return data[uuid] return data[uuid]
connection = libvirt.virConnect() connection = libvirt.virConnect()
when(manager).get_max_ram(connection, any_string). \ when(manager).get_max_ram(connection, any_string). \
then_call(mock_get_max_ram) then_call(mock_get_max_ram)
assert manager.get_ram(connection, data.keys()) == data
@qc(10)
def get_ram_long(
data=dict_(
keys=str_(of='abc123-', min_length=36, max_length=36),
values=int_(min=1, max=100),
min_length=0, max_length=10
)
):
data = dict([(k, long(v)) for (k, v) in data.iteritems()])
with MockTransaction:
def mock_get_max_ram(vir_connection, uuid):
return data[uuid]
connection = libvirt.virConnect()
when(manager).get_max_ram(connection, any_string). \
then_call(mock_get_max_ram)
assert manager.get_ram(connection, data.keys()) == data assert manager.get_ram(connection, data.keys()) == data
@qc(10) @qc(10)
@ -159,7 +174,20 @@ class LocalManager(TestCase):
expect(connection).lookupByUUIDString(uuid). \ expect(connection).lookupByUUIDString(uuid). \
and_return(domain).once() and_return(domain).once()
expect(domain).maxMemory().and_return(x).once() expect(domain).maxMemory().and_return(x).once()
assert manager.get_max_ram(connection, uuid) == int(x / 1024) assert manager.get_max_ram(connection, uuid) == int(x) / 1024
@qc(10)
def get_max_ram_long(
uuid=str_(of='abc123-', min_length=36, max_length=36),
x=int_(min=0)
):
with MockTransaction:
connection = libvirt.virConnect()
domain = mock('domain')
expect(connection).lookupByUUIDString(uuid). \
and_return(domain).once()
expect(domain).maxMemory().and_return(long(x)).once()
assert manager.get_max_ram(connection, uuid) == long(x) / 1024
@qc(1) @qc(1)
def get_max_ram_none( def get_max_ram_none(
@ -167,7 +195,7 @@ class LocalManager(TestCase):
): ):
with MockTransaction: with MockTransaction:
def raise_libvirt_error(): def raise_libvirt_error():
raise libvirt.libvirtError(None) raise libvirt.libvirtError(None)
connection = libvirt.virConnect() connection = libvirt.virConnect()
expect(connection).lookupByUUIDString(uuid). \ expect(connection).lookupByUUIDString(uuid). \
and_call(lambda _: raise_libvirt_error()) and_call(lambda _: raise_libvirt_error())

View File

@ -174,3 +174,18 @@ class Common(TestCase):
migration_time = float(sum(ram)) / len(ram) / bandwidth migration_time = float(sum(ram)) / len(ram) / bandwidth
assert common.calculate_migration_time(data, bandwidth) == \ assert common.calculate_migration_time(data, bandwidth) == \
migration_time migration_time
@qc(10)
def calculate_migration_time_long(
data=dict_(
keys=str_(of='abc123-', min_length=36, max_length=36),
values=int_(min=1, max=1000),
min_length=1, max_length=10
),
bandwidth=float_(min=1., max=100.)
):
data = dict([(k, long(v)) for (k, v) in data.iteritems()])
ram = data.values()
migration_time = float(sum(ram)) / len(ram) / bandwidth
assert common.calculate_migration_time(data, bandwidth) == \
migration_time

View File

@ -121,14 +121,14 @@ class Db(TestCase):
assert host['cpu_cores'] == 4 assert host['cpu_cores'] == 4
assert host['ram'] == 4000 assert host['ram'] == 4000
db.update_host('host1', 3500, 8, 8000) db.update_host('host1', 3500, 8, 8000L)
hosts = db.hosts.select().execute().fetchall() hosts = db.hosts.select().execute().fetchall()
assert len(hosts) == 1 assert len(hosts) == 1
host = hosts[0] host = hosts[0]
assert host['hostname'] == 'host1' assert host['hostname'] == 'host1'
assert host['cpu_mhz'] == 3500 assert host['cpu_mhz'] == 3500
assert host['cpu_cores'] == 8 assert host['cpu_cores'] == 8
assert host['ram'] == 8000 assert host['ram'] == 8000L
@qc(10) @qc(10)
def select_cpu_mhz_for_host( def select_cpu_mhz_for_host(