예제 #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({}))
예제 #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({}))
예제 #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({}))
예제 #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({}))
예제 #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({}))
예제 #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({}))
예제 #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
예제 #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}}."
예제 #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
예제 #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({}))
예제 #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({}))
예제 #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
예제 #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
예제 #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({}))
예제 #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({}))
예제 #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({}))
예제 #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({}))
예제 #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"
예제 #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)
예제 #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
예제 #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
예제 #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
예제 #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({}))
예제 #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"] == {}
예제 #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
예제 #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 == {}
예제 #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"
예제 #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
예제 #29
0
def test_json_less_than():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonValueLessThan("path", 5)
    exp.validate_value(validation, 5, 3)
예제 #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({}))
예제 #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({}))
예제 #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
예제 #33
0
def test_repr():
    name = "http://thread-stats.qaprod.pearsonopenclass.com/version"
    validation = HttpValidation.get(name)
    validation.__repr__()
예제 #34
0
def test_properly_times_out(slowserver):
    val = HttpValidation.get(slowserver.url, timeout=3)
    with pytest.raises(requests.exceptions.Timeout):
        val.perform({})
예제 #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({}))
예제 #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({}))
예제 #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])
예제 #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)
예제 #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({}))
예제 #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")
예제 #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])
예제 #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
예제 #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({}))
예제 #44
0
def test_properly_times_out(slowserver):
    val = HttpValidation.get(slowserver.url, timeout=3)
    with pytest.raises(requests.exceptions.Timeout):
        val.perform({})
예제 #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)
예제 #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
예제 #47
0
def test_json_equality():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonEquality("path", 5)
    exp.validate_value(validation, 5, 5)
예제 #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
예제 #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)
예제 #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
예제 #51
0
def test_json_greater_than():
    validation = HttpValidation.get("url")
    exp = ExpectedJsonValueGreaterThan("path", 5)
    exp.validate_value(validation, 5, 7)
예제 #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({}))
예제 #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)
예제 #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