def test_detector_update(sfx_recorder, session): api_token = 'foo' label = 'foo' def program(threshold): return Program( Detect(GT(Data('cpu.utilization'), threshold)).publish(label=label)) rule = Rule()\ .for_label(label)\ .with_severity(Severity.Major)\ .with_notifications(EmailNotification('*****@*****.**')) # Assert that we can actually update values within a detector with sfx_recorder.use_cassette('detector_update_success', serialize_with='prettyjson'): def detector(threshold): return Detector(session=session)\ .with_name('test_update')\ .with_program(program(threshold))\ .with_rules(rule) create_response = detector(90).with_api_token(api_token).create() import time time.sleep(3) update_response = detector(99).with_api_token(api_token).update() # These assertions should fail assert '90' in create_response['programText'] assert '99' in update_response['programText'] assert create_response['id'] == update_response['id']
def fun(name, threshold): program = Program( Detect(GT(Data("cpu.utilization"), threshold)).publish(label=name)) rule = (Rule().for_label(name).with_severity( Severity.Info).with_notifications( EmailNotification("*****@*****.**"))) return (Detector(session=session).with_name(name).with_program( program).with_rules(rule))
def test_detector_with_rules(): rule = Rule().with_notifications(EmailNotification('*****@*****.**')) d = Detector().with_rules(rule) assert d.options['rules'] == [rule.options]
def test_rule_stringy_things(name): expected = 'foo' rule = Rule() fn = mk_rule_fn(rule, name) assert fn('foo').options[name] == expected
def test_rule_with_notifiations_multi(): expected = [EmailNotification('*****@*****.**'), TeamNotification('lol')] rule = Rule().with_notifications(*expected) for n in rule.options['notifications']: assert n in map(lambda x: x.options, expected)
def test_rule_with_notifications_single(): expected = EmailNotification('*****@*****.**') rule = Rule().with_notifications(expected) assert rule.options['notifications'] == [expected.options]
def test_rule_is_disabled(): assert Rule().is_disabled(disabled=True).options['disabled'] is True
def test_rule_is_disabled_default(): assert Rule().is_disabled().options['disabled'] is False
def test_rule_with_severity(): expected = Severity.Critical rule = Rule().with_severity(expected) assert rule.options['severity'] == expected.value
def test_rule_with_description(): expected = 'foo' rule = Rule().with_description(expected) assert rule.options['description'] == expected
def test_rule_invalid(method): with pytest.raises(ValueError): rule = Rule() fn = getattr(rule, method) fn(None)
def test_rule_for_label(): expected = 'foo' rule = Rule().for_label(expected) assert rule.options['detectLabel'] == expected
def test_rule_init(): assert Rule().options == {}
""" Example 1: from scratch This is useful when you want to monitor something that isn't tied to an existing chart or dashboard. """ alert_label = 'CPU is too low for 75% of the last 2 minutes!' filters = And(Filter('app', 'my-app'), Filter('env', 'test')) data = Data('cpu.utilization', filter=filters).publish(label='A') cpu_too_low = Detect(When(LT(data, 50), '2m', 0.75)).publish(alert_label) program = Program(cpu_too_low) info_rule = Rule()\ .for_label(alert_label)\ .with_severity(Severity.Info)\ .with_notifications(EmailNotification('*****@*****.**')) detector = Detector()\ .with_name('TEST: example detector')\ .with_program(program)\ .with_rules(info_rule) """ Example 2: from an existing chart This is useful when you want to alert on behavior seen in a chart that was created using the signal_analog library. """ test_program = Program(data)