def test_creates(self): """Ensures entity gets created.""" def fetch(*args, **kwargs): return ['url/name'] self.mock(instances, 'fetch', fetch) key = instances.get_instance_key( 'base-name', 'revision', 'zone', 'name', ) models.InstanceGroupManager( key=key.parent(), url='url', ).put() expected_instances = [ key, ] expected_url = 'url/name' instances.ensure_entities_exist(key.parent()) self.assertItemsEqual(key.parent().get().instances, expected_instances) self.assertEqual(key.get().url, expected_url)
def test_already_exists(self): """Ensures nothing happens when the entity already exists.""" def fetch(*args, **kwargs): return ['url/name'] self.mock(instances, 'fetch', fetch) key = models.Instance( key=instances.get_instance_key( 'base-name', 'revision', 'zone', 'name', ), ).put() models.InstanceGroupManager( key=key.parent(), url='url', ).put() expected_instances = [ key, ] instances.ensure_entities_exist(key.parent()) self.failIf(key.get().url) self.assertItemsEqual(key.parent().get().instances, expected_instances)
def test_creates(self): """Ensures entity gets created.""" def fetch(*args, **kwargs): return ['url/name'] def send_machine_event(*args, **kwargs): pass self.mock(instances, 'fetch', fetch) self.mock(instances.metrics, 'send_machine_event', send_machine_event) key = instances.get_instance_key( 'base-name', 'revision', 'zone', 'name', ) models.InstanceGroupManager( key=instances.get_instance_group_manager_key(key), url='url', ).put() expected_instances = [ key, ] expected_url = 'url/name' instances.ensure_entities_exist( instances.get_instance_group_manager_key(key)) self.assertItemsEqual( instances.get_instance_group_manager_key(key).get().instances, expected_instances, ) self.assertEqual(key.get().url, expected_url)
def post(self): """Fetches instances for the given InstanceGroupManager. Params: key: URL-safe key for a models.InstanceGroupManager. """ key = ndb.Key(urlsafe=self.request.get('key')) assert key.kind() == 'InstanceGroupManager', key instances.ensure_entities_exist(key)
def test_url_unspecified(self): """Ensures nothing happens when URL is unspecified.""" key = models.InstanceGroupManager( key=instance_group_managers.get_instance_group_manager_key( 'base-name', 'revision', 'zone', ), ).put() instances.ensure_entities_exist(key) self.failIf(key.get().instances)
def test_no_instances(self): """Ensures nothing happens when there are no instances.""" def fetch(*args, **kwargs): return [] self.mock(instances, 'fetch', fetch) key = models.InstanceGroupManager( key=instance_group_managers.get_instance_group_manager_key( 'base-name', 'revision', 'zone', ), url='url', ).put() instances.ensure_entities_exist(key) self.failIf(key.get().instances)
def test_entity_doesnt_exist(self): """Ensures nothing happens when the entity doesn't exist.""" key = ndb.Key(models.InstanceGroupManager, 'fake-key') instances.ensure_entities_exist(key) self.failIf(key.get())