tests/app/soc/logic/test_allocations.py
author Sverre Rabbelier <srabbelier@gmail.com>
Fri, 13 Feb 2009 23:18:39 +0000
changeset 1308 35b75ffcbb37
parent 1056 d0c82bdc2de2
child 1717 d4c4c8668871
permissions -rw-r--r--
Partially reverted "Update the copyright notice for 2009." This partially reverts commit r1933. Only the files that were created in 2008 were reverted. Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1056
diff changeset
     3
# Copyright 2009 the Melange authors.
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
__authors__ = [
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
  ]
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
import unittest
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from soc.logic import allocations
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
class Student(object):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
  """Mocker for Student object.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
  """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
  def __init__(self, id):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
    """Simple init that stores id for later use.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
    self.id = id
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  def __eq__(self, other):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
    """Simple eq that compares ids.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
    return self.id == other.id
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
  def __str__(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
    """Simple str that returns str(id).
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
    return str(self.id)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  def __repr__(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
    """Simple repr that returns repr(id).
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
    return repr(self.id)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
class AllocationsTest(unittest.TestCase):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
  """Tests related to the slot allocation algorithm.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
  """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
  def setUp(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
    """Set up required for the slot allocation tests.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
    self.slots = 60
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
    self.max_slots_per_org = 40
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
    self.allocated = 0
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
    self.applications = {
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
        'asf': self.allocate(20),
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
        'gcc': self.allocate(15),
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
        'git': self.allocate(6),
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
        'google': self.allocate(3),
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
        'melange': self.allocate(100),
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
        }
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
    self.orgs = self.applications.keys()
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
    self.allocater = allocations.Allocator(self.orgs, self.applications,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
                                           self.slots, self.max_slots_per_org)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
  def allocate(self, count):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
    """Returns a list with count new student objects.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
    i = self.allocated
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
    j = i + count
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
    self.allocated += count
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
    return [Student(i) for i in range(i,j)]
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
  def testAllocate(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
    """Test that the allocate helper works properly.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
    A meta-test, it never hurts to be certain.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
    stash = self.allocated
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
    self.allocated = 0
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
    expected = [Student(0), Student(1), Student(2)]
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
    actual = self.allocate(3)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
    self.failUnlessEqual(expected, actual)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
    expected = []
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
    actual = self.allocate(0)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
    self.failUnlessEqual(expected, actual)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
    expected = [Student(3)]
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
    actual = self.allocate(1)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
    self.failUnlessEqual(expected, actual)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
    self.allocated = stash
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
  def testInitialAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
    """Test that an allocation with no arguments does not crash.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
    self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
  def testLockedSlotsAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
    """Test that an allocation with an org locked does not crash.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
    locked_slots = {'melange': 3}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   128
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
    self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
  def testAdjustedSlotsAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
    """Test that an allocation with an org adjusted does not crash.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
    adjusted_slots = {'google': -1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
    self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
  def testInvalidSlotsAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
    """Test that an allocation with an org locked and adjusted errors out.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   143
    locked_slots = {'git': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
    adjusted_slots = {'git': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
    self.failUnlessRaises(allocations.Error, self.allocater.allocate,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
                          locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
  def testNonExistantOrgAllocation1(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
    """Test that locking a non-existing org errors out.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
    locked_slots = {'gnome': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
    self.failUnlessRaises(allocations.Error, self.allocater.allocate,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
                          locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
  def testNonExistantOrgAllocation2(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
    """Test that adjusting a non-existing org errors out.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
    adjusted_slots = {'gnome': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
    self.failUnlessRaises(allocations.Error, self.allocater.allocate,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
                          locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
  def testInitialAllocationBelowMaxSlots(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   167
    """Test that the initial allocation is below the max slot count.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   169
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   170
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   171
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   173
    result = self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
    self.failIf(sum(result.values()) > self.slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   176
  def testLockedAllocationCorrect(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
    """Test that locking an allocation assigns the org the allocation.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   180
    locked_slots = {'git': 6}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
    result = self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   185
    expected = 6
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
    actual = result['git']
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
    self.failUnlessEqual(expected, actual)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
  def testOverassignedAllocationCorrect(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
    """Test that over-assigned allocation are cut down.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
    locked_slots = {'git': 20}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
    result = self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
    expected = 6
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   200
    actual = result['git']
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
    self.failUnlessEqual(expected, actual)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   203
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
  def testAdjustedAllocationCorrect(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
    """Test that locking an allocation assigns the org the allocation.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   209
    adjusted_slots = {'google': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   210
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
    with_adjusting = self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
    without_adjusting = self.allocater.allocate(locked_slots, {})
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
    expected = without_adjusting['google'] + 1
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
    actual = with_adjusting['google']
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   216
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   217
    self.failUnlessEqual(expected, actual)