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({}))
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({}))
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({}))
def test_expected_header(httpserver): httpserver.serve_content(code=200, headers={"header": "exists"}, content="this") (HttpValidation.get(httpserver.url) .expect_header("header", "exists") .perform({}))
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({}))
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({}))
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
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}}."
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
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({}))
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({}))
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
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({}))
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({}))
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({}))
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({}))
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"
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)
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
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
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({}))
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"] == {}
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
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 == {}
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"
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
def test_json_less_than(): validation = HttpValidation.get("url") exp = ExpectedJsonValueLessThan("path", 5) exp.validate_value(validation, 5, 3)
def test_expected_code_default_to_200(httpserver): httpserver.serve_content(code=200, headers={"content-type": "application/json"}, content='{}') (HttpValidation.get(httpserver.url) .perform({}))
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({}))
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
def test_repr(): name = "http://thread-stats.qaprod.pearsonopenclass.com/version" validation = HttpValidation.get(name) validation.__repr__()
def test_properly_times_out(slowserver): val = HttpValidation.get(slowserver.url, timeout=3) with pytest.raises(requests.exceptions.Timeout): val.perform({})
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({}))
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({}))
def test_json_wildcard_equality(): validation = HttpValidation.get("url") exp = ExpectedJsonEquality("path[*]", [1, 2, 3]) exp.validate_value(validation, 2, [1, 2, 3])
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)
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({}))
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")
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])
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
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({}))
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)
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
def test_json_equality(): validation = HttpValidation.get("url") exp = ExpectedJsonEquality("path", 5) exp.validate_value(validation, 5, 5)
def test_json_equality_fails(): validation = HttpValidation.get("url") exp = ExpectedJsonEquality("path", 5) with pytest.raises(ValidationFailure): exp.validate_value(validation, 5, 6)
def test_json_greater_than(): validation = HttpValidation.get("url") exp = ExpectedJsonValueGreaterThan("path", 5) exp.validate_value(validation, 5, 7)
def test_expected_header(httpserver): httpserver.serve_content(code=200, headers={"header": "exists"}, content="this") (HttpValidation.get(httpserver.url).expect_header("header", "exists").perform({}))
def test_json_greater_than_fails(): validation = HttpValidation.get("url") exp = ExpectedJsonValueGreaterThan("path", 5) with pytest.raises(ValidationFailure): exp.validate_value(validation, 5, 5)