Skip to content

Commit

Permalink
Add e2e tests for Registration API
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Talreja <[email protected]>
  • Loading branch information
Gauravtalreja1 authored and ogajduse committed Jan 17, 2023
1 parent fbfb8ad commit f23689c
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pytest_fixtures/component/provision_pxe.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@pytest.fixture(scope='module')
def module_provisioning_capsule(module_target_sat, module_location):
"""Assigns the `module_location` to Satellite's internal capsule and returns it"""
capsule = module_target_sat.internal_capsule
capsule = module_target_sat.nailgun_smart_proxy
capsule.location = [module_location]
return capsule.update(['location'])

Expand Down
6 changes: 6 additions & 0 deletions robottelo/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class Colored(Box):
SATELLITE_VERSION = "6.14"
SATELLITE_OS_VERSION = "8"

# Default system ports
HTTPS_PORT = '443'

# Client port for HTTP traffic for Satellite & Capsule
CLIENT_PORT = HTTPS_PORT

LOCALES = (
'cs_CZ',
'de',
Expand Down
43 changes: 22 additions & 21 deletions robottelo/hosts.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import importlib
import io
import json
import random
import re
import time
from configparser import ConfigParser
from contextlib import contextmanager
from functools import cached_property
from functools import lru_cache
from pathlib import Path
from pathlib import PurePath
from tempfile import NamedTemporaryFile
from urllib.parse import urlencode
from urllib.parse import urljoin
from urllib.parse import urlunsplit

Expand Down Expand Up @@ -363,6 +364,14 @@ def subscription_manager_attach_pool(self, pool_list=[]):
result.append(self.execute(f'subscription-manager attach --pool={pool}'))
return result

@property
def subscription_config(self):
"Returns subscription config for the host as ConfigParser object"
config = self.execute('cat /etc/rhsm/rhsm.conf').stdout
cp = ConfigParser()
cp.read_file(io.StringIO(config))
return cp

def create_custom_repos(self, **kwargs):
"""Create custom repofiles.
Each ``kwargs`` item will result in one repository file created. Where
Expand Down Expand Up @@ -509,7 +518,7 @@ def register(
loc,
activation_keys,
satellite=None,
target=satellite,
target=None,
setup_insights=False,
setup_remote_execution=True,
setup_remote_execution_pull=False,
Expand All @@ -523,18 +532,16 @@ def register(
ignore_subman_errors=False,
force=False,
insecure=True,
username=settings.server.admin_username,
password=settings.server.admin_password,
hostgroup=None,
):
"""Registers content host to the Satellite or Capsule server
using a global registration template.
:param target: Satellite or Capusle hostname to register to, required.
:param satellite: Satellite object, used for running hammer CLI when target is smart_proxy.
:param org: Organization to register content host for, required.
:param loc: Location to register content host for, required.
:param activation_keys: Activation key name to register content host
with, required.
:param activation_keys: Activation key name to register content host with, required.
:param setup_insights: Install and register Insights client, requires OS repo.
:param setup_remote_execution: Copy remote execution SSH key.
:param setup_remote_execution_pull: Deploy pull provider client on host
Expand All @@ -548,12 +555,8 @@ def register(
:param ignore_subman_errors: Ignore subscription manager errors.
:param force: Register the content host even if it's already registered.
:param insecure: Don't verify server authenticity.
:param username: Satellite admin username
:param password: Satellite admin password
:param hostgroup: hostgroup to register with
:param port: Capsule port, if unset template is pulled straight from Satellite
:return: SSHCommandResult instance filled with the result of the
registration.
:return: SSHCommandResult instance filled with the result of the registration
"""
options = {
'activation-keys': activation_keys,
Expand All @@ -564,6 +567,9 @@ def register(
}
if target.__class__.__name__ == 'Capsule':
options['smart-proxy'] = target.hostname
elif target is not None and target.__class__.__name__ not in ['Capsule', 'Satellite']:
raise ValueError('Global registration method can be used with Satellite/Capsule only')

if lifecycle_environment is not None:
options['lifecycle_environment_id'] = lifecycle_environment.id
if operating_system is not None:
Expand All @@ -573,7 +579,7 @@ def register(
if packages is not None:
options['packages'] = '+'.join(packages)
if repo is not None:
options['repo'] = urlencode({'repo': repo})
options['repo'] = repo
if setup_insights is not None:
options['setup-insights'] = str(setup_insights).lower()
if setup_remote_execution is not None:
Expand Down Expand Up @@ -1294,9 +1300,11 @@ class Capsule(ContentHost, CapsuleMixins):

@property
def nailgun_capsule(self):
from nailgun.entities import Capsule as NailgunCapsule
return self.satellite.api.Capsule().search(query={'search': f'name={self.hostname}'})[0]

return NailgunCapsule().search(query={'search': f'name={self.hostname}'})[0]
@property
def nailgun_smart_proxy(self):
return self.satellite.api.SmartProxy().search(query={'search': f'name={self.hostname}'})[0]

@cached_property
def is_upstream(self):
Expand Down Expand Up @@ -1448,8 +1456,6 @@ def cli(self):

class Satellite(Capsule, SatelliteMixins):
def __init__(self, hostname=None, **kwargs):
from robottelo.config import settings

hostname = hostname or settings.server.hostname # instance attr set by broker.Host
self.port = kwargs.get('port', settings.server.port)
super().__init__(hostname=hostname, **kwargs)
Expand Down Expand Up @@ -1517,11 +1523,6 @@ def cli(self):
pass
return self._cli

@property
def internal_capsule(self):
capsule_list = self.api.SmartProxy().search(query={'search': f'name={self.hostname}'})
return None if not capsule_list else capsule_list[0]

def ui_session(self, testname=None, user=None, password=None, url=None, login=True):
"""Initialize an airgun Session object and store it as self.ui_session"""

Expand Down
5 changes: 2 additions & 3 deletions tests/foreman/api/test_convert2rhel.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ def register_host(sat, act_key, module_org, module_loc, host, ubi=None):
location=module_loc,
insecure=True,
repo=ubi,
).create()['registration_command']
result = host.execute(command)
assert result.status == 0
).create()
assert host.execute(command).status == 0


@pytest.fixture(scope='module')
Expand Down
90 changes: 90 additions & 0 deletions tests/foreman/api/test_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""Tests for registration.
:Requirement: Registration
:CaseLevel: Acceptance
:CaseComponent: Registration
:CaseAutomation: Automated
:CaseImportance: Critical
:Assignee: sbible
:TestType: Functional
:Upstream: No
"""
import pytest

from robottelo.constants import CLIENT_PORT
from robottelo.utils.issue_handlers import is_open

pytestmark = pytest.mark.tier1


@pytest.mark.e2e
@pytest.mark.no_containers
def test_host_registration_end_to_end(
module_org,
module_location,
module_ak_with_synced_repo,
module_target_sat,
module_capsule_configured,
rhel_contenthost,
):
"""Verify content host registration with global registration
:id: 219567a8-856a-11ed-944d-03d9b43011c2
:steps:
1. Register host with global registration template to Satellite and Capsule
:expectedresults: Host registered successfully
:BZ: 2156926
:customerscenario: true
"""
command = module_target_sat.api.RegistrationCommand(
organization=module_org,
activation_keys=[module_ak_with_synced_repo.name],
location=module_location,
).create()

result = rhel_contenthost.execute(command)
if is_open('BZ:2156926') and rhel_contenthost.os_version.major == 6:
assert result.status == 1, f'Failed to register host: {result.stderr}'
else:
assert result.status == 0, f'Failed to register host: {result.stderr}'

# Verify server.hostname and server.port from subscription-manager config
assert module_target_sat.hostname == rhel_contenthost.subscription_config['server']['hostname']
assert CLIENT_PORT == rhel_contenthost.subscription_config['server']['port']

# Update module_capsule_configured to include module_org/module_location
nc = module_capsule_configured.nailgun_smart_proxy
module_target_sat.api.SmartProxy(id=nc.id, organization=[module_org]).update(['organization'])
module_target_sat.api.SmartProxy(id=nc.id, location=[module_location]).update(['location'])

command = module_target_sat.api.RegistrationCommand(
smart_proxy=nc,
organization=module_org,
activation_keys=[module_ak_with_synced_repo.name],
location=module_location,
force=True,
).create()
result = rhel_contenthost.execute(command)

if is_open('BZ:2156926') and rhel_contenthost.os_version.major == 6:
assert result.status == 1, f'Failed to register host: {result.stderr}'
else:
assert result.status == 0, f'Failed to register host: {result.stderr}'

# Verify server.hostname and server.port from subscription-manager config
assert (
module_capsule_configured.hostname
== rhel_contenthost.subscription_config['server']['hostname']
)
assert CLIENT_PORT == rhel_contenthost.subscription_config['server']['port']
41 changes: 32 additions & 9 deletions tests/foreman/cli/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
import pytest

from robottelo.config import settings
from robottelo.constants import CLIENT_PORT
from robottelo.utils.issue_handlers import is_open

pytestmark = pytest.mark.tier1


@pytest.mark.e2e
@pytest.mark.no_containers
@pytest.mark.rhel_ver_match('[^6].*')
def test_host_registration_end_to_end(
module_org,
module_location,
Expand All @@ -42,11 +43,23 @@ def test_host_registration_end_to_end(
1. Register host with global registration template to Satellite and Capsule
:expectedresults: Host registered successfully
:BZ: 2156926
:customerscenario: true
"""
result = rhel_contenthost.register(
module_org, module_location, module_ak_with_synced_repo.name, satellite=module_target_sat
)
assert result.status == 0, f'Failed to register host: {result.stderr}'

if is_open('BZ:2156926') and rhel_contenthost.os_version.major == 6:
assert result.status == 1, f'Failed to register host: {result.stderr}'
else:
assert result.status == 0, f'Failed to register host: {result.stderr}'

# Verify server.hostname and server.port from subscription-manager config
assert module_target_sat.hostname == rhel_contenthost.subscription_config['server']['hostname']
assert CLIENT_PORT == rhel_contenthost.subscription_config['server']['port']

# Update module_capsule_configured to include module_org/module_location
module_target_sat.cli.Capsule.update(
Expand All @@ -64,7 +77,17 @@ def test_host_registration_end_to_end(
satellite=module_target_sat,
force=True,
)
assert result.status == 0, f'Failed to register host: {result.stderr}'
if is_open('BZ:2156926') and rhel_contenthost.os_version.major == 6:
assert result.status == 1, f'Failed to register host: {result.stderr}'
else:
assert result.status == 0, f'Failed to register host: {result.stderr}'

# Verify server.hostname and server.port from subscription-manager config
assert (
module_capsule_configured.hostname
== rhel_contenthost.subscription_config['server']['hostname']
)
assert CLIENT_PORT == rhel_contenthost.subscription_config['server']['port']


def test_upgrade_katello_ca_consumer_rpm(
Expand Down Expand Up @@ -101,9 +124,9 @@ def test_upgrade_katello_ca_consumer_rpm(
f'rpm -Uvh "http://{target_sat.hostname}/pub/{consumer_cert_name}-1.0-1.noarch.rpm"'
)
# Check server URL is not Red Hat CDN's "subscription.rhsm.redhat.com"
result = vm.execute('grep ^hostname /etc/rhsm/rhsm.conf')
assert 'subscription.rhsm.redhat.com' not in result.stdout
assert target_sat.hostname in result.stdout
assert 'subscription.rhsm.redhat.com' != vm.subscription_config['server']['hostname']
assert target_sat.hostname == vm.subscription_config['server']['hostname']

# Get consumer cert source file
assert vm.execute(f'curl -O "http://{target_sat.hostname}/pub/{consumer_cert_src}"')
# Install repo for build tools
Expand All @@ -122,9 +145,9 @@ def test_upgrade_katello_ca_consumer_rpm(
# Install new rpmbuild/RPMS/noarch/katello-ca-consumer-*-2.noarch.rpm
assert vm.execute(f'yum install -y rpmbuild/RPMS/noarch/{new_consumer_cert_rpm}')
# Check server URL is not Red Hat CDN's "subscription.rhsm.redhat.com"
result = vm.execute('grep ^hostname /etc/rhsm/rhsm.conf')
assert 'subscription.rhsm.redhat.com' not in result.stdout
assert target_sat.hostname in result.stdout
assert 'subscription.rhsm.redhat.com' != vm.subscription_config['server']['hostname']
assert target_sat.hostname == vm.subscription_config['server']['hostname']

# Register as final check
vm.register_contenthost(module_org.label)
result = vm.execute('subscription-manager identity')
Expand Down
24 changes: 24 additions & 0 deletions tests/foreman/cli/test_remoteexecution.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,14 @@ def test_positive_run_job_on_host_registered_to_async_ssh_provider(
:parametrized: yes
"""
# Update module_capsule_configured_async_ssh to include module_org/smart_proxy_location
module_target_sat.cli.Capsule.update(
{
'name': module_capsule_configured_async_ssh.hostname,
'organization-ids': module_org.id,
'location-ids': smart_proxy_location.id,
}
)
result = rhel_contenthost.register(
module_org,
smart_proxy_location,
Expand Down Expand Up @@ -986,6 +994,14 @@ def test_positive_run_job_on_host_converted_to_pull_provider(
release='Client',
os_release=rhel_contenthost.os_version.major,
)
# Update module_capsule_configured_mqtt to include module_org/smart_proxy_location
module_target_sat.cli.Capsule.update(
{
'name': module_capsule_configured_mqtt.hostname,
'organization-ids': module_org.id,
'location-ids': smart_proxy_location.id,
}
)
# register host with rex, enable client repo, install katello-agent
result = rhel_contenthost.register(
module_org,
Expand Down Expand Up @@ -1075,6 +1091,14 @@ def test_positive_run_job_on_host_registered_to_pull_provider(
release='Client',
os_release=rhel_contenthost.os_version.major,
)
# Update module_capsule_configured_mqtt to include module_org/smart_proxy_location
module_target_sat.cli.Capsule.update(
{
'name': module_capsule_configured_mqtt.hostname,
'organization-ids': module_org.id,
'location-ids': smart_proxy_location.id,
}
)
# register host with pull provider rex (SAT-1677)
result = rhel_contenthost.register(
module_org,
Expand Down

0 comments on commit f23689c

Please sign in to comment.