Skip to content

Commit 94ae334

Browse files
committed
Add cms_session fixture and update zk_auto fixture to use cluster hosts
1 parent feafec2 commit 94ae334

File tree

1 file changed

+76
-8
lines changed

1 file changed

+76
-8
lines changed

tests/unit/conftest.py

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,83 @@ def cm_api_client(conn) -> ApiClient:
204204

205205

206206
@pytest.fixture(scope="session")
207-
def base_cluster(cm_api_client, request) -> Generator[ApiCluster]:
207+
def cms_session(cm_api_client) -> Generator[ApiService]:
208+
"""
209+
Provisions the Cloudera Manager Service. If the Cloudera Manager Service
210+
is present, will read and yield this reference. Otherwise, will
211+
yield a new Cloudera Manager Service, deleting it after use.
212+
213+
If it does create a new Cloudera Manager Service, it will do so on the
214+
first available host and will auto-configure the following roles:
215+
- HOSTMONITOR
216+
- SERVICEMONITOR
217+
- EVENTSERVER
218+
- ALERTPUBLISHER
219+
220+
It starts this Cloudera Manager Service, yields, and will remove this
221+
service if it has created it.
222+
223+
Args:
224+
cm_api_client (ApiClient): CM API client
225+
226+
Yields:
227+
Generator[ApiService]: Cloudera Manager Service
228+
"""
229+
230+
cms_api = MgmtServiceResourceApi(cm_api_client)
231+
232+
try:
233+
# Return if the Cloudera Manager Service is already present
234+
yield cms_api.read_service()
235+
236+
# Do nothing on teardown
237+
return
238+
except ApiException as ae:
239+
if ae.status != 404 or "Cannot find management service." not in str(ae.body):
240+
raise Exception(str(ae))
241+
242+
service_api = MgmtServiceResourceApi(cm_api_client)
243+
host_api = HostsResourceApi(cm_api_client)
244+
245+
host = next((h for h in host_api.read_hosts().items if not h.cluster_ref), None)
246+
247+
if host is None:
248+
raise Exception("No available hosts to assign Cloudera Manager Service roles")
249+
250+
name = "pytest-" + "".join(random.choices(string.ascii_lowercase, k=6))
251+
252+
service_api.setup_cms(
253+
body=ApiService(
254+
name=name,
255+
type="MGMT",
256+
roles=[
257+
ApiRole(type="HOSTMONITOR"),
258+
ApiRole(type="SERVICEMONITOR"),
259+
ApiRole(type="EVENTSERVER"),
260+
ApiRole(type="ALERTPUBLISHER"),
261+
],
262+
)
263+
)
264+
service_api.auto_configure()
265+
266+
monitor_command(cm_api_client, service_api.start_command())
267+
268+
# Return the newly-minted CMS
269+
yield service_api.read_service()
270+
271+
# Delete the created CMS
272+
cms_api.delete_cms()
273+
274+
275+
@pytest.fixture(scope="session")
276+
def base_cluster(cm_api_client, cms_session) -> Generator[ApiCluster]:
208277
"""Provision a Cloudera on premise base cluster for the session.
209278
If the variable 'CM_CLUSTER' is present, will attempt to read and yield
210279
a reference to this cluster. Otherwise, will yield a new base cluster
211280
with a single host, deleting the cluster once completed.
212281
213282
Args:
214283
cm_api_client (ApiClient): CM API client
215-
request (FixtureRequest): Fixture request
216284
217285
Raises:
218286
Exception: _description_
@@ -236,7 +304,7 @@ def base_cluster(cm_api_client, request) -> Generator[ApiCluster]:
236304
)
237305

238306
name = (
239-
Path(request.fixturename).stem
307+
cms_session.name
240308
+ "_"
241309
+ "".join(random.choices(string.ascii_lowercase, k=6))
242310
)
@@ -314,19 +382,19 @@ def zk_auto(cm_api_client, base_cluster, request) -> Generator[ApiService]:
314382
service_api = ServicesResourceApi(cm_api_client)
315383
host_api = HostsResourceApi(cm_api_client)
316384

317-
host = next((h for h in host_api.read_hosts().items if not h.cluster_ref), None)
385+
host = next((h for h in host_api.read_hosts().items if h.cluster_ref), None)
318386

319387
if host is None:
320388
raise NoHostsFoundException(
321-
"No available hosts to assign Cloudera Manager Service roles"
389+
"No available hosts to assign ZooKeeper service roles"
322390
)
323391

324392
payload = ApiService(
325-
name=f"zk-{Path(request.node.parent.name).stem}",
393+
name="-".join(["zk", request.node.name]),
326394
type="ZOOKEEPER",
327395
roles=[
328396
ApiRole(
329-
type="ZOOKEEPER",
397+
type="SERVER",
330398
host_ref=ApiHostRef(host.host_id, host.hostname),
331399
),
332400
],
@@ -779,7 +847,7 @@ def handle_commands(api_client: ApiClient, commands: ApiBulkCommandList):
779847

780848

781849
def monitor_command(
782-
api_client: ApiClient, command: ApiCommand, polling: int = 10, delay: int = 15
850+
api_client: ApiClient, command: ApiCommand, polling: int = 120, delay: int = 10
783851
):
784852
poll_count = 0
785853
while command.active:

0 commit comments

Comments
 (0)