def stand_up_cluster(): class Instances(FakeObject): def add(self, *args): for instance in args: self.obj.instance_list.append(instance) def all(self): return self.obj.instance_list class InstanceGroup(FakeObject): def __init__(self, **kwargs): super(InstanceGroup, self).__init__(**kwargs) self.instance_list = [] @property def instances(self): mgr = Instances(obj=self) return mgr class Instance(FakeObject): pass ig_small = InstanceGroup(name='ig_small') ig_large = InstanceGroup(name='ig_large') tower = InstanceGroup(name='tower') i1 = Instance(hostname='i1', capacity=200) i2 = Instance(hostname='i2', capacity=200) i3 = Instance(hostname='i3', capacity=200) ig_small.instances.add(i1) ig_large.instances.add(i2, i3) tower.instances.add(i2) return [tower, ig_large, ig_small]
def test_fit_task_to_most_remaining_capacity_instance(self, task, instances, instance_fit_index, reason): InstanceGroup(id=10) tm_igs = TaskManagerInstanceGroups(instance_groups={'controlplane': {'instances': instances}}) instance_picked = tm_igs.fit_task_to_most_remaining_capacity_instance(task, 'controlplane') if instance_fit_index is None: assert instance_picked is None, reason else: assert instance_picked == instances[instance_fit_index], reason
def test_fit_task_to_most_remaining_capacity_instance( self, task, instances, instance_fit_index, reason): ig = InstanceGroup(id=10) instance_picked = ig.fit_task_to_most_remaining_capacity_instance( task, instances) if instance_fit_index is None: assert instance_picked is None, reason else: assert instance_picked == instances[instance_fit_index], reason
def handle(self, **options): queuename = options.get('queuename') if not queuename: raise CommandError("Specify `--queuename` to use this command.") changed = False with advisory_lock('instance_group_registration_%s' % queuename): ig = InstanceGroup.objects.filter(name=queuename) control_ig = None if options.get('controller'): control_ig = InstanceGroup.objects.filter( name=options.get('controller')).first() if ig.exists(): print("Instance Group already registered {}".format( ig[0].name)) ig = ig[0] if control_ig and ig.controller_id != control_ig.pk: ig.controller = control_ig ig.save() print("Set controller group {} on {}.".format( control_ig.name, ig.name)) changed = True else: print("Creating instance group {}".format(queuename)) ig = InstanceGroup( name=queuename, policy_instance_percentage=options.get('instance_percent'), policy_instance_minimum=options.get('instance_minimum')) if control_ig: ig.controller = control_ig ig.save() changed = True hostname_list = [] if options.get('hostnames'): hostname_list = options.get('hostnames').split(",") instance_list = [x.strip() for x in hostname_list if x] for inst_name in instance_list: instance = Instance.objects.filter(hostname=inst_name) if instance.exists() and instance[0] not in ig.instances.all(): ig.instances.add(instance[0]) print("Added instance {} to {}".format( instance[0].hostname, ig.name)) changed = True elif not instance.exists(): print("Instance does not exist: {}".format(inst_name)) if changed: print('(changed: True)') sys.exit(1) else: print("Instance already registered {}".format( instance[0].hostname)) ig.policy_instance_list = instance_list ig.save() if changed: print('(changed: True)')
def test_find_largest_idle_instance(self, instances, instance_fit_index, reason): def filter_offline_instances(*args): return filter(lambda i: i.capacity > 0, instances) InstanceGroup(id=10) instances_online_only = filter_offline_instances(instances) tm_igs = TaskManagerInstanceGroups(instance_groups={'controlplane': {'instances': instances_online_only}}) if instance_fit_index is None: assert tm_igs.find_largest_idle_instance('controlplane') is None, reason else: assert tm_igs.find_largest_idle_instance('controlplane') == instances[instance_fit_index], reason
def test_fit_task_to_most_remaining_capacity_instance(self, task, instances, instance_fit_index, reason): with mock.patch.object(InstanceGroup, 'instances', Mock(spec_set=['filter'], filter=lambda *args, **kargs: Mock(spec_set=['order_by'], order_by=lambda x: instances))): ig = InstanceGroup(id=10) if instance_fit_index is None: assert ig.fit_task_to_most_remaining_capacity_instance(task) is None, reason else: assert ig.fit_task_to_most_remaining_capacity_instance(task) == \ instances[instance_fit_index], reason
def test_find_largest_idle_instance(self, instances, instance_fit_index, reason): def filter_offline_instances(*args): return filter(lambda i: i.capacity > 0, instances) ig = InstanceGroup(id=10) instances_online_only = filter_offline_instances(instances) if instance_fit_index is None: assert ig.find_largest_idle_instance( instances_online_only) is None, reason else: assert ig.find_largest_idle_instance( instances_online_only) == instances[instance_fit_index], reason
def test_containerized_group_default_fields(instance_group, kube_credential): ig = InstanceGroup(name="test_policy_field_defaults") ig.policy_instance_list = [1] ig.policy_instance_minimum = 5 ig.policy_instance_percentage = 5 ig.save() assert ig.policy_instance_list == [1] assert ig.policy_instance_minimum == 5 assert ig.policy_instance_percentage == 5 ig.credential = kube_credential ig.save() assert ig.policy_instance_list == [] assert ig.policy_instance_minimum == 0 assert ig.policy_instance_percentage == 0
def test_find_largest_idle_instance(self, instances, instance_fit_index, reason): def filter_offline_instances(*args): return filter(lambda i: i.capacity > 0, instances) with mock.patch.object(InstanceGroup, 'instances', Mock(spec_set=['filter'], filter=lambda *args, **kargs: Mock(spec_set=['order_by'], order_by=filter_offline_instances))): ig = InstanceGroup(id=10) if instance_fit_index is None: assert ig.find_largest_idle_instance() is None, reason else: assert ig.find_largest_idle_instance() == \ instances[instance_fit_index], reason
def isolated_instance_group(instance_group, instance): ig = InstanceGroup(name="iso", controller=instance_group) ig.save() ig.instances.set([instance]) ig.save() return ig
def instance_group(job_factory): ig = InstanceGroup(name="east") ig.save() return ig
def tower_instance_group(): ig = InstanceGroup(name='tower') ig.save() return ig
def containerized_instance_group(instance_group, kube_credential): ig = InstanceGroup(name="container") ig.credential = kube_credential ig.is_container_group = True ig.save() return ig
def container_group(): instance_group = mock.Mock(InstanceGroup(name='container-group')) return instance_group
def isolated_instance_group(instance_group): ig = InstanceGroup(name="iso", controller=instance_group) ig.save() return ig