def test_push_too_many(self): """ test_push_too_many""" pool_name = "fake.pool" (host1, _) = models.Host.objects.get_or_create(connection="localhost", product="fake") (pool1, _) = models.Pool.objects.get_or_create( name="fake.pool", host=host1, template_name="test.template", resource_max=10) pool = api.pool_get(pool1) self.assertTrue(pool) algo.destroy(pool, pool1) rtc = algo.adapt(pool, pool1) self.assertEqual(rtc, 10) rsrc = algo.pop(pool_name, 1) self.assertTrue(rsrc) algo.push(rsrc.id) with self.assertRaises(algo.ResourceReleased): algo.push(rsrc.id) api_exts = ext.api_ext_list() server.adapt(api_exts)
def test_shrink(self): """ test_shrink. test when the pool shrinks. """ (host1, _) = models.Host.objects.get_or_create(connection=CONNECTION, product=PRODUCT) defaults = {"resource_max": 3, "template_name": TEMPLATE} (pool1, _) = models.Pool.objects.update_or_create( name=TEST_POOL, host=host1, defaults=defaults) args = FakeArgs() server.args_process(args) self.assertEqual(server.main(args), 0) ## # Now shrink the pool to two pool1.resource_max = 2 pool1.save() ## args = FakeArgs() args.setup = False server.args_process(args) self.assertEqual(server.main(args), 0) exts = ext.api_ext_list() pool = exts[PRODUCT].pool_get(pool1) self.assertEqual(len(pool.list(pool1)), 2)
def _do_resource_detail(args): """ Resource Detail content. """ rsrc = models.Resource.objects.get(pool__name=args.pool, name=args.name) exts = ext.api_ext_list() pool = exts[rsrc.pool.host.product].pool_get(rsrc.pool) print "Name: %s" % args.name ip_address = pool.ip_get(args.name) print "IP: %s" % ip_address
def test_expiration(self): """ test_expiration. """ resource_max = 3 (host1, _) = models.Host.objects.get_or_create(connection=CONNECTION, product=PRODUCT) defaults = {"resource_max": resource_max, "template_name": TEMPLATE} (pool1, _) = models.Pool.objects.update_or_create(name=TEST_POOL, host=host1, defaults=defaults) args = FakeArgs() server.args_process(args) self.assertEqual(server.main(args), 0) rsrcs = pool1.resource_set.filter(status=models.Resource.READY) self.assertEqual(len(rsrcs), resource_max) rsrc = rsrcs[0] ## # Acquire for 3 seconds. rsrc.transition(models.Resource.RESERVED, algo.ACTION_DESTROY, 3) time.sleep(5) args.setup = False args.count = 2 args.sleep_time = 1 args.max_sleep_time = 1 args.min_sleep_time = 1 server.args_process(args) self.assertEqual(server.main(args), 0) ## exts = ext.api_ext_list() server.adapt(exts) rsrcs = pool1.resource_set.filter(status=models.Resource.READY) ## # Check to see if the expiration happens. self.assertEqual(rsrcs.count(), 2)
def test_expiration(self): """ test_expiration. """ resource_max = 3 (host1, _) = models.Host.objects.get_or_create(connection=CONNECTION, product=PRODUCT) defaults = {"resource_max": resource_max, "template_name": TEMPLATE} (pool1, _) = models.Pool.objects.update_or_create( name=TEST_POOL, host=host1, defaults=defaults) args = FakeArgs() server.args_process(args) self.assertEqual(server.main(args), 0) rsrcs = pool1.resource_set.filter(status=models.Resource.READY) self.assertEqual(len(rsrcs), resource_max) rsrc = rsrcs[0] ## # Acquire for 3 seconds. rsrc.transition(models.Resource.RESERVED, algo.ACTION_DESTROY, 3) time.sleep(5) args.setup = False args.count = 2 args.sleep_time = 1 args.max_sleep_time = 1 args.min_sleep_time = 1 server.args_process(args) self.assertEqual(server.main(args), 0) ## exts = ext.api_ext_list() server.adapt(exts) rsrcs = pool1.resource_set.filter(status=models.Resource.READY) ## # Check to see if the expiration happens. self.assertEqual(rsrcs.count(), 2)
def test_expand(self): """ test_expand. Check when pool increases. """ (host1, _) = models.Host.objects.get_or_create(connection=CONNECTION, product=PRODUCT) defaults = {"resource_max": 2, "template_name": TEMPLATE} (pool1, _) = models.Pool.objects.update_or_create( name=TEST_POOL, host=host1, defaults=defaults) ## # Now expand to 3 pool1.resource_max = 3 pool1.save() ## args = FakeArgs() server.args_process(args) self.assertEqual(server.main(args), 0) exts = ext.api_ext_list() pool = exts[PRODUCT].pool_get(pool1) self.assertEqual(len(pool.list(pool1)), 3)
def action_resource(rsrc): """ Handle resource actions. A resource can be destroyed, cloned or its IP address determined. """ exts = ext.api_ext_list() LOGGER.info("%s: status %s action %s at %s", rsrc.name, models.Resource.status_to_str(rsrc.status), rsrc.action, rsrc.action_time.strftime("%Y-%m-%d %H:%M:%S")) try: if rsrc.action == algo.ACTION_DESTROY: action_destroy(exts, rsrc) elif rsrc.action == algo.ACTION_CLONE: action_clone(exts, rsrc) elif rsrc.action == algo.ACTION_ATTR: action_attr(exts, rsrc) elif rsrc.action == algo.ACTION_NONE: pass except models.Pool.DoesNotExist: LOGGER.debug("action %s deleted because pool missing.", rsrc.name) rsrc.delete()
def main(args): """ Main entry point for server. """ count = args.count LOGGER.info("testpool server started") if count != FOREVER and count < 0: raise ValueError("count should be a positive number or FOREVER") ## # Restart the daemon if extensions change. Keep a handle to the extensions. # if the list of extensions changes, this daemon must be restarted to # retrive the new extensions. exts = ext.api_ext_list() if args.setup: exceptions.try_catch(coding.Curry(setup, exts)) else: LOGGER.info("testpool server setup skipped") exceptions.try_catch(coding.Curry(adapt, exts)) ## while count == FOREVER or count > 0: events_show("Resources") if mode_test_stop(args): return 0 ## # Retrieve resources. For action items that are ready run the # required action. current = datetime.datetime.now() LOGGER.info("checking actions %s", current) rsrcs = models.Resource.objects.exclude(status=models.Resource.READY) rsrcs = rsrcs.order_by("action_time") if not rsrcs: LOGGER.info("testpool no actions sleeping %s (seconds)", args.max_sleep_time) time.sleep(args.max_sleep_time) continue ## # Check the first action. If its not ready to fire then all # actions are not ready to fire. Calculate the amount of time # to sleep but take into consideration the minimum and maximum # sleep times as well. # # The maximum amount of sleep time should be reasonable so that # changes to the database will be detected in a reasonable amount # of time. rsrc = rsrcs.first() if rsrc.action_time > current and args.max_sleep_time != 0: action_delay = abs(rsrc.action_time - current).seconds sleep_time = min(args.max_sleep_time, action_delay) sleep_time = max(args.min_sleep_time, sleep_time) LOGGER.info("testpool sleeping %s (seconds)", sleep_time) time.sleep(sleep_time) continue LOGGER.info("testpool actions fired") for rsrc in rsrcs: if rsrc.action_time < current or args.max_sleep_time == 0: exceptions.try_catch(coding.Curry(action_resource, rsrc)) else: break if count != FOREVER: count -= 1 LOGGER.info("testpool server stopped") return 0