# Path: planb/factories.py
# class FilesetFactory(DjangoModelFactory):
#     host_prefix = factory.Faker('hostname', levels=0)
#     host_suffix = factory.Faker('domain_word')
#     tld = factory.Faker('tld')
# 
#     @factory.lazy_attribute
#     def friendly_name(self):
#         # Set friendly name as the full hostname within the hostgroup domain.
#         return '.'.join((
#             self.host_prefix, self.host_suffix, self.hostgroup.name, self.tld))
# 
#     storage_alias = 'dummy'
#     hostgroup = factory.SubFactory(HostGroupFactory)
# 
#     class Meta:
#         model = 'planb.Fileset'
#         exclude = ['host_prefix', 'host_suffix', 'tld']
# 
# Path: planb/tests/base.py
# class PlanbTestCase(TestCase):
#     def setUp(self):
#         super().setUp()
# 
#         # Reset storage_pools, otherwise we might get stale data from
#         # previous dummy's. This is a dict, that everyone has loaded
#         # already. Flush the contents of the dict.
#         for storage in storage_pools.values():
#             storage.close()
#         storage_pools.clear()
#         storage_pools.update(load_storage_pools())


import datetime
from unittest.mock import patch
from django.test import override_settings
from django.utils import timezone
from planb.factories import FilesetFactory
from planb.tests.base import PlanbTestCase
            fileset.rename_dataset(new_name)

            mock_zfs.assert_any_call(
                ('get', '-Ho', 'value', 'mountpoint', old_name))
            mock_zfs.assert_any_call(
                ('rename', old_name, new_name))
            mock_zfs.assert_any_call(
                ('get', '-Ho', 'value', 'mountpoint', new_name))

            mock_rmdir.assert_called_with('/' + old_name)  # old mountpoint
            mock_mkdir.assert_called_with('/' + new_name)  # new mountpoint

        self.assertEqual(fileset.dataset_name, new_name)

    def test_snapshot_create(self):
        fileset = FilesetFactory(storage_alias='dummy')
        with patch('planb.models.datetime') as m:
            m.utcnow.return_value = datetime.datetime(2020, 5, 3, 14, 42)
            # snapshot create will always create a snapshot.
            # The snapshot name is a utc timestamp.
            self.assertEqual(
                fileset.snapshot_create(), 'planb-20200503T1442Z')
            m.utcnow.return_value = datetime.datetime(2020, 5, 3, 15, 31)
            self.assertEqual(
                fileset.snapshot_create(), 'planb-20200503T1531Z')

    @override_settings(PLANB_BLACKLIST_HOURS='9-17')
    @patch('planb.models.timezone')
    def test_blacklist_hours(self, timezone):
        # Using system defaults.

# Path: planb/factories.py
# class FilesetFactory(DjangoModelFactory):
#     host_prefix = factory.Faker('hostname', levels=0)
#     host_suffix = factory.Faker('domain_word')
#     tld = factory.Faker('tld')
# 
#     @factory.lazy_attribute
#     def friendly_name(self):
#         # Set friendly name as the full hostname within the hostgroup domain.
#         return '.'.join((
#             self.host_prefix, self.host_suffix, self.hostgroup.name, self.tld))
# 
#     storage_alias = 'dummy'
#     hostgroup = factory.SubFactory(HostGroupFactory)
# 
#     class Meta:
#         model = 'planb.Fileset'
#         exclude = ['host_prefix', 'host_suffix', 'tld']
# 
# Path: planb/tests/base.py
# class PlanbTestCase(TestCase):
#     def setUp(self):
#         super().setUp()
# 
#         # Reset storage_pools, otherwise we might get stale data from
#         # previous dummy's. This is a dict, that everyone has loaded
#         # already. Flush the contents of the dict.
#         for storage in storage_pools.values():
#             storage.close()
#         storage_pools.clear()
#         storage_pools.update(load_storage_pools())


# Path: planb/tests/test_models.py
import datetime
from unittest.mock import patch
from django.test import override_settings
from django.utils import timezone
from planb.factories import FilesetFactory
from planb.tests.base import PlanbTestCase

            fileset.rename_dataset(new_name)

            mock_zfs.assert_any_call(
                ('get', '-Ho', 'value', 'mountpoint', old_name))
            mock_zfs.assert_any_call(
                ('rename', old_name, new_name))
            mock_zfs.assert_any_call(
                ('get', '-Ho', 'value', 'mountpoint', new_name))

            mock_rmdir.assert_called_with('/' + old_name)  # old mountpoint
            mock_mkdir.assert_called_with('/' + new_name)  # new mountpoint

        self.assertEqual(fileset.dataset_name, new_name)

    def test_snapshot_create(self):
        fileset = FilesetFactory(storage_alias='dummy')
        with patch('planb.models.datetime') as m:
            m.utcnow.return_value = datetime.datetime(2020, 5, 3, 14, 42)
            # snapshot create will always create a snapshot.
            # The snapshot name is a utc timestamp.
            self.assertEqual(
                fileset.snapshot_create(), 'planb-20200503T1442Z')
            m.utcnow.return_value = datetime.datetime(2020, 5, 3, 15, 31)
            self.assertEqual(
                fileset.snapshot_create(), 'planb-20200503T1531Z')

    @override_settings(PLANB_BLACKLIST_HOURS='9-17')
    @patch('planb.models.timezone')
    def test_blacklist_hours(self, timezone):
        # Using system defaults.

        fileset = FilesetFactory()