Skip to content

Commit 066f4cc

Browse files
test: Added JSON output validation in connect tests
- Added comprehensive validation for connect command output - Update test documentation to reflect working JSON validation - Clean up code formatting
1 parent 0c8374e commit 066f4cc

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

integration-tests/test_connect.py

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import contextlib
10+
import json
1011
import time
1112
import logging
1213
import pytest
@@ -19,8 +20,10 @@
1920
check_yggdrasil_journalctl_logs,
2021
)
2122

23+
2224
logger = logging.getLogger(__name__)
2325

26+
2427
@pytest.mark.tier1
2528
@pytest.mark.parametrize(
2629
"auth, output_format",
@@ -29,7 +32,7 @@
2932
("basic", "json"),
3033
("activation-key", None),
3134
("activation-key", "json"),
32-
]
35+
],
3336
)
3437
def test_connect(external_candlepin, rhc, test_config, auth, output_format):
3538
"""
@@ -58,66 +61,64 @@ def test_connect(external_candlepin, rhc, test_config, auth, output_format):
5861
"Connected to Red Hat Subscription Management",
5962
"Activated the yggdrasil service" or "Activated the Remote Host Configuration daemon"
6063
and "Successfully connected to Red Hat!".
61-
For JSON output, no specific assertions are made due to a known issue (CCT-1191).
62-
"""
64+
For JSON output, comprehensive validation is performed on the response structure and values.
65+
"""
6366

6467
# rhc+satellite does not support basic auth for now
6568
# refer: https://issues.redhat.com/browse/RHEL-53436
6669
if "satellite" in test_config.environment and auth == "basic":
6770
pytest.skip("rhc+satellite only support activation key registration now")
6871
with contextlib.suppress(Exception):
6972
rhc.disconnect()
70-
command_args = prepare_args_for_connect(test_config, auth=auth, output_format=output_format)
73+
command_args = prepare_args_for_connect(
74+
test_config, auth=auth, output_format=output_format
75+
)
7176
command = ["connect"] + command_args
7277
result = rhc.run(*command)
7378
assert rhc.is_registered
7479
assert yggdrasil_service_is_active()
7580

7681
if output_format is None:
82+
# Verify connection messages
7783
assert "Connected to Red Hat Subscription Management" in result.stdout
7884
assert "Connected to Red Hat Insights" in result.stdout
79-
elif output_format == "json":
80-
pass
81-
# TODO: parse result.stdout, when CCT-1191 is fixed. It is not possible now, because
82-
# "rhc connect --format json" prints JSON document to stderr (not stdout)
83-
# json_output = json.loads(result.stdout)
84-
# assert json_output["rhsm_connected"] is True
8585

86-
if pytest.service_name == "rhcd":
87-
if output_format is None:
88-
assert "Activated the Remote Host Configuration daemon" in result.stdout
89-
elif output_format == "json":
90-
pass
91-
# TODO: parse result.stdout, when CCT-1191 is fixed
92-
else:
93-
if output_format is None:
94-
assert "Activated the yggdrasil service" in result.stdout
95-
elif output_format == "json":
96-
pass
97-
# TODO: parse result.stdout, when CCT-1191 is fixed
86+
assert "Activated the yggdrasil service" in result.stdout
9887

99-
if output_format is None:
88+
# Verify final success message
10089
assert "Successfully connected to Red Hat!" in result.stdout
90+
10191
elif output_format == "json":
102-
pass
103-
# TODO: parse result.stdout, when CCT-1191 is fixed
92+
json_output = json.loads(result.stdout)
93+
94+
# Verify field types and values
95+
assert type(json_output["hostname"]) == str
96+
assert type(json_output["uid"]) == int
97+
assert (
98+
type(json_output["rhsm_connected"]) == bool
99+
and json_output["rhsm_connected"] is True
100+
)
101+
assert type(json_output["features"]) == dict
102+
103+
# Verify feature types and values
104+
features = json_output["features"]
105+
for feature_name in ["content", "analytics", "remote_management"]:
106+
for key in ["enabled", "successful"]:
107+
value = features[feature_name][key]
108+
assert (
109+
isinstance(value, bool) and value is True
110+
), f"{feature_name}.{key} should be True boolean, got {value!r}"
104111

105112

106113
@pytest.mark.parametrize(
107114
"credentials,return_code",
108115
[
109116
( # username: invalid, password: valid
110-
{
111-
"username": "non-existent-user",
112-
"password": "candlepin.password"
113-
},
117+
{"username": "non-existent-user", "password": "candlepin.password"},
114118
None,
115119
),
116120
( # username: valid, password: invalid
117-
{
118-
"username": "candlepin.username",
119-
"password": "xpto123"
120-
},
121+
{"username": "candlepin.username", "password": "xpto123"},
121122
None,
122123
),
123124
( # organization: invalid, activation-key: valid
@@ -128,10 +129,7 @@ def test_connect(external_candlepin, rhc, test_config, auth, output_format):
128129
None,
129130
),
130131
( # organization: valid, activation-key: invalid
131-
{
132-
"organization": "candlepin.org",
133-
"activation-key": "xpto123"
134-
},
132+
{"organization": "candlepin.org", "activation-key": "xpto123"},
135133
None,
136134
),
137135
( # invalid combination of parameters (username & activation-key)
@@ -187,9 +185,7 @@ def test_connect_wrong_parameters(
187185
# An attempt to bring system in disconnected state in case it is not.
188186
with contextlib.suppress(Exception):
189187
rhc.disconnect()
190-
command_args = prepare_args_for_connect(
191-
test_config, credentials=credentials
192-
)
188+
command_args = prepare_args_for_connect(test_config, credentials=credentials)
193189
command = ["connect"] + command_args
194190
result = rhc.run(*command, check=False)
195191
if return_code is not None:

0 commit comments

Comments
 (0)