def test_create_not_owned_subscription(self): """ Test creating not_owned filter, destination and filter and determining if they are retained by the server after the subscription manager is closed. """ sm = "test_create_delete_subscription" server = WBEMServer(self.conn) listener_url = '%s:%s' % (self.conn.url, 50000) # Create a single not_owned subscription with WBEMSubscriptionManager(subscription_manager_id=sm) as sub_mgr: server_id = sub_mgr.add_server(server) dests = sub_mgr.add_listener_destinations(server_id, listener_url, owned=False) dest_paths = [dest.path for dest in dests] filter_path = self.add_filter(sub_mgr, server_id, 'NotUsed', owned=False) subscriptions = sub_mgr.add_subscriptions( server_id, filter_path, destination_paths=dest_paths, owned=False) subscription_paths = [inst.path for inst in subscriptions] self.confirm_created(sub_mgr, server_id, filter_path, subscription_paths, owned=False) assert self.get_object_count(sub_mgr, server_id) # Test that subscriptions instances are still in repo # self.conn.display_repository() assert self.get_objects_from_server() == 3 # Create a new submgr and and test for filters, etc. retrieved. with WBEMSubscriptionManager(subscription_manager_id=sm) as sub_mgr: server_id = sub_mgr.add_server(server) assert len(sub_mgr.get_all_destinations(server_id)) == 1 assert len(sub_mgr.get_all_filters(server_id)) == 1 assert len(sub_mgr.get_all_subscriptions(server_id)) == 1 assert self.get_object_count(sub_mgr, server_id) == 3 sub_paths = [sub.path for sub in sub_mgr.get_all_subscriptions(server_id)] sub_mgr.remove_subscriptions(server_id, sub_paths) dest_paths = [dest.path for dest in sub_mgr.get_all_destinations(server_id)] sub_mgr.remove_destinations( server_id, dest_paths) for filter_ in sub_mgr.get_all_filters(server_id): sub_mgr.remove_filter(server_id, filter_.path) assert self.get_object_count(sub_mgr, server_id) == 0
def test_create_owned_subscription(self): """ Test Basic Creation of sub mgr, creation of owned subscription and cleanup based on the WBEMSubscriptionManager context manager. """ sm = "test_create_delete_subscription" server = WBEMServer(self.conn) listener_url = '%s:%s' % (self.conn.url, 50000) with WBEMSubscriptionManager(subscription_manager_id=sm) as sub_mgr: server_id = sub_mgr.add_server(server) sub_mgr.add_listener_destinations(server_id, listener_url, owned=True) filter_path = self.add_filter(sub_mgr, server_id, 'NotUsed', owned=True) subscriptions = sub_mgr.add_subscriptions(server_id, filter_path, owned=True) subscription_paths = [inst.path for inst in subscriptions] # self.conn.display_repository() self.confirm_created(sub_mgr, server_id, filter_path, subscription_paths, owned=True) # confirm destination instance paths match assert sub_mgr.get_all_destinations(server_id) # TODO: ks Finish this test completely when we add other # changes for filter ids sub_mgr.remove_subscriptions(server_id, subscription_paths) sub_mgr.remove_filter(server_id, filter_path) self.confirm_removed(sub_mgr, server_id, filter_path, subscription_paths) assert self.get_object_count(sub_mgr, server_id) == 0 sub_mgr.remove_server(server_id) # confirm no filters, destinations, subscriptions in server assert self.get_objects_from_server() == 0
from pywbem import WBEMServer, WBEMSubscriptionManager def display_paths(instances, type_str): """Display the count and paths for the list of instances in instances.""" print('%ss: count=%s' % ( type_str, len(instances), )) for path in [instance.path for instance in instances]: print('%s: %s' % (type_str, path)) if len(instances): print('') MY_SERVER = WBEMServer(CONN) # noqa: F821 print('Interop namespace=%s' % MY_SERVER.interop_ns) with WBEMSubscriptionManager('fred') as sub_mgr: server_id = sub_mgr.add_server(MY_SERVER) display_paths(sub_mgr.get_all_filters(server_id), 'Filter') display_paths(sub_mgr.get_all_destinations(server_id), 'Destination') display_paths(sub_mgr.get_all_subscriptions(server_id), 'Subscription') print('Quit Scriplet. Quiting wbemcli') quit()
def run_test(svr_url, listener_host, user, password, http_listener_port, \ https_listener_port, requested_indications, repeat_loop): """ Runs a test that: 1. Creates a server 2. Creates a dynamic listener and starts ti 3. Creates a filter and subscription 4. Calls the server to execute a method that creates an indication 5. waits for indications to be received. 6. Removes the filter and subscription and stops the listener """ if os.path.exists(LOGFILE): os.remove(LOGFILE) try: conn = WBEMConnection(svr_url, (user, password), no_verification=True) server = WBEMServer(conn) # Create subscription_manager here to be sure we can communicate with # server before Creating listener, etc. sub_mgr = WBEMSubscriptionManager( subscription_manager_id='pegasusIndicationTest') # Add server to subscription manager server_id = sub_mgr.add_server(server) old_filters = sub_mgr.get_all_filters(server_id) old_subs = sub_mgr.get_all_subscriptions(server_id) # TODO filter for our sub mgr if len(old_subs) != 0 or len(old_filters) != 0: sub_mgr.remove_subscriptions(server_id, [inst.path for inst in old_subs]) for filter_ in old_filters: sub_mgr.remove_filter(server_id, filter_.path) except ConnectionError as ce: print('Connection Error %s with %s' % (ce, svr_url)) sys.exit(2) except Error as er: print('Error communicationg with WBEMServer %s' % er) sys.exit(1) # Create the listener and listener call back and start the listener #pylint: disable=global-statement global LISTENER ####stream=sys.stderr, logging.basicConfig(filename='pegasusindicationtest.log', level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s') # Create and start local listener LISTENER = WBEMListener(listener_host, http_port=http_listener_port, https_port=https_listener_port) # Start connect and start listener. LISTENER.add_callback(consume_indication) LISTENER.start() listener_url = '%s://%s:%s' % ('http', 'localhost', http_listener_port) sub_mgr.add_listener_destinations(server_id, listener_url) # Create a dynamic alert indication filter and subscribe for it filter_ = sub_mgr.add_filter(server_id, TEST_CLASS_NAMESPACE, TEST_QUERY, query_language="DMTF:CQL") subscriptions = sub_mgr.add_subscriptions(server_id, filter_.path) # Request server to create indications by invoking method # This is pegasus specific class_name = CIMClassName(TEST_CLASS, namespace=TEST_CLASS_NAMESPACE) while repeat_loop > 0: repeat_loop += -1 global RECEIVED_INDICATION_COUNT, INDICATION_START_TIME RECEIVED_INDICATION_COUNT = 0 INDICATION_START_TIME = None if send_request_for_indications(conn, class_name, requested_indications): # Wait for indications to be received. success = wait_for_indications(requested_indications) if not success: insts = conn.EnumerateInstances('PG_ListenerDestinationQueue', namespace='root/PG_Internal') for inst in insts: print('%s queueFullDropped %s, maxretry %s, InQueue %s' % \ (inst['ListenerDestinationName'], inst['QueueFullDroppedIndications'], inst['RetryAttemptsExceededIndications'], inst['CurrentIndications'])) if repeat_loop > 0: time.sleep(requested_indications / 150) sub_mgr.remove_subscriptions(server_id, [inst.path for inst in subscriptions]) sub_mgr.remove_filter(server_id, filter_.path) sub_mgr.remove_server(server_id) LISTENER.stop() # Test for all expected indications received. if RECEIVED_INDICATION_COUNT != requested_indications: print('Incorrect count of indications received expected=%s, received' '=%s' % (requested_indications, RECEIVED_INDICATION_COUNT)) sys.exit(1) else: print('Success, %s indications' % requested_indications) print('Max time between indications %s' % MAX_TIME_BETWEEN_INDICATIONS)
def run_test( svr_url, listener_host, user, password, http_listener_port, https_listener_port, requested_indications, repeat_loop ): """ Runs a test that: 1. Creates a server 2. Creates a dynamic listener and starts ti 3. Creates a filter and subscription 4. Calls the server to execute a method that creates an indication 5. waits for indications to be received. 6. Removes the filter and subscription and stops the listener """ if os.path.exists(LOGFILE): os.remove(LOGFILE) try: conn = WBEMConnection(svr_url, (user, password), no_verification=True) server = WBEMServer(conn) # Create subscription_manager here to be sure we can communicate with # server before Creating listener, etc. sub_mgr = WBEMSubscriptionManager(subscription_manager_id="pegasusIndicationTest") # Add server to subscription manager server_id = sub_mgr.add_server(server) old_filters = sub_mgr.get_all_filters(server_id) old_subs = sub_mgr.get_all_subscriptions(server_id) # TODO filter for our sub mgr if len(old_subs) != 0 or len(old_filters) != 0: sub_mgr.remove_subscriptions(server_id, [inst.path for inst in old_subs]) for filter_ in old_filters: sub_mgr.remove_filter(server_id, filter_.path) except ConnectionError as ce: print("Connection Error %s with %s" % (ce, svr_url)) sys.exit(2) except Error as er: print("Error communicationg with WBEMServer %s" % er) sys.exit(1) # Create the listener and listener call back and start the listener # pylint: disable=global-statement global LISTENER ####stream=sys.stderr, logging.basicConfig( filename="pegasusindicationtest.log", level=logging.INFO, format="%(asctime)s %(levelname)s: %(message)s" ) # Create and start local listener LISTENER = WBEMListener(listener_host, http_port=http_listener_port, https_port=https_listener_port) # Start connect and start listener. LISTENER.add_callback(consume_indication) LISTENER.start() listener_url = "%s://%s:%s" % ("http", "localhost", http_listener_port) sub_mgr.add_listener_destinations(server_id, listener_url) # Create a dynamic alert indication filter and subscribe for it filter_ = sub_mgr.add_filter(server_id, TEST_CLASS_NAMESPACE, TEST_QUERY, query_language="DMTF:CQL") subscriptions = sub_mgr.add_subscriptions(server_id, filter_.path) # Request server to create indications by invoking method # This is pegasus specific class_name = CIMClassName(TEST_CLASS, namespace=TEST_CLASS_NAMESPACE) while repeat_loop > 0: repeat_loop += -1 global RECEIVED_INDICATION_COUNT, INDICATION_START_TIME RECEIVED_INDICATION_COUNT = 0 INDICATION_START_TIME = None if send_request_for_indications(conn, class_name, requested_indications): # Wait for indications to be received. success = wait_for_indications(requested_indications) if not success: insts = conn.EnumerateInstances("PG_ListenerDestinationQueue", namespace="root/PG_Internal") for inst in insts: print( "%s queueFullDropped %s, maxretry %s, InQueue %s" % ( inst["ListenerDestinationName"], inst["QueueFullDroppedIndications"], inst["RetryAttemptsExceededIndications"], inst["CurrentIndications"], ) ) if repeat_loop > 0: time.sleep(requested_indications / 150) sub_mgr.remove_subscriptions(server_id, [inst.path for inst in subscriptions]) sub_mgr.remove_filter(server_id, filter_.path) sub_mgr.remove_server(server_id) LISTENER.stop() # Test for all expected indications received. if RECEIVED_INDICATION_COUNT != requested_indications: print( "Incorrect count of indications received expected=%s, received" "=%s" % (requested_indications, RECEIVED_INDICATION_COUNT) ) sys.exit(1) else: print("Success, %s indications" % requested_indications) print("Max time between indications %s" % MAX_TIME_BETWEEN_INDICATIONS)