Esempio n. 1
0
    def test_positive_deploy_configure_by_id(self, form_data, virtwho_config):
        """ Verify " hammer virt-who-config deploy"

        :id: 19ffe76e-7e3d-48c7-b846-10a83afe0f3e

        :expectedresults: Config can be created and deployed

        :CaseLevel: Integration

        :CaseImportance: High
        """
        assert virtwho_config['status'] == 'No Report Yet'
        command = get_configure_command(virtwho_config['id'])
        hypervisor_name, guest_name = deploy_configure_by_command(command, debug=True)
        virt_who_instance = VirtWhoConfig.info({'id': virtwho_config['id']})[
            'general-information'
        ]['status']
        assert virt_who_instance == 'OK'
        hosts = [
            (hypervisor_name, f'product_id={settings.virtwho.sku_vdc_physical} and type=NORMAL'),
            (guest_name, f'product_id={settings.virtwho.sku_vdc_physical} and type=STACK_DERIVED'),
        ]
        for hostname, sku in hosts:
            host = Host.list({'search': hostname})[0]
            subscriptions = Subscription.list({'organization': DEFAULT_ORG, 'search': sku})
            vdc_id = subscriptions[0]['id']
            if 'type=STACK_DERIVED' in sku:
                for item in subscriptions:
                    if hypervisor_name.lower() in item['type']:
                        vdc_id = item['id']
                        break
            result = Host.subscription_attach({'host-id': host['id'], 'subscription-id': vdc_id})
            assert 'attached to the host successfully' in '\n'.join(result)
        VirtWhoConfig.delete({'name': virtwho_config['name']})
        assert not VirtWhoConfig.exists(search=('name', form_data['name']))
Esempio n. 2
0
    def test_positive_deploy_configure_by_script(self, form_data, virtwho_config):
        """ Verify " hammer virt-who-config fetch"

        :id: ef0f1e33-7084-4d0e-95f1-d3080dfbb4cc

        :expectedresults: Config can be created, fetch and deploy

        :CaseLevel: Integration

        :CaseImportance: High
        """
        assert virtwho_config['status'] == 'No Report Yet'
        script = VirtWhoConfig.fetch({'id': virtwho_config['id']}, output_format='base')
        hypervisor_name, guest_name = deploy_configure_by_script(script, debug=True)
        virt_who_instance = VirtWhoConfig.info({'id': virtwho_config['id']})[
            'general-information'
        ]['status']
        assert virt_who_instance == 'OK'
        hosts = [
            (hypervisor_name, f'product_id={settings.virtwho.sku_vdc_physical} and type=NORMAL'),
            (guest_name, f'product_id={settings.virtwho.sku_vdc_physical} and type=STACK_DERIVED'),
        ]
        for hostname, sku in hosts:
            host = Host.list({'search': hostname})[0]
            subscriptions = Subscription.list({'organization': DEFAULT_ORG, 'search': sku})
            vdc_id = subscriptions[0]['id']
            if 'type=STACK_DERIVED' in sku:
                for item in subscriptions:
                    if hypervisor_name.lower() in item['type']:
                        vdc_id = item['id']
                        break
            result = Host.subscription_attach({'host-id': host['id'], 'subscription-id': vdc_id})
            assert 'attached to the host successfully' in '\n'.join(result)
        VirtWhoConfig.delete({'name': virtwho_config['name']})
        assert not VirtWhoConfig.exists(search=('name', form_data['name']))
Esempio n. 3
0
    def test_positive_deploy_configure_by_script(self, default_org, form_data,
                                                 virtwho_config):
        """Verify " hammer virt-who-config fetch"

        :id: 6aaffaeb-aaf2-42cf-b0dc-ca41a53d42a6

        :expectedresults: Config can be created, fetch and deploy

        :CaseLevel: Integration

        :CaseImportance: High
        """
        assert virtwho_config['status'] == 'No Report Yet'
        script = VirtWhoConfig.fetch({'id': virtwho_config['id']},
                                     output_format='base')
        hypervisor_name, guest_name = deploy_configure_by_script(
            script,
            form_data['hypervisor-type'],
            debug=True,
            org=default_org.label)
        virt_who_instance = VirtWhoConfig.info(
            {'id': virtwho_config['id']})['general-information']['status']
        assert virt_who_instance == 'OK'
        hosts = [
            (
                hypervisor_name,
                f'product_id={settings.virtwho.sku.vdc_physical} and type=NORMAL',
            ),
            (
                guest_name,
                f'product_id={settings.virtwho.sku.vdc_physical} and type=STACK_DERIVED',
            ),
        ]
        for hostname, sku in hosts:
            host = Host.list({'search': hostname})[0]
            subscriptions = Subscription.list({
                'organization': default_org.name,
                'search': sku
            })
            vdc_id = subscriptions[0]['id']
            if 'type=STACK_DERIVED' in sku:
                for item in subscriptions:
                    if hypervisor_name.lower() in item['type']:
                        vdc_id = item['id']
                        break
            result = Host.subscription_attach({
                'host-id': host['id'],
                'subscription-id': vdc_id
            })
            assert result.strip(
            ) == 'Subscription attached to the host successfully.'
        VirtWhoConfig.delete({'name': virtwho_config['name']})
        assert not VirtWhoConfig.exists(search=('name', form_data['name']))
Esempio n. 4
0
    def test_positive_deploy_configure_by_id(self, default_org, form_data,
                                             virtwho_config):
        """Verify " hammer virt-who-config deploy"

        :id: e66bf88a-bd4e-409a-91a8-bc5e005d95dd

        :expectedresults: Config can be created and deployed

        :CaseLevel: Integration

        :CaseImportance: High
        """
        assert virtwho_config['status'] == 'No Report Yet'
        command = get_configure_command(virtwho_config['id'], default_org.name)
        hypervisor_name, guest_name = deploy_configure_by_command(
            command,
            form_data['hypervisor-type'],
            debug=True,
            org=default_org.label)
        virt_who_instance = VirtWhoConfig.info(
            {'id': virtwho_config['id']})['general-information']['status']
        assert virt_who_instance == 'OK'
        hosts = [
            (hypervisor_name,
             f'product_id={settings.virtwho.sku.vdc_physical} and type=NORMAL'
             ),
            (guest_name,
             f'product_id={settings.virtwho.sku.vdc_physical} and type=STACK_DERIVED'
             ),
        ]
        for hostname, sku in hosts:
            host = Host.list({'search': hostname})[0]
            subscriptions = Subscription.list({
                'organization': default_org.name,
                'search': sku
            })
            vdc_id = subscriptions[0]['id']
            if 'type=STACK_DERIVED' in sku:
                for item in subscriptions:
                    if hypervisor_name.lower() in item['type']:
                        vdc_id = item['id']
                        break
            result = Host.subscription_attach({
                'host-id': host['id'],
                'subscription-id': vdc_id
            })
            assert result.strip(
            ) == 'Subscription attached to the host successfully.'
        VirtWhoConfig.delete({'name': virtwho_config['name']})
        assert not VirtWhoConfig.exists(search=('name', form_data['name']))
Esempio n. 5
0
    def test_positive_deploy_configure_by_id(self):
        """ Verify " hammer virt-who-config deploy"

        :id: 19ffe76e-7e3d-48c7-b846-10a83afe0f3e

        :expectedresults: Config can be created and deployed

        :CaseLevel: Integration

        :CaseImportance: High
        """
        name = gen_string('alpha')
        args = self._make_virtwho_configure()
        args.update({'name': name})
        vhd = VirtWhoConfig.create(args)['general-information']
        self.assertEqual(vhd['status'], 'No Report Yet')
        command = get_configure_command(vhd['id'])
        hypervisor_name, guest_name = deploy_configure_by_command(command,
                                                                  debug=True)
        self.assertEqual(
            VirtWhoConfig.info({'id':
                                vhd['id']})['general-information']['status'],
            'OK')
        hosts = [
            (hypervisor_name,
             'product_id={} and type=NORMAL'.format(self.vdc_physical)),
            (guest_name,
             'product_id={} and type=STACK_DERIVED'.format(self.vdc_physical))
        ]
        for hostname, sku in hosts:
            host = Host.list({'search': hostname})[0]
            subscriptions = Subscription.list({
                'organization': DEFAULT_ORG,
                'search': sku,
            })
            vdc_id = subscriptions[0]['id']
            if 'type=STACK_DERIVED' in sku:
                for item in subscriptions:
                    if hypervisor_name.lower() in item['type']:
                        vdc_id = item['id']
                        break
            result = Host.subscription_attach({
                'host-id': host['id'],
                'subscription-id': vdc_id
            })
            self.assertTrue(
                'attached to the host successfully' in '\n'.join(result))
        VirtWhoConfig.delete({'name': name})
        self.assertFalse(VirtWhoConfig.exists(search=('name', name)))
Esempio n. 6
0
    def test_positive_restore_content_hosts_with_modified_subscription(self):
        """Restore content hosts subscription from an exported content host csv
        file with modified subscription.

        :id: d8ac08fe-24e0-41e7-b3d8-0ca13a702a64

        :customerscenario: true

        :steps:
            1. Setup activation key , lifecycle environment and content view
               with RH tools repository
            2. Setup hosts (minimum two) and subscribe them to activation key
            3. Attach RH subscription to the created content hosts
            4. Export the organization content hosts to a csv file
            5. Create a new csv file and modify the subscription with an other
               one (the new subscription must have other data than the default
               one)
            6. Import the new csv file to organization content hosts

        :expectedresults: content hosts restored with the new subscription

        :BZ: 1296978

        :CaseImportance: Critical
        """
        lce = make_lifecycle_environment({'organization-id': self.org['id']})
        activation_key = make_activation_key({
            'organization-id': self.org['id'],
            'lifecycle-environment-id': lce['id'],
        })
        ActivationKey.update({
            'organization-id': self.org['id'],
            'id': activation_key['id'],
            'auto-attach': 'false',
        })
        # Create RH tools repository and contents, this step should upload
        # the default manifest
        setup_org_for_a_rh_repo({
            'product': PRDS['rhel'],
            'repository-set': REPOSET['rhst7'],
            'repository': REPOS['rhst7']['name'],
            'organization-id': self.org['id'],
            'lifecycle-environment-id': lce['id'],
            'activationkey-id': activation_key['id'],
        }, force_use_cdn=True)
        # Export and download the organization subscriptions to prepare the new
        # subscription (The replacement of the default subscription)
        org_subs_csv_filename = 'subs_{0}.csv'.format(self.org['name'])
        org_subs_csv_remote_file_path = '/tmp/{0}'.format(
            org_subs_csv_filename)
        # export organization subscription to csv file
        CSV_.subscriptions({
            'export': True,
            'file': org_subs_csv_remote_file_path,
            'organization': self.org['name'],
        })
        # download the organization subscriptions
        org_subs_csv_local_file_path = os.path.join(
            tempfile.gettempdir(), org_subs_csv_filename)
        download_file(
            org_subs_csv_remote_file_path, org_subs_csv_local_file_path)
        _, org_subscriptions = self._read_csv_file(
            org_subs_csv_local_file_path)
        new_subscription = None
        for sub in org_subscriptions:
            if sub['Subscription Name'] == SATELLITE_SUBSCRIPTION_NAME:
                new_subscription = sub
                break
        self.assertIsNotNone(new_subscription)
        # retrieve the default subscription id
        org_subs = Subscription.list({u'organization-id': self.org['id']})
        default_subscription_id = None
        for sub in org_subs:
            if sub['name'] == DEFAULT_SUBSCRIPTION_NAME:
                default_subscription_id = sub['id']
                break
        self.assertIsNotNone(
            default_subscription_id, msg='Default subscription not found')
        # create 2 Virtual machines
        with VirtualMachine() as client1, VirtualMachine() as client2:
            hosts = []
            for client in [client1, client2]:
                client.install_katello_ca()
                client.register_contenthost(
                    self.org['label'], activation_key=activation_key['name'])
                self.assertTrue(client.subscribed)
                host = Host.info({'name': client.hostname})
                hosts.append(host)
                Host.subscription_attach({
                    'host-id': host['id'],
                    'subscription-id': default_subscription_id,
                })
                host_subscriptions = ActivationKey.subscriptions({
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                    'host-id': host['id'],
                }, output_format='json')
                self.assertEqual(len(host_subscriptions), 1)
                self.assertEqual(
                    host_subscriptions[0]['name'], DEFAULT_SUBSCRIPTION_NAME)
            # export the content host data to csv file
            chs_export_file_name = 'chs_export_{0}.csv'.format(
                self.org['label'])
            chs_export_remote_file_path = (
                '/tmp/{0}'.format(chs_export_file_name)
            )
            CSV_.content_hosts({
                'export': True,
                'file': chs_export_remote_file_path,
                'organization': self.org['name'],
            })
            # download the csv file
            chs_export_local_file_path = os.path.join(
                tempfile.gettempdir(), chs_export_file_name)
            download_file(
                chs_export_remote_file_path, chs_export_local_file_path)
            # modify the content hosts subscription
            field_names, csv_data = self._read_csv_file(
                chs_export_local_file_path)
            # each client is represented by one row of data
            self.assertEqual(len(csv_data), 2)
            for row_data in csv_data:
                # The subscription is saved in the following format:
                # """<quantity>|<sku>|<name>|<contract>|<account>"""
                subscription_data = row_data['Subscriptions'].strip(
                    '"').split('|')
                # change the subscription SKU (looks like RH00001)
                subscription_data[1] = new_subscription['Subscription SKU']
                # change the name
                subscription_data[2] = new_subscription['Subscription Name']
                # change the contract number
                subscription_data[3] = new_subscription[
                    'Subscription Contract']
                # change the subscription account
                subscription_data[4] = new_subscription[
                    'Subscription Account']
                # modify the subscription data
                row_data['Subscriptions'] = '"{0}"'.format(
                    '|'.join(subscription_data))
            # generate a new csv file
            chs_import_file_name = 'chs_import_{0}.csv'.format(
                self.org['name'])
            chs_import_local_file_path = os.path.join(
                tempfile.gettempdir(), chs_import_file_name)
            self._write_csv_file(
                chs_import_local_file_path, field_names, csv_data)
            # upload the file
            chs_import_remote_file_path = (
                '/tmp/{0}'.format(chs_import_file_name)
            )
            upload_file(
                chs_import_local_file_path, chs_import_remote_file_path)
            # import content hosts data from csv file
            CSV_.content_hosts({
                'file': chs_import_remote_file_path,
                'organization': self.org['name'],
            })
            for host in hosts:
                host_subscriptions = ActivationKey.subscriptions({
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                    'host-id': host['id'],
                }, output_format='json')
                self.assertEqual(len(host_subscriptions), 1)
                self.assertEqual(
                    host_subscriptions[0]['name'], SATELLITE_SUBSCRIPTION_NAME)
                self.assertEqual(
                    host_subscriptions[0]['contract'],
                    new_subscription['Subscription Contract'])
                self.assertEqual(
                    host_subscriptions[0]['account'],
                    new_subscription['Subscription Account'])
Esempio n. 7
0
    def test_positive_restore_ak_and_content_hosts_subscriptions(self):
        """Restore activation key and content hosts subscriptions

        :id: a44fdeda-9c8c-4316-85b4-a9b6b9f1ffdb

        :customerscenario: true

        :steps:
            1. Setup activation key , lifecycle environment and content view
               with RH repository
            2. Add RH subscription to activation key
            3. Setup hosts (minimum two) and subscribe them to activation key
            4. Attach RH subscription to the created content hosts
            5. export the activation key and content hosts subscriptions
            6. Delete the subscription manifest
            7. Ensure that the activation key and content hosts subscriptions
               does not exist
            8. Upload the subscription manifest
            9. Ensure the activation key and content hosts subscriptions does
               not exist
            10. Restore the activation key and content hosts subscriptions

        :expectedresults: activation key and content hosts subscriptions
            restored

        :CaseImportance: Critical
        """
        lce = make_lifecycle_environment({'organization-id': self.org['id']})
        activation_key = make_activation_key({
            'organization-id': self.org['id'],
            'lifecycle-environment-id': lce['id'],
        })
        ActivationKey.update({
            'organization-id': self.org['id'],
            'id': activation_key['id'],
            'auto-attach': 'false',
        })
        setup_org_for_a_rh_repo({
            'product': PRDS['rhel'],
            'repository-set': REPOSET['rhst7'],
            'repository': REPOS['rhst7']['name'],
            'organization-id': self.org['id'],
            'lifecycle-environment-id': lce['id'],
            'activationkey-id': activation_key['id'],
        }, force_use_cdn=True)
        org_subs = Subscription.list({u'organization-id': self.org['id']})
        default_subscription_id = None
        for sub in org_subs:
            if sub['name'] == DEFAULT_SUBSCRIPTION_NAME:
                default_subscription_id = sub['id']
                break
        self.assertIsNotNone(
            default_subscription_id, msg='Default subscription not found')
        ak_subs = ActivationKey.subscriptions({
            'organization-id': self.org['id'],
            'id': activation_key['id'],
        }, output_format='json')
        self.assertIn(
            DEFAULT_SUBSCRIPTION_NAME, [sub['name'] for sub in ak_subs])
        with VirtualMachine() as client1, VirtualMachine() as client2:
            hosts = []
            for client in [client1, client2]:
                client.install_katello_ca()
                client.register_contenthost(
                    self.org['label'], activation_key=activation_key['name'])
                self.assertTrue(client.subscribed)
                host = Host.info({'name': client.hostname})
                hosts.append(host)
                Host.subscription_attach({
                    'host-id': host['id'],
                    'subscription-id': default_subscription_id,
                })
                host_subscriptions = ActivationKey.subscriptions({
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                    'host-id': host['id'],
                }, output_format='json')
                self.assertIn(
                    DEFAULT_SUBSCRIPTION_NAME,
                    [sub['name'] for sub in host_subscriptions]
                )
            # export the current activations and content hosts subscriptions
            ak_file_path = '/tmp/ak_{0}.csv'.format(self.org['label'])
            ch_file_path = '/tmp/content_hosts_{0}.csv'.format(
                self.org['label'])
            CSV_.activation_keys({
                'export': True,
                'file': ak_file_path,
                'organization': self.org['name'],
                'itemized-subscriptions': True,
            })
            CSV_.content_hosts({
                'export': True,
                'file': ch_file_path,
                'organization': self.org['name'],
                'itemized-subscriptions': True,
            })
            # delete the manifest
            Subscription.delete_manifest({'organization-id': self.org['id']})
            # ensure that the subscription does not exist any more
            ak_subs = ActivationKey.subscriptions({
                'organization-id': self.org['id'],
                'id': activation_key['id'],
            }, output_format='json')
            self.assertNotIn(
                DEFAULT_SUBSCRIPTION_NAME, [sub['name'] for sub in ak_subs])
            for host in hosts:
                host_subscriptions = ActivationKey.subscriptions({
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                    'host-id': host['id'],
                }, output_format='json')
                self.assertNotIn(
                    DEFAULT_SUBSCRIPTION_NAME,
                    [sub['name'] for sub in host_subscriptions]
                )
            # upload the manifest again
            self._upload_manifest(self.org['id'])
            # ensure that the subscription was not auto attached
            ak_subs = ActivationKey.subscriptions({
                'organization-id': self.org['id'],
                'id': activation_key['id'],
            }, output_format='json')
            self.assertNotIn(
                DEFAULT_SUBSCRIPTION_NAME, [sub['name'] for sub in ak_subs])
            for host in hosts:
                host_subscriptions = ActivationKey.subscriptions({
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                    'host-id': host['id'],
                }, output_format='json')
                self.assertNotIn(
                    DEFAULT_SUBSCRIPTION_NAME,
                    [sub['name'] for sub in host_subscriptions]
                )
            # restore from the saved activation key and content hosts
            # subscriptions
            CSV_.activation_keys({
                'file': ak_file_path,
                'organization': self.org['name'],
                'itemized-subscriptions': True,
            })
            CSV_.content_hosts({
                'file': ch_file_path,
                'organization': self.org['name'],
                'itemized-subscriptions': True,
            })
            # ensure that the subscriptions has been restored
            ak_subs = ActivationKey.subscriptions({
                'organization-id': self.org['id'],
                'id': activation_key['id'],
            }, output_format='json')
            self.assertIn(
                DEFAULT_SUBSCRIPTION_NAME, [sub['name'] for sub in ak_subs])
            for host in hosts:
                host_subscriptions = ActivationKey.subscriptions({
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                    'host-id': host['id'],
                }, output_format='json')
                self.assertIn(
                    DEFAULT_SUBSCRIPTION_NAME,
                    [sub['name'] for sub in host_subscriptions]
                )
Esempio n. 8
0
    def test_positive_restore_content_hosts_with_modified_subscription(self):
        """Restore content hosts subscription from an exported content host csv
        file with modified subscription.

        :id: d8ac08fe-24e0-41e7-b3d8-0ca13a702a64

        :customerscenario: true

        :steps:
            1. Setup activation key , lifecycle environment and content view
               with RH tools repository
            2. Setup hosts (minimum two) and subscribe them to activation key
            3. Attach RH subscription to the created content hosts
            4. Export the organization content hosts to a csv file
            5. Create a new csv file and modify the subscription with an other
               one (the new subscription must have other data than the default
               one)
            6. Import the new csv file to organization content hosts

        :expectedresults: content hosts restored with the new subscription

        :BZ: 1296978

        :CaseImportance: Critical
        """
        lce = make_lifecycle_environment({'organization-id': self.org['id']})
        activation_key = make_activation_key({
            'organization-id':
            self.org['id'],
            'lifecycle-environment-id':
            lce['id'],
        })
        ActivationKey.update({
            'organization-id': self.org['id'],
            'id': activation_key['id'],
            'auto-attach': 'false',
        })
        # Create RH tools repository and contents, this step should upload
        # the default manifest
        setup_org_for_a_rh_repo(
            {
                'product': PRDS['rhel'],
                'repository-set': REPOSET['rhst7'],
                'repository': REPOS['rhst7']['name'],
                'organization-id': self.org['id'],
                'lifecycle-environment-id': lce['id'],
                'activationkey-id': activation_key['id'],
            },
            force_use_cdn=True)
        # Export and download the organization subscriptions to prepare the new
        # subscription (The replacement of the default subscription)
        org_subs_csv_filename = 'subs_{0}.csv'.format(self.org['name'])
        org_subs_csv_remote_file_path = '/tmp/{0}'.format(
            org_subs_csv_filename)
        # export organization subscription to csv file
        CSV_.subscriptions({
            'export': True,
            'file': org_subs_csv_remote_file_path,
            'organization': self.org['name'],
        })
        # download the organization subscriptions
        org_subs_csv_local_file_path = os.path.join(tempfile.gettempdir(),
                                                    org_subs_csv_filename)
        download_file(org_subs_csv_remote_file_path,
                      org_subs_csv_local_file_path)
        _, org_subscriptions = self._read_csv_file(
            org_subs_csv_local_file_path)
        new_subscription = None
        for sub in org_subscriptions:
            if sub['Subscription Name'] == SATELLITE_SUBSCRIPTION_NAME:
                new_subscription = sub
                break
        self.assertIsNotNone(new_subscription)
        # retrieve the default subscription id
        org_subs = Subscription.list({u'organization-id': self.org['id']})
        default_subscription_id = None
        for sub in org_subs:
            if sub['name'] == DEFAULT_SUBSCRIPTION_NAME:
                default_subscription_id = sub['id']
                break
        self.assertIsNotNone(default_subscription_id,
                             msg='Default subscription not found')
        # create 2 Virtual machines
        with VirtualMachine() as client1, VirtualMachine() as client2:
            hosts = []
            for client in [client1, client2]:
                client.install_katello_ca()
                client.register_contenthost(
                    self.org['label'], activation_key=activation_key['name'])
                self.assertTrue(client.subscribed)
                host = Host.info({'name': client.hostname})
                hosts.append(host)
                Host.subscription_attach({
                    'host-id':
                    host['id'],
                    'subscription-id':
                    default_subscription_id,
                })
                host_subscriptions = ActivationKey.subscriptions(
                    {
                        'organization-id': self.org['id'],
                        'id': activation_key['id'],
                        'host-id': host['id'],
                    },
                    output_format='json')
                self.assertEqual(len(host_subscriptions), 1)
                self.assertEqual(host_subscriptions[0]['name'],
                                 DEFAULT_SUBSCRIPTION_NAME)
            # export the content host data to csv file
            chs_export_file_name = 'chs_export_{0}.csv'.format(
                self.org['label'])
            chs_export_remote_file_path = (
                '/tmp/{0}'.format(chs_export_file_name))
            CSV_.content_hosts({
                'export': True,
                'file': chs_export_remote_file_path,
                'organization': self.org['name'],
            })
            # download the csv file
            chs_export_local_file_path = os.path.join(tempfile.gettempdir(),
                                                      chs_export_file_name)
            download_file(chs_export_remote_file_path,
                          chs_export_local_file_path)
            # modify the content hosts subscription
            field_names, csv_data = self._read_csv_file(
                chs_export_local_file_path)
            # each client is represented by one row of data
            self.assertEqual(len(csv_data), 2)
            for row_data in csv_data:
                # The subscription is saved in the following format:
                # """<quantity>|<sku>|<name>|<contract>|<account>"""
                subscription_data = row_data['Subscriptions'].strip('"').split(
                    '|')
                # change the subscription SKU (looks like RH00001)
                subscription_data[1] = new_subscription['Subscription SKU']
                # change the name
                subscription_data[2] = new_subscription['Subscription Name']
                # change the contract number
                subscription_data[3] = new_subscription[
                    'Subscription Contract']
                # change the subscription account
                subscription_data[4] = new_subscription['Subscription Account']
                # modify the subscription data
                row_data['Subscriptions'] = '"{0}"'.format(
                    '|'.join(subscription_data))
            # generate a new csv file
            chs_import_file_name = 'chs_import_{0}.csv'.format(
                self.org['name'])
            chs_import_local_file_path = os.path.join(tempfile.gettempdir(),
                                                      chs_import_file_name)
            self._write_csv_file(chs_import_local_file_path, field_names,
                                 csv_data)
            # upload the file
            chs_import_remote_file_path = (
                '/tmp/{0}'.format(chs_import_file_name))
            upload_file(chs_import_local_file_path,
                        chs_import_remote_file_path)
            # import content hosts data from csv file
            CSV_.content_hosts({
                'file': chs_import_remote_file_path,
                'organization': self.org['name'],
            })
            for host in hosts:
                host_subscriptions = ActivationKey.subscriptions(
                    {
                        'organization-id': self.org['id'],
                        'id': activation_key['id'],
                        'host-id': host['id'],
                    },
                    output_format='json')
                self.assertEqual(len(host_subscriptions), 1)
                self.assertEqual(host_subscriptions[0]['name'],
                                 SATELLITE_SUBSCRIPTION_NAME)
                self.assertEqual(host_subscriptions[0]['contract'],
                                 new_subscription['Subscription Contract'])
                self.assertEqual(host_subscriptions[0]['account'],
                                 new_subscription['Subscription Account'])
Esempio n. 9
0
    def test_positive_restore_ak_and_content_hosts_subscriptions(self):
        """Restore activation key and content hosts subscriptions

        :id: a44fdeda-9c8c-4316-85b4-a9b6b9f1ffdb

        :customerscenario: true

        :steps:
            1. Setup activation key , lifecycle environment and content view
               with RH repository
            2. Add RH subscription to activation key
            3. Setup hosts (minimum two) and subscribe them to activation key
            4. Attach RH subscription to the created content hosts
            5. export the activation key and content hosts subscriptions
            6. Delete the subscription manifest
            7. Ensure that the activation key and content hosts subscriptions
               does not exist
            8. Upload the subscription manifest
            9. Ensure the activation key and content hosts subscriptions does
               not exist
            10. Restore the activation key and content hosts subscriptions

        :expectedresults: activation key and content hosts subscriptions
            restored

        :CaseImportance: Critical
        """
        lce = make_lifecycle_environment({'organization-id': self.org['id']})
        activation_key = make_activation_key({
            'organization-id':
            self.org['id'],
            'lifecycle-environment-id':
            lce['id'],
        })
        ActivationKey.update({
            'organization-id': self.org['id'],
            'id': activation_key['id'],
            'auto-attach': 'false',
        })
        setup_org_for_a_rh_repo(
            {
                'product': PRDS['rhel'],
                'repository-set': REPOSET['rhst7'],
                'repository': REPOS['rhst7']['name'],
                'organization-id': self.org['id'],
                'lifecycle-environment-id': lce['id'],
                'activationkey-id': activation_key['id'],
            },
            force_use_cdn=True)
        org_subs = Subscription.list({u'organization-id': self.org['id']})
        default_subscription_id = None
        for sub in org_subs:
            if sub['name'] == DEFAULT_SUBSCRIPTION_NAME:
                default_subscription_id = sub['id']
                break
        self.assertIsNotNone(default_subscription_id,
                             msg='Default subscription not found')
        ak_subs = ActivationKey.subscriptions(
            {
                'organization-id': self.org['id'],
                'id': activation_key['id'],
            },
            output_format='json')
        self.assertIn(DEFAULT_SUBSCRIPTION_NAME,
                      [sub['name'] for sub in ak_subs])
        with VirtualMachine() as client1, VirtualMachine() as client2:
            hosts = []
            for client in [client1, client2]:
                client.install_katello_ca()
                client.register_contenthost(
                    self.org['label'], activation_key=activation_key['name'])
                self.assertTrue(client.subscribed)
                host = Host.info({'name': client.hostname})
                hosts.append(host)
                Host.subscription_attach({
                    'host-id':
                    host['id'],
                    'subscription-id':
                    default_subscription_id,
                })
                host_subscriptions = ActivationKey.subscriptions(
                    {
                        'organization-id': self.org['id'],
                        'id': activation_key['id'],
                        'host-id': host['id'],
                    },
                    output_format='json')
                self.assertIn(DEFAULT_SUBSCRIPTION_NAME,
                              [sub['name'] for sub in host_subscriptions])
            # export the current activations and content hosts subscriptions
            ak_file_path = '/tmp/ak_{0}.csv'.format(self.org['label'])
            ch_file_path = '/tmp/content_hosts_{0}.csv'.format(
                self.org['label'])
            CSV_.activation_keys({
                'export': True,
                'file': ak_file_path,
                'organization': self.org['name'],
                'itemized-subscriptions': True,
            })
            CSV_.content_hosts({
                'export': True,
                'file': ch_file_path,
                'organization': self.org['name'],
                'itemized-subscriptions': True,
            })
            # delete the manifest
            Subscription.delete_manifest({'organization-id': self.org['id']})
            # ensure that the subscription does not exist any more
            ak_subs = ActivationKey.subscriptions(
                {
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                },
                output_format='json')
            self.assertNotIn(DEFAULT_SUBSCRIPTION_NAME,
                             [sub['name'] for sub in ak_subs])
            for host in hosts:
                host_subscriptions = ActivationKey.subscriptions(
                    {
                        'organization-id': self.org['id'],
                        'id': activation_key['id'],
                        'host-id': host['id'],
                    },
                    output_format='json')
                self.assertNotIn(DEFAULT_SUBSCRIPTION_NAME,
                                 [sub['name'] for sub in host_subscriptions])
            # upload the manifest again
            self._upload_manifest(self.org['id'])
            # ensure that the subscription was not auto attached
            ak_subs = ActivationKey.subscriptions(
                {
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                },
                output_format='json')
            self.assertNotIn(DEFAULT_SUBSCRIPTION_NAME,
                             [sub['name'] for sub in ak_subs])
            for host in hosts:
                host_subscriptions = ActivationKey.subscriptions(
                    {
                        'organization-id': self.org['id'],
                        'id': activation_key['id'],
                        'host-id': host['id'],
                    },
                    output_format='json')
                self.assertNotIn(DEFAULT_SUBSCRIPTION_NAME,
                                 [sub['name'] for sub in host_subscriptions])
            # restore from the saved activation key and content hosts
            # subscriptions
            CSV_.activation_keys({
                'file': ak_file_path,
                'organization': self.org['name'],
                'itemized-subscriptions': True,
            })
            CSV_.content_hosts({
                'file': ch_file_path,
                'organization': self.org['name'],
                'itemized-subscriptions': True,
            })
            # ensure that the subscriptions has been restored
            ak_subs = ActivationKey.subscriptions(
                {
                    'organization-id': self.org['id'],
                    'id': activation_key['id'],
                },
                output_format='json')
            self.assertIn(DEFAULT_SUBSCRIPTION_NAME,
                          [sub['name'] for sub in ak_subs])
            for host in hosts:
                host_subscriptions = ActivationKey.subscriptions(
                    {
                        'organization-id': self.org['id'],
                        'id': activation_key['id'],
                        'host-id': host['id'],
                    },
                    output_format='json')
                self.assertIn(DEFAULT_SUBSCRIPTION_NAME,
                              [sub['name'] for sub in host_subscriptions])