Esempio n. 1
0
def test_expected_code_correct_on_set(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{}')
    (HttpValidation.get(httpserver.url)
     .expect_status_codes(set(range(200, 300)))
     .perform({}))
Esempio n. 2
0
def test_expected_text(httpserver):
    httpserver.serve_content(code=200,
                             headers={"header": "exists"},
                             content="Here is some text!")
    (HttpValidation.get(httpserver.url)
     .expect_contains_text("is some")
     .perform({}))
Esempio n. 3
0
def test_expected_code(httpserver):
    httpserver.serve_content(code=300,
                             headers={"content-type": "application/json"},
                             content='{}')
    (HttpValidation.get(httpserver.url)
     .expect_status_codes([300])
     .perform({}))
Esempio n. 4
0
def test_expected_header(httpserver):
    httpserver.serve_content(code=200,
                             headers={"header": "exists"},
                             content="this")
    (HttpValidation.get(httpserver.url)
     .expect_header("header", "exists")
     .perform({}))
Esempio n. 5
0
def test_get_json_value_greater_than(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{"mode": "NORMAL","numExecutors":3}')
    (HttpValidation.get(httpserver.url)
     .expect_json_property_value_greater_than("numExecutors", 2)
     .perform({}))
Esempio n. 6
0
def test_get_json_value(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{"mode": "NORMAL"}')
    (HttpValidation.get(httpserver.url)
     .expect_json_property_value("mode", "NORMAL")
     .perform({}))
Esempio n. 7
0
def test_get_email_settings(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_type = "test_alert"
    subject = "subject test line"
    body = "body test"
    recipient_array = [{"real_name": "Test Recipient",
                        "address": "*****@*****.**"}]
    sender = {"real_name": "Alarmageddon Monitor",
              "address": "*****@*****.**"}
    email_settings = {"email_type": email_type,
                      "subject": subject,
                      "body": body,
                      "sender": sender,
                      "recipients": recipient_array
                      }
    emailer.enrich(http_validator, email_settings)
    result = Success("validation name", http_validator)
    enriched_email_settings = email_pub.get_email_settings(result)
    assert enriched_email_settings['email_type'] == email_type
    assert enriched_email_settings['subject'] == subject
    assert enriched_email_settings['body'] == body
    assert enriched_email_settings['sender'] == sender
    assert enriched_email_settings['recipients'] == recipient_array
Esempio n. 8
0
def test_configure_replacement_context_email_type_found(tmpdir, smtpserver,
                                                        httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }
    emailer.enrich(http_validator, email_settings)
    result = Failure("validation name", http_validator,
                     description="A failure occurred.")
    email_pub.configure_replacement_context(result)
    replacement_context = email_pub._replacement_context
    assert replacement_context["test_name"] == "validation name"
    assert replacement_context["test_description"] == "A failure occurred."
    assert replacement_context["env"] == "test"
    assert replacement_context["email_type"] == "test_alert"
    assert replacement_context["email_custom_message"] == \
        "Validation failed in environment {{env}}: {{test_name}}."
Esempio n. 9
0
def test_send(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    create_subject_template(tmpdir)
    create_body_template(tmpdir)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    general_defaults = email_pub._config["email_defaults"]["general"]
    email_settings = {"email_type": "test_alert",
                      "subject": general_defaults["email_subject_template"],
                      "body": general_defaults["email_template"],
                      "sender": general_defaults["email_sender"],
                      "recipients": general_defaults["email_recipients"]}
    emailer.enrich(http_validator, email_settings)
    failure_message = "Validation failure. Expected 200, received 404."
    result = Failure("Check Status Route", http_validator,
                     description=failure_message)
    email_pub.send(result)
    print(smtpserver.outbox[0])
    assert len(smtpserver.outbox) == 1
    payload = str(smtpserver.outbox[0].get_payload()[0])
    assert payload.split('\n')[5] == "Validation Failure in environment test:"
    assert payload.split('\n')[6] == "Test Name: [Check Status Route]"
    assert payload.split('\n')[7] == \
        "Test Description: [Validation failure. Expected 200, received 404.]"
    custom_message = "Custom Message: [Validation failed in environment " \
                     "test: Check Status Route.]"
    assert payload.split('\n')[8] == custom_message
Esempio n. 10
0
def test_expected_code_default_correctly_fails(httpserver):
    httpserver.serve_content(code=400,
                             headers={"content-type": "application/json"},
                             content='{}')
    with pytest.raises(ValidationFailure):
        (HttpValidation.get(httpserver.url)
         .perform({}))
Esempio n. 11
0
def test_get_json_value_less_than_zero(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{"mode": "NORMAL","failed":0}')
    (HttpValidation.get(httpserver.url)
     .expect_json_property_value_less_than('failed', 2)
     .perform({}))
Esempio n. 12
0
def test_properly_records_elapsed_time_on_timeout(slowserver):
    val = HttpValidation.get(slowserver.url, timeout=3)
    try:
        val.perform({})
    except requests.exceptions.Timeout:
        pass
    #specifically in this case the elapsed time should be the timeout
    assert val._elapsed_time == 3
Esempio n. 13
0
def test_properly_records_elapsed_time_on_timeout(slowserver):
    val = HttpValidation.get(slowserver.url, timeout=3)
    try:
        val.perform({})
    except requests.exceptions.Timeout:
        pass
    #specifically in this case the elapsed time should be the timeout
    assert val._elapsed_time == 3
Esempio n. 14
0
def test_get_json_key_fails(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{"key": "NORMAL"}')
    with pytest.raises(ValidationFailure):
        (HttpValidation.get(httpserver.url)
         .expect_json_property_value("mode", "NORMAL")
         .perform({}))
Esempio n. 15
0
def test_expected_header_bad_value(httpserver):
    httpserver.serve_content(code=200,
                             headers={"header": "nope"},
                             content="this")
    with pytest.raises(ValidationFailure):
        (HttpValidation.get(httpserver.url)
         .expect_header("header", "exists")
         .perform({}))
Esempio n. 16
0
def test_expected_text_correctly_fails(httpserver):
    httpserver.serve_content(code=200,
                             headers={"header": "exists"},
                             content="Here is some text!")
    with pytest.raises(ValidationFailure):
        (HttpValidation.get(httpserver.url)
         .expect_contains_text("words")
         .perform({}))
Esempio n. 17
0
def test_get_array_value(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{"num":3,"views":[{"name": "All"},' +
                                     '{"name": "None"}]}')
    (HttpValidation.get(httpserver.url)
     .expect_json_property_value("views[0].name", "All")
     .perform({}))
Esempio n. 18
0
def test_simple_email(httpserver, smtpserver):
    email_pub = SimpleEmailPublisher({"real_name": "test", "address": "*****@*****.**"},
                                     [{"real_name": "test", "address": "*****@*****.**"}],
                                     host=smtpserver.addr[0], port=smtpserver.addr[1])
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    result = Failure("Check Status Route", http_validator,
                     description="Validation failure message")
    email_pub.send(result)
    assert len(smtpserver.outbox) == 1
    payload = str(smtpserver.outbox[0].get_payload()[0])
    assert payload.split("\n")[-1] == "Validation failure message"
Esempio n. 19
0
def test_enrich_requires_recipients(httpserver):
    httpserver.serve_content("Not Found", 404)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"}
                      }
    with pytest.raises(KeyError):
        emailer.enrich(http_validator, email_settings)
Esempio n. 20
0
def test_can_publish(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }
    emailer.enrich(http_validator, email_settings)
    result = Success("validation name", http_validator)
    assert email_pub._can_publish(result) is True
Esempio n. 21
0
def test_duplicate_with_hosts():
    validation = HttpValidation.get("hostname:8080")
    new = validation.duplicate_with_hosts(["firstname", "secondname"])

    first = new[0]
    assert first._data == validation._data
    assert first._method == validation._method
    assert first._headers == validation._headers
    assert first._expectations == validation._expectations
    assert first._retries == validation._retries
    assert first._ignore_ssl_cert_errors == validation._ignore_ssl_cert_errors
    assert first._auth == validation._auth

    names = [v._url for v in new]
    assert "firstname" in names[0]
    assert "secondname" in names[1]
    assert len(names) == 2
Esempio n. 22
0
def test_duplicate_with_hosts():
    validation = HttpValidation.get("hostname:8080")
    new = validation.duplicate_with_hosts(["firstname", "secondname"])

    first = new[0]
    assert first._data == validation._data
    assert first._method == validation._method
    assert first._headers == validation._headers
    assert first._expectations == validation._expectations
    assert first._retries == validation._retries
    assert first._ignore_ssl_cert_errors == validation._ignore_ssl_cert_errors
    assert first._auth == validation._auth

    names = [v._url for v in new]
    assert "firstname" in names[0]
    assert "secondname" in names[1]
    assert len(names) == 2
Esempio n. 23
0
def test_nested_json_value(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content=json.dumps({
                                 "took": 270,
                                 "timed_out": False,
                                 "_shards": {
                                     "total": 576,
                                     "successful": 576,
                                     "failed": 0
                                 },
                                 "hits": {
                                     "total": 0,
                                     "max_score": 0.0,
                                     "hits": []
                                 }
                             }))
    (HttpValidation.get(httpserver.url).expect_json_property_value(
        'hits.total', 0).perform({}))
Esempio n. 24
0
def test_enrich_no_runtime_context(httpserver):
    httpserver.serve_content("Not Found", 404)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }

    emailer.enrich(http_validator, email_settings)
    temp_pub = EmailPublisher({"fake": "config"})
    data = http_validator.get_enriched(temp_pub)
    assert data["email_settings"] == email_settings
    assert data["runtime_context"] == {}
Esempio n. 25
0
def test_can_publish_requires_recipients(tmpdir, smtpserver,
                                         monkeypatch, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }
    emailer.enrich(http_validator, email_settings)
    temp_pub = EmailPublisher({"fake": "config"})
    data = http_validator.get_enriched(temp_pub)
    monkeypatch.delitem(data['email_settings'], "recipients")
    result = Success("validation name", http_validator)
    assert email_pub._can_publish(result) is False
Esempio n. 26
0
def test_get_runtime_context_no_context(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_type = "test_alert"
    subject = "subject test line"
    body = "body test"
    recipient_array = [{"real_name": "Test Recipient",
                        "address": "*****@*****.**"}]
    sender = {"real_name": "Alarmageddon Monitor",
              "address": "*****@*****.**"}
    email_settings = {"email_type": email_type,
                      "subject": subject,
                      "body": body,
                      "sender": sender,
                      "recipients": recipient_array
                      }
    emailer.enrich(http_validator, email_settings)
    result = Success("validation name", http_validator)
    enriched_context = email_pub.get_runtime_context(result)
    assert enriched_context == {}
Esempio n. 27
0
def test_get_runtime_context(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_type = "test_alert"
    subject = "subject test line"
    body = "body test"
    recipient_array = [{"real_name": "Test Recipient",
                        "address": "*****@*****.**"}]
    sender = {"real_name": "Alarmageddon Monitor",
              "address": "*****@*****.**"}
    email_settings = {"email_type": email_type,
                      "subject": subject,
                      "body": body,
                      "sender": sender,
                      "recipients": recipient_array
                      }
    context = {"custom_replacement": "hello world"}
    emailer.enrich(http_validator, email_settings, runtime_context=context)
    result = Success("validation name", http_validator)
    enriched_context = email_pub.get_runtime_context(result)
    assert enriched_context['custom_replacement'] == "hello world"
Esempio n. 28
0
def test_can_publish_requires_enrichment(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    result = Success("validation name", http_validator)
    assert email_pub._can_publish(result) is False
Esempio n. 29
0
def test_json_less_than():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonValueLessThan("path", 5)
    exp.validate_value(validation, 5, 3)
Esempio n. 30
0
def test_expected_code_default_to_200(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{}')
    (HttpValidation.get(httpserver.url)
     .perform({}))
Esempio n. 31
0
def test_expected_content_type(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{"mode": "NORMAL"}')
    (HttpValidation.get(httpserver.url).expect_json_property_value(
        "mode", "NORMAL").expect_content_type("application/json").perform({}))
Esempio n. 32
0
def test_properly_records_elapsed_time(slowserver):
    val = HttpValidation.get(slowserver.url)
    val.perform({})
    #should be ~4, but add some buffer since sleep doesn't actually
    #guarantee anything about how long it will sleep
    assert 3.8 < val._elapsed_time < 4.2
Esempio n. 33
0
def test_repr():
    name = "http://thread-stats.qaprod.pearsonopenclass.com/version"
    validation = HttpValidation.get(name)
    validation.__repr__()
Esempio n. 34
0
def test_properly_times_out(slowserver):
    val = HttpValidation.get(slowserver.url, timeout=3)
    with pytest.raises(requests.exceptions.Timeout):
        val.perform({})
Esempio n. 35
0
def test_get_json_value_less_than_zero(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{"mode": "NORMAL","failed":0}')
    (HttpValidation.get(httpserver.url).expect_json_property_value_less_than(
        'failed', 2).perform({}))
Esempio n. 36
0
def test_expected_text(httpserver):
    httpserver.serve_content(code=200,
                             headers={"header": "exists"},
                             content="Here is some text!")
    (HttpValidation.get(
        httpserver.url).expect_contains_text("is some").perform({}))
Esempio n. 37
0
def test_json_wildcard_equality():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonEquality("path[*]", [1, 2, 3])
    exp.validate_value(validation, 2, [1, 2, 3])
Esempio n. 38
0
def test_validate_bad_json():
    resp = MockResponse({"path": {"no": {"value": 1}}, "distractor": "value"})
    validation = HttpValidation.get("url")
    exp = ExpectedJsonEquality("path.to.value", 4)
    with pytest.raises(ValidationFailure):
        exp.validate(validation, resp)
Esempio n. 39
0
def test_expected_code_correct_on_set(httpserver):
    httpserver.serve_content(code=200,
                             headers={"content-type": "application/json"},
                             content='{}')
    (HttpValidation.get(httpserver.url).expect_status_codes(
        set(range(200, 300))).perform({}))
Esempio n. 40
0
def test_run_works_with_config():
    name = "http://127.0.0.1/version"
    validation = HttpValidation.get(name)
    run.run_tests([validation], config="config",
                  config_path="path", environment_name="stg")
Esempio n. 41
0
def test_json_greater_than_wildcard():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonValueGreaterThan("path[*]", [1, 2, 3])
    with pytest.raises(ValidationFailure):
        exp.validate_value(validation, 1, [1, 2, 3])
Esempio n. 42
0
def test_http_name_post():
    name = "http://thread-stats.qaprod.pearsonopenclass.com/version"
    validation = HttpValidation.post(name)
    name = validation.timer_name()
    expected = "http.com.pearsonopenclass.qaprod.thread-stats.version.POST"
    assert name == expected
Esempio n. 43
0
def test_expected_code(httpserver):
    httpserver.serve_content(code=300,
                             headers={"content-type": "application/json"},
                             content='{}')
    (HttpValidation.get(httpserver.url).expect_status_codes([300]).perform({}))
Esempio n. 44
0
def test_properly_times_out(slowserver):
    val = HttpValidation.get(slowserver.url, timeout=3)
    with pytest.raises(requests.exceptions.Timeout):
        val.perform({})
Esempio n. 45
0
def test_validate():
    resp = MockResponse({"path": {"to": {"value": 1}}, "distractor": "value"})
    validation = HttpValidation.get("url")
    exp = ExpectedJsonEquality("path.to.value", 1)
    exp.validate(validation, resp)
Esempio n. 46
0
def test_http_name_get_query():
    name = "http://thread-stats.pearsonopenclass.com/version?foo=bar"
    validation = HttpValidation.get(name)
    name = validation.timer_name()
    expected = "http.com.pearsonopenclass.thread-stats.version.foo=bar.GET"
    assert name == expected
Esempio n. 47
0
def test_json_equality():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonEquality("path", 5)
    exp.validate_value(validation, 5, 5)
Esempio n. 48
0
def test_http_name_post():
    name = "http://thread-stats.qaprod.pearsonopenclass.com/version"
    validation = HttpValidation.post(name)
    name = validation.timer_name()
    expected = "http.com.pearsonopenclass.qaprod.thread-stats.version.POST"
    assert name == expected
Esempio n. 49
0
def test_json_equality_fails():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonEquality("path", 5)
    with pytest.raises(ValidationFailure):
        exp.validate_value(validation, 5, 6)
Esempio n. 50
0
def test_properly_records_elapsed_time(slowserver):
    val = HttpValidation.get(slowserver.url)
    val.perform({})
    #should be ~4, but add some buffer since sleep doesn't actually
    #guarantee anything about how long it will sleep
    assert 3.8 < val._elapsed_time < 4.2
Esempio n. 51
0
def test_json_greater_than():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonValueGreaterThan("path", 5)
    exp.validate_value(validation, 5, 7)
Esempio n. 52
0
def test_expected_header(httpserver):
    httpserver.serve_content(code=200,
                             headers={"header": "exists"},
                             content="this")
    (HttpValidation.get(httpserver.url).expect_header("header",
                                                      "exists").perform({}))
Esempio n. 53
0
def test_json_greater_than_fails():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonValueGreaterThan("path", 5)
    with pytest.raises(ValidationFailure):
        exp.validate_value(validation, 5, 5)
Esempio n. 54
0
def test_http_name_get_query():
    name = "http://thread-stats.pearsonopenclass.com/version?foo=bar"
    validation = HttpValidation.get(name)
    name = validation.timer_name()
    expected = "http.com.pearsonopenclass.thread-stats.version.foo=bar.GET"
    assert name == expected