1616
1717from __future__ import absolute_import , division , print_function
1818
19+ from cm_client .api_client import ApiClient
20+
1921__metaclass__ = type
22+
2023import os
2124import logging
2225import pytest
26+ import re
2327
2428from pathlib import Path
2529
30+ from cm_client .rest import ApiException
31+ from cm_client import (
32+ ClouderaManagerResourceApi ,
33+ ApiConfigList ,
34+ ApiConfig ,
35+ )
36+
37+
2638from ansible_collections .cloudera .cluster .plugins .modules import cm_kerberos
2739from ansible_collections .cloudera .cluster .tests .unit import (
2840 AnsibleExitJson ,
2941 AnsibleFailJson ,
42+ wait_for_command ,
3043)
3144
3245LOG = logging .getLogger (__name__ )
3346
3447
35- def test_pytest_enable_kerberos (module_args , conn , request ):
48+ @pytest .fixture (scope = "function" )
49+ def krb_disabled (cm_api_client , request ) -> None :
50+ """
51+ Disable any existing Kerberos setup on the target Cloudera on Premise deployment.
52+
53+ This fixture does not restore any prior configurations.
54+ """
55+
56+ cm_api = ClouderaManagerResourceApi (cm_api_client )
57+
58+ cm_api .delete_credentials_command ()
59+
60+ reset_params = dict (
61+ krb_enc_types = "aes256-cts" ,
62+ security_realm = "HADOOP.COM" ,
63+ kdc_type = "MIT KDC" ,
64+ kdc_admin_host = "" ,
65+ kdc_host = "" ,
66+ krb_auth_enable = False ,
67+ ad_account_prefix = "" ,
68+ ad_kdc_domain = "ou=hadoop,DC=hadoop,DC=com" ,
69+ ad_delete_on_regenerate = False ,
70+ ad_set_encryption_types = False ,
71+ kdc_account_creation_host_override = "" ,
72+ gen_keytab_script = "" ,
73+ )
74+
75+ body = ApiConfigList (
76+ items = [ApiConfig (name = k , value = v ) for k , v in reset_params .items ()]
77+ )
78+
79+ cm_api .update_config (
80+ message = f"{ Path (request .node .parent .name ).stem } ::{ request .node .name } ::cleared" ,
81+ body = body ,
82+ )
83+
84+
85+ # TODO Should parameterize with a marker
86+ @pytest .fixture (scope = "function" )
87+ def krb_freeipa (cm_api_client , request , krb_disabled ) -> None :
88+ """
89+ Reset any existing Kerberos setup on the target Cloudera on Premise deployment.
90+
91+ This fixture does not restore any prior configurations.
92+ """
93+
94+ cm_api = ClouderaManagerResourceApi (cm_api_client )
95+
96+ setup_params = dict (
97+ krb_enc_types = "aes256-cts aes128-cts rc4-hmac" ,
98+ security_realm = "HADOOP.COM" ,
99+ kdc_type = "Red Hat IPA" ,
100+ kdc_admin_host = os .getenv ("KDC_HOST" ),
101+ kdc_host = os .getenv ("KDC_HOST" ),
102+ )
103+
104+ body = ApiConfigList (
105+ items = [ApiConfig (name = k , value = v ) for k , v in setup_params .items ()]
106+ )
107+
108+ cm_api .update_config (
109+ message = f"{ Path (request .node .parent .name ).stem } ::{ request .node .name } ::enabled" ,
110+ body = body ,
111+ )
112+
113+ cmd = cm_api .import_admin_credentials (
114+ username = os .getenv ("KDC_ADMIN_USER" ),
115+ password = os .getenv ("KDC_ADMIN_PASSWORD" ),
116+ )
117+
118+ try :
119+ wait_for_command (
120+ api_client = cm_api_client ,
121+ command = cmd ,
122+ )
123+ except Exception as e :
124+ if re .search ("user with name" , str (e )):
125+ LOG .info ("Reusing existing KDC user for Cloudera Manager" )
126+ else :
127+ raise e
128+
129+
130+ def test_pytest_enable_kerberos (module_args , conn , krb_disabled , request ):
36131
37132 if os .getenv ("KDC_ADMIN_USER" , None ):
38133 conn .update (kdc_admin_user = os .getenv ("KDC_ADMIN_USER" ))
@@ -60,8 +155,14 @@ def test_pytest_enable_kerberos(module_args, conn, request):
60155
61156 assert e .value .changed == True
62157
158+ # Idempotency
159+ with pytest .raises (AnsibleExitJson ) as e :
160+ cm_kerberos .main ()
63161
64- def test_enable_invalid_admin_password (module_args , conn , request ):
162+ assert e .value .changed == False
163+
164+
165+ def test_enable_invalid_admin_password (module_args , conn , krb_disabled , request ):
65166
66167 if os .getenv ("KDC_ADMIN_USER" , None ):
67168 conn .update (kdc_admin_user = os .getenv ("KDC_ADMIN_USER" ))
@@ -85,22 +186,27 @@ def test_enable_invalid_admin_password(module_args, conn, request):
85186 with pytest .raises (
86187 AnsibleFailJson ,
87188 match = "Error during Import KDC Account Manager Credentials command" ,
88- ) as e :
189+ ):
89190 cm_kerberos .main ()
90- print ("At end" )
91191
92192
93- def test_pytest_disable_kerberos (module_args , conn ):
193+ def test_pytest_disable_kerberos (module_args , conn , krb_freeipa ):
94194
95195 module_args ({** conn , "state" : "absent" })
96196
97197 with pytest .raises (AnsibleExitJson ) as e :
98198 cm_kerberos .main ()
99199
100- # assert e.value.changed == True
200+ assert e .value .changed == True
101201
202+ # Idempotency
203+ with pytest .raises (AnsibleExitJson ) as e :
204+ cm_kerberos .main ()
205+
206+ assert e .value .changed == False
102207
103- def test_force_enable_kerberos (module_args , conn , request ):
208+
209+ def test_force_enable_kerberos (module_args , conn , krb_freeipa , request ):
104210
105211 if os .getenv ("KDC_ADMIN_USER" , None ):
106212 conn .update (kdc_admin_user = os .getenv ("KDC_ADMIN_USER" ))
@@ -112,20 +218,6 @@ def test_force_enable_kerberos(module_args, conn, request):
112218 conn .update (kdc_admin_host = os .getenv ("KDC_HOST" ))
113219 conn .update (kdc_host = os .getenv ("KDC_HOST" ))
114220
115- # Ensure Kerberos is enabled
116- module_args (
117- {
118- ** conn ,
119- "kdc_type" : "Red Hat IPA" ,
120- "krb_enc_types" : ["aes256-cts" , "aes128-cts" , "rc4-hmac" ],
121- "security_realm" : "CLDR.INTERNAL" ,
122- }
123- )
124-
125- with pytest .raises (AnsibleExitJson ) as e :
126- cm_kerberos .main ()
127-
128- # Add force to module call
129221 module_args (
130222 {
131223 ** conn ,
@@ -136,5 +228,8 @@ def test_force_enable_kerberos(module_args, conn, request):
136228 "message" : f"{ Path (request .node .parent .name ).stem } ::{ request .node .name } " ,
137229 }
138230 )
231+
139232 with pytest .raises (AnsibleExitJson ) as e :
140233 cm_kerberos .main ()
234+
235+ assert e .value .changed == True
0 commit comments