def update_product_subscription_in_ak(product, yum_repo, ak, org):
    """ Updates given products subscription in given AK

    :param str product: products name to calcualte subscription id
    :param nailgun.entity.Repository yum_repo: yum repository
    :param nailgun.entity.ActivationKey ak: Ak
    :param nailgun.entity.Organization org: Organization
    """
    cv_from_ak = ak.content_view
    cv = cv_from_ak.read()
    cv.repository.append(yum_repo)
    cv = cv.update(['repository'])
    cv.publish()
    cv = cv.read()  # Published CV with new version
    # Promote CV
    environment = entities.ActivationKey(organization=org).search(
        query={'search': 'name={}'.format(ak.name)}
    )[0].environment
    cvv = entities.ContentViewVersion(
        id=max([cvv.id for cvv in cv.version])
    ).read()
    cvv.promote(
        data={
            u'environment_id': environment.id,
            u'force': False
        }
    )
    subscription = entities.Subscription(organization=org).search(query={
        'search': 'name={}'.format(product.name)
    })[0]
    ak.add_subscriptions(data={
        'subscription_id': subscription.id})
Example #2
0
    def test_post_version_cv_export_import(
        self, request, set_importing_org, dependent_scenario_name, default_sat
    ):
        """After upgrade, content view version import and export works on the existing content
         view(that we created before the upgrade).

        :id: postupgrade-f19e4928-94db-4df6-8ce8-b5e4afe34258

        :parametrized: yes

        :steps:
            1: Export the existing content-view version.
            2: Import the existing content-view version.
            3: Delete the imported and exported content-vew, product, repo and organization.

        :expectedresults: After upgrade,
            1: Content view created before upgrade should be imported and exported successfully.
            2: Imported and Exported content view should be deleted successfully
        """
        pre_test_name = dependent_scenario_name
        export_base = '/var/lib/pulp/katello-export/'
        org = entities.Organization().search(query={'search': f'name="{pre_test_name}_org"'})[0]
        request.addfinalizer(org.delete)
        product = entities.Product(organization=org).search(
            query={'search': f'name="{pre_test_name}_prod"'}
        )[0]
        request.addfinalizer(product.delete)
        exporting_cv = entities.ContentView(organization=org).search(
            query={'search': f'name="{pre_test_name}_cv"'}
        )[0]
        request.addfinalizer(exporting_cv.delete)

        exporting_cvv_id = max(cvv.id for cvv in exporting_cv.version)
        exporting_cvv_version = entities.ContentViewVersion(id=exporting_cvv_id).read().version

        ContentView.version_export({'export-dir': f'{export_base}', 'id': exporting_cvv_id})
        exported_tar = f'{export_base}/export-{exporting_cv.name}-{exporting_cvv_version}.tar'

        result = default_sat.execute(f'[ -f {exported_tar} ]')
        assert result.status == 0

        exported_packages = Package.list({'content-view-version-id': exporting_cvv_id})
        assert len(exported_packages) > 0
        importing_cv, importing_org = set_importing_org
        ContentView.version_import(
            {'export-tar': f'{exported_tar}', 'organization-id': importing_org.id}
        )
        importing_cvv = importing_cv.read().version
        assert len(importing_cvv) == 1
        imported_packages = Package.list({'content-view-version-id': importing_cvv[0].id})
        assert len(imported_packages) > 0
        assert len(exported_packages) == len(imported_packages)
        default_sat.execute(f'rm -rf {export_base}/*')
        exporting_cv_json = exporting_cv.read_json()
        importing_cv_json = importing_cv.read_json()
        exporting_cv_env_id = exporting_cv_json['environments'][0]['id']
        importing_cv_env_id = importing_cv_json['environments'][0]['id']
        assert exporting_cv.delete_from_environment(exporting_cv_env_id)
        assert importing_cv.delete_from_environment(importing_cv_env_id)
Example #3
0
    def test_pre_scenario_preclient_package_installation(self):
        """Create product and repo from which the package will be installed
        post upgrade

        :id: preupgrade-eedab638-fdc9-41fa-bc81-75dd2790f7be

        :steps:

            1. Create a content host with existing client ak
            2. Create and sync repo from which the package will be
                installed on content host
            3. Add repo to CV and then in Activation key

        :expectedresults:

            1. The content host is created
            2. The new repo and its product has been added to ak using which
                the content host is created

        """
        rhel7_client = dockerize(distro='rhel7')
        product = entities.Product(
            name='preclient_scenario_product',
            organization=1,
        ).create()
        yum_repo = entities.Repository(
            name='preclient_scenario_repo', product=product
        ).create()
        yum_repo.sync()
        self.cv.repository = [yum_repo]
        cv = self.cv.update(['repository'])
        cv.publish()
        cv = cv.read()  # Published CV with new version
        # Promote CV
        environment = entities.ActivationKey().search(
            query={'search': 'name={}'.format(self.ak)}
        )[0].environment
        cvv = entities.ContentViewVersion(
            id=max([cvv.id for cvv in cv.version])
        ).read()
        cvv.promote(
            data={
                u'environment_id': environment.id,
                u'force': False
            }
        )
        create_dict(
            {self.__class__.__name__: rhel7_client}
        )
Example #4
0
    def test_post_version_cv_export_import(self):
        """Export and Import cv version created before upgrade

        :id: postupgrade-f19e4928-94db-4df6-8ce8-b5e4afe34258

        :steps: Export and Import the Content Views version created before upgrade

        :expectedresults: Content-view created before upgrade should be exported and imported
            after upgrade
         """
        prescene_dict = get_entity_data(self.__class__.__name__)
        exporting_cv = entities.ContentView(
            organization=prescene_dict['exporting_orgid']).search(query={
                'search':
                'name={}'.format(prescene_dict['exporting_cvname'])
            })[0]
        exporting_cvv_id = max([cvv.id for cvv in exporting_cv.version])
        exporting_cvv_version = entities.ContentViewVersion(
            id=exporting_cvv_id).read().version
        ContentView.version_export({
            'export-dir': '{}'.format(self.export_base),
            'id': exporting_cvv_id
        })
        exported_tar = '{0}/export-{1}-{2}.tar'.format(self.export_base,
                                                       exporting_cv.name,
                                                       exporting_cvv_version)
        result = ssh.command("[ -f {0} ]".format(exported_tar))
        self.assertEqual(result.return_code, 0)
        exported_packages = Package.list(
            {'content-view-version-id': exporting_cvv_id})
        self.assertTrue(len(exported_packages) > 0)
        self.set_importing_org(
            prescene_dict['exporting_prodname'],
            prescene_dict['exporting_reponame'],
            exporting_cv.name,
        )
        ContentView.version_import({
            'export-tar': exported_tar,
            'organization-id': self.importing_org.id
        })
        importing_cvv = self.importing_cv.read().version
        self.assertTrue(len(importing_cvv) == 1)
        imported_packages = Package.list(
            {'content-view-version-id': importing_cvv[0].id})
        self.assertTrue(len(imported_packages) > 0)
        self.assertEqual(len(exported_packages), len(imported_packages))
        self.tearDownScenario()
Example #5
0
    def test_positive_noapply_api(self):
        """Check if api incremental update can be done without
        actually applying it

        :id: 481c5ff2-801f-4eff-b1e0-95ea5bb37f95

        :Setup:  The prerequisites are already covered in the setUpClass() but
            for easy debug, get the content view id, Repository id and
            Lifecycle environment id using hammer and plug these statements on
            the top of the test. For example::

                self.rhel_6_partial_cv = ContentView(id=38).read()
                self.rhva_6_repo = Repository(id=164).read()
                self.qe_lce = LifecycleEnvironment(id=46).read()

        :expectedresults: Incremental update completed with no errors and
            Content view has a newer version

        :CaseLevel: System
        """
        # Get the content view versions and use the recent one.  API always
        # returns the versions in ascending order so it is safe to assume the
        # last one in the list is the recent
        cv_versions = self.rhel_6_partial_cv.version

        # Get the applicable errata
        errata_list = self.get_applicable_errata(self.rhva_6_repo)
        self.assertGreater(len(errata_list), 0)

        # Apply incremental update using the first applicable errata
        entities.ContentViewVersion().incremental_update(
            data={
                'content_view_version_environments': [{
                    'content_view_version_id':
                    cv_versions[-1].id,
                    'environment_ids': [self.qe_lce.id]
                }],
                'add_content': {
                    'errata_ids': [errata_list[0].id]
                }
            })

        # Re-read the content view to get the latest versions
        self.rhel_6_partial_cv = self.rhel_6_partial_cv.read()
        self.assertGreater(len(self.rhel_6_partial_cv.version),
                           len(cv_versions))
Example #6
0
def test_positive_noapply_api(module_manifest_org, module_cv, custom_repo,
                              host, dev_lce):
    """Check if api incremental update can be done without
    actually applying it

    :id: 481c5ff2-801f-4eff-b1e0-95ea5bb37f95

    :Setup:  get the content view id, Repository id and
        Lifecycle environment id

    :expectedresults: Incremental update completed with no errors and
        Content view has a newer version

    :parametrized: yes

    :CaseLevel: System
    """
    # Promote CV to new LCE
    versions = sorted(module_cv.read().version, key=lambda ver: ver.id)
    cvv = versions[-1].read()
    promote(cvv, dev_lce.id)
    # Read CV to pick up LCE ID and next_version
    module_cv = module_cv.read()

    # Get the content view versions and use the recent one. API always
    # returns the versions in ascending order (last in the list is most recent)
    cv_versions = module_cv.version
    # Get the applicable errata
    errata_list = get_applicable_errata(custom_repo)
    assert len(errata_list) > 0

    # Apply incremental update using the first applicable errata
    entities.ContentViewVersion().incremental_update(
        data={
            'content_view_version_environments': [{
                'content_view_version_id':
                cv_versions[-1].id,
                'environment_ids': [dev_lce.id],
            }],
            'add_content': {
                'errata_ids': [errata_list[0].id]
            },
        })
    # Re-read the content view to get the latest versions
    module_cv = module_cv.read()
    assert len(module_cv.version) > len(cv_versions)
def create_activation_key_for_client_registration(
        ak_name, client_os, org, environment, sat_state):
    """Creates Activation key for client registration

    :param str ak_name: Activation key name
    :param str client_os: rhel6/rhel7
    :param nailgun.entity.Organization org: Organization
    :param nailgun.entity.Environment environment: Environment
    :param str sat_state: pre or post

    :return nailgun.entity: Activation key
    """
    client_os = client_os.upper()
    from_ver = os.environ.get('FROM_VERSION')
    rhel_prod_name = 'scenarios_rhel{}_prod'.format(client_os[-1])
    rhel_repo_name = '{}_repo'.format(rhel_prod_name)
    rhel_url = os.environ.get('{}_CUSTOM_REPO'.format(client_os))
    if rhel_url is None:
        raise ValueError('The RHEL Repo URL environment variable for OS {} '
                         'is not provided!'.format(client_os))
    rhel_prod = entities.Product(
        name=rhel_prod_name, organization=org.id).create()
    if sat_state.lower() == 'pre' and from_ver in ['6.1', '6.2']:
        rhel_repo = entities.Repository(
            name=rhel_repo_name,
            product=rhel_prod,
            url=rhel_url,
            content_type='yum'
        ).create()
    else:
        rhel_repo = entities.Repository(
            name=rhel_repo_name,
            product=rhel_prod,
            url=rhel_url,
            content_type='yum',
            verify_ssl_on_sync=False
        ).create()
    call_entity_method_with_timeout(rhel_repo.sync, timeout=1400)
    if sat_state.lower() == 'pre':
        product_name = 'Red Hat Enterprise Linux Server'
        repo_name = 'Red Hat Satellite Tools {0} for RHEL ' \
                    '{1} Server RPMs x86_64'.format(from_ver, client_os[-1])
        tools_prod = entities.Product(
            organization=org.id
        ).search(
            query={
                'per_page': 1000,
                'search': 'name="{}"'.format(product_name)
            }
        )[0]
        tools_repo = entities.Repository(
            organization=org.id, product=tools_prod
        ).search(
            query={
                'per_page': 1000,
                'search': 'name="{}"'.format(repo_name)
            }
        )[0]
    elif sat_state.lower() == 'post':
        product_name = 'scenarios_tools_product'
        tools_repo_url = os.environ.get(
            'TOOLS_URL_{}'.format(client_os.upper()))
        if tools_repo_url is None:
            raise ValueError('The Tools Repo URL environment variable for '
                             'OS {} is not provided!'.format(client_os))
        repo_name = '{}_repo'.format(product_name)
        tools_prod = entities.Product(
            organization=org.id
        ).search(query={'search': 'name={}'.format(product_name)})
        if not tools_prod:
            tools_prod = entities.Product(
                name=product_name, organization=org.id).create()
            tools_repo = entities.Repository(
                name=repo_name,
                product=tools_prod,
                url=tools_repo_url,
                content_type='yum'
            ).create()
            tools_repo.sync()
        else:
            tools_repo = entities.Repository(
                organization=org.id, product=tools_prod
            ).search(query={'search': 'name={}'.format(repo_name)})
    tools_cv = entities.ContentView(
        name=ak_name + '_cv',
        label=ak_name + '_cv',
        organization=org.id
    ).create()
    tools_cv.repository = [tools_repo, rhel_repo]
    tools_cv = tools_cv.update(['repository'])
    tools_cv.publish()
    tools_cv = tools_cv.read()  # Published CV with new version
    # Promote CV
    cvv = entities.ContentViewVersion(
        id=max([cvv.id for cvv in tools_cv.version])
    ).read()
    cvv.promote(
        data={
            u'environment_id': environment.id,
            u'force': False
        }
    )
    tools_ak = entities.ActivationKey(
        name=ak_name,
        content_view=tools_cv,
        organization=org.id,
        environment=environment
    ).create()
    if sat_state == 'pre':
        tools_sub = 'Red Hat Satellite Employee Subscription'
        tools_content = 'rhel-{0}-server-satellite-tools-{1}-rpms'.format(
            client_os[-1], from_ver)
    else:
        tools_sub = tools_prod.name
    tools_subscription = entities.Subscription(organization=org.id).search(
        query={
            'search': 'name="{}"'.format(tools_sub),
            'per_page': 1000
        }
    )[0]
    rhel_subscription = entities.Subscription(organization=org.id).search(
        query={
            'search': 'name={}'.format(rhel_prod.name),
            'per_page': 1000
        }
    )[0]
    tools_ak.add_subscriptions(data={
        'subscription_id': tools_subscription.id})
    if sat_state == 'pre':
        tools_ak.content_override(data={
            'content_override':
                {
                    u'content_label': tools_content,
                    u'value': u'1'
                }}
        )
    tools_ak.add_subscriptions(data={
        'subscription_id': rhel_subscription.id})
    return tools_ak
Example #8
0
def sync_capsule_repos_to_upgrade(capsules):
    """This syncs capsule repo in Satellite server and also attaches
    the capsule repo subscription to each capsule

    :param list capsules: The list of capsule hostnames to which new capsule
    repo subscription will be attached

    Following environment variable affects this function:

    CAPSULE_URL
        The url for capsule repo from latest satellite compose.
        If not provided, capsule repo from Red Hat repositories will be enabled
    TOOLS_URL_RHEL{}.format(os_ver)
        The url for capsuletools repo from latest satellite compose.
        If not provided, capsule repo from Red Hat repositories will be enabled
    FROM_VERSION
        Current Satellite version - to differentiate default organization.
        e.g. '6.1', '6.0'
    TO_VERSION
        Upgradable Satellite version - To enable capsule repo
        e.g '6.1', '6.2'
    OS
        OS version to enable next version capsule repo
        e.g 'rhel7', 'rhel6'

    Personal Upgrade Env Vars:

    CAPSULE_AK
        The AK name used in capsule subscription

    RHEV upgrade Env Vars:

    RHEV_CAPSULE_AK
        The AK name used in capsule subscription
    """
    to_version = os.environ.get('TO_VERSION')
    logger.info('Syncing latest capsule repos in Satellite ...')
    os_ver = os.environ.get('OS')[-1]
    capsule_repo = os.environ.get('CAPSULE_URL')
    capsuletools_url = os.environ.get('TOOLS_URL_RHEL{}'.format(os_ver))
    ak_name = os.environ.get('CAPSULE_AK', os.environ.get('RHEV_CAPSULE_AK'))
    if ak_name is None:
        logger.warning(
            'The AK name is not provided for Capsule upgrade! Aborting...')
        sys.exit(1)
    org = entities.Organization(id=1).read()
    ak = entities.ActivationKey(organization=org).search(
        query={'search': 'name={}'.format(ak_name)})[0]
    cv = ak.content_view.read()
    lenv = ak.environment.read()
    # Fix dead pulp tasks
    if os_ver == '6':
        run('for i in pulp_resource_manager pulp_workers pulp_celerybeat; '
            'do service $i restart; done')
    _sync_capsule_subscription_to_capsule_ak(ak)
    if float(to_version) >= 6.3:
        _add_additional_subscription_for_capsule(ak, capsuletools_url)
    # Publishing and promoting the CV with all newly added capsule, capsuletools, rhscl and
    # server repos combine
    call_entity_method_with_timeout(cv.read().publish, timeout=2000)
    published_ver = entities.ContentViewVersion(
        id=max([cv_ver.id for cv_ver in cv.read().version])).read()
    published_ver.promote(data={'environment_id': lenv.id, 'force': False})
    # Add capsule and tools custom prod subscription to capsules
    if capsule_repo:
        add_custom_product_subscription_to_hosts(
            customcontents['capsule']['prod'], capsules)
    if float(to_version) >= 6.3:
        if capsuletools_url:
            add_custom_product_subscription_to_hosts(
                customcontents['capsule_tools']['prod'], capsules)
Example #9
0
def sync_tools_repos_to_upgrade(client_os, hosts):
    """This syncs tools repo in Satellite server and also attaches
    the new tools repo subscription onto each client

    :param string client_os: The client OS of which tools repo to be synced
        e.g: rhel6, rhel7
    :param list hosts: The list of capsule hostnames to which new capsule
        repo subscription will be attached

    Following environment variable affects this function:

    TOOLS_URL_{client_os}
        The url of tools repo from latest satellite compose.
    FROM_VERSION
        Current Satellite version - to differentiate default organization.
        e.g. '6.1', '6.0'

    Personal Upgrade Env Vars:

    CLIENT_AK
        The ak_name attached to subscription of client

    Rhevm upgrade Env Vars:

    RHEV_CLIENT_AK
        The AK name used in client subscription
    """
    client_os = client_os.upper()
    tools_repo_url = os.environ.get('TOOLS_URL_{}'.format(client_os))
    if tools_repo_url is None:
        logger.warning('The Tools Repo URL for {} is not provided '
                       'to perform Client Upgrade !'.format(client_os))
        sys.exit(1)
    ak_name = os.environ.get(
        'CLIENT_AK_{}'.format(client_os),
        os.environ.get('RHEV_CLIENT_AK_{}'.format(client_os)))
    if ak_name is None:
        logger.warning('The AK details are not provided for {0} Client '
                       'upgrade!'.format(client_os))
        sys.exit(1)
    org = entities.Organization().search(
        query={'search': 'name="{}"'.format("Default Organization")})[0]
    ak = entities.ActivationKey(organization=org).search(
        query={'search': 'name={}'.format(ak_name)})[0]
    cv = ak.content_view.read()
    lenv = ak.environment.read()
    toolsproduct_name = customcontents['tools']['prod'].format(
        client_os=client_os)
    toolsrepo_name = customcontents['tools']['repo'].format(
        client_os=client_os)
    # adding sleeps in between to avoid race conditions
    tools_product = entities.Product(name=toolsproduct_name,
                                     organization=org).create()
    tools_repo = entities.Repository(name=toolsrepo_name,
                                     product=tools_product,
                                     url=tools_repo_url,
                                     organization=org,
                                     content_type='yum').create()
    entities.Repository(id=tools_repo.id).sync()
    cv.repository += [tools_repo]
    cv.update(['repository'])
    call_entity_method_with_timeout(cv.read().publish, timeout=2500)
    published_ver = entities.ContentViewVersion(
        id=max([cv_ver.id for cv_ver in cv.read().version])).read()
    published_ver.promote(data={'environment_id': lenv.id, 'force': False})
    tools_sub = entities.Subscription().search(
        query={'search': 'name={0}'.format(toolsproduct_name)})[0]
    ak.add_subscriptions(data={
        'quantity': 1,
        'subscription_id': tools_sub.id,
    })
    # Add this latest tools repo to hosts to upgrade
    sub = entities.Subscription().search(
        query={'search': 'name={0}'.format(toolsproduct_name)})[0]
    for host in hosts:
        if float(os.environ.get('FROM_VERSION')) <= 6.1:
            # If not User Hosts then, attach sub to dockered clients
            if not all([
                    os.environ.get('CLIENT6_HOSTS'),
                    os.environ.get('CLIENT7_HOSTS')
            ]):
                docker_vm = os.environ.get('DOCKER_VM')
                execute(attach_subscription_to_host_from_content_host,
                        sub.cp_id,
                        True,
                        host,
                        host=docker_vm)
            # Else, Attach subs to user hosts
            else:
                execute(attach_subscription_to_host_from_content_host,
                        sub.cp_id,
                        host=host)
        else:
            host = entities.Host().search(
                query={'search': 'name={}'.format(host)})[0]
            entities.HostSubscription(host=host).add_subscriptions(
                data={'subscriptions': [{
                    'id': sub.id,
                    'quantity': 1
                }]})
Example #10
0
    def test_post_scenario_postclient_package_installation(self):
        """Post-upgrade scenario that creates and installs the package on
        post-upgrade client remotely and then verifies if the package installed

        :id: postupgrade-1a881c07-595f-425f-aca9-df2337824a8e

        :steps:

            1. Create a content host with existing client ak
            2. Create and sync repo from which the package will be
                installed on content host
            3. Add repo to CV and then in Activation key
            4. Install package on a pre-upgrade client

        :expectedresults:

            1. The content host is created
            2. The new repo and its product has been added to ak using which
                the content host is created
            3. The package is installed on post-upgrade client
        """
        rhel7_client = dockerize(distro='rhel7')
        product = entities.Product(
            name='postclient_scenario_product',
            organization=1,
        ).create()
        yum_repo = entities.Repository(
            name='postclient_scenario_repo', product=product
        ).create()
        yum_repo.sync()
        self.cv.repository = [yum_repo]
        cv = self.cv.update(['repository'])
        cv.publish()
        cv = cv.read()  # Published CV with new version
        # Promote CV
        environment = entities.ActivationKey().search(
            query={'search': 'name={}'.format(self.ak)}
        )[0].environment
        cvv = entities.ContentViewVersion(
            id=max([cvv.id for cvv in cv.version])
        ).read()
        cvv.promote(
            data={
                u'environment_id': environment.id,
                u'force': False
            }
        )
        client_id = entities.Host().search(
            query={'search': 'name={}'.format(rhel7_client.keys()[0])}
        )[0].id
        entities.Host().install_content(data={
            'organization_id': 1,
            'included': {'ids': [client_id]},
            'content_type': 'package',
            'content': [self.package_name],
        })
        # Validate if that package is really installed
        installed_package = execute(
            docker_execute_command,
            rhel7_client.values()[0],
            'rpm -q {}'.format(self.package_name),
            host=self.docker_vm
        )[self.docker_vm]
        time.sleep(10)
        self.assertIn(self.package_name, installed_package)