Exemplo n.º 1
0
class TestPrepareTemporaryDirectory(unittest.TestCase):
    def setUp(self):
        self.docker_client = mock.Mock()
        self.log = mock.Mock()
        self.image = "whatever"
        self.squash = Squash(self.log, self.image, self.docker_client)

    @mock.patch('docker_scripts.squash.tempfile')
    def test_create_tmp_directory_if_not_provided(self, mock_tempfile):
        self.squash._prepare_tmp_directory(None)
        mock_tempfile.mkdtemp.assert_called_with(prefix="docker-squash-")

    @mock.patch('docker_scripts.squash.tempfile')
    @mock.patch('docker_scripts.squash.os.path.exists', return_value=True)
    def test_should_raise_if_directory_already_exists(self, mock_path,
                                                      mock_tempfile):
        with self.assertRaises(SquashError) as cm:
            self.squash._prepare_tmp_directory('tmp')
        self.assertEquals(
            str(cm.exception),
            "The 'tmp' directory already exists, please remove it before you proceed"
        )
        mock_path.assert_called_with('tmp')
        self.assertTrue(len(mock_tempfile.mkdtemp.mock_calls) == 0)

    @mock.patch('docker_scripts.squash.os.path.exists', return_value=False)
    @mock.patch('docker_scripts.squash.os.makedirs', return_value=False)
    def test_should_use_provided_tmp_dir(self, mock_makedirs, mock_path):
        self.assertEqual(self.squash._prepare_tmp_directory('tmp'), 'tmp')
        mock_path.assert_called_with('tmp')
        mock_makedirs.assert_called_with('tmp')
Exemplo n.º 2
0
class TestPrepareTemporaryDirectory(unittest.TestCase):

    def setUp(self):
        self.docker_client = mock.Mock()
        self.log = mock.Mock()
        self.image = "whatever"
        self.squash = Squash(self.log, self.image, self.docker_client)

    @mock.patch('docker_scripts.squash.tempfile')
    def test_create_tmp_directory_if_not_provided(self, mock_tempfile):
        self.squash._prepare_tmp_directory(None)
        mock_tempfile.mkdtemp.assert_called_with(prefix="docker-squash-")

    @mock.patch('docker_scripts.squash.tempfile')
    @mock.patch('docker_scripts.squash.os.path.exists', return_value=True)
    def test_should_raise_if_directory_already_exists(self, mock_path, mock_tempfile):
        with self.assertRaises(SquashError) as cm:
            self.squash._prepare_tmp_directory('tmp')
        self.assertEquals(
            str(cm.exception), "The 'tmp' directory already exists, please remove it before you proceed")
        mock_path.assert_called_with('tmp')
        self.assertTrue(len(mock_tempfile.mkdtemp.mock_calls) == 0)

    @mock.patch('docker_scripts.squash.os.path.exists', return_value=False)
    @mock.patch('docker_scripts.squash.os.makedirs', return_value=False)
    def test_should_use_provided_tmp_dir(self, mock_makedirs, mock_path):
        self.assertEqual(self.squash._prepare_tmp_directory('tmp'), 'tmp')
        mock_path.assert_called_with('tmp')
        mock_makedirs.assert_called_with('tmp')