'Test group can be created and deleted.'

@mock.patch.object(GROUP_QUOTAS, 'reserve', return_value=['RESERVATION'])
@mock.patch.object(GROUP_QUOTAS, 'commit')
@mock.patch.object(GROUP_QUOTAS, 'rollback')
@mock.patch.object(driver.VolumeDriver, 'delete_group', return_value=({'status': fields.GroupStatus.DELETED}, []))
def test_create_delete_group(self, fake_delete_grp, fake_rollback, fake_commit, fake_reserve):

    def fake_driver_create_grp(context, group):
        'Make    sure    that    the    pool    is    part    of    the    host.'
        self.assertIn('host', group)
        host = group.host
        pool = volutils.extract_host(host, level='pool')
        self.assertEqual('fakepool', pool)
        return {'status': fields.GroupStatus.AVAILABLE}
    self.mock_object(self.volume.driver, 'create_group', fake_driver_create_grp)
    group = tests_utils.create_group(self.context, availability_zone=CONF.storage_availability_zone, volume_type_ids=[fake.VOLUME_TYPE_ID], host='fakehost@fakedrv#fakepool', group_type_id=fake.GROUP_TYPE_ID)
    group = objects.Group.get_by_id(self.context, group.id)
    self.assertEqual(0, len(self.notifier.notifications), self.notifier.notifications)
    self.volume.create_group(self.context, group)
    self.assertEqual(2, len(self.notifier.notifications), self.notifier.notifications)
    msg = self.notifier.notifications[0]
    self.assertEqual('group.create.start', msg['event_type'])
    expected = {'status': fields.GroupStatus.AVAILABLE, 'name': 'test_group', 'availability_zone': 'nova', 'tenant_id': self.context.project_id, 'created_at': mock.ANY, 'user_id': fake.USER_ID, 'group_id': group.id, 'group_type': fake.GROUP_TYPE_ID}
    self.assertDictEqual(expected, msg['payload'])
    msg = self.notifier.notifications[1]
    self.assertEqual('group.create.end', msg['event_type'])
    self.assertDictEqual(expected, msg['payload'])
    self.assertEqual(group.id, objects.Group.get_by_id(context.get_admin_context(), group.id).id)
    self.volume.delete_group(self.context, group)
    grp = objects.Group.get_by_id(context.get_admin_context(read_deleted='yes'), group.id)
    self.assertEqual(fields.GroupStatus.DELETED, grp.status)
    self.assertEqual(4, len(self.notifier.notifications), self.notifier.notifications)
    msg = self.notifier.notifications[2]
    self.assertEqual('group.delete.start', msg['event_type'])
    self.assertDictEqual(expected, msg['payload'])
    msg = self.notifier.notifications[3]
    self.assertEqual('group.delete.end', msg['event_type'])
    expected['status'] = fields.GroupStatus.DELETED
    self.assertDictEqual(expected, msg['payload'])
    self.assertRaises(exception.NotFound, objects.Group.get_by_id, self.context, group.id)
