Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration Tests for DBus Register Method #3477

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions integration-tests/README.md
jstavel marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Integration Test for subscription-manager

There are integration tests for all parts of subscription-manager
in this directory.

DBus tests are presented currently - they verify DBus api of *rhsm.service*
see [DBus objects](https://www.candlepinproject.org/docs/subscription-manager/dbus_objects.html)

The tests use pytest ecosystem.

## Installation

1) Run local candlepin

```shell
podman run -d --name canlepin -p 8080:8080 -p 8443:8443 --hostname candlepin.local ghcr.io/ptoscano/candlepin-unofficial:latest
```

2) Create additional testing data in candlepin

Environments for *donaldduck* organization

```
curl --stderr /dev/null --insecure --user admin:admin --request POST \
--data '{"id": "env-id-1", "name": "env-name-1", "description": "Testing environment num. 1"}' \
--header 'accept: application/json' --header 'content-type: application/json' \
https://localhost:8443/candlepin/owners/donaldduck/environments

curl --stderr /dev/null --insecure --user admin:admin --request POST \
--data '{"id": "env-id-2", "name": "env-name-2", "description": "Testing environment num. 2"}' \
--header 'accept: application/json' --header 'content-type: application/json' \
https://localhost:8443/candlepin/owners/donaldduck/environments
```

> citation from 'man subscription-manager'
> With on-premise subscription services, such as Subscription Asset
> Manager, the infrastructure is more complex. The local
> administrator can define independent groups called organizations
> which represent physical or organizational divisions (--org).
> Those organizations can be subdivided into environments.

Activation keys for *donaldduck* organization

> The tests use already installed test activation keys
> They are:
> - *default_key*
> - *awesome_os_pool"

## Configuration

Tests use [Dynaconf](https://www.dynaconf.com/) to load config
values.

They are stored in a file in this directory *settings.toml*

Config values for _testing_ environment

```yaml
[testing]
candlepin.host = "localhost"
candlepin.port = 8443
candlepin.insecure = true
candlepin.prefix = "/candlepin"
candlepin.username = "duey"
candlepin.password = "password"
candlepin.org = "donaldduck"
candlepin.activation_keys = ["default_key","awesome_os_pool"]
candlepin.environment.names = ["env-name-01","env-name-02"]
candlepin.environment.ids = ["env-id-01","env-id-02"]

insights.legacy_upload = false
console.host = "cert.console.redhat.com"

auth_proxy.host =
auth_proxy.port = 3127
auth_proxy.username = "redhat"
auth_proxy.password = "redhat"

noauth_proxy.host =
noauth_proxy.port = 3129

insights.hbi_host = "cert.console.redhat.com"
```

Configuration for pytest

> There is a file *pytest.ini* in the main directory of this repo.
> It has nothing to do with integration-tests. It is a confiuration
> for unittests.

*integration-tests/pytest.ini*

```ini
[pytest]
addopts = "-srxv --capture=sys"
testpaths = "./"
log_cli = true
log_level = INFO
```

## Python virtual environment for testing

It is good practice to use python virtual environment to run the
tests. All required packages for pytest are stored in
*requirements.txt*.

> There is a file *requirements.txt* in the main directory of the
> repo. It is used by unittests. I has nothing to do with
> integration-tests at all.

```shell
cd integration-tests
python3 -mvenv venv
source venv
pip install -r requirements.txt
deactivate
```

jstavel marked this conversation as resolved.
Show resolved Hide resolved
## Running the tests

```shell
cd integration-tests
source venv
pytest
deativate
```

> There is a nice help for pytest in [Testing](../TESTING.md). It is
> full of interesting hits to run just a few tests, to increase output
> of a test run ...

### Runnning integration tests using tmt

You can use [Testing Farm](https://docs.testing-farm.io/Testing%20Farm/0.1/index.html)
to run the tests.

It suposes that the package *subscription-manager* is installed at a local box.

```shell
cd subscription-manager
sudo tmt --feeling-safe run -vvv --all provision --how local
```

> All details for tmt to run are stored at directory *systemtest*
> It is a starting point for deeper investigation to understand how
> the tests are run using tmt.
47 changes: 47 additions & 0 deletions integration-tests/conftest.py
jstavel marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import logging
import os

from dasbus.connection import MessageBus
from gi.repository import Gio

import gi

gi.require_version("Gio", "2.0")

logger = logging.getLogger(__name__)


class RHSMPrivateBus(MessageBus):
"""Representation of RHSM private bus connection that can be used as a context manager."""

def __init__(self, rhsm_register_server_proxy, *args, **kwargs):
"""Representation of RHSM private bus connection that can be used as a context manager.

:param rhsm_register_server_proxy: DBus proxy for the RHSM RegisterServer object
"""
super().__init__(*args, **kwargs)
self._rhsm_register_server_proxy = rhsm_register_server_proxy
self._private_bus_address = None

def __enter__(self):
logger.debug("subscription: starting RHSM private DBus session")
locale = os.environ.get("LANG", "")
self._private_bus_address = self._rhsm_register_server_proxy.Start(locale)
logger.debug("subscription: RHSM private DBus session has been started")
return self

def __exit__(self, _exc_type, _exc_value, _exc_traceback):
logger.debug("subscription: shutting down the RHSM private DBus session")
self.disconnect()
locale = os.environ.get("LANG", "")
self._rhsm_register_server_proxy.Stop(locale)
logger.debug("subscription: RHSM private DBus session has been shutdown")

def _get_connection(self):
"""Get a connection to RHSM private DBus session."""
# the RHSM private bus address is potentially sensitive
# so we will not log it
logger.info("Connecting to the RHSM private DBus session.")
return self._provider.get_addressed_bus_connection(
bus_address=self._private_bus_address, flags=Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT
)
25 changes: 25 additions & 0 deletions integration-tests/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from dasbus.identifier import DBusObjectIdentifier, DBusServiceIdentifier
from dasbus.connection import SystemMessageBus


HOST_DETAILS: str = "/var/lib/insights/host-details.json"
MACHINE_ID_FILE: str = "/etc/insights-client/machine-id"
RHSM_CONFIG_FILE_PATH: str = "/etc/rhsm/rhsm.conf"

RHSM_NAMESPACE = ("com", "redhat", "RHSM1")

RHSM = DBusServiceIdentifier(namespace=RHSM_NAMESPACE, message_bus=SystemMessageBus())

RHSM_CONFIG = DBusObjectIdentifier(namespace=RHSM_NAMESPACE, basename="Config")

RHSM_REGISTER_SERVER = DBusObjectIdentifier(namespace=RHSM_NAMESPACE, basename="RegisterServer")

RHSM_REGISTER = DBusObjectIdentifier(namespace=RHSM_NAMESPACE, basename="Register")

RHSM_UNREGISTER = DBusObjectIdentifier(namespace=RHSM_NAMESPACE, basename="Unregister")

RHSM_ENTITLEMENT = DBusObjectIdentifier(namespace=RHSM_NAMESPACE, basename="Entitlement")

RHSM_SYSPURPOSE = DBusObjectIdentifier(namespace=RHSM_NAMESPACE, basename="Syspurpose")

RHSM_CONSUMER = DBusObjectIdentifier(namespace=RHSM_NAMESPACE, basename="Consumer")
5 changes: 5 additions & 0 deletions integration-tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
addopts = "-srxv -vvv --capture=sys"
testpaths = "./"
log_cli = true
log_level = DEBUG
13 changes: 11 additions & 2 deletions integration-tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# the version of black is specified also in the stylish.yml github workflow;
# please update the version there in case it is bumped here
black==24.3.0
flake8
git+https://github.com/ptoscano/pytest-client-tools@main
pytest
pyyaml
pytest-randomly
pytest-timeout
simplejson
dasbus
pycairo
PyGObject
sh
iniparse
funcy
30 changes: 30 additions & 0 deletions integration-tests/scripts/post-environments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

USERNAME=admin
PASSWORD=admin
ORG=donaldduck

jstavel marked this conversation as resolved.
Show resolved Hide resolved

# The script will create two environments for an organization.
#
# citation from 'man subscription-manager':
#
# With on-premise subscription services, such as Subscription Asset Manager,
# the infrastructure is more complex. The local administrator can define
# independent groups called organizations which represent physical
# or organizational divisions (--org). Those organizations can be subdivided
# into environments (--environment).
#

curl -k --request POST --user ${USERNAME}:${PASSWORD} \
--data '{"id": "env-id-01", "name": "env-name-01", "description": "Testing environment num. 1"}' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
https://localhost:8443/candlepin/owners/${ORG}/environments

curl -k --request POST --user ${USERNAME}:${PASSWORD} \
--data '{"id": "env-id-02", "name": "env-name-02", "description": "Testing environment num. 2", "type": "content-template"}' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
https://localhost:8443/candlepin/owners/${ORG}/environments

jstavel marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 9 additions & 0 deletions integration-tests/scripts/run-local-candlepin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
#
# A local candlepin is used for most of integration tests.
# It is fast to run the tests and the candlepin comes with testing data.
# So the environment is mostly prepared in the candlepin after we run a contianer.
#
# For most information see https://github.com/ptoscano/candlepin-container-unofficial
#
podman run -d --name candlepin -p 8080:8080 -p 8443:8443 --hostname candlepin.local ghcr.io/ptoscano/candlepin-unofficial:latest
24 changes: 0 additions & 24 deletions integration-tests/test_consumer.py

This file was deleted.

Loading
Loading