def setUp(self): """ Create a check """ core = MimicCore(Clock(), []) self.root = MimicRoot(core).app.resource() self.create_check = { "check": { "attributes": { "name": "name", "module": "module", "target": "target", "period": "period", "timeout": "timeout", "filterset": "filterset" } }} self.create_check_xml_payload = xmltodict.unparse(self.create_check ).encode("utf-8") self.check_id = uuid.uuid4() url = "noit/checks/set/{0}".format(self.check_id) (self.response, response_body) = self.successResultOf( request_with_content(self, self.root, "PUT", url, body=self.create_check_xml_payload)) self.create_json_response = xmltodict.parse(response_body)
def setUp(self): """ Create a check """ core = MimicCore(Clock(), []) self.root = MimicRoot(core).app.resource() self.create_check = { "check": { "attributes": { "name": "name", "module": "module", "target": "target", "period": "period", "timeout": "timeout", "filterset": "filterset" } } } self.create_check_xml_payload = xmltodict.unparse( self.create_check).encode("utf-8") self.check_id = uuid.uuid4() url = "noit/checks/set/{0}".format(self.check_id) (self.response, response_body) = self.successResultOf( request_with_content(self, self.root, "PUT", url, body=self.create_check_xml_payload)) self.create_json_response = xmltodict.parse(response_body)
def test_get_all_checks(self): """ Test to verify :func:`get_all_checks` on ``GET /config/checks`` """ (response, body) = self.successResultOf( request_with_content(self, self.root, "GET", "noit/config/checks")) self.assertEqual(response.code, 200) json_response = xmltodict.parse(body) self.assertTrue(len(json_response["checks"]["check"]) > 0)
def test_delete_check(self): """ Test to verify :func:`delete_checks` on ``DELETE /checks/delete/<check_id>`` """ (del_response, body) = self.successResultOf( request_with_content(self, self.root, "DELETE", "noit/checks/delete/{0}".format(self.check_id))) self.assertEqual(del_response.code, 200)
def test_delete_not_existant_check(self): """ Test to verify :func:`delete_checks` on ``DELETE /checks/delete/<check_id>`` when the check_id was never created. """ (del_response, body) = self.successResultOf( request_with_content(self, self.root, "DELETE", "noit/checks/delete/1234556")) self.assertEqual(del_response.code, 404)
def test_create_check_fails_with_500_for_invalid_check_id(self): """ Test to verify :func:`set_check` results in error 500, when the xml cannot be parsed. """ (response, body) = self.successResultOf( request_with_content(self, self.root, "PUT", "noit/checks/set/123444", body=self.create_check_xml_payload)) self.assertEqual(response.code, 500)
def test_test_check(self): """ Test to verify :func:`test_check` on ``POST /checks/test`` """ (response, body) = self.successResultOf( request_with_content(self, self.root, "POST", "noit/checks/test", body=self.create_check_xml_payload)) json_response = xmltodict.parse(body) self.assertEqual(response.code, 200) self.assertTrue(json_response["check"]["state"]["metrics"])
def test_delete_check(self): """ Test to verify :func:`delete_checks` on ``DELETE /checks/delete/<check_id>`` """ (del_response, body) = self.successResultOf( request_with_content( self, self.root, "DELETE", "noit/checks/delete/{0}".format(self.check_id))) self.assertEqual(del_response.code, 200)
def delete_behavior(self, behavior_id, status=204, expected_body=b''): """ Given a behavior ID, attempts to delete it. """ response, body = self.successResultOf(request_with_content( self, self.bhelper.root, "DELETE", "{0}/{1}".format(self.bhelper.behavior_api_endpoint, behavior_id))) self.assertEqual(response.code, status) self.assertEqual(body, expected_body)
def test_get_check(self): """ Test to verify :func:`get_checks` on ``GET /checks/show/<check_id>`` """ (get_response, body) = self.successResultOf( request_with_content(self, self.root, "GET", "noit/checks/show/{0}".format(self.check_id))) json_response = xmltodict.parse(body) self.assertEqual(get_response.code, 200) self.assertEqual(self.create_check["check"]["attributes"], json_response["check"]["attributes"])
def delete_behavior(self, behavior_id, status=204, expected_body=b''): """ Given a behavior ID, attempts to delete it. """ response, body = self.successResultOf( request_with_content( self, self.bhelper.root, "DELETE", "{0}/{1}".format(self.bhelper.behavior_api_endpoint, behavior_id))) self.assertEqual(response.code, status) self.assertEqual(body, expected_body)
def test_create_check_fails_with_500(self): """ Test to verify :func:`set_check` results in error 500, when the xml cannot be parsed. """ (response, body) = self.successResultOf( request_with_content(self, self.root, "PUT", "noit/checks/set/{0}".format(self.check_id), body=self.create_check_xml_payload.replace( '</check>', ' abc'))) self.assertEqual(response.code, 500)
def test_test_check_fails_with_404_for_invalid_check_payload(self): """ Test to verify :func:`test_check` results in error 404, when the request check body is invalid. """ del self.create_check["check"]["attributes"]["target"] invalid_check_xml_payload = xmltodict.unparse(self.create_check ).encode("utf-8") (response, body) = self.successResultOf( request_with_content(self, self.root, "POST", "noit/checks/test".format(self.check_id), body=invalid_check_xml_payload)) self.assertEqual(response.code, 404)
def test_providing_invalid_json_fails_with_400(self): """ Providing invalid JSON for the behavior registration request results in a 400. """ name, params = self.bhelper.names_and_params[0] almost_correct = json.dumps({'name': name, 'parameters': params}) for invalid in ('', '{}', almost_correct): response, body = self.successResultOf( request_with_content(self, self.bhelper.root, "POST", self.bhelper.behavior_api_endpoint, invalid)) self.assertEqual(response.code, 400) self.assertEqual(b"", body)
def test_providing_invalid_json_fails_with_400(self): """ Providing invalid JSON for the behavior registration request results in a 400. """ name, params = self.bhelper.names_and_params[0] almost_correct = json.dumps({'name': name, 'parameters': params}) for invalid in ('', '{}', almost_correct): response, body = self.successResultOf(request_with_content( self, self.bhelper.root, "POST", self.bhelper.behavior_api_endpoint, invalid)) self.assertEqual(response.code, 400) self.assertEqual(b"", body)
def test_get_version(self): """ Test to verify :func:`test_check` on ``POST /checks/test``. When the check module is selfcheck, :func:`test_check` should return the version of the Noit instance """ self.create_check["check"]["attributes"]["module"] = 'selfcheck' (response, body) = self.successResultOf( request_with_content(self, self.root, "POST", "noit/checks/test", body=xmltodict.unparse(self.create_check).encode('utf-8'))) json_response = xmltodict.parse(body) self.assertEqual(response.code, 200) self.assertEqual( json_response["check"]["state"]["metrics"][1]["metric"][0]["@name"], "version")
def test_test_check_fails_with_404_for_invalid_check_payload(self): """ Test to verify :func:`test_check` results in error 404, when the request check body is invalid. """ del self.create_check["check"]["attributes"]["target"] invalid_check_xml_payload = xmltodict.unparse( self.create_check).encode("utf-8") (response, body) = self.successResultOf( request_with_content(self, self.root, "POST", "noit/checks/test".format(self.check_id), body=invalid_check_xml_payload)) self.assertEqual(response.code, 404)
def test_root(self): """ The root (``/``, handled by :func:`MimicRoot.help`) has a bit of help text pointing to the identity endpoint """ core = MimicCore(Clock(), []) root = MimicRoot(core).app.resource() (response, content) = self.successResultOf(request_with_content( self, root, 'GET', '/')) self.assertEqual(200, response.code) self.assertEqual(['text/plain'], response.headers.getRawHeaders('content-type')) self.assertIn(' POST ', content) self.assertIn('/identity/v2.0/tokens', content)
def test_update_check(self): """ Test to verify update check on :func:`set_check` using ``PUT /checks/set/<check_id>`` """ self.create_check["check"]["attributes"]["name"] = "rename" (response, body) = self.successResultOf( request_with_content(self, self.root, "PUT", "noit/checks/set/{0}".format(self.check_id), body=xmltodict.unparse(self.create_check ).encode("utf-8"))) json_response = xmltodict.parse(body) self.assertEqual(self.response.code, 200) self.assertEqual(json_response["check"]["attributes"]["name"], "rename")
def test_test_check_for_given_module(self): """ Test to verify :func:`test_check` results in response containing the metrics for the given module. """ self.create_check["check"]["attributes"]["module"] = "selfcheck" check_xml_payload = xmltodict.unparse(self.create_check ).encode("utf-8") (response, body) = self.successResultOf( request_with_content(self, self.root, "POST", "noit/checks/test".format(self.check_id), body=check_xml_payload)) json_response = xmltodict.parse(body) self.assertEqual(response.code, 200) self.assertEqual(json_response["check"]["state"]["metrics"][ 1]["metric"], metrics["selfcheck"]["metric"])
def test_update_check(self): """ Test to verify update check on :func:`set_check` using ``PUT /checks/set/<check_id>`` """ self.create_check["check"]["attributes"]["name"] = "rename" (response, body) = self.successResultOf( request_with_content(self, self.root, "PUT", "noit/checks/set/{0}".format(self.check_id), body=xmltodict.unparse( self.create_check).encode("utf-8"))) json_response = xmltodict.parse(body) self.assertEqual(self.response.code, 200) self.assertEqual(json_response["check"]["attributes"]["name"], "rename")
def test_same_tenant_different_regions(self): """ Creating an LB for a tenant in one different regions should create it in another region for that tenant. """ helper = APIMockHelper(self, [LoadBalancerApi(regions=["ORD", "DFW"])]) self._create_loadbalancer(api_helper=helper) list_lb_response, list_lb_response_body = self.successResultOf( request_with_content( self, helper.root, "GET", helper.nth_endpoint_public(1) + "/loadbalancers")) self.assertEqual(list_lb_response.code, 200) list_lb_response_body = json.loads(list_lb_response_body) self.assertEqual(list_lb_response_body, {"loadBalancers": []})
def test_service_endpoint_returns_service_resource(self): """ When the URI used to access a real service has the right service ID and right region, the service's resource is used to respond to the request. """ core = MimicCore(Clock(), [ExampleAPI('response!')]) root = MimicRoot(core).app.resource() # get the region and service id registered for the example API (region, service_id) = one_api(self, core) (response, content) = self.successResultOf(request_with_content( self, root, "GET", "http://mybase/mimicking/{0}/{1}".format(service_id, region) )) self.assertEqual(200, response.code) self.assertEqual('response!', content)
def test_different_tenants_same_region_different_lbs(self): """ Creating a LB for one tenant in a particular region should not create it for other tenants in the same region. """ self._create_loadbalancer() other_tenant = TenantAuthentication(self, self.root, "other", "other") list_lb_response, list_lb_response_body = self.successResultOf( request_with_content( self, self.root, "GET", other_tenant.nth_endpoint_public(0) + "/loadbalancers")) self.assertEqual(list_lb_response.code, 200) list_lb_response_body = json.loads(list_lb_response_body) self.assertEqual(list_lb_response_body, {"loadBalancers": []})
def test_test_check_for_given_module(self): """ Test to verify :func:`test_check` results in response containing the metrics for the given module. """ self.create_check["check"]["attributes"]["module"] = "selfcheck" check_xml_payload = xmltodict.unparse( self.create_check).encode("utf-8") (response, body) = self.successResultOf( request_with_content(self, self.root, "POST", "noit/checks/test".format(self.check_id), body=check_xml_payload)) json_response = xmltodict.parse(body) self.assertEqual(response.code, 200) self.assertEqual( json_response["check"]["state"]["metrics"][1]["metric"], metrics["selfcheck"]["metric"])
def test_get_version(self): """ Test to verify :func:`test_check` on ``POST /checks/test``. When the check module is selfcheck, :func:`test_check` should return the version of the Noit instance """ self.create_check["check"]["attributes"]["module"] = 'selfcheck' (response, body) = self.successResultOf( request_with_content(self, self.root, "POST", "noit/checks/test", body=xmltodict.unparse( self.create_check).encode('utf-8'))) json_response = xmltodict.parse(body) self.assertEqual(response.code, 200) self.assertEqual( json_response["check"]["state"]["metrics"][1]["metric"][0] ["@name"], "version")
def request_with_content(self, method, relative_uri, **kwargs): """ Helper function that makes a request and gets the non-json content. """ return request_with_content(self, self.helper.root, method, self.helper.uri + relative_uri, **kwargs)