Skip to content

Commit 6039b0b

Browse files
wmudgejimright
authored andcommitted
Add fixtures for Kerberos enable/disable for test preparation (#1)
Add idempotence to tests Signed-off-by: Webster Mudge <wmudge@cloudera.com>
1 parent 73830fa commit 6039b0b

File tree

3 files changed

+133
-40
lines changed

3 files changed

+133
-40
lines changed

plugins/modules/cm_kerberos.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18-
from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import (
19-
ClouderaManagerModule,
20-
ClouderaManagerMutableModule,
21-
resolve_parameter_updates,
22-
)
23-
from cm_client.rest import ApiException
24-
from cm_client import (
25-
ClouderaManagerResourceApi,
26-
ApiConfigList,
27-
ApiConfig,
28-
)
29-
import re
30-
3118
DOCUMENTATION = r"""
3219
module: cm_kerberos
3320
short_description: Manage and configure Kerberos Authentication for CDP
@@ -154,7 +141,6 @@
154141
"""
155142

156143
EXAMPLES = r"""
157-
---
158144
- name: Enable Kerberos
159145
cloudera.cluster.cm_kerberos:
160146
host: example.cloudera.com
@@ -178,7 +164,6 @@
178164
"""
179165

180166
RETURN = r"""
181-
---
182167
cm_config:
183168
description:
184169
- Cloudera Manager Server configurations with Kerberos settings where available.
@@ -250,6 +235,19 @@
250235
returned: when supported
251236
"""
252237

238+
import re
239+
240+
from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import (
241+
ClouderaManagerMutableModule,
242+
resolve_parameter_updates,
243+
)
244+
245+
from cm_client import (
246+
ClouderaManagerResourceApi,
247+
ApiConfigList,
248+
ApiConfig,
249+
)
250+
253251

254252
class ClouderaManagerKerberos(ClouderaManagerMutableModule):
255253
def __init__(self, module):

tests/unit/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import sys
2727
import yaml
2828

29-
from collections.abc import Generator
29+
from collections.abc import Generator, Callable
3030
from pathlib import Path
3131
from time import sleep
3232

@@ -128,7 +128,7 @@ def fail_json(*args, **kwargs):
128128

129129

130130
@pytest.fixture
131-
def module_args():
131+
def module_args() -> Callable[[dict], None]:
132132
"""Prepare module arguments"""
133133

134134
def prep_args(args=dict()):
@@ -139,7 +139,7 @@ def prep_args(args=dict()):
139139

140140

141141
@pytest.fixture
142-
def yaml_args():
142+
def yaml_args() -> Callable[[dict], None]:
143143
"""Prepare module arguments from YAML"""
144144

145145
def prep_args(args: str = ""):
@@ -150,7 +150,7 @@ def prep_args(args: str = ""):
150150

151151

152152
@pytest.fixture(scope="session")
153-
def conn():
153+
def conn() -> dict:
154154
conn = dict(username=os.getenv("CM_USERNAME"), password=os.getenv("CM_PASSWORD"))
155155

156156
if os.getenv("CM_HOST", None):

tests/unit/plugins/modules/cm_kerberos/test_cm_kerberos.py

Lines changed: 116 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,118 @@
1616

1717
from __future__ import absolute_import, division, print_function
1818

19+
from cm_client.api_client import ApiClient
20+
1921
__metaclass__ = type
22+
2023
import os
2124
import logging
2225
import pytest
26+
import re
2327

2428
from 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+
2638
from ansible_collections.cloudera.cluster.plugins.modules import cm_kerberos
2739
from ansible_collections.cloudera.cluster.tests.unit import (
2840
AnsibleExitJson,
2941
AnsibleFailJson,
42+
wait_for_command,
3043
)
3144

3245
LOG = 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

Comments
 (0)