19
19
CMD_RHSM_STATUS = ('subscription-manager' , 'status' )
20
20
CMD_RHSM_RELEASE = ('subscription-manager' , 'release' )
21
21
CMD_RHSM_LIST_ENABLED_REPOS = ('subscription-manager' , 'repos' , '--list-enabled' )
22
+ CMD_RHSM_IDENTITY = ('subscription-manager' , 'identity' , '--no-progress-messages' )
22
23
23
24
RHSM_STATUS_OUTPUT_NOSCA = '''
24
25
+-------------------------------------------+
@@ -79,11 +80,13 @@ def call(self, cmd, *args, **dummy_kwargs):
79
80
tuple (cmd ), # Cast to tuple, as list is not hashable
80
81
self .call_return )
81
82
82
- def add_mocked_command_call_with_stdout (self , cmd , stdout ):
83
+ def add_mocked_command_call (self , cmd , stdout = None , stderr = None , exit_code = 0 ):
83
84
# We cast `cmd` from list to tuple, as a list cannot be hashed
84
85
self .mocked_command_call_outputs [tuple (cmd )] = {
85
86
'stdout' : stdout ,
86
- 'stderr' : None }
87
+ 'stderr' : stderr ,
88
+ 'exit_code' : exit_code
89
+ }
87
90
88
91
def full_path (self , path ):
89
92
return path
@@ -203,7 +206,7 @@ def test_inhibit_on_duplicate_repos_no_dups(monkeypatch):
203
206
204
207
def test_sku_listing (monkeypatch , actor_mocked , context_mocked ):
205
208
"""Tests whether the rhsm library can obtain used SKUs correctly."""
206
- context_mocked .add_mocked_command_call_with_stdout (CMD_RHSM_LIST_CONSUMED , 'SKU: 598339696910' )
209
+ context_mocked .add_mocked_command_call (CMD_RHSM_LIST_CONSUMED , 'SKU: 598339696910' )
207
210
208
211
attached_skus = rhsm .get_attached_skus (context_mocked )
209
212
@@ -234,7 +237,7 @@ def test_scanrhsminfo_with_skip_rhsm(monkeypatch, context_mocked):
234
237
235
238
def test_get_release (monkeypatch , actor_mocked , context_mocked ):
236
239
"""Tests whether the library correctly retrieves release from RHSM."""
237
- context_mocked .add_mocked_command_call_with_stdout (CMD_RHSM_RELEASE , 'Release: 7.9' )
240
+ context_mocked .add_mocked_command_call (CMD_RHSM_RELEASE , 'Release: 7.9' )
238
241
239
242
release = rhsm .get_release (context_mocked )
240
243
@@ -245,7 +248,7 @@ def test_get_release(monkeypatch, actor_mocked, context_mocked):
245
248
def test_get_release_with_release_not_set (monkeypatch , actor_mocked , context_mocked ):
246
249
"""Tests whether the library does not retrieve release information when the release is not set."""
247
250
# Test whether no release is detected correctly too
248
- context_mocked .add_mocked_command_call_with_stdout (CMD_RHSM_RELEASE , 'Release not set' )
251
+ context_mocked .add_mocked_command_call (CMD_RHSM_RELEASE , 'Release not set' )
249
252
250
253
release = rhsm .get_release (context_mocked )
251
254
@@ -255,15 +258,15 @@ def test_get_release_with_release_not_set(monkeypatch, actor_mocked, context_moc
255
258
256
259
def test_is_manifest_sca_on_nonsca_system (monkeypatch , actor_mocked , context_mocked ):
257
260
"""Tests whether the library obtains the SCA information correctly from a non-SCA system."""
258
- context_mocked .add_mocked_command_call_with_stdout (CMD_RHSM_STATUS , RHSM_STATUS_OUTPUT_NOSCA )
261
+ context_mocked .add_mocked_command_call (CMD_RHSM_STATUS , RHSM_STATUS_OUTPUT_NOSCA )
259
262
260
263
is_sca = rhsm .is_manifest_sca (context_mocked )
261
264
assert not is_sca , 'SCA was detected on a non-SCA system.'
262
265
263
266
264
267
def test_is_manifest_sca_on_sca_system (monkeypatch , actor_mocked , context_mocked ):
265
268
"""Tests whether the library obtains the SCA information from SCA system correctly."""
266
- context_mocked .add_mocked_command_call_with_stdout (CMD_RHSM_STATUS , RHSM_STATUS_OUTPUT_SCA )
269
+ context_mocked .add_mocked_command_call (CMD_RHSM_STATUS , RHSM_STATUS_OUTPUT_SCA )
267
270
268
271
is_sca = rhsm .is_manifest_sca (context_mocked )
269
272
assert is_sca , 'Failed to detected SCA on a SCA system.'
@@ -286,7 +289,7 @@ def test_get_enabled_repo_ids(monkeypatch, actor_mocked, context_mocked):
286
289
rhsm_output_fragment += '\n '
287
290
rhsm_list_enabled_output += rhsm_output_fragment
288
291
289
- context_mocked .add_mocked_command_call_with_stdout (CMD_RHSM_LIST_ENABLED_REPOS , rhsm_list_enabled_output )
292
+ context_mocked .add_mocked_command_call (CMD_RHSM_LIST_ENABLED_REPOS , rhsm_list_enabled_output )
290
293
291
294
enabled_repo_ids = rhsm .get_enabled_repo_ids (context_mocked )
292
295
@@ -385,3 +388,24 @@ def mocked_listdir(path):
385
388
assert len (existing_product_certificates ) == 1 , fail_description
386
389
fail_description = 'Library failed to identify certificate from mocked outputs.'
387
390
assert existing_product_certificates [0 ] == '/etc/pki/product-default/cert' , fail_description
391
+
392
+
393
+ def test_is_registered_on_registered_system (context_mocked ):
394
+ """Tests whether the library obtains the registraton status correctly from a registered system."""
395
+ context_mocked .add_mocked_command_call (CMD_RHSM_IDENTITY , exit_code = 0 )
396
+ assert rhsm .is_rhsm_registered (context_mocked )
397
+
398
+
399
+ def test_is_registered_on_unregistered_system (context_mocked ):
400
+ """Tests whether the library obtains the registraton status correctly from an unregistered system."""
401
+ context_mocked .add_mocked_command_call (CMD_RHSM_IDENTITY , exit_code = 1 )
402
+ assert not rhsm .is_rhsm_registered (context_mocked )
403
+
404
+
405
+ def test_is_registered_error (context_mocked ):
406
+ """Tests whether the is_rhsm_registered function correctly handles command errors"""
407
+ context_mocked .add_mocked_command_call (CMD_RHSM_IDENTITY , exit_code = 2 )
408
+ with pytest .raises (StopActorExecutionError ) as err :
409
+ rhsm .is_rhsm_registered (context_mocked )
410
+
411
+ assert 'A subscription-manager command failed to execute' in str (err )
0 commit comments