def test_get_package_by_definition_except_io_error(self, mock_tempfile,
                                                       mock_log):
        fqn = 'io.murano.apps.test'
        path, _ = utils.compose_package('test',
                                        self.location,
                                        archive_dir=self.location)
        with open(path, 'rb') as f:
            package_data = f.read()

        package = mock.MagicMock()
        package.fully_qualified_name = fqn
        package.id = '123'
        package.version = '0.0.1'

        self.murano_client.packages.download = mock.MagicMock(
            return_value=package_data)
        mock_package_file = mock.MagicMock(write=mock.MagicMock(
            side_effect=IOError))
        mock_package_file.configure_mock(name='test_package_file')
        mock_tempfile.NamedTemporaryFile().__enter__.return_value =\
            mock_package_file

        expected_error_msg = 'Unable to extract package data for {0}'\
                             .format(package.id)
        with self.assertRaisesRegex(pkg_exc.PackageLoadError,
                                    expected_error_msg):
            self.loader._get_package_by_definition(package)
    def test_get_package_by_definition_except_http_exception(self):
        fqn = 'io.murano.apps.test'
        path, _ = utils.compose_package('test',
                                        self.location,
                                        archive_dir=self.location)

        package = mock.MagicMock()
        package.fully_qualified_name = fqn
        package.id = '123'
        package.version = '0.0.1'

        self.murano_client.packages.download.side_effect =\
            muranoclient_exc.HTTPException

        expected_error_msg = 'Error loading package id {0}:'.format(package.id)
        with self.assertRaisesRegex(pkg_exc.PackageLoadError,
                                    expected_error_msg):
            self.loader._get_package_by_definition(package)
    def test_load_package_with_get_definiton(self, mock_to_dsl_package):
        fqn = 'io.murano.apps.test_package'
        package = mock.MagicMock()
        package.id = 'test_package_id'
        package.name = 'test_package_name'
        package.fully_qualified_name = fqn
        package.version = '2.5.3'

        path, _ = utils.compose_package('test_package',
                                        self.location,
                                        archive_dir=self.location,
                                        version=package.version)
        with open(path, 'rb') as f:
            package_data = f.read()

        self.murano_client.packages.filter = mock.MagicMock(
            return_value=[package])
        self.murano_client.packages.download = mock.MagicMock(
            return_value=package_data)
        mock_to_dsl_package.return_value = package

        spec = semantic_version.Spec('*')
        retrieved_pkg = self.loader.load_package(fqn, spec)
        self.assertEqual(retrieved_pkg, package)

        self.assertTrue(os.path.isdir(os.path.join(self.location, fqn)))
        self.assertTrue(
            os.path.isdir(os.path.join(self.location, fqn, package.version)))
        self.assertTrue(
            os.path.isdir(
                os.path.join(self.location, fqn, package.version, package.id)))
        self.assertTrue(
            os.path.isfile(
                os.path.join(self.location, fqn, package.version, package.id,
                             'manifest.yaml')))
        self.murano_client.packages.download.assert_called_once_with(
            package.id)

        expected_fixations = collections.defaultdict(set)
        expected_fixations[fqn] = set(
            [semantic_version.Version(package.version)])
        self.assertEqual(expected_fixations, self.loader._fixations)
        self.loader.cleanup()
    def test_get_package_by_definition_except_package_load_error(
            self, mock_load_utils, mock_os, mock_log):
        # Test that the first instance of the exception is caught.
        temp_directory = tempfile.mkdtemp(prefix='test-package-loader-',
                                          dir=tempfile.tempdir)
        mock_os.path.isdir.return_value = True
        mock_os.path.join.return_value = temp_directory
        mock_load_utils.load_from_dir.side_effect = pkg_exc.PackageLoadError

        fqn = 'io.murano.apps.test'
        path, _ = utils.compose_package('test',
                                        self.location,
                                        archive_dir=self.location)
        with open(path, 'rb') as f:
            package_data = f.read()

        package = mock.MagicMock()
        package.fully_qualified_name = fqn
        package.id = '123'
        package.version = '0.0.1'

        self.murano_client.packages.download = mock.MagicMock(
            return_value=package_data)

        self.loader._get_package_by_definition(package)

        mock_log.exception.assert_called_once_with(
            'Unable to load package from cache. Clean-up.')
        mock_log.exception.reset_mock()

        # Test that the second instance of the exception is caught.
        mock_os.path.isdir.return_value = [False, True]

        self.loader._get_package_by_definition(package)

        mock_log.exception.assert_called_once_with(
            'Unable to load package from cache. Clean-up.')
        os.remove(temp_directory)
    def test_load_class_package_with_get_definition(self):
        fqn = 'io.murano.apps.test'
        path, name = utils.compose_package('test',
                                           self.location,
                                           archive_dir=self.location)
        with open(path, 'rb') as f:
            package_data = f.read()
        spec = semantic_version.Spec('*')

        first_id, second_id, third_id = '123', '456', '789'
        package = mock.MagicMock()
        package.fully_qualified_name = fqn
        package.id = first_id
        package.version = '0.0.1'

        self.murano_client.packages.filter = mock.MagicMock(
            return_value=[package])
        self.murano_client.packages.download = mock.MagicMock(
            return_value=package_data)

        # load the package
        self.loader.load_class_package(fqn, spec)

        # assert that everything got created
        self.assertTrue(os.path.isdir(os.path.join(self.location, fqn)))
        self.assertTrue(
            os.path.isdir(os.path.join(self.location, fqn, package.version)))
        self.assertTrue(
            os.path.isdir(
                os.path.join(self.location, fqn, package.version, first_id)))
        self.assertTrue(
            os.path.isfile(
                os.path.join(self.location, fqn, package.version, first_id,
                             'manifest.yaml')))

        # assert that we called download
        self.assertEqual(self.murano_client.packages.download.call_count, 1)

        # now that the cache is in place, call it for the 2d time
        self.loader._package_cache = {}
        self.loader._class_cache = {}
        self.loader.load_class_package(fqn, spec)

        # check that we didn't download a thing
        self.assertEqual(self.murano_client.packages.download.call_count, 1)

        # changing id, new package would be downloaded.
        package.id = second_id
        self.loader._package_cache = {}
        self.loader._class_cache = {}
        self.loader.load_class_package(fqn, spec)

        # check that we didn't download a thing
        self.assertEqual(self.murano_client.packages.download.call_count, 2)

        # check that old directories were not deleted
        # we did not call cleanup and did not release the locks
        self.assertTrue(
            os.path.isdir(
                os.path.join(self.location, fqn, package.version, first_id)))

        # check that new directories got created correctly
        self.assertTrue(os.path.isdir(os.path.join(self.location, fqn)))
        self.assertTrue(
            os.path.isdir(os.path.join(self.location, fqn, package.version)))
        self.assertTrue(
            os.path.isdir(
                os.path.join(self.location, fqn, package.version, second_id)))
        self.assertTrue(
            os.path.isfile(
                os.path.join(self.location, fqn, package.version, second_id,
                             'manifest.yaml')))

        self.assertTrue(
            os.path.isdir(os.path.join(self.location, fqn, package.version)))
        self.assertTrue(
            os.path.isdir(
                os.path.join(self.location, fqn, package.version, second_id)))

        # changing id, new package would be downloaded.
        package.id = third_id
        self.loader._package_cache = {}
        self.loader._class_cache = {}

        # release all the locks
        self.loader.cleanup()
        self.loader.load_class_package(fqn, spec)

        # check that we didn't download a thing
        self.assertEqual(self.murano_client.packages.download.call_count, 3)

        # check that old directories were *deleted*
        self.assertFalse(
            os.path.isdir(
                os.path.join(self.location, fqn, package.version, first_id)))
        self.assertFalse(
            os.path.isdir(
                os.path.join(self.location, fqn, package.version, second_id)))

        # check that new directories got created correctly
        self.assertTrue(
            os.path.isdir(
                os.path.join(self.location, fqn, package.version, third_id)))
        self.assertTrue(
            os.path.isfile(
                os.path.join(self.location, fqn, package.version, third_id,
                             'manifest.yaml')))