'Test group_snapshot can be created and deleted.'

@ddt.data((CONF.host, None), ((CONF.host + 'fake'), 'mycluster'))
@ddt.unpack
@mock.patch('cinder.tests.unit.fake_notifier.FakeNotifier._notify')
@mock.patch('cinder.volume.driver.VolumeDriver.create_group', autospec=True, return_value={'status': 'available'})
@mock.patch('cinder.volume.driver.VolumeDriver.delete_group', autospec=True, return_value=({'status': 'deleted'}, []))
@mock.patch('cinder.volume.driver.VolumeDriver.create_group_snapshot', autospec=True, return_value=({'status': 'available'}, []))
@mock.patch('cinder.volume.driver.VolumeDriver.delete_group_snapshot', autospec=True, return_value=({'status': 'deleted'}, []))
def test_create_delete_group_snapshot(self, host, cluster, mock_del_grpsnap, mock_create_grpsnap, mock_del_grp, _mock_create_grp, mock_notify):

    self.volume.cluster = cluster
    group = tests_utils.create_group(self.context, cluster_name=cluster, availability_zone=CONF.storage_availability_zone, volume_type_ids=[fake.VOLUME_TYPE_ID], group_type_id=fake.GROUP_TYPE_ID, host=host)
    volume = tests_utils.create_volume(self.context, group_id=group.id, host=group.host, cluster_name=group.cluster_name, volume_type_id=fake.VOLUME_TYPE_ID)
    self.volume.create_volume(self.context, volume)
    self.assert_notify_called(mock_notify, (['INFO', 'volume.create.start'], ['INFO', 'volume.create.end']))
    group_snapshot_returns = self._create_group_snapshot(group.id, [volume.id])
    group_snapshot = group_snapshot_returns[0]
    self.volume.create_group_snapshot(self.context, group_snapshot)
    self.assertEqual(group_snapshot.id, objects.GroupSnapshot.get_by_id(context.get_admin_context(), group_snapshot.id).id)
    self.assert_notify_called(mock_notify, (['INFO', 'volume.create.start'], ['INFO', 'volume.create.end'], ['INFO', 'group_snapshot.create.start'], ['INFO', 'snapshot.create.start'], ['INFO', 'group_snapshot.create.end'], ['INFO', 'snapshot.create.end']))
    self.volume.delete_group_snapshot(self.context, group_snapshot)
    self.assert_notify_called(mock_notify, (['INFO', 'volume.create.start'], ['INFO', 'volume.create.end'], ['INFO', 'group_snapshot.create.start'], ['INFO', 'snapshot.create.start'], ['INFO', 'group_snapshot.create.end'], ['INFO', 'snapshot.create.end'], ['INFO', 'group_snapshot.delete.start'], ['INFO', 'snapshot.delete.start'], ['INFO', 'group_snapshot.delete.end'], ['INFO', 'snapshot.delete.end']))
    grpsnap = objects.GroupSnapshot.get_by_id(context.get_admin_context(read_deleted='yes'), group_snapshot.id)
    self.assertEqual('deleted', grpsnap.status)
    self.assertRaises(exception.NotFound, objects.GroupSnapshot.get_by_id, self.context, group_snapshot.id)
    self.volume.delete_group(self.context, group)
    self.assertTrue(mock_create_grpsnap.called)
    self.assertTrue(mock_del_grpsnap.called)
    self.assertTrue(mock_del_grp.called)
