Ejemplo n.º 1
0
    def __init__(self,
                 method,
                 url,
                 data=None,
                 headers=None,
                 priority=Priority.NORMAL,
                 timeout=None,
                 group=None,
                 retries=1,
                 ignore_ssl_cert_errors=False,
                 auth=None):
        """Creates an HttpValidation object that will make an HTTP request to
        the provided URL passing the provided headers.

        """
        Validation.__init__(self,
                            "{0} {1}".format(method, url),
                            priority=priority,
                            timeout=timeout,
                            group=group)

        self._url = url
        self._data = data
        self._method = method
        self._headers = copy.copy(headers) or {}
        self._response_code_expectation = _ExpectedStatusCodes(set([200]))
        self._expectations = []
        self._retries = retries
        self._ignore_ssl_cert_errors = ignore_ssl_cert_errors
        self._auth = auth or ()
        self._elapsed_time = -1
Ejemplo n.º 2
0
    def __init__(self,
                 rabbitmq_context,
                 name,
                 queue_name,
                 max_queue_size,
                 priority=Priority.NORMAL,
                 timeout=None,
                 num_attempts=4,
                 seconds_between_attempts=2,
                 group=None,
                 ignore_connection_failure=False):
        """Creates a RabbitMqValidation object."""
        Validation.__init__(
            self,
            ("queue '{0}' should have less than {1} messages in in " +
             "it on RabbitMQ host: '{2}' ({3})").format(
                 queue_name, max_queue_size, rabbitmq_context.host, name),
            priority=priority,
            timeout=timeout,
            group=group)

        self.rabbitmq_context = rabbitmq_context
        self.max_queue_size = max_queue_size
        self.queue_name = queue_name
        self.num_attempts = num_attempts
        self.seconds_between_attempts = seconds_between_attempts
        self.ignore_connection_failure = ignore_connection_failure
Ejemplo n.º 3
0
def test_get_wrong_enrichment_forced_namespace():
    pub_values = {1: 2, "a": "b"}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    page = pagerduty.PagerDutyPublisher("url", "token")
    valid.enrich(pub, pub_values, force_namespace=True)
    assert valid.get_enriched(page) == {}
Ejemplo n.º 4
0
def test_simple_enrichment_forced_namespace():
    values = {1: 2, "a": "b"}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    valid.enrich(pub, values, force_namespace=True)
    data = valid.get_enriched(pub)
    assert data == values
Ejemplo n.º 5
0
def test_generate_id_different_valid_different_id():
    pub = PagerDutyPublisher("url", "token")
    v = Validation("low", priority=Priority.CRITICAL)
    v2 = Validation("what", priority=Priority.CRITICAL)
    failure = Failure("bar", v, "unable to transmogrify")
    another = Failure("foo", v2, "to transmogrify")
    assert pub._generate_id(failure) != pub._generate_id(another)
Ejemplo n.º 6
0
def test_enrichment_fails_on_duplication_different_data():
    values = {1: 2, "a": "b"}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    valid.enrich(pub, values)
    with pytest.raises(EnrichmentFailure):
        valid.enrich(pub, {"different": "values"})
Ejemplo n.º 7
0
def test_simple_enrichment_forced_namespace():
    values = {1: 2, "a": "b"}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    valid.enrich(pub, values, force_namespace=True)
    data = valid.get_enriched(pub)
    assert data == values
Ejemplo n.º 8
0
def test_get_wrong_enrichment_forced_namespace():
    pub_values = {1: 2, "a": "b"}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    page = pagerduty.PagerDutyPublisher("url", "token")
    valid.enrich(pub, pub_values, force_namespace=True)
    assert valid.get_enriched(page) == {}
Ejemplo n.º 9
0
def test_empty_enrichment_case():
    values = {}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    valid.enrich(pub, values)
    data = valid.get_enriched(pub)
    assert data == values
Ejemplo n.º 10
0
def test_dry_run():
    validations = [Validation("low", Priority.LOW),
                   Validation("normal", Priority.NORMAL),
                   Validation("critical", Priority.CRITICAL)]

    publishers = [Publisher("low_pub", priority_threshold=Priority.LOW),
                  Publisher("norm_pub", priority_threshold=Priority.NORMAL),
                  Publisher("crit_pub", priority_threshold=Priority.CRITICAL)]

    associations = run._compute_dry_run(validations, publishers)
    lows = associations[publishers[0]]
    norms = associations[publishers[1]]
    crits = associations[publishers[2]]
    assert len(lows) == 3
    assert len(norms) == 2
    assert len(crits) == 1

    assert validations[0] in lows
    assert validations[1] in lows
    assert validations[2] in lows

    assert validations[0] not in norms
    assert validations[1] in norms
    assert validations[2] in norms

    assert validations[0] not in crits
    assert validations[1] not in crits
    assert validations[2] in crits
Ejemplo n.º 11
0
def test_empty_enrichment_case():
    values = {}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    valid.enrich(pub, values)
    data = valid.get_enriched(pub)
    assert data == values
Ejemplo n.º 12
0
def test_run_validations_sets_time_with_function_if_available(env, processes):
    reporter = env["reporter"]
    publishers = [MockPublisher()]
    reporter.publishers = publishers
    validation = Validation("success")
    validation.get_elapsed_time = return_5
    run._run_validations([validation], reporter, processes)
    assert reporter._reports[0].time == 5
Ejemplo n.º 13
0
def test_run_validations_sets_time_on_failure(env, processes):
    reporter = env["reporter"]
    publishers = [MockPublisher()]
    reporter.publishers = publishers
    validation = Validation("success")
    validation.perform = slow_fail
    run._run_validations([validation], reporter, processes)
    assert abs(reporter._reports[0].time - 2) <= 0.2
Ejemplo n.º 14
0
def test_run_validations_sets_time_with_function_if_available(env, processes):
    reporter = env["reporter"]
    publishers = [MockPublisher()]
    reporter.publishers = publishers
    validation = Validation("success")
    validation.get_elapsed_time = return_5
    run._run_validations([validation], reporter, processes)
    assert reporter._reports[0].time == 5
Ejemplo n.º 15
0
def test_run_validations_sets_time_on_failure(env, processes):
    reporter = env["reporter"]
    publishers = [MockPublisher()]
    reporter.publishers = publishers
    validation = Validation("success")
    validation.perform = slow_fail
    run._run_validations([validation], reporter, processes)
    assert abs(reporter._reports[0].time - 2) <= 0.2
Ejemplo n.º 16
0
 def __init__(self, context, name, metric_name,
              time_range=datetime.timedelta(hours=1),
              **kwargs):
     """Creates a GraphiteValidation object"""
     Validation.__init__(self, name, **kwargs)
     self._context = context
     self.time_range = time_range
     self.metric_name = metric_name
     self._expectations = []
Ejemplo n.º 17
0
 def __init__(self, context, name, metric_name,
              time_range=datetime.timedelta(hours=1),
              **kwargs):
     """Creates a GraphiteValidation object"""
     Validation.__init__(self, name, **kwargs)
     self._context = context
     self.time_range = time_range
     self.metric_name = metric_name
     self._expectations = []
Ejemplo n.º 18
0
def test_run_validations_fails_on_slow_success(env, processes):
    reporter = env["reporter"]
    publishers = [MockPublisher()]
    reporter.publishers = publishers
    validation = Validation("success")
    validation.perform = slow_success
    validation.timeout = 1
    run._run_validations([validation], reporter, processes)
    assert reporter._reports[0].is_failure()
Ejemplo n.º 19
0
def test_run_validations_batch(env, processes):
    reporter = env["reporter"]
    publishers = [MockPublisher()]
    reporter.publishers = publishers
    validations = [Validation("success"),
                   Validation("success"),
                   construct_failing_validation("failed")]
    run._run_validations(validations, reporter, processes)
    assert publishers[0].successes == 2
    assert publishers[0].failures == 1
Ejemplo n.º 20
0
def test_run_validations_group_success(env, processes):
    reporter = env["reporter"]
    publishers = [MockPublisher()]
    reporter.publishers = publishers
    validations = [Validation("success", group="a"),
                   GroupValidation("group a", "a", low_threshold=2),
                   Validation("success", group="a"),
                   construct_failing_validation("failed", group="a")]
    run._run_validations(validations, reporter, processes)
    assert publishers[0].successes == 3
    assert publishers[0].failures == 1
Ejemplo n.º 21
0
 def __init__(self, ssh_context, name,
              priority=Priority.NORMAL, timeout=None,
              group=None, connection_retries=0,
              hosts=None):
     """Creates an SshValidation object"""
     Validation.__init__(self,
         name, priority, timeout, group=group)
     self.context = ssh_context
     if hosts is not None:
         self.hosts = hosts
     else:
         self.hosts = []
     self.expectations = []
     self.retries = connection_retries
     self._exit_code_expectation = _ExitCodeEquals(self, 0)
Ejemplo n.º 22
0
def testSendFailure():
    graphite = new_publisher()
    v = Validation("low", priority=Priority.LOW)
    failure = Failure("foo", v, "unable to frobnicate")
    graphite.send(failure)
    assert graphite._graphite.counter["passed"] == 0
    assert graphite._graphite.counter["failed"] == 1
Ejemplo n.º 23
0
def test_send_success():
    graphite = new_publisher()
    v = Validation("low", priority=Priority.LOW)
    success = Success("bar", v)
    graphite.send(success)
    assert graphite._graphite.counter["passed"] == 1
    assert graphite._graphite.counter["failed"] == 0
Ejemplo n.º 24
0
def test_run_validations_success(env, processes):
    reporter = env["reporter"]
    publishers = [MockPublisher()]
    reporter.publishers = publishers
    validations = [Validation("success")]
    run._run_validations(validations, reporter, processes)
    assert publishers[0].successes == 1
    assert publishers[0].failures == 0
Ejemplo n.º 25
0
    def __init__(self, rabbitmq_context, name, queue_name, max_queue_size,
                 priority=Priority.NORMAL, timeout=None, num_attempts=4,
                 seconds_between_attempts=2, group=None,
                 ignore_connection_failure=False):
        """Creates a RabbitMqValidation object."""
        Validation.__init__(self,
            ("queue '{0}' should have less than {1} messages in in " +
            "it on RabbitMQ host: '{2}' ({3})")
            .format(queue_name, max_queue_size, rabbitmq_context.host, name),
            priority=priority, timeout=timeout, group=group)

        self.rabbitmq_context = rabbitmq_context
        self.max_queue_size = max_queue_size
        self.queue_name = queue_name
        self.num_attempts = num_attempts
        self.seconds_between_attempts = seconds_between_attempts
        self.ignore_connection_failure = ignore_connection_failure
Ejemplo n.º 26
0
def test_does_not_publish_success(goodserver):
    publisher = HttpPublisher(name="Test",
                              url=goodserver.url,
                              publish_successes=False)
    publisher.send(
        Success("success", Validation("validation", priority=Priority.NORMAL),
                "description"))
    assert not request_sent
Ejemplo n.º 27
0
def test_publish_failure(httpserver):
    httpserver.serve_content(code=300,
                             headers={"content-type": "text/plain"},
                             content='{"mode":"NORMAL"}')
    pub = PagerDutyPublisher(httpserver.url, "token")
    v = Validation("low", priority=Priority.CRITICAL)
    failure = Failure("bar", v, "message")
    with pytest.raises(PublishFailure):
        pub.send(failure)
Ejemplo n.º 28
0
def test_message_length_capped(httpserver):
    httpserver.serve_content(code=300,
                             headers={"content-type": "text/plain"},
                             content='{"mode":"NORMAL"}')
    pub = PagerDutyPublisher(httpserver.url, "token")
    v = Validation("low", priority=Priority.CRITICAL)
    failure = Failure("bar", v, "-" * 2000)
    message = pub._construct_message(failure)
    assert len(message) == pagerduty.MAX_LEN
Ejemplo n.º 29
0
def test_publish_retries(ratelimited):
    global cutoff
    cutoff = 3
    global hits
    hits = 0
    pub = PagerDutyPublisher(ratelimited.url, "token")
    v = Validation("low", priority=Priority.CRITICAL)
    failure = Failure("bar", v, "message")
    pub.send(failure)
Ejemplo n.º 30
0
 def __init__(self,
              ssh_context,
              name,
              priority=Priority.NORMAL,
              timeout=None,
              group=None,
              connection_retries=0,
              hosts=None):
     """Creates an SshValidation object"""
     Validation.__init__(self, name, priority, timeout, group=group)
     self.context = ssh_context
     if hosts is not None:
         self.hosts = hosts
     else:
         self.hosts = []
     self.expectations = []
     self.retries = connection_retries
     self._exit_code_expectation = _ExitCodeEquals(self, 0)
Ejemplo n.º 31
0
def test_construct_tree_batch():
    pub = JUnitPublisher("should_not_be_created.xml")
    v = Validation("low", priority=Priority.CRITICAL)
    failure = Failure("bar", v, "message", time=30)
    success = Success("foo", v, time=20)
    tree = pub._construct_tree([failure, failure, success]).getroot()
    assert tree.get("failures") == str(2)
    assert tree.get("tests") == str(3)
    assert float(tree.get("time")) == 80
    assert len(tree) == 3
Ejemplo n.º 32
0
def test_publishes_failure_to_correct_url(goodserver):
    publisher = HttpPublisher(name="Test",
                              success_url=goodserver.url + '/success',
                              failure_url=goodserver.url + '/failure',
                              publish_successes=True)
    publisher.send(
        Failure("failure", Validation("validation", priority=Priority.NORMAL),
                "description"))
    assert request_sent
    assert requested_url == '/failure'
Ejemplo n.º 33
0
def test_publish_stops_retrying(ratelimited):
    global cutoff
    cutoff = 4
    global hits
    hits = 0
    pub = PagerDutyPublisher(ratelimited.url, "token")
    v = Validation("low", priority=Priority.CRITICAL)
    failure = Failure("bar", v, "message")
    with pytest.raises(PublishFailure):
        pub.send(failure)
Ejemplo n.º 34
0
def test_not_enough_attempts(failingserver):
    global times_to_fail
    times_to_fail = 3

    publisher = HttpPublisher(name="Test", url=failingserver.url, attempts=3)

    with pytest.raises(PublishFailure):
        publisher.send(
            Failure("failure",
                    Validation("validation", priority=Priority.NORMAL),
                    "description"))
Ejemplo n.º 35
0
def test_construct_tree_success():
    pub = JUnitPublisher("should_not_be_created.xml")
    v = Validation("low", priority=Priority.CRITICAL)
    success = Success("bar", v, time=30)
    tree = pub._construct_tree([success]).getroot()
    assert tree.get("failures") == str(0)
    assert tree.get("tests") == str(1)
    assert float(tree.get("time")) == 30
    assert len(tree) == 1
    for element in tree:
        assert float(element.get("time")) == 30
        assert len(element) == 0
Ejemplo n.º 36
0
def test_enrichment_fails_on_duplication_different_data():
    values = {1: 2, "a": "b"}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    valid.enrich(pub, values)
    with pytest.raises(EnrichmentFailure):
        valid.enrich(pub, {"different": "values"})
Ejemplo n.º 37
0
def test_two_enrichments_correctness_independent_of_force_reverse(bools):
    pub_values = {1: 2, "a": "b"}
    page_values = {1: 5, "a": "3", "what": "who"}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    page = pagerduty.PagerDutyPublisher("url", "token")
    valid.enrich(page, page_values, force_namespace=bools)
    valid.enrich(pub, pub_values, force_namespace=bools)
    pub_data = valid.get_enriched(pub)
    page_data = valid.get_enriched(page)
    for item in pub_values.items():
        assert item in pub_data.items()
    for item in page_values.items():
        assert item in page_data.items()
Ejemplo n.º 38
0
def test_timeout_too_short(slowserver):
    global sleep_time
    sleep_time = 2

    publisher = HttpPublisher(name="Test",
                              url=slowserver.url + '/failure',
                              timeout_seconds=3)

    publisher.send(
        Failure("failure", Validation("validation", priority=Priority.NORMAL),
                "description"))
    assert request_sent
    assert requested_url == '/failure'
Ejemplo n.º 39
0
def test_timeout_too_short(slowserver):
    global sleep_time
    sleep_time = 2

    publisher = HttpPublisher(name="Test",
                              url=slowserver.url,
                              timeout_seconds=1)

    with pytest.raises(PublishFailure):
        publisher.send(
            Failure("failure",
                    Validation("validation", priority=Priority.NORMAL),
                    "description"))
Ejemplo n.º 40
0
    def __init__(self, method, url, data=None, headers=None,
                 priority=Priority.NORMAL, timeout=None,
                 group=None, retries=1, ignore_ssl_cert_errors=False,
                 auth=None):
        """Creates an HttpValidation object that will make an HTTP request to
        the provided URL passing the provided headers.

        """
        Validation.__init__(self, "{0} {1}".format(method, url),
                            priority=priority,
                            timeout=timeout,
                            group=group)

        self._url = url
        self._data = data
        self._method = method
        self._headers = copy.copy(headers) or {}
        self._response_code_expectation = _ExpectedStatusCodes(set([200]))
        self._expectations = []
        self._retries = retries
        self._ignore_ssl_cert_errors = ignore_ssl_cert_errors
        self._auth = auth or ()
        self._elapsed_time = -1
Ejemplo n.º 41
0
def test_two_enrichments_correctness_independent_of_force_reverse(bools):
    pub_values = {1: 2, "a": "b"}
    page_values = {1: 5, "a": "3", "what": "who"}
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    page = pagerduty.PagerDutyPublisher("url", "token")
    valid.enrich(page, page_values, force_namespace=bools)
    valid.enrich(pub, pub_values, force_namespace=bools)
    pub_data = valid.get_enriched(pub)
    page_data = valid.get_enriched(page)
    for item in pub_values.items():
        assert item in pub_data.items()
    for item in page_values.items():
        assert item in page_data.items()
Ejemplo n.º 42
0
 def fail(self, reason):
     """Causes this GraphiteValidation to fail with the given reason."""
     Validation.fail(self, reason)
Ejemplo n.º 43
0
def test_repr():
    v = Validation("name")
    v.__repr__()
Ejemplo n.º 44
0
def test_validation_str():
    v = Validation("name")
    v.__str__()
Ejemplo n.º 45
0
def test_get_empty_enrichment():
    valid = Validation("low", Priority.LOW)
    pub = publisher.Publisher(priority_threshold=Priority.CRITICAL)
    assert valid.get_enriched(pub) == {}
Ejemplo n.º 46
0
def construct_failing_validation(name, group=None):
    valid = Validation(name, group=group)
    valid.perform = fail
    return valid