def test_negative_create_with_invalid_host_limit_and_priority():
    """Create a discovery rule with invalid host limit and priority

    :id: e3c7acb1-ac56-496b-ac04-2a83f66ec290

    :expectedresults: Validation error should be raised
    """
    with pytest.raises(HTTPError):
        entities.DiscoveryRule(max_count=gen_string('alpha')).create()
    with pytest.raises(HTTPError):
        entities.DiscoveryRule(priority=gen_string('alpha')).create()
Beispiel #2
0
def test_negative_delete_with_discovery_rule(session, module_org, module_loc):
    """Attempt to delete hostgroup which has dependent discovery rule

    :id: bd046e9a-f0d0-4110-8f94-fd04193cb3af

    :customerscenario: true

    :BZ: 1254102

    :expectedresults: Hostgroup was not deleted. Informative error message
        was shown

    :CaseImportance: High

    :CaseLevel: Integration
    """
    hostgroup = entities.HostGroup(
        organization=[module_org], location=[module_loc]).create()
    entities.DiscoveryRule(
        hostgroup=hostgroup, organization=[module_org], location=[module_loc]).create()
    with session:
        assert session.hostgroup.search(hostgroup.name)[0]['Name'] == hostgroup.name
        # Make an attempt to delete host group that associated with discovery rule
        with raises(AssertionError) as context:
            session.hostgroup.delete(hostgroup.name)
        assert "Cannot delete record because dependent discovery rules exist" in str(
            context.value)
        assert session.hostgroup.search(hostgroup.name)[0]['Name'] == hostgroup.name
Beispiel #3
0
def test_negative_delete_rule_with_non_admin_user(module_loc, module_org,
                                                  reader_user, test_name):
    """Delete rule with non-admin user by associating discovery_reader role

    :id: 23a7627c-6a9b-493b-871f-698543adf1d2

    :expectedresults: User should validation error and rule should not be
        deleted successfully.

    :CaseLevel: Integration
    """
    hg = entities.HostGroup(organization=[module_org]).create()
    dr = entities.DiscoveryRule(
        hostgroup=hg,
        organization=[module_org],
        location=[module_loc]
    ).create()
    with Session(
            test_name,
            user=reader_user.login,
            password=reader_user.password
    ) as session:
        with raises(ValueError):
            session.discoveryrule.delete(dr.name)
        dr_val = session.discoveryrule.read_all()
        assert dr.name in [rule['Name'] for rule in dr_val]
Beispiel #4
0
def test_positive_view_existing_rule_with_non_admin_user(module_loc,
                                                         module_org,
                                                         reader_user,
                                                         test_name):
    """Existing rule should be viewed to non-admin user by associating
    discovery_reader role.

    :id: 0f5b0221-43be-47bc-8619-749824c4e54f

    :Steps:

        1. create a rule with admin user
        2. create a non-admin user and assign 'Discovery Reader' role
        3. Login with non-admin user

    :expectedresults: Rule should be visible to non-admin user.

    :CaseLevel: Integration
    """
    hg = entities.HostGroup(organization=[module_org]).create()
    dr = entities.DiscoveryRule(
        hostgroup=hg,
        organization=[module_org],
        location=[module_loc]
    ).create()
    with Session(
            test_name,
            user=reader_user.login,
            password=reader_user.password
    ) as session:
        dr_val = session.discoveryrule.read_all()
        assert dr.name in [rule['Name'] for rule in dr_val]
Beispiel #5
0
def test_positive_delete_rule_with_non_admin_user(manager_loc, manager_user,
                                                  module_org, test_name):
    """Delete rule with non-admin user by associating discovery_manager role

    :id: 7fa56bab-82d7-46c9-a4fa-c44ef173c703

    :expectedresults: Rule should be deleted successfully.

    :CaseLevel: Integration
    """
    hg = entities.HostGroup(organization=[module_org]).create()
    dr = entities.DiscoveryRule(
        hostgroup=hg,
        organization=[module_org],
        location=[manager_loc]
    ).create()
    with Session(
            test_name,
            user=manager_user.login,
            password=manager_user.password
    ) as session:
        dr_val = session.discoveryrule.read_all()
        assert dr.name in [rule['Name'] for rule in dr_val]
        session.discoveryrule.delete(dr.name)
        dr_val = session.discoveryrule.read_all()
        assert dr.name not in [rule['Name'] for rule in dr_val]
def test_positive_end_to_end_crud(module_org, module_location,
                                  module_hostgroup):
    """Create a new discovery rule with several attributes, update them
    and delete the rule itself.

    :id: 25366930-b7f4-4db8-a9c3-a470fe4f3583

    :expectedresults: Rule should be created, modified and deleted successfully
        with given attributes.

    :CaseImportance: Critical
    """
    # Create discovery rule
    searches = [
        'CPU_Count = 1',
        'disk_count < 5',
        'memory > 500',
        'model = KVM',
        'Organization = Default_Organization',
    ]
    name = gen_choice(list(valid_data_list().values()))
    search = gen_choice(searches)
    hostname = 'myhost-<%= rand(99999) %>'
    discovery_rule = entities.DiscoveryRule(
        name=name,
        search_=search,
        hostname=hostname,
        organization=[module_org],
        location=[module_location],
        hostgroup=module_hostgroup,
    ).create()
    assert name == discovery_rule.name
    assert hostname == discovery_rule.hostname
    assert search == discovery_rule.search_
    assert module_org.name == discovery_rule.organization[0].read().name
    assert module_location.name == discovery_rule.location[0].read().name
    assert discovery_rule.enabled is True

    # Update discovery rule
    name = gen_choice(list(valid_data_list().values()))
    search = 'Location = Default_Location'
    max_count = gen_integer(1, 100)
    enabled = False
    discovery_rule.name = name
    discovery_rule.search_ = search
    discovery_rule.max_count = max_count
    discovery_rule.enabled = enabled
    discovery_rule = discovery_rule.update(
        ['name', 'search_', 'max_count', 'enabled'])
    assert name == discovery_rule.name
    assert search == discovery_rule.search_
    assert max_count == discovery_rule.max_count
    assert enabled == discovery_rule.enabled

    # Delete discovery rule
    discovery_rule.delete()
    with pytest.raises(HTTPError):
        discovery_rule.read()
Beispiel #7
0
    def setUpClass(cls):
        """Creates two non-admin users with discovery Manager/Reader roles"""
        super(DiscoveryRuleRoleTestCase, cls).setUpClass()
        cls.per_page = entities.Setting().search(
            query={'search': 'name="entries_per_page"'})[0]
        cls.saved_per_page = str(cls.per_page.value)
        cls.per_page.value = '100000'
        cls.per_page.update({'value'})
        cls.org = entities.Organization().create()
        cls.loc = entities.Location().create()
        cls.manager_loc = entities.Location().create()
        cls.host_group = entities.HostGroup(
            organization=[cls.org]).create()

        # Create a discovery rule
        cls.rule_name = gen_string('alpha')
        entities.DiscoveryRule(
            name=cls.rule_name,
            hostgroup=cls.host_group,
            search_='cpu_count = 1',
            organization=[cls.org],
            location=[cls.loc],
        ).create()

        # create non-admin reader user with Discovery_Reader role
        reader_role = entities.Role().search(
            query={'search': 'name="Discovery Reader"'}
        )[0]
        cls.reader_user = gen_string('alpha')
        cls.reader_user_password = gen_string('alphanumeric')
        cls.read_user = entities.User(
            login=cls.reader_user,
            role=[reader_role],
            password=cls.reader_user_password,
            organization=[cls.org],
            location=[cls.loc],
            default_organization=cls.org,
            default_location=cls.loc,
        ).create()

        # create non-admin manager user with Discovery_Manager role
        manager_role = entities.Role().search(
            query={'search': 'name="Discovery Manager"'}
        )[0]
        cls.manager_user = gen_string('alpha')
        cls.manager_user_password = gen_string('alphanumeric')
        cls.manage_user = entities.User(
            login=cls.manager_user,
            role=[manager_role],
            password=cls.manager_user_password,
            organization=[cls.org],
            location=[cls.loc, cls.manager_loc],
            default_organization=cls.org,
        ).create()
Beispiel #8
0
def test_positive_update(session, module_loc, module_org):
    hg = entities.HostGroup(organization=[module_org]).create()
    dr = entities.DiscoveryRule(
        hostgroup=hg,
        organization=[module_org]
    ).create()
    with session:
        session.organization.select(org_name=module_org.name)
        session.discoveryrule.update(
            dr.name, {'locations.resources.assigned': [module_loc.name]})
        dr_val = session.discoveryrule.read(dr.name)
        assert dr_val[
                   'locations']['resources']['assigned'][0] == module_loc.name
Beispiel #9
0
def test_positive_delete(session, module_org):
    hg = entities.HostGroup(organization=[module_org]).create()
    dr = entities.DiscoveryRule(
        hostgroup=hg,
        organization=[module_org]
    ).create()
    with session:
        session.organization.select(org_name=module_org.name)
        dr_val = session.discoveryrule.read_all()
        assert dr.name in [rule['Name'] for rule in dr_val]
        session.discoveryrule.delete(dr.name)
        dr_val = session.discoveryrule.read_all()
        assert dr.name not in [rule['Name'] for rule in dr_val]
Beispiel #10
0
def test_positive_disable_and_enable(session, module_org):
    hg = entities.HostGroup(organization=[module_org]).create()
    dr = entities.DiscoveryRule(
        hostgroup=hg,
        organization=[module_org]
    ).create()
    with session:
        session.organization.select(org_name=module_org.name)
        # enable checkbox is true, by default
        session.discoveryrule.disable(dr.name)
        dr_val = session.discoveryrule.read(dr.name)
        assert (not dr_val['primary']['enabled'])
        session.discoveryrule.enable(dr.name)
        dr_val = session.discoveryrule.read(dr.name)
        assert dr_val['primary']['enabled']
Beispiel #11
0
    def test_positive_create_with_org_loc(self):
        """Create discovery rule by associating org and location

        @id: 121e0a30-8a24-47d7-974d-998886ed1ea7

        @Assert: Rule was created and with given org & location.
        """
        org = entities.Organization().create()
        loc = entities.Location().create()
        hostgroup = entities.HostGroup(organization=[org]).create()
        discovery_rule = entities.DiscoveryRule(
            hostgroup=hostgroup,
            search_='cpu_count = 1',
            organization=[org],
            location=[loc],
        ).create()
        self.assertEqual(org.name, discovery_rule.organization[0].read().name)
        self.assertEqual(loc.name, discovery_rule.location[0].read().name)
Beispiel #12
0
def test_positive_auto_provision_host_with_rule(session, discovery_org,
                                                discovery_location,
                                                module_host_group):
    """Create a new discovery rule and automatically create host from discovered host using that
    discovery rule.

    Set query as (e.g IP=IP_of_discovered_host)

    :id: 4488ab9a-d462-4a62-a1a1-e5656c8a8b99

    :Setup: Host should already be discovered

    :expectedresults: Host should reboot and provision

    :BZ: 1665471, 1731112

    :CaseImportance: High
    """
    host_ip = gen_ipaddr()
    discovered_host_name = create_discovered_host(ip_address=host_ip)['name']
    domain = module_host_group.domain.read()
    discovery_rule = entities.DiscoveryRule(
        max_count=1,
        hostgroup=module_host_group,
        search_=f'ip = {host_ip}',
        location=[discovery_location],
        organization=[discovery_org],
    ).create()
    with session:
        session.discoveredhosts.apply_action('Auto Provision',
                                             [discovered_host_name])
        host_name = f'{discovered_host_name}.{domain.name}'
        assert session.host.search(host_name)[0]['Name'] == host_name
        host_values = session.host.get_details(host_name)
        assert host_values['properties']['properties_table']['Status'] == 'OK'
        assert host_values['properties']['properties_table'][
            'IP Address'] == host_ip
        assert (
            host_values['properties']['properties_table']['Comment'] ==
            f"Auto-discovered and provisioned via rule '{discovery_rule.name}'"
        )
        assert not session.discoveredhosts.search(
            f'name = {discovered_host_name}')
    def setUp(self):
        """Instantiate a ``DiscoveryRule`` and give it several default attrs.

        Instantiate a ``DiscoveryRule`` object in memory, but do not create it
        on the Satellite server. This allows the object to be customized. Save
        it as ``self.discovery_rule``, set its ``hostgroup`` and ``search_``
        fields, and give its ``hostname`` field a default value.
        """
        searches = [
            'CPU_Count = 1',
            'disk_count < 5',
            'memory > 500',
            'model = KVM',
            'Organization = Default_Organization',
        ]
        self.discovery_rule = entities.DiscoveryRule(
            hostgroup=self.hostgroup,
            search_=gen_choice(searches),
        )
        self.discovery_rule._fields['hostname'].default = (
            'myhost-<%= rand(99999) %>')
Beispiel #14
0
    def test_negative_delete_with_discovery_rule(self):
        """Attempt to delete hostgroup which has dependent discovery rule

        :id: bd046e9a-f0d0-4110-8f94-fd04193cb3af

        :customerscenario: true

        :BZ: 1254102

        :expectedresults: Hostgroup was not deleted. Informative error message
            was shown

        :CaseImportance: High
        """
        hostgroup = entities.HostGroup(
            organization=[self.session_org]).create()
        entities.DiscoveryRule(
            hostgroup=hostgroup,
            organization=[self.session_org],
        ).create()
        with Session(self):
            self.hostgroup.search(hostgroup.name)
            self.hostgroup.click(common_locators['select_action_dropdown'] %
                                 hostgroup.name)
            self.hostgroup.click(common_locators['delete_button'] %
                                 hostgroup.name,
                                 wait_for_ajax=False)
            self.hostgroup.handle_alert(True)
            error = self.hostgroup.wait_until_element(
                common_locators['alert.error'])
            self.assertIsNotNone(error)
            self.assertEqual(
                error.text,
                'Error: Cannot delete record because dependent discovery '
                'rules exist')
            self.assertIsNotNone(self.hostgroup.search(hostgroup.name))
def test_positive_auto_provision_pxe_host(_module_user, module_org,
                                          module_location, discovery_settings,
                                          provisioning_env):
    """Auto provision a pxe-based host by executing discovery rules

    :id: c93fd7c9-41ef-4eb5-8042-f72e87e67e10

    :parametrized: yes

    :Setup: Provisioning should be configured and a host should be
        discovered

    :Steps: POST /api/v2/discovered_hosts/:id/auto_provision

    :expectedresults: Selected Host should be auto-provisioned successfully

    :CaseAutomation: Automated

    :CaseImportance: Critical
    """
    cfg = get_nailgun_config()
    if _module_user:
        cfg.auth = (_module_user[0].login, _module_user[1])

    # open a ssh channel and attach it to foreman-tail output
    ssh_client = ssh.get_client()
    with ssh_client.invoke_shell() as channel:
        channel.send('foreman-tail\r')

        with LibvirtGuest() as pxe_host:
            discovered_host = _assert_discovered_host(pxe_host,
                                                      channel,
                                                      user_config=cfg)
            # Provision just discovered host
            discovered_host.hostgroup = entities.HostGroup(
                cfg, id=provisioning_env['hostgroup']['id']).read()

            # create a discovery rule that will match hosts MAC address
            entities.DiscoveryRule(
                name=gen_string('alphanumeric'),
                search_=f"mac = {discovered_host.mac}",
                organization=[module_org],
                location=[module_location],
                hostgroup=entities.HostGroup(
                    cfg, id=provisioning_env['hostgroup']['id']).read(),
            ).create()
            # Auto-provision the host
            discovered_host.auto_provision()

            # Assertions
            provisioned_host = entities.Host(cfg).search(
                query={
                    'search':
                    'name={}.{}'.format(discovered_host.name,
                                        provisioning_env['domain']['name'])
                })[0]
            assert provisioned_host.subnet.read(
            ).name == provisioning_env['subnet']['name']
            assert (provisioned_host.operatingsystem.read().ptable[0].read().
                    name == provisioning_env['ptable']['name'])
            assert provisioned_host.operatingsystem.read(
            ).title == provisioning_env['os']['title']
            assert not entities.DiscoveredHost(cfg).search(
                query={'search': f'name={discovered_host.name}'})
Beispiel #16
0
def test_positive_list_host_based_on_rule_search_query(session, module_org,
                                                       module_loc,
                                                       module_discovery_env):
    """List all the discovered hosts resolved by given rule's search query
    e.g. all discovered hosts with cpu_count = 2, and list rule's associated
    hosts.

    :id: f7473fa2-7349-42d3-9cdb-f74b55d2f440

    :Steps:

        1. discovered host with cpu_count = 2
        2. Define a rule 'rule1' with search query cpu_count = 2
        3. Click on 'Discovered Hosts' from rule1
        4. Auto Provision the discovered host
        5. Click on 'Associated Hosts' from rule1

    :expectedresults:

        1. After step 3, the rule's Discovered host should be listed.
        2. The rule's Associated Host should be listed.

    :BZ: 1731112
    """
    ip_address = gen_ipaddr()
    cpu_count = gen_integer(2, 10)
    rule_search = 'cpu_count = {0}'.format(cpu_count)
    # any way create a host to be sure that this org has more than one host
    host = entities.Host(organization=module_org, location=module_loc).create()
    host_group = entities.HostGroup(
        organization=[module_org],
        location=[module_loc],
        medium=host.medium,
        root_pass=gen_string('alpha'),
        operatingsystem=host.operatingsystem,
        ptable=host.ptable,
        domain=host.domain,
        architecture=host.architecture,
    ).create()
    discovery_rule = entities.DiscoveryRule(hostgroup=host_group,
                                            search_=rule_search,
                                            organization=[module_org],
                                            location=[module_loc]).create()
    discovered_host = create_discovered_host(
        ip_address=ip_address, options={'physicalprocessorcount': cpu_count})
    # create an other discovered host with an other cpu count
    create_discovered_host(options={'physicalprocessorcount': cpu_count + 1})
    provisioned_host_name = '{0}.{1}'.format(discovered_host['name'],
                                             host.domain.read().name)
    with session:
        session.organization.select(org_name=module_org.name)
        session.location.select(loc_name=module_loc.name)
        values = session.discoveryrule.read_all()
        assert discovery_rule.name in [rule['Name'] for rule in values]
        values = session.discoveryrule.read_discovered_hosts(
            discovery_rule.name)
        assert values['searchbox'] == rule_search
        assert len(values['table']) == 1
        assert values['table'][0]['IP Address'] == ip_address
        assert values['table'][0]['CPUs'] == str(cpu_count)
        # auto provision the discovered host
        session.discoveredhosts.apply_action('Auto Provision',
                                             [discovered_host['name']])
        assert not session.discoveredhosts.search('name = "{0}"'.format(
            discovered_host['name']))
        values = session.discoveryrule.read_associated_hosts(
            discovery_rule.name)
        assert (values['searchbox'] == 'discovery_rule = "{0}"'.format(
            discovery_rule.name))
        assert len(values['table']) == 1
        assert values['table'][0]['Name'] == provisioned_host_name
        values = session.host.get_details(provisioned_host_name)
        assert (values['properties']['properties_table']['IP Address'] ==
                ip_address)