def test_inheritance(): load_config_file("tests/data/event_config/1.conf") configs = getEventConfigs() assert sorted(list(configs)) == sorted([ 'gui_startup', 'gui_startup{cache_ready}', 'gui_startup{installation_pending}', 'gui_startup{user_logged_in}', 'maintenance', 'net_connection', 'on_demand', 'on_demand{user_logged_in}', 'on_shutdown', 'on_shutdown{installation_pending}', 'opsiclientd_start', 'opsiclientd_start{cache_ready}', 'silent_install', 'software_on_demand', 'sync_completed', 'sync_completed{cache_ready_user_logged_in}', 'sync_completed{cache_ready}', 'timer', 'timer_silentinstall', 'user_login' ]) assert configs["on_demand"]["shutdownWarningTime"] == 3600 assert configs["on_demand{user_logged_in}"]["shutdownWarningTime"] == 36000 Config().set(section="event_default", option="shutdown_warning_time", value=12345) reconfigureEventGenerators() configs = getEventConfigs() assert configs["on_demand"]["shutdownWarningTime"] == 12345 assert configs["on_demand{user_logged_in}"]["shutdownWarningTime"] == 36000 assert configs["gui_startup"]["shutdownWarningTime"] == 12345 assert configs["gui_startup{cache_ready}"]["shutdownWarningTime"] == 12345 assert configs["gui_startup{installation_pending}"][ "shutdownWarningTime"] == 12345
def testGettingEventConfiguration(): """ Testing if event configuration can be read from an config file. No check if the data is correct. """ configs = getEventConfigs() assert configs, 'no event configurations read'
def reconfigureEventGenerators(): eventConfigs = getEventConfigs() for eventGenerator in _EVENT_GENERATORS.values(): eventGenerator.setEventConfigs([]) for (eventConfigId, eventConfig) in eventConfigs.items(): mainEventConfigId = eventConfigId.split('{')[0] try: eventGenerator = _EVENT_GENERATORS[mainEventConfigId] except KeyError: logger.info( "Cannot reconfigure event generator for event '%s': not found", eventConfigId) continue try: eventType = eventConfig['type'] del eventConfig['type'] ec = EventConfigFactory(eventType, eventConfigId, **eventConfig) eventGenerator.addEventConfig(ec) logger.notice("Event config '%s' added to event generator '%s'", eventConfigId, mainEventConfigId) except Exception as err: # pylint: disable=broad-except logger.error("Failed to reconfigure event generator '%s': %s", mainEventConfigId, err)
def testNotificationServer(): configs = getEventConfigs() eventConfig = EventConfig(configs["on_demand"]) evt = Event(eventConfig=eventConfig, eventInfo={}) ept = EventProcessingThread(opsiclientd=None, event=evt) ept.startNotificationServer() ept._messageSubject.setMessage("pytest") # pylint: disable=protected-access choiceSubject = ChoiceSubject(id = 'choice') choiceSubject.setChoices(["abort", "start"]) choiceSubject.pyTestDone = False def abortActionCallback(_choiceSubject): pass def startActionCallback(_choiceSubject): _choiceSubject.pyTestDone = True choiceSubject.setCallbacks([abortActionCallback, startActionCallback]) ept._notificationServer.addSubject(choiceSubject) # pylint: disable=protected-access sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(("127.0.0.1", ept.notificationServerPort)) try: data = json.loads(sock.recv(10 * 1024)) ids = [] for subject in data.get("params")[0]: ids.append(subject["id"]) assert "choice" in ids, "subject id choice not received" rpc1 = { "id": 1, "method": "setSelectedIndexes", "params": ["choice", 1] } rpc2 = { "id": 2, "method": "selectChoice", "params": ["choice"] } sock.send( (json.dumps(rpc1) + "\r\n" + json.dumps(rpc2) + "\r\n").encode("utf-8") ) time.sleep(1) assert choiceSubject.pyTestDone is True, "selectChoice did not set pyTestDone on choiceSubject" finally: sock.close() ept.stopNotificationServer()
def test_gui_startup_event_on_windows_only(): createEventGenerators(None) configs = getEventConfigs() assert configs assert 'gui_startup' in configs
def test_fire_event(): ocd = Opsiclientd() createEventGenerators(ocd) getEventConfigs() controlServer = ControlServer.OpsiclientdRpcInterface(ocd) controlServer.fireEvent('on_demand')
def createEventGenerators(opsiclientd): # pylint: disable=too-many-branches,too-many-statements enabled_events = {} panicEventConfig = PanicEventConfig(EVENT_CONFIG_TYPE_PANIC, actionProcessorCommand=config.get( 'action_processor', 'command', raw=True)) _EVENT_GENERATORS[EVENT_CONFIG_TYPE_PANIC] = EventGeneratorFactory( opsiclientd, panicEventConfig) enabled_events[EVENT_CONFIG_TYPE_PANIC] = True event_configs = getEventConfigs() # Create event generators for events without preconditions for (eventConfigId, eventConfig) in copy.deepcopy(event_configs).items(): mainEventConfigId = eventConfigId.split('{')[0] if mainEventConfigId != eventConfigId: continue enabled_events[eventConfigId] = False if eventConfig['type'] in config.disabledEventTypes: logger.info("Event '%s' of type '%s' is temporary disabled (main)", eventConfigId, eventConfig['type']) continue if not eventConfig['active']: logger.info("Event '%s' of type '%s' is disabled (main)", eventConfigId, eventConfig['type']) continue try: eventType = eventConfig['type'] del eventConfig['type'] ec = EventConfigFactory(eventType, eventConfigId, **eventConfig) _EVENT_GENERATORS[eventConfigId] = EventGeneratorFactory( opsiclientd, ec) logger.info("Event generator '%s' created", eventConfigId) enabled_events[eventConfigId] = True except Exception as err: # pylint: disable=broad-except logger.error("Failed to create event generator '%s': %s", mainEventConfigId, err) # Create event generators for events with preconditions for (eventConfigId, eventConfig) in copy.deepcopy(event_configs).items(): mainEventConfigId = eventConfigId.split('{')[0] if not mainEventConfigId in enabled_events: enabled_events[mainEventConfigId] = False if not eventConfigId in enabled_events: enabled_events[eventConfigId] = False if eventConfig['type'] in config.disabledEventTypes: logger.info( "Event '%s' of type '%s' is temporary disabled (precondition)", eventConfigId, eventConfig['type']) continue if not eventConfig['active']: logger.info("Event '%s' of type '%s' is disabled (precondition)", eventConfigId, eventConfig['type']) continue eventType = eventConfig['type'] del eventConfig['type'] ec = EventConfigFactory(eventType, eventConfigId, **eventConfig) if mainEventConfigId not in _EVENT_GENERATORS: try: _EVENT_GENERATORS[mainEventConfigId] = EventGeneratorFactory( opsiclientd, ec) logger.info("Event generator '%s' created", mainEventConfigId) except Exception as err: # pylint: disable=broad-except logger.error("Failed to create event generator '%s': %s", mainEventConfigId, err) try: _EVENT_GENERATORS[mainEventConfigId].addEventConfig(ec) logger.info("Event config '%s' added to event generator '%s'", eventConfigId, mainEventConfigId) enabled_events[eventConfigId] = True except Exception as err: # pylint: disable=broad-except logger.error( "Failed to add event config '%s' to event generator '%s': %s", eventConfigId, mainEventConfigId, err) logger.notice("Configured events: %s", ", ".join(sorted(list(enabled_events)))) logger.notice("Enabled events: %s", ", ".join( sorted([evt_id for evt_id in enabled_events if enabled_events[evt_id]]))) # pylint: disable=consider-using-dict-items