7
7
"""
8
8
9
9
import contextlib
10
+ import json
10
11
import time
11
12
import logging
12
13
import pytest
19
20
check_yggdrasil_journalctl_logs ,
20
21
)
21
22
23
+
22
24
logger = logging .getLogger (__name__ )
23
25
26
+
24
27
@pytest .mark .tier1
25
28
@pytest .mark .parametrize (
26
29
"auth, output_format" ,
29
32
("basic" , "json" ),
30
33
("activation-key" , None ),
31
34
("activation-key" , "json" ),
32
- ]
35
+ ],
33
36
)
34
37
def test_connect (external_candlepin , rhc , test_config , auth , output_format ):
35
38
"""
@@ -58,66 +61,64 @@ def test_connect(external_candlepin, rhc, test_config, auth, output_format):
58
61
"Connected to Red Hat Subscription Management",
59
62
"Activated the yggdrasil service" or "Activated the Remote Host Configuration daemon"
60
63
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
+ """
63
66
64
67
# rhc+satellite does not support basic auth for now
65
68
# refer: https://issues.redhat.com/browse/RHEL-53436
66
69
if "satellite" in test_config .environment and auth == "basic" :
67
70
pytest .skip ("rhc+satellite only support activation key registration now" )
68
71
with contextlib .suppress (Exception ):
69
72
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
+ )
71
76
command = ["connect" ] + command_args
72
77
result = rhc .run (* command )
73
78
assert rhc .is_registered
74
79
assert yggdrasil_service_is_active ()
75
80
76
81
if output_format is None :
82
+ # Verify connection messages
77
83
assert "Connected to Red Hat Subscription Management" in result .stdout
78
84
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
85
85
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
98
87
99
- if output_format is None :
88
+ # Verify final success message
100
89
assert "Successfully connected to Red Hat!" in result .stdout
90
+
101
91
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} "
104
111
105
112
106
113
@pytest .mark .parametrize (
107
114
"credentials,return_code" ,
108
115
[
109
116
( # username: invalid, password: valid
110
- {
111
- "username" : "non-existent-user" ,
112
- "password" : "candlepin.password"
113
- },
117
+ {"username" : "non-existent-user" , "password" : "candlepin.password" },
114
118
None ,
115
119
),
116
120
( # username: valid, password: invalid
117
- {
118
- "username" : "candlepin.username" ,
119
- "password" : "xpto123"
120
- },
121
+ {"username" : "candlepin.username" , "password" : "xpto123" },
121
122
None ,
122
123
),
123
124
( # organization: invalid, activation-key: valid
@@ -128,10 +129,7 @@ def test_connect(external_candlepin, rhc, test_config, auth, output_format):
128
129
None ,
129
130
),
130
131
( # organization: valid, activation-key: invalid
131
- {
132
- "organization" : "candlepin.org" ,
133
- "activation-key" : "xpto123"
134
- },
132
+ {"organization" : "candlepin.org" , "activation-key" : "xpto123" },
135
133
None ,
136
134
),
137
135
( # invalid combination of parameters (username & activation-key)
@@ -187,9 +185,7 @@ def test_connect_wrong_parameters(
187
185
# An attempt to bring system in disconnected state in case it is not.
188
186
with contextlib .suppress (Exception ):
189
187
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 )
193
189
command = ["connect" ] + command_args
194
190
result = rhc .run (* command , check = False )
195
191
if return_code is not None :
0 commit comments