from copy import deepcopy
import unittest
from unittest.mock import patch

from deployment_kit import storage
from deployment_kit.site_guard import verify
from deployment_kit.core import KitError


class TrainingStorageTest(unittest.TestCase):
    def value(self):
        return dict(schema=1,install_root='/workspace/contextra/runtime',data_root='/workspace/contextra/data',
                    storage={k:dict(physical='/volumes/'+k,logical='/workspace/contextra/data/'+k,container=v[0])
                             for k,v in storage.LAYOUT.items()})

    def test_optional_training_and_sibling_roots(self):
        value=self.value();storage.validate_map(value)
        del value['storage']['training'];storage.validate_map(value)
        value['install_root']='/workspace/contextra'
        with self.assertRaises(KitError):storage.validate_map(value)

    def test_guard_missing_view_refuses_start(self):
        value=self.value()
        with patch('deployment_kit.site_guard.site.load',return_value=value), \
             patch('deployment_kit.site_guard.storage.checks'), \
             patch('deployment_kit.site_guard.Path.exists',return_value=True), \
             patch('deployment_kit.site_guard.os.path.samefile',return_value=False):
            with self.assertRaises(KitError):verify('fixture')

    def test_mount_identity_check_cannot_be_bypassed(self):
        value=self.value()
        with patch('deployment_kit.site_guard.site.load',return_value=value), \
             patch('deployment_kit.site_guard.storage.checks',side_effect=RuntimeError('wrong filesystem')):
            with self.assertRaises(RuntimeError):verify('fixture')


if __name__=='__main__':unittest.main()
