Пример #1
0
    def configure_puppet(self, rhel_repo=None):
        """Configures puppet on the virtual machine/Host.

        :param rhel_repo: Red Hat repository link from properties file.
        :return: None.

        """
        sat6_hostname = conf.properties['main.server.hostname']
        self.configure_rhel_repo(rhel_repo)
        puppet_conf = (
            'pluginsync      = true\n'
            'report          = true\n'
            'ignoreschedules = true\n'
            'daemon          = false\n'
            'ca_server       = {0}\n'
            'server          = {1}\n'
            .format(sat6_hostname, sat6_hostname)
        )
        result = self.run(u'yum install puppet -y')
        if result.return_code != 0:
            raise VirtualMachineError(
                'Failed to install the puppet rpm')
        self.run(
            'echo "{0}" >> /etc/puppet/puppet.conf'
            .format(puppet_conf)
        )
        # This particular puppet run on client would populate a cert on sat6
        # under the capsule --> certifcates or via cli "puppet cert list", so
        # that we sign it.
        self.run(u'puppet agent -t')
        ssh.command(u'puppet cert sign --all')
        # This particular puppet run would create the host entity under
        # 'All Hosts' and let's redirect stderr to /dev/null as errors at this
        # stage can be ignored.
        self.run(u'puppet agent -t 2> /dev/null')
Пример #2
0
def default_url_on_new_port(oldport, newport):
    """Creates context where the default smart-proxy is forwarded on a new port

    :param int oldport: Port to be forwarded.
    :param int newport: New port to be used to forward `oldport`.

    :return: A string containing the new capsule URL with port.
    :rtype: str

    """
    logger = logging.getLogger('robottelo')
    domain = conf.properties['main.server.hostname']
    user = conf.properties['main.server.ssh.username']
    key = conf.properties['main.server.ssh.key_private']
    ssh.upload_file(key, '/tmp/dsa_{0}'.format(newport))
    ssh.command('chmod 700 /tmp/dsa_{0}'.format(newport))

    with ssh._get_connection() as connection:
        command = u'ssh -i {0} -L {1}:{2}:{3} {4}@{5}'.format(
            '/tmp/dsa_{0}'.format(newport),
            newport, domain, oldport, user, domain)
        logger.debug('Creating tunnel {0}'.format(command))
        # Run command and timeout in 30 seconds.
        _, _, stderr = connection.exec_command(command, 30)

        stderr = stderr.read()
        if len(stderr) > 0:
            logger.debug('Tunnel failed: {0}'.format(stderr))
            # Something failed, so raise an exception.
            raise SSHTunnelError(stderr)
        yield 'https://{0}:{1}'.format(domain, newport)
Пример #3
0
 def _restore_from_savepoint(self, savepoint):
     """Restore from savepoint"""
     if savepoint == '':
         self.logger.warning('No savepoint while continuing test!')
         return
     self.logger.info('Reset db from /home/backup/{}'.format(savepoint))
     ssh.command('./reset-db.sh /home/backup/{}'.format(savepoint))
Пример #4
0
    def test_selinux_foreman_module(self):
        """@Test: SELinux foreman module have the right version

        @Feature: Server health

        @Assert: Foreman RPM and SELinux module versions match

        """
        rpm_result = ssh.command('rpm -q foreman-selinux')
        self.assertEqual(rpm_result.return_code, 0)
        semodule_result = ssh.command('semodule -l | grep foreman')
        self.assertEqual(semodule_result.return_code, 0)

        # Sample rpm output: foreman-selinux-1.7.2.8-1.el7sat.noarch
        rpm_version = self.version_regex.search(''.join(
            rpm_result.stdout)).group(1)
        # Sample semodule output: foreman        1.7.2.8.1
        semodule_version = self.version_regex.search(''.join(
            semodule_result.stdout)).group(1)

        if rpm_version.endswith('-0'):
            # Examples of matching RPM and semodule version numbers:
            #
            # 1.7.2.8-0    1.7.2.8
            # 1.7.2.8-1    1.7.2.8.1
            # 1.7.2.8-2    1.7.2.8.2
            rpm_version = rpm_version[:-2]

        self.assertEqual(rpm_version.replace('-', '.'), semodule_version)
Пример #5
0
def default_url_on_new_port(oldport, newport):
    """Creates context where the default smart-proxy is forwarded on a new port

    :param int oldport: Port to be forwarded.
    :param int newport: New port to be used to forward `oldport`.

    :return: A string containing the new capsule URL with port.
    :rtype: str

    """
    logger = logging.getLogger('robottelo')
    domain = conf.properties['main.server.hostname']
    user = conf.properties['main.server.ssh.username']
    key = conf.properties['main.server.ssh.key_private']
    ssh.upload_file(key, '/tmp/dsa_{0}'.format(newport))
    ssh.command('chmod 700 /tmp/dsa_{0}'.format(newport))

    with ssh._get_connection() as connection:
        command = u'ssh -i {0} -L {1}:{2}:{3} {4}@{5}'.format(
            '/tmp/dsa_{0}'.format(newport), newport, domain, oldport, user,
            domain)
        logger.debug('Creating tunnel {0}'.format(command))
        # Run command and timeout in 30 seconds.
        _, _, stderr = connection.exec_command(command, 30)

        stderr = stderr.read()
        if len(stderr) > 0:
            logger.debug('Tunnel failed: {0}'.format(stderr))
            # Something failed, so raise an exception.
            raise SSHTunnelError(stderr)
        yield 'https://{0}:{1}'.format(domain, newport)
Пример #6
0
    def destroy(self):
        """Destroys the virtual machine on the provisioning server"""
        if not self._created:
            return

        result = ssh.command(
            'virsh destroy {0}'.format(self.target_image),
            hostname=self.provisioning_server
        )
        if result.return_code > 0:
            logger.warning(result.stderr)
        result = ssh.command(
            'virsh undefine {0}'.format(self.target_image),
            hostname=self.provisioning_server
        )
        if result.return_code > 0:
            logger.warning(result.stderr)

        image_name = '{0}.img'.format(self.target_image)
        result = ssh.command(
            'rm {0}'.format(os.path.join(self.image_dir, image_name)),
            hostname=self.provisioning_server
        )
        if result.return_code > 0:
            logger.warning(result.stderr)
Пример #7
0
    def test_selinux_foreman_module(self):
        """@Test: SELinux foreman module have the right version

        @Feature: Server health

        @Assert: Foreman RPM and SELinux module versions match

        """
        rpm_result = ssh.command('rpm -q foreman-selinux')
        self.assertEqual(rpm_result.return_code, 0)
        semodule_result = ssh.command('semodule -l | grep foreman')
        self.assertEqual(semodule_result.return_code, 0)

        # Sample rpm output: foreman-selinux-1.7.2.8-1.el7sat.noarch
        rpm_version = self.version_regex.search(
            ''.join(rpm_result.stdout)).group(1)
        # Sample semodule output: foreman        1.7.2.8.1
        semodule_version = self.version_regex.search(
            ''.join(semodule_result.stdout)).group(1)

        if rpm_version.endswith('-0'):
            # Examples of matching RPM and semodule version numbers:
            #
            # 1.7.2.8-0    1.7.2.8
            # 1.7.2.8-1    1.7.2.8.1
            # 1.7.2.8-2    1.7.2.8.2
            rpm_version = rpm_version[:-2]

        self.assertEqual(rpm_version.replace('-', '.'), semodule_version)
Пример #8
0
    def run_puppet_module(self):
        """
        Import some parameterized puppet class. This is required to make sure
        that we have smart class variable available.
        """

        ssh.command('puppet module install --force puppetlabs/ntp')
Пример #9
0
 def _restore_from_savepoint(self, savepoint):
     """Restore from a given savepoint"""
     if savepoint == '':
         self.logger.warning('No savepoint while continuing test!')
         return
     self.logger.info('Reset db from /home/backup/{}'.format(savepoint))
     ssh.command('./reset-db.sh /home/backup/{}'.format(savepoint))
Пример #10
0
    def run_puppet_module(self):
        """
        Import some parameterized puppet class. This is required to make sure
        that we have smart class variable available.
        """

        ssh.command('puppet module install --force puppetlabs/ntp')
Пример #11
0
    def run_puppet_agent(self):
        """Retrieves the client configuration from the puppet master and
        applies it to the local host. This is required to make sure
        that we have reports available.

        """

        ssh.command('puppet agent -t')
    def single_register_attach(cls, sub_id, default_org, environment, vm):
        """Subscribe VM to Satellite by Register + Attach"""
        ssh.command('subscription-manager clean', hostname=vm)

        time_reg = cls.sub_mgr_register_authentication(default_org,
                                                       environment, vm)

        time_att = cls.sub_mgr_attach(sub_id, vm)
        return (time_reg, time_att)
    def single_register_attach(cls, sub_id, default_org, environment, vm):
        """Subscribe VM to Satellite by Register + Attach"""
        ssh.command('subscription-manager clean', hostname=vm)

        time_reg = cls.sub_mgr_register_authentication(
            default_org, environment, vm)

        time_att = cls.sub_mgr_attach(sub_id, vm)
        return (time_reg, time_att)
Пример #14
0
    def test_installer_check_services(self):
        """@Test: Services services start correctly

        @Feature: Installer

        @Assert: All services {'elasticsearch', 'foreman-proxy',
        'foreman-tasks', 'httpd', 'mongod', 'postgresql', 'pulp_celerybeat',
        'pulp_resource_manager', 'pulp_workers', 'qdrouterd', 'qpidd',
        'tomcat'} are started

        """
        services = (
            'elasticsearch',
            'foreman-proxy',
            'foreman-tasks',
            'httpd',
            'mongod',
            'postgresql',
            'pulp_celerybeat',
            'pulp_resource_manager',
            'pulp_workers',
            'qdrouterd',
            'qpidd',
            'tomcat',
        )

        # check `services` status using service command
        if get_host_info()[1] >= 7:
            status_format = 'systemctl status {0}'
        else:
            status_format = 'service {0} status'

        for service in services:
            result = ssh.command(status_format.format(service))
            self.assertEqual(result.return_code, 0)
            self.assertEqual(len(result.stderr), 0)

        # check status reported by hammer ping command
        result = ssh.command(u'hammer -u {0[0]} -p {0[1]} ping'.format(
            get_server_credentials()
        ))

        # iterate over the lines grouping every 3 lines
        # example [1, 2, 3, 4, 5, 6] will return [(1, 2, 3), (4, 5, 6)]
        for service, status, server_response in izip(
                *[iter(result.stdout)] * 3):
            service = service.replace(':', '').strip()
            status = status.split(':')[1].strip().lower()
            server_response = server_response.split(':', 1)[1].strip()
            self.assertEqual(
                status, 'ok',
                '{0} responded with {1}'.format(service, server_response)
            )
Пример #15
0
    def create(self):
        """Creates a virtual machine on the provisioning server using
        snap-guest

        :raises robottelo.vm.VirtualMachineError: Whenever a virtual machine
            could not be executed.

        """
        if self._created:
            return

        if self.provisioning_server is None:
            raise VirtualMachineError('Provisioning server is not configured')

        command_args = [
            'snap-guest',
            '-b {source_image}',
            '-t {target_image}',
            '-m {vm_ram}',
            '-c {vm_cpu}',
            '-n bridge=br0 -f',
        ]

        if self.image_dir is not None:
            command_args.append('-p {image_dir}')

        command = ' '.join(command_args).format(
            source_image='{0}-base'.format(self.distro),
            target_image=self.target_image,
            vm_ram=self.ram,
            vm_cpu=self.cpu,
            image_dir=self.image_dir,
        )

        result = ssh.command(command, self.provisioning_server)

        if result.return_code != 0:
            raise VirtualMachineError(
                'Failed to run snap-guest: {0}'.format(result.stderr))

        # Give some time to machine boot
        time.sleep(60)

        result = ssh.command(
            'ping -c 1 {}.local'.format(self.target_image),
            self.provisioning_server
        )
        if result.return_code != 0:
            raise VirtualMachineError(
                'Failed to fetch virtual machine IP address information')
        output = ''.join(result.stdout)
        self.ip_addr = output.split('(')[1].split(')')[0]
        self._created = True
Пример #16
0
    def create(self):
        """Creates a virtual machine on the provisioning server using
        snap-guest

        :raises robottelo.vm.VirtualMachineError: Whenever a virtual machine
            could not be executed.

        """
        if self._created:
            return

        if self.provisioning_server is None:
            raise VirtualMachineError('Provisioning server is not configured')

        command_args = [
            'snap-guest',
            '-b {source_image}',
            '-t {target_image}',
            '-m {vm_ram}',
            '-c {vm_cpu}',
            '-n bridge=br0 -f',
        ]

        if self.image_dir is not None:
            command_args.append('-p {image_dir}')

        command = ' '.join(command_args).format(
            source_image='{0}-base'.format(self.distro),
            target_image=self.target_image,
            vm_ram=self.ram,
            vm_cpu=self.cpu,
            image_dir=self.image_dir,
        )

        result = ssh.command(command, self.provisioning_server)

        if result.return_code != 0:
            raise VirtualMachineError('Failed to run snap-guest: {0}'.format(
                result.stderr))

        # Give some time to machine boot
        time.sleep(60)

        result = ssh.command('ping -c 1 {}.local'.format(self.target_image),
                             self.provisioning_server)
        if result.return_code != 0:
            raise VirtualMachineError(
                'Failed to fetch virtual machine IP address information')
        output = ''.join(result.stdout)
        self.ip_addr = output.split('(')[1].split(')')[0]
        self._created = True
Пример #17
0
    def test_import_orgs_recovery(self, test_data):
        """@test: Try to Import organizations with the same name to invoke
        usage of a recovery strategy (rename, map, none)

        @feature: Import Organizations Recover

        @assert: 2nd Import will result in No Action Taken, 3rd one will rename
        the new organizations, and the 4th one will map them

        """
        # prepare the data
        files = dict(self.default_dataset[1])
        files['users'] = update_csv_values(
            files['users'],
            'organization_id',
            test_data,
            self.default_dataset[0]
        )
        # initial import
        self.assertEqual(
            Import.organization({'csv-file': files['users']}).return_code, 0)
        # clear the .transition_data to clear the transition mapping
        ssh.command('rm -rf "${HOME}"/.transition_data')

        # use the 'none' strategy
        orgs_before = Org.list().stdout
        Import.organization({'csv-file': files['users'], 'recover': 'none'})
        self.assertEqual(orgs_before, Org.list().stdout)

        # use the default (rename) strategy
        ssh_imp_rename = Import.organization_with_tr_data(
            {'csv-file': files['users']}
        )
        self.assertEqual(len(ssh_imp_rename[1]), len(test_data))
        for record in ssh_imp_rename[1]:
            self.assertEqual(Org.info({'id': record['sat6']}).return_code, 0)
        Import.organization({'csv-file': files['users'], 'delete': True})

        # use the 'map' strategy
        ssh_imp_map = Import.organization_with_tr_data({
            'csv-file': files['users'],
            'recover': 'map',
        })
        for record in ssh_imp_map[1]:
            self.assertEqual(
                Org.info({'id': record['sat6']}).return_code, 0
            )
        Import.organization({'csv-file': files['users'], 'delete': True})
Пример #18
0
    def read_transition_csv(csv_files, key='sat5'):
        """Process remote CSV files and transition them to Dictionary

        The result depends on the order of the processed files. This script
        should mimic the behavior of Katello's hammer-cli-import
        `load_persistent_maps()`_ method.

        :param csv_files: A list of Strings containing absolute paths of the
            remote CSV files
        :param key: (Optional) key to be used to uniquely identify an entity
            ('sat5' by default)
        :returns: A Dictionary object holding the key-value pairs of
            the current state of entity mapping

        .. _load_persistent_maps():
            https://github.com/Katello/hammer-cli-import/blob/master/lib
            /hammer_cli_import/persistentmap.rb#L113

        """
        result = []
        for csv_file in csv_files:
            ssh_cat = ssh.command(u'cat {0}'.format(csv_file))
            if ssh_cat.return_code != 0:
                raise AssertionError(ssh_cat.stderr)
            csv = ssh_cat.stdout[:-1]
            keys = csv[0].split(',')
            for val in csv[1:]:
                record = dict(zip(keys, val.split(',')))
                for entity in result:
                    if entity[key] == record[key]:
                        entity.update(record)
                        break
                else:
                    result.append(record)
        return result
Пример #19
0
def csv_to_dataset(csv_files):
    """Process and return remote CSV files.

    Read the remote CSV files, and return a list of dictionaries for them

    :param csv_files: A list of strings, where each string is a path to a CSV
        file on the remote server.

    :returns: A list of dict, where each dict holds the contents of one CSV
        file.

    """
    result = []
    for csv_file in csv_files:
        ssh_cat = ssh.command(u'cat {0}'.format(csv_file))
        if ssh_cat.return_code != 0:
            raise AssertionError(ssh_cat.stderr)
        csv = ssh_cat.stdout[:-1]
        keys = csv[0].split(',')
        result.extend([
            dict(zip(keys, val.split(',')))
            for val
            in csv[1:]
        ])
    return result
Пример #20
0
    def execute(cls,
                command,
                user=None,
                password=None,
                expect_csv=False,
                timeout=None):
        """
        Executes the command
        """
        if user is None:
            user = cls.katello_user
        if password is None:
            password = cls.katello_passwd

        output_csv = u""

        if expect_csv:
            output_csv = u" --output csv"
        shell_cmd = u"LANG=%s hammer -v -u %s -p %s %s %s"

        cmd = shell_cmd % (cls.locale, user, password, output_csv, command)

        return ssh.command(cmd.encode('utf-8'),
                           expect_csv=expect_csv,
                           timeout=timeout)
Пример #21
0
    def single_register_activation_key(cls, ak_name, default_org, vm_ip):
        """Subscribe VM to Satellite by Register + ActivationKey"""

        # note: must create ssh keys for vm if running on local
        result = ssh.command('subscription-manager clean', hostname=vm_ip)
        result = ssh.command(
            'time -p subscription-manager register --activationkey={0} '
            '--org={1}'.format(ak_name, default_org),
            hostname=vm_ip
        )

        if result.return_code != 0:
            LOGGER.error('Fail to subscribe {} by ak!'.format(vm_ip))
        else:
            LOGGER.info('Subscribe client {} successfully'.format(vm_ip))
        return cls.get_real_time(result.stderr)
Пример #22
0
    def _traverse_command_tree(self, command):
        """Recursively walk through the hammer commands tree and assert that
        the expected options are present.

        """
        output = hammer.parse_help(
            ssh.command('{0} --help'.format(command)).stdout
        )
        command_options = set([option['name'] for option in output['options']])
        command_subcommands = set(
            [subcommand['name'] for subcommand in output['subcommands']]
        )
        if 'discovery_rule' in command and bz_bug_is_open(1219610):
            # Adjust the discovery_rule subcommand name. The expected data is
            # already with the naming convetion name
            expected = self._fetch_command_info(
                command.replace('discovery_rule', 'discovery-rule'))
        else:
            expected = self._fetch_command_info(command)
        expected_options = set()
        expected_subcommands = set()

        if expected is not None:
            expected_options = set(
                [option['name'] for option in expected['options']]
            )
            expected_subcommands = set(
                [subcommand['name'] for subcommand in expected['subcommands']]
            )

        if command == 'hammer' and bz_bug_is_open(1219610):
            # Adjust the discovery_rule subcommand name
            command_subcommands.discard('discovery_rule')
            command_subcommands.add('discovery-rule')

        added_options = tuple(command_options - expected_options)
        removed_options = tuple(expected_options - command_options)
        added_subcommands = tuple(command_subcommands - expected_subcommands)
        removed_subcommands = tuple(expected_subcommands - command_subcommands)

        if (added_options or added_subcommands or removed_options or
                removed_subcommands):
            diff = {
                'added_command': expected is None,
            }
            if added_options:
                diff['added_options'] = added_options
            if removed_options:
                diff['removed_options'] = removed_options
            if added_subcommands:
                diff['added_subcommands'] = added_subcommands
            if removed_subcommands:
                diff['removed_subcommands'] = removed_subcommands
            self.differences[command] = diff

        if len(output['subcommands']) > 0:
            for subcommand in output['subcommands']:
                self._traverse_command_tree(
                    '{0} {1}'.format(command, subcommand['name'])
                )
Пример #23
0
    def test_hammer_ping(self):
        """
        @test: hammer ping return code
        @feature: hammer ping
        @steps:
        1. Execute hammer ping
        2. Check its return code, should be 0 if all services are ok else != 0
        @assert: hammer ping returns a right return code
        @bz: 1094826
        """
        result = ssh.command('hammer ping')
        self.assertEqual(len(result.stderr), 0)

        status_count = 0
        ok_count = 0

        # iterate over the lines grouping every 3 lines
        # example [1, 2, 3, 4, 5, 6] will return [(1, 2, 3), (4, 5, 6)]
        # only the status line is relevant for this test
        for _, status, _ in izip(*[iter(result.stdout)]*3):
            status_count += 1

            if status.split(':')[1].strip().lower() == 'ok':
                ok_count += 1

        if status_count == ok_count:
            self.assertEqual(
                result.return_code, 0,
                'Return code should be 0 if all services are ok')
        else:
            self.assertNotEqual(
                result.return_code, 0,
                'Return code should not be 0 if any service is not ok')
    def single_register_activation_key(cls, ak_name, default_org, vm):
        """Subscribe VM to Satellite by Register + ActivationKey"""

        # note: must create ssh keys for vm if running on local
        result = ssh.command('subscription-manager clean', hostname=vm)
        result = ssh.command(
            'time -p subscription-manager register --activationkey={0} '
            '--org={1}'.format(ak_name, default_org),
            hostname=vm)

        if result.return_code != 0:
            LOGGER.error('Fail to subscribe {} by ak!'.format(vm))
            return
        LOGGER.info('Subscribe client {} successfully'.format(vm))
        real_time = [
            real for real in result.stderr.split('\n')
            if real.startswith('real')
        ]
        return float(real_time[0].split(' ')[1])
Пример #25
0
def default_url_on_new_port(oldport, newport):
    """
    Creates context where the default smart-proxy is forwarded on a new port
    """
    logger = logging.getLogger("robottelo")
    domain = conf.properties['main.server.hostname']
    user = conf.properties['main.server.ssh.username']
    key = conf.properties['main.server.ssh.key_private']
    ssh.upload_file(key, "/tmp/dsa_%s" % newport)
    ssh.command("chmod 700 /tmp/dsa_%s" % newport)
    with ssh._get_connection() as connection:
        command = "ssh -i %s -L %s:%s:%s %s@%s" % (
            "/tmp/dsa_%s" % newport, newport, domain, oldport, user, domain
        )
        logger.debug("Creating tunell %s", command)
        _, _, stderr = connection.exec_command(command, 1000)
        if len(stderr) > 0:
            logger.debug("Tunell failed %s", stderr)
        yield "https://%s:%s" % (domain, newport)
    def _get_registered_uuids(self):
        """Get all registered systems' uuids from Postgres"""
        self.logger.info('Get UUID from database:')

        result = ssh.command("""su postgres -c 'psql -A -t -d foreman -c """
                             """"SELECT uuid FROM katello_systems;"'""")

        if result.return_code != 0:
            self.logger.error('Fail to fetch uuids.')
            raise RuntimeError('Invalid uuids. Stop!')
        return result.stdout
Пример #27
0
    def _download_manifest(self):
        self.logger.info(
            'Start downloading manifest: {}'.format(MANIFEST_FILE_NAME))

        result = ssh.command('rm -f {0}; curl {1} -o /root/{0}'.format(
            MANIFEST_FILE_NAME, self.manifest_file))

        if result.return_code != 0:
            self.logger.error('Fail to download manifest!')
            raise RuntimeError('Unable to download manifest. Stop!')
        self.logger.info('Downloading manifest complete.')
Пример #28
0
    def destroy(self):
        """Destroys the virtual machine on the provisioning server"""
        if not self._created:
            return

        result = ssh.command('virsh destroy {0}'.format(self.target_image),
                             hostname=self.provisioning_server)
        if result.return_code > 0:
            logger.warning(result.stderr)
        result = ssh.command('virsh undefine {0}'.format(self.target_image),
                             hostname=self.provisioning_server)
        if result.return_code > 0:
            logger.warning(result.stderr)

        image_name = '{0}.img'.format(self.target_image)
        result = ssh.command('rm {0}'.format(
            os.path.join(self.image_dir, image_name)),
                             hostname=self.provisioning_server)
        if result.return_code > 0:
            logger.warning(result.stderr)
Пример #29
0
    def test_installer_check_services(self):
        # devnote:
        # maybe `hammer ping` command might be useful here to check the health
        # status
        """
        @test: Services services start correctly
        @feature: Installer
        @assert: All services {katello-jobs, tomcat6, foreman, pulp,
        passenger-analytics, httpd, foreman_proxy, elasticsearch, postgresql,
        mongod} are started
        """

        services = ('katello-jobs', 'tomcat6', 'foreman', 'httpd',
                    'elasticsearch', 'postgresql', 'mongod')

        # check `services` status using service command
        for service in services:
            result = ssh.command('service %s status' % service)
            logger.debug(result.stdout)

            if service == 'foreman':
                self.assertEqual(result.return_code, 1)
            else:
                self.assertEqual(result.return_code, 0)

            self.assertEqual(len(result.stderr), 0)

        # check status reported by hammer ping command
        result = ssh.command('hammer ping')

        # iterate over the lines grouping every 3 lines
        # example [1, 2, 3, 4, 5, 6] will return [(1, 2, 3), (4, 5, 6)]
        for service, status, server_response in izip(
                *[iter(result.stdout)] * 3):
            service = service.replace(':', '').strip()
            status = status.split(':')[1].strip().lower()
            server_response = server_response.split(':', 1)[1].strip()
            logger.debug('%s [%s] - %s', service, status, server_response)
            self.assertEqual(status, 'ok',
                             '%s responded with %s' % (service,
                                                       server_response))
    def _download_manifest(self):
        self.logger.info(
            'Start downloading manifest: {}'.format(MANIFEST_FILE_NAME))

        result = ssh.command(
            'rm -f {0}; curl {1} -o /root/{0}'
            .format(MANIFEST_FILE_NAME, self.manifest_file))

        if result.return_code != 0:
            self.logger.error('Fail to download manifest!')
            raise RuntimeError('Unable to download manifest. Stop!')
        self.logger.info('Downloading manifest complete.')
Пример #31
0
    def sub_mgr_attach(cls, pool_id, vm_ip):
        """subscription-manager attach --pool=pool_id"""
        result = ssh.command(
            'time -p subscription-manager attach --pool={}'.format(pool_id),
            hostname=vm_ip
        )

        if result.return_code != 0:
            LOGGER.error('Fail to attach client {}'.format(vm_ip))
        else:
            LOGGER.info('Attach client {} successfully'.format(vm_ip))
        return cls.get_real_time(result.stderr)
    def single_register_activation_key(cls, ak_name, default_org, vm):
        """Subscribe VM to Satellite by Register + ActivationKey"""

        # note: must create ssh keys for vm if running on local
        result = ssh.command('subscription-manager clean', hostname=vm)
        result = ssh.command(
            'time -p subscription-manager register --activationkey={0} '
            '--org={1}'.format(ak_name, default_org),
            hostname=vm
        )

        if result.return_code != 0:
            LOGGER.error('Fail to subscribe {} by ak!'.format(vm))
            return
        LOGGER.info('Subscribe client {} successfully'.format(vm))
        real_time = [
            real
            for real in result.stderr.split('\n')
            if real.startswith('real')
        ]
        return float(real_time[0].split(' ')[1])
    def _get_registered_uuids(self):
        """Get all registered systems' uuids from Postgres"""
        self.logger.info('Get UUID from database:')

        result = ssh.command(
            """su postgres -c 'psql -A -t -d foreman -c """
            """"SELECT uuid FROM katello_systems;"'""")

        if result.return_code != 0:
            self.logger.error('Fail to fetch uuids.')
            raise RuntimeError('Invalid uuids. Stop!')
        return result.stdout
Пример #34
0
    def test_import_users_merge(self, test_data):
        """@test: Try to Merge users with the same name using 'merge-users'
        option.

        @feature: Import Users Map-users

        @assert: Users imported in 2nd import are being mapped to the existing
        ones with the same name

        """
        # prepare the data
        tmp_dir = self.default_dataset[0]
        files = dict(self.default_dataset[1])
        pwdfiles = [
            os.path.join(tmp_dir, gen_string('alpha', 6)) for _ in range(2)
        ]
        files['users'] = update_csv_values(
            files['users'],
            u'organization_id',
            test_data,
            self.default_dataset[0]
        )
        # initial import
        for result in (
            Import.organization({'csv-file': files['users']}),
            Import.user({
                'csv-file': files['users'], 'new-passwords': pwdfiles[0],
            }),
        ):
            self.assertEqual(result.return_code, 0)
        # clear the .transition_data to clear the transition mapping
        ssh.command('rm -rf "${HOME}"/.transition_data/users*')
        # import users using merge-users option
        import_merge = Import.user_with_tr_data({
            'csv-file': files['users'],
            'new-passwords': pwdfiles[1],
            'merge-users': True,
        })
        for record in import_merge[1]:
            self.assertNotEqual(User.info({'id': record['sat6']}).stdout, '')
Пример #35
0
def generate_command_tree(command):
    """Recursively walk trhough the hammer commands and subcommands and fetch
    their help. Return a dictionary with the contents.

    """
    output = ssh.command('{0} --help'.format(command)).stdout
    contents = hammer.parse_help(output)
    if len(contents['subcommands']) > 0:
        for subcommand in contents['subcommands']:
            subcommand.update(generate_command_tree(
                '{0} {1}'.format(command, subcommand['name'])
            ))
    return contents
Пример #36
0
    def test_reimport_users_default_negative(self, test_data):
        """@test: Try to Import all users from the
        predefined source and try to import them again

        @feature: Repetitive User Import

        @assert: 2nd Import will result in No Action Taken

        """
        tmp_dir = self.default_dataset[0]
        files = dict(self.default_dataset[1])
        pwdfile = os.path.join(tmp_dir, gen_string('alpha', 6))
        files['users'] = update_csv_values(
            files['users'],
            u'organization_id',
            test_data,
            self.default_dataset[0]
        )
        # Import the organizations first
        self.assertEqual(
            Import.organization({
                'csv-file': files['users'],
            }).return_code, 0)
        self.assertEqual(
            Import.user({
                'csv-file': files['users'],
                'new-passwords': pwdfile,
            }).return_code, 0)
        ssh.command(u'rm -rf {}'.format(pwdfile))
        users_before = set(user['login'] for user in User.list().stdout)
        self.assertEqual(
            Import.user({
                'csv-file': files['users'],
                'new-passwords': pwdfile,
            }).return_code, 0)
        users_after = set(user['login'] for user in User.list().stdout)
        self.assertTrue(users_after.issubset(users_before))
Пример #37
0
    def execute(cls, command, user=None, password=None, output_format=None,
                timeout=None):
        """Executes the cli ``command`` on the server via ssh"""
        user, password = cls._get_username_password(user, password)

        cmd = u'LANG={0} hammer -v -u {1} -p {2} {3} {4}'.format(
            conf.properties['main.locale'],
            user,
            password,
            u'--output={0}'.format(output_format) if output_format else u'',
            command
        )

        return ssh.command(
            cmd.encode('utf-8'), output_format=output_format, timeout=timeout)
Пример #38
0
    def user_with_tr_data(cls, options=None):
        """Import Users (from spacewalk-report users).

        :returns: A tuple of SSHCommandResult and a Dictionary containing
        the transition data of the Import

        """
        result = cls.user(options)
        transition_data = []
        if result.return_code == 0:
            transition_data = cls.read_transition_csv(
                ssh.command(
                    u'ls -v ${HOME}/.transition_data/users*'
                ).stdout[:-1]
            )
        return (result, transition_data)
Пример #39
0
    def host_collection_with_tr_data(cls, options=None):
        """Import Host Collections (from spacewalk-report system-groups).

        :returns: A tuple of SSHCommandResult and a Dictionary containing
        the transition data of the Import

        """
        result = cls.host_collection(options)
        transition_data = []
        if result.return_code == 0:
            transition_data = cls.read_transition_csv(
                ssh.command(
                    u'ls -v ${HOME}/.transition_data/host_collections*'
                ).stdout[:-1]
            )
        return (result, transition_data)
    def sub_mgr_attach(cls, pool_id, vm):
        """subscription-manager attach --pool=pool_id"""
        result = ssh.command(
            'time -p subscription-manager attach --pool={}'.format(pool_id),
            hostname=vm)

        if result.return_code != 0:
            LOGGER.error('Fail to attach client {}'.format(vm))
            return
        LOGGER.info('Attach client {} successfully'.format(vm))
        real_time = [
            real for real in result.stderr.split('\n')
            if real.startswith('real')
        ]
        real_time = real_time[0].split(' ')[1]
        return float(real_time)
Пример #41
0
    def test_foreman_version(self):
        """@Test: Check if /usr/share/foreman/VERSION does not contain the
        develop tag.

        @Feature: Smoke Test

        @Assert: The file content does not have the develop tag.

        """
        result = ssh.command("cat /usr/share/foreman/VERSION")
        self.assertEqual(result.return_code, 0)

        if get_server_software() == "downstream":
            self.assertNotIn("develop", u"".join(result.stdout))
        else:
            self.assertIn("develop", u"".join(result.stdout))
Пример #42
0
    def execute(self, command, user=None, password=None, expect_csv=False):
        """
        Executes the command
        """
        if user is None:
            user = self.katello_user
        if password is None:
            password = self.katello_passwd

        output_csv = ""
        if expect_csv:
            output_csv = " --output csv"
        shell_cmd = "LANG=%s hammer -v -u %s -p %s" + output_csv + " %s"
        cmd = shell_cmd % (self.locale, user, password, command)

        return ssh.command(cmd, expect_csv=expect_csv)
Пример #43
0
    def run(self, cmd):
        """Runs a ssh command on the virtual machine

        :param str cmd: Command to run on the virtual machine
        :return: A :class:`robottelo.common.ssh.SSHCommandResult` instance with
            the commands results
        :rtype: robottelo.common.ssh.SSHCommandResult
        :raises robottelo.vm.VirtualMachineError: If the virtual machine is not
            created.

        """
        if not self._created:
            raise VirtualMachineError(
                'The virtual machine should be created before running any ssh '
                'command')

        return ssh.command(cmd, hostname=self.ip_addr)
Пример #44
0
    def execute(cls,
                command,
                user=None,
                password=None,
                output_format=None,
                timeout=None):
        """Executes the cli ``command`` on the server via ssh"""
        user, password = cls._get_username_password(user, password)

        cmd = u'LANG={0} hammer -v -u {1} -p {2} {3} {4}'.format(
            conf.properties['main.locale'], user, password,
            u'--output={0}'.format(output_format) if output_format else u'',
            command)

        return ssh.command(cmd.encode('utf-8'),
                           output_format=output_format,
                           timeout=timeout)
    def sub_mgr_register_authentication(cls, default_org, environment, vm):
        """subscription-manager register -u -p --org --environment"""
        result = ssh.command(
            'time -p subscription-manager register --username={0} '
            '--password={1} '
            '--org={2} '
            '--environment={3}'.format(cls.username, cls.password, default_org,
                                       environment),
            hostname=vm)

        if result.return_code != 0:
            LOGGER.error('Fail to register client {} by sub-mgr!'.format(vm))
            return
        LOGGER.info('Register client {} successfully'.format(vm))
        real_time = [
            real for real in result.stderr.split('\n')
            if real.startswith('real')
        ]
        real_time = real_time[0].split(' ')[1]
        return float(real_time)
Пример #46
0
    def test_hammer_ping(self):
        """@test: hammer ping return code

        @feature: hammer ping

        @steps:
        1. Execute hammer ping
        2. Check its return code, should be 0 if all services are ok else != 0

        @assert: hammer ping returns a right return code

        """
        result = ssh.command('hammer -u {0} -p {1} ping'.format(
            conf.properties['foreman.admin.username'],
            conf.properties['foreman.admin.password']))
        self.assertEqual(len(result.stderr), 0)

        status_count = 0
        ok_count = 0

        # iterate over the lines grouping every 3 lines
        # example [1, 2, 3, 4, 5, 6] will return [(1, 2, 3), (4, 5, 6)]
        # only the status line is relevant for this test
        for _, status, _ in izip(*[iter(result.stdout)] * 3):
            status_count += 1

            if status.split(':')[1].strip().lower() == 'ok':
                ok_count += 1

        if status_count == ok_count:
            self.assertEqual(result.return_code, 0,
                             'Return code should be 0 if all services are ok')
        else:
            self.assertNotEqual(
                result.return_code, 0,
                'Return code should not be 0 if any service is not ok')
Пример #47
0
from writer import Writer

import sys
import time

old_stdout = sys.stdout
log = open('timing-subscription', 'w')
# log = open('test.log','w')
sys.stdout = Writer(sys.stdout, log)
'''
for i in range(100):
	print "Attempt: {0}".format(i)
'''

print "Timing register by ak:"
for i in range(20):
    print "Attempt: {0}".format(i)
    result = ssh.command('subscription-manager clean', hostname='10.12.23.223')
    if result.return_code != 0:
        print "Failed to subscribe: {0} and return code: {1}".format(
            result.stderr, result.return_code)
    start = time.time()
    result = ssh.command(
        'subscription-manager register --activationkey=ak-1 --org="Default_Organization"',
        hostname='10.12.23.223')
    end = time.time()
    print 'real', '	', end - start, 's'

sys.stdout = old_stdout
log.close()
# Testing upload manifest to Red Hat Satellite 6 by Robottelo Framework
import os
import sys
import time

os.getcwd()
#sys.stdout = open('timing-upload','w')
#os.chdir('/home/chozhang/Documents/robottelo/robottelo/')

from robottelo.common import ssh
from robottelo.cli.subscription import Subscription

# open log file
print 'Timing uploading manfest to server:'
r = ssh.command('hostname')
print r.stdout[0]
ssh.command('rm test_manifest.zip')

ssh.upload_file('/home/chozhang/Documents/satellite6/20150526-10k-RHEL-Manifest.zip','test_manifest.zip')
start = time.time()
result = Subscription.upload({
  'file':'./test_manifest.zip',
  'organization-id':'1'
})

if result.return_code != 0:
  print "Failed to upload manifest: {0} and return code: {1}" \
	.format(result.stderr, result.return_code)
else:
   # print subscription list
   result = Subscription.list({'organization-id':'1'}, per_page=False)