11import json
22import logging
3+ import time
34from dotenv import load_dotenv
45import os
56import pytest
2122
2223# Initialize the global variables.
2324environment_config = {}
25+ kafka_cluster_id = ""
26+ principal_id = ""
2427
2528
2629@pytest .fixture (autouse = True )
@@ -34,6 +37,62 @@ def load_configurations():
3437 environment_config [ENVIRONMENT_CONFIG ["confluent_cloud_api_secret" ]] = os .getenv ("CONFLUENT_CLOUD_API_SECRET" )
3538 environment_config [ENVIRONMENT_CONFIG ["environment_id" ]] = os .getenv ("ENVIRONMENT_ID" )
3639
40+ global kafka_cluster_id
41+ global principal_id
42+
43+ # Set the Kafka cluster ID and owner ID.
44+ kafka_cluster_id = os .getenv ("KAFKA_CLUSTER_ID" )
45+ principal_id = os .getenv ("PRINCIPAL_ID" )
46+
47+ def test_create_kafka_api_key ():
48+ """Test the create_kafka_api_key() function."""
49+
50+ # Instantiate the EnvironmentClient class.
51+ environment_client = EnvironmentClient (environment_config = environment_config )
52+
53+ http_status_code , error_message , api_key_pair = environment_client .create_kafka_api_key (kafka_cluster_id = kafka_cluster_id , principal_id = principal_id )
54+
55+ try :
56+ assert http_status_code == HttpStatus .ACCEPTED , f"HTTP Status Code: { http_status_code } "
57+
58+ beautified = json .dumps (api_key_pair , indent = 4 , sort_keys = True )
59+ logger .info (f"Kafka API Key Pair: { beautified } " )
60+ except AssertionError as e :
61+ logger .error (e )
62+ logger .error ("HTTP Status Code: %d, Error Message: %s, Kafka API Key Pair: %s" , http_status_code , error_message , api_key_pair )
63+ return
64+
65+ def test_delete_kafka_api_key ():
66+ """Test the delete_kafka_api_key() function."""
67+
68+ # Instantiate the EnvironmentClient class.
69+ environment_client = EnvironmentClient (environment_config = environment_config )
70+
71+ http_status_code , error_message , api_key_pair = environment_client .create_kafka_api_key (kafka_cluster_id = kafka_cluster_id , principal_id = principal_id )
72+
73+ try :
74+ assert http_status_code == HttpStatus .ACCEPTED , f"HTTP Status Code: { http_status_code } "
75+
76+ beautified = json .dumps (api_key_pair , indent = 4 , sort_keys = True )
77+ logger .info (f"Kafka API Key Pair: { beautified } " )
78+ except AssertionError as e :
79+ logger .error (e )
80+ logger .error ("HTTP Status Code: %d, Error Message: %s, Kafka API Key Pair: %s" , http_status_code , error_message , api_key_pair )
81+ return
82+
83+ time .sleep (10 ) # Wait for 10 seconds before deleting the API key.
84+
85+ http_status_code , error_message = environment_client .delete_kafka_api_key (api_key = api_key_pair ["key" ])
86+
87+ try :
88+ assert http_status_code == HttpStatus .NO_CONTENT , f"HTTP Status Code: { http_status_code } "
89+
90+ logger .info (f"Successfully deleted Kafka API Key: { api_key_pair ['key' ]} " )
91+ except AssertionError as e :
92+ logger .error (e )
93+ logger .error ("HTTP Status Code: %d, Error Message: %s" , http_status_code , error_message )
94+ return
95+
3796def test_get_environment_list ():
3897 """Test the get_environment_list() function."""
3998
0 commit comments