def test_add_package_without_matching_package_type(self):
        """Testing PendingInstall.add_package without matching package type"""
        pending_install = PendingInstall()

        bundle = PackageBundle(name='MyPackage')
        channel = PackageChannel(bundle, name='1.0.x')
        channel._loaded = True
        bundle._channels = [channel]

        release = PackageRelease(channel=channel, version='1.0')
        channel._releases = [release]

        rules = PackageRules(channel=channel,
                             version_range='*',
                             package_type='rpm',
                             package_name='TestPackage',
                             systems=['*'])
        channel._package_rules = [rules]

        self.assertRaises(
            PackageInstallError,
            lambda: pending_install.add_package(release, 'python'))

        self.assertEqual(pending_install._bundle_infos, [])
        self.assertEqual(pending_install._bundle_infos_map, {})
    def test_get_latest_release_for_version_range_with_channel_types(self):
        """Testing PackageBundle.get_latest_release_for_version_range with
        channel_types
        """
        bundle = PackageBundle(manifest_url="packages/TestPackage/index.json", name="MyPackage")
        bundle._loaded = True

        channel1 = PackageChannel(
            bundle=bundle, name="2.0.x", current=False, channel_type=PackageChannel.CHANNEL_TYPE_PRERELEASE
        )
        channel1._loaded = True
        channel2 = PackageChannel(
            bundle=bundle, name="1.0.x", current=True, channel_type=PackageChannel.CHANNEL_TYPE_RELEASE
        )
        channel2._loaded = True
        bundle._channels = [channel1, channel2]

        release1 = PackageRelease(channel=channel1, version="2.0")
        channel1._releases = [release1]

        release2 = PackageRelease(channel=channel2, version="1.0")
        channel2._releases = [release2]

        self.assertEqual(
            bundle.get_latest_release_for_version_range(
                version_range=">=1.0", channel_types=[PackageChannel.CHANNEL_TYPE_RELEASE]
            ),
            release2,
        )
        self.assertEqual(
            bundle.get_latest_release_for_version_range(">=2.0", channel_types=[PackageChannel.CHANNEL_TYPE_RELEASE]),
            None,
        )
    def test_current_channel_with_no_current(self):
        """Testing PackageBundle.current_channel with no current channel"""
        bundle = PackageBundle(manifest_url="packages/TestPackage/index.json")
        bundle._loaded = True

        channel = PackageChannel(
            bundle=bundle, name="staging", current=False, visible=False, manifest_url="staging.json"
        )
        bundle._channels = [channel]

        self.assertEqual(bundle.current_channel, None)
    def test_current_channel(self):
        """Testing PackageBundle.current_channel"""
        bundle = PackageBundle(manifest_url="packages/TestPackage/index.json")
        bundle._loaded = True

        channel1 = PackageChannel(bundle=bundle, name="beta", current=False, visible=True, manifest_url="beta.json")
        channel2 = PackageChannel(bundle=bundle, name="1.0.x", current=True, visible=True, manifest_url="1.0.x.json")
        channel3 = PackageChannel(bundle=bundle, name="0.5.x", current=False, visible=True, manifest_url="0.5.x.json")
        bundle._channels = [channel1, channel2, channel3]

        self.assertEqual(bundle.current_channel, channel2)
    def test_serialize(self):
        """Testing PackageBundle.serialize"""
        bundle = PackageBundle(
            manifest_url="packages/TestPackage/index.json",
            name="TestPackage",
            description="This is a summary.\n\nThis is a description.",
            created_timestamp=datetime(2015, 10, 10, 8, 17, 29, 958569),
            last_updated_timestamp=datetime(2015, 10, 15, 8, 17, 29, 958569),
            current_version="1.0.0",
            channel_aliases={"stable": "1.0.x", "beta": "2.0.x"},
            package_names=[{"system": ["centos", "rhel"], "name": "reviewboard", "type": "rpm"}],
        )
        bundle._loaded = True

        channel = PackageChannel(
            bundle=bundle,
            name="1.0.x",
            created_timestamp=datetime(2015, 10, 11, 8, 17, 29, 958569),
            last_updated_timestamp=datetime(2015, 10, 12, 8, 17, 29, 958569),
            latest_version="1.0.0",
            current=True,
            visible=True,
            manifest_url="1.0.x.json",
        )
        bundle._channels = [channel]

        self.assertEqual(
            bundle.serialize(),
            {
                "format_version": "1.0",
                "name": "TestPackage",
                "description": ["This is a summary.", "", "This is a description."],
                "created_timestamp": "2015-10-10T08:17:29.958569",
                "last_updated_timestamp": "2015-10-15T08:17:29.958569",
                "current_version": "1.0.0",
                "package_names": [{"system": ["centos", "rhel"], "name": "reviewboard", "type": "rpm"}],
                "channel_aliases": {"stable": "1.0.x", "beta": "2.0.x"},
                "channels": [
                    {
                        "name": "1.0.x",
                        "created_timestamp": "2015-10-11T08:17:29.958569",
                        "last_updated_timestamp": "2015-10-12T08:17:29.958569",
                        "latest_version": "1.0.0",
                        "channel_type": "release",
                        "current": True,
                        "visible": True,
                        "manifest_file": "1.0.x.json",
                    }
                ],
            },
        )
    def test_get_latest_release_for_version_range(self):
        """Testing PackageBundle.get_latest_release_for_version_range"""
        bundle = PackageBundle(manifest_url="packages/TestPackage/index.json", name="MyPackage")
        bundle._loaded = True

        channel = PackageChannel(bundle=bundle, name="staging", current=True)
        channel._loaded = True
        bundle._channels = [channel]

        release1 = PackageRelease(channel=channel, version="1.5")
        release2 = PackageRelease(channel=channel, version="1.0")
        channel._releases = [release1, release2]

        self.assertEqual(bundle.get_latest_release_for_version_range(">=1.0"), release1)
        self.assertEqual(bundle.get_latest_release_for_version_range("<1.0"), None)
    def test_add_package(self):
        """Testing PendingInstall.add_package"""
        pending_install = PendingInstall()

        bundle = PackageBundle(name='MyPackage')
        channel = PackageChannel(bundle, name='1.0.x')
        channel._loaded = True
        bundle._channels = [channel]

        release = PackageRelease(channel=channel, version='1.0')
        channel._releases = [release]

        rules1 = PackageRules(channel=channel,
                              version_range='*',
                              package_type='rpm',
                              package_name='TestPackage',
                              systems=['*'])
        rules2 = PackageRules(channel=channel,
                              version_range='*',
                              package_type='python',
                              package_name='TestPackage',
                              systems=['*'])
        channel._package_rules = [rules1, rules2]

        pending_install.add_package(release, 'python')

        self.assertEqual(
            pending_install._bundle_infos,
            [
                {
                    'bundle': bundle,
                    'release': release,
                    'package_type': 'python',
                    'rules': rules2,
                }
            ])

        self.assertEqual(
            pending_install._bundle_infos_map,
            {
                'MyPackage': {
                    'bundle': bundle,
                    'release': release,
                    'package_type': 'python',
                    'rules': rules2,
                }
            })
    def test_add_package_without_available_rules(self):
        """Testing PendingInstall.add_package without available rules"""
        pending_install = PendingInstall()

        bundle = PackageBundle(name='MyPackage')
        channel = PackageChannel(bundle, name='1.0.x')
        channel._loaded = True
        bundle._channels = [channel]

        release = PackageRelease(channel=channel, version='1.0')
        channel._releases = [release]

        self.assertRaises(
            PackageInstallError,
            lambda: pending_install.add_package(release, 'python'))

        self.assertEqual(pending_install._bundle_infos, [])
        self.assertEqual(pending_install._bundle_infos_map, {})
    def test_get_latest_release_for_version_range_with_release_types(self):
        """Testing PackageBundle.get_latest_release_for_version_range with
        release_types
        """
        bundle = PackageBundle(manifest_url="packages/TestPackage/index.json", name="MyPackage")
        bundle._loaded = True

        channel = PackageChannel(bundle=bundle, name="2.0.x", current=False)
        channel._loaded = True
        bundle._channels = [channel]

        release1 = PackageRelease(channel=channel, version="2.0", release_type=PackageRelease.TYPE_BETA)
        release2 = PackageRelease(channel=channel, version="1.0", release_type=PackageRelease.TYPE_STABLE)
        channel._releases = [release1, release2]

        self.assertEqual(
            bundle.get_latest_release_for_version_range(
                version_range=">=1.0", release_types=[PackageRelease.TYPE_STABLE]
            ),
            release2,
        )
        self.assertEqual(
            bundle.get_latest_release_for_version_range(">=1.0", release_types=[PackageRelease.TYPE_ALPHA]), None
        )
    def test_get_install_order(self):
        """Testing PendingInstall.get_install_order"""
        self.data_loader.path_to_content.update({
            '/packages/DepPackage1/index.json': {
                'format_version': '1.0',
                'name': 'DepPackage1',
                'created_timestamp': '2015-10-11T08:17:29.958569',
                'last_updated_timestamp': '2015-10-12T08:17:29.958569',
                'current_version': '1.5',
                'channels': [
                    {
                        'name': '1.x',
                        'created_timestamp': '2015-10-13T08:17:29.958569',
                        'last_updated_timestamp': '2015-10-14T08:17:29.958569',
                        'latest_version': '1.5',
                        'current': True,
                        'visible': True,
                        'manifest_file': '1.x.json',
                    },
                ],
            },
            '/packages/DepPackage1/1.x.json': {
                'format_version': '1.0',
                'created_timestamp': '2015-10-11T08:17:29.958569',
                'last_updated_timestamp': '2015-10-12T08:17:29.958569',
                'releases': [
                    {
                        'version': '1.5',
                        'type': 'stable',
                        'visible': True,
                    }
                ],
                'package_rules': [
                    {
                        'version_range': '*',
                        'package_type': 'python',
                        'package_name': 'DepPackage1',
                        'systems': ['*'],
                        'dependencies': {
                            'required': [
                                'DepPackage2>=1.5',
                            ],
                        },
                    },
                ],
            },
            '/packages/DepPackage2/index.json': {
                'format_version': '1.0',
                'name': 'DepPackage2',
                'created_timestamp': '2015-10-11T08:17:29.958569',
                'last_updated_timestamp': '2015-10-12T08:17:29.958569',
                'current_version': '1.5',
                'channels': [
                    {
                        'name': '1.x',
                        'created_timestamp': '2015-10-13T08:17:29.958569',
                        'last_updated_timestamp': '2015-10-14T08:17:29.958569',
                        'latest_version': '1.5',
                        'current': True,
                        'visible': True,
                        'manifest_file': '1.x.json',
                    },
                ],
            },
            '/packages/DepPackage2/1.x.json': {
                'format_version': '1.0',
                'created_timestamp': '2015-10-11T08:17:29.958569',
                'last_updated_timestamp': '2015-10-12T08:17:29.958569',
                'releases': [
                    {
                        'version': '1.5',
                        'type': 'stable',
                        'visible': True,
                    },
                ],
                'package_rules': [
                    {
                        'version_range': '*',
                        'package_type': 'python',
                        'package_name': 'DepPackage2',
                        'systems': ['*'],
                    },
                ],
            },
        })

        pending_install = PendingInstall()

        bundle = PackageBundle(name='MyPackage')
        channel = PackageChannel(bundle, name='1.0.x')
        channel._loaded = True
        bundle._channels = [channel]

        release = PackageRelease(channel=channel, version='1.0')
        channel._releases = [release]

        rules = PackageRules(
            channel=channel,
            version_range='*',
            required_dependencies=[
                'DepPackage1>=1.0',
            ],
            package_type='python',
            package_name='TestPackage',
            systems=['*'])
        channel._package_rules = [rules]

        pending_install.add_package(release, 'python')
        pending_install.resolve_dependencies()

        install_order = pending_install.get_install_order()
        self.assertEqual(len(install_order), 3)
        self.assertEqual(install_order[0]['bundle'].name, 'DepPackage2')
        self.assertEqual(install_order[1]['bundle'].name, 'DepPackage1')
        self.assertEqual(install_order[2]['bundle'].name, 'MyPackage')
    def test_resolve_dependencies_with_prerelease(self):
        """Testing PendingInstall.resolve_dependencies with considering
        pre-release packages
        """
        self.data_loader.path_to_content.update({
            '/packages/DepPackage1/index.json': {
                'format_version': '1.0',
                'name': 'DepPackage1',
                'created_timestamp': '2015-10-11T08:17:29.958569',
                'last_updated_timestamp': '2015-10-12T08:17:29.958569',
                'current_version': '1.5',
                'channels': [
                    {
                        'name': '2.x',
                        'created_timestamp': '2015-10-13T08:17:29.958569',
                        'last_updated_timestamp': '2015-10-14T08:17:29.958569',
                        'latest_version': '2.0',
                        'current': False,
                        'visible': True,
                        'type': 'prerelease',
                        'manifest_file': '2.x.json',
                    },
                    {
                        'name': '1.x',
                        'created_timestamp': '2015-10-13T08:17:29.958569',
                        'last_updated_timestamp': '2015-10-14T08:17:29.958569',
                        'latest_version': '1.5',
                        'current': True,
                        'visible': True,
                        'type': 'release',
                        'manifest_file': '1.x.json',
                    },
                ],
            },
            '/packages/DepPackage1/2.x.json': {
                'format_version': '1.0',
                'created_timestamp': '2015-10-11T08:17:29.958569',
                'last_updated_timestamp': '2015-10-12T08:17:29.958569',
                'releases': [
                    {
                        'version': '2.0',
                        'type': 'stable',
                        'visible': True,
                    }
                ],
                'package_rules': [
                    {
                        'version_range': '*',
                        'package_type': 'python',
                        'package_name': 'DepPackage1',
                        'systems': ['*'],
                    },
                ],
            },
            '/packages/DepPackage1/1.x.json': {
                'format_version': '1.0',
                'created_timestamp': '2015-10-11T08:17:29.958569',
                'last_updated_timestamp': '2015-10-12T08:17:29.958569',
                'releases': [
                    {
                        'version': '1.5',
                        'type': 'stable',
                        'visible': True,
                    }
                ],
                'package_rules': [
                    {
                        'version_range': '*',
                        'package_type': 'python',
                        'package_name': 'DepPackage1',
                        'systems': ['*'],
                    },
                ],
            },
        })

        pending_install = PendingInstall(PendingInstall.INSTALL_DEPS_ALL)

        bundle = PackageBundle(name='MyPackage')
        channel = PackageChannel(
            bundle,
            name='1.0.x',
            channel_type=PackageChannel.CHANNEL_TYPE_PRERELEASE)
        channel._loaded = True
        bundle._channels = [channel]

        release = PackageRelease(channel=channel, version='1.0')
        channel._releases = [release]

        rules = PackageRules(
            channel=channel,
            version_range='*',
            required_dependencies=[
                'DepPackage1>=1.0',
            ],
            package_type='python',
            package_name='TestPackage',
            systems=['*'])
        channel._package_rules = [rules]

        pending_install.add_package(release, 'python')

        pending_install.resolve_dependencies()

        self.assertEqual(len(pending_install._bundle_infos), 2)
        self.assertEqual(pending_install._bundle_infos[0]['bundle'].name,
                         'MyPackage')
        self.assertEqual(pending_install._bundle_infos[1]['bundle'].name,
                         'DepPackage1')
        self.assertEqual(pending_install._bundle_infos[1]['release'].version,
                         '2.0')
        self.assertEqual(len(pending_install._bundle_infos_map), 2)
        self.assertTrue('MyPackage' in pending_install._bundle_infos_map)
        self.assertTrue('DepPackage1' in pending_install._bundle_infos_map)