Exemplo n.º 1
0
def fix_dmaap_mr(monkeypatch):
    """monkeyed requests to dmaap_mr"""
    def monkeyed_dmaap_mr_get(uri, **kwargs):
        """monkeypatch policy-update request.get to dmaap_mr"""
        if kwargs.get("params"):
            _LOGGER.info("--- fix_dmaap_mr --- sleeping 3 secs...")
            time.sleep(3)
        else:
            _LOGGER.info("--- fix_dmaap_mr --- sleeping 0.5 secs...")
            time.sleep(0.5)
        _LOGGER.info("--- fix_dmaap_mr --- send back the response")
        return MockHttpResponse("get", uri, **kwargs)

    _LOGGER.info("setup fix_dmaap_mr")
    audit = Audit(req_message="fix_dmaap_mr")
    DmaapMr._lazy_inited = False
    DmaapMr._lazy_init(audit)

    monkeypatch.setattr(
        'policyhandler.pdp_api.dmaap_mr.DmaapMr._requests_session.get',
        monkeyed_dmaap_mr_get)

    yield fix_dmaap_mr
    audit.audit_done("teardown")
    _LOGGER.info("teardown fix_dmaap_mr")
Exemplo n.º 2
0
    def test_zzzzz_shutdown(self):
        """test shutdown"""
        _LOGGER.info("start shutdown")
        assert not PolicyReceiver.is_running()
        audit = Audit(job_name="test_zzzzz_shutdown",
                      req_message="start shutdown")
        PolicyReceiver.run(audit)

        _LOGGER.info("sleep before send_notification...")
        time.sleep(2)

        MockWebSocket.send_notification([1, 3, 5])
        _LOGGER.info("sleep after send_notification...")
        time.sleep(3)

        result = self.getPage("/healthcheck")
        _LOGGER.info("healthcheck result: %s", result)

        WebServer2018Test.do_gc_test = False
        _LOGGER.info("shutdown...")
        audit.audit_done("shutdown")
        result = self.getPage("/shutdown")
        _LOGGER.info("shutdown result: %s", result)
        self.assertStatus('200 OK')
        _LOGGER.info("got shutdown: %s", self.body)
        time.sleep(1)
        assert not PolicyReceiver.is_running()

        Tracker.validate()
Exemplo n.º 3
0
def fix_deploy_handler(monkeypatch):
    """monkeyed requests to deployment-handler"""
    def monkeyed_deploy_handler_put(uri, **kwargs):
        """monkeypatch for policy-update request.put to deploy_handler"""
        return MockHttpResponse("put", uri, **kwargs)

    def monkeyed_deploy_handler_get(uri, **kwargs):
        """monkeypatch policy-update request.get to deploy_handler"""
        return MockHttpResponse("get", uri, **kwargs)

    _LOGGER.info("setup fix_deploy_handler")
    audit = None
    if DeployHandler._lazy_inited is False:
        audit = Audit(req_message="fix_deploy_handler")
        DeployHandler._lazy_init(audit)

    monkeypatch.setattr(
        'policyhandler.deploy_handler.DeployHandler._requests_session.put',
        monkeyed_deploy_handler_put)
    monkeypatch.setattr(
        'policyhandler.deploy_handler.DeployHandler._requests_session.get',
        monkeyed_deploy_handler_get)

    yield fix_deploy_handler
    if audit:
        audit.audit_done("teardown")
    _LOGGER.info("teardown fix_deploy_handler")
Exemplo n.º 4
0
def run_policy_handler():
    """main run function for policy-handler"""
    Config.init_config()

    from policyhandler import LogWriter
    from policyhandler.policy_receiver import PolicyReceiver
    from policyhandler.service_activator import ServiceActivator
    from policyhandler.web_server import PolicyWeb

    logger = Utils.get_logger(__file__)
    sys.stdout = LogWriter(logger.info)
    sys.stderr = LogWriter(logger.error)

    logger.info("========== run_policy_handler ========== %s", __package__)
    Audit.init(Config.system_name, Config.LOGGER_CONFIG_FILE_PATH)

    audit = Audit(req_message="start policy handler")

    Config.discover(audit)
    ServiceActivator.determine_mode_of_operation(audit)
    logger.info(
        audit.info("starting policy_handler with config: {}".format(
            Config.discovered_config)))

    PolicyReceiver.run(audit)
    PolicyWeb.run_forever(audit)
def fix_deploy_handler_413(monkeypatch):
    """monkeyed failed discovery request.get"""
    def monkeyed_deploy_handler_put(uri, **kwargs):
        """monkeypatch for deploy_handler"""
        return MockHttpResponse(
            "put", uri,
            res_json={"server_instance_uuid": MockSettings.deploy_handler_instance_uuid},
            status_code=413, **kwargs
        )

    def monkeyed_deploy_handler_get(uri, **kwargs):
        """monkeypatch policy-update request.get to deploy_handler"""
        return MockHttpResponse("get", uri, res_json=MockDeploymentHandler.get_deployed_policies(),
                                **kwargs)

    _LOGGER.info("setup fix_deploy_handler_413")
    audit = None
    if DeployHandler._lazy_inited is False:
        audit = Audit(req_message="fix_deploy_handler_413")
        DeployHandler._lazy_init(audit)

    monkeypatch.setattr('policyhandler.deploy_handler.DeployHandler._requests_session.put',
                        monkeyed_deploy_handler_put)
    monkeypatch.setattr('policyhandler.deploy_handler.DeployHandler._requests_session.get',
                        monkeyed_deploy_handler_get)

    yield fix_deploy_handler_413
    if audit:
        audit.audit_done("teardown")
    _LOGGER.info("teardown fix_deploy_handler_413")
def test_healthcheck():
    """test /healthcheck"""
    audit = Audit(job_name="test_healthcheck", req_message="get /healthcheck")
    metrics = Metrics(aud_parent=audit, targetEntity="test_healthcheck")
    metrics.metrics_start("test /healthcheck")
    time.sleep(0.1)

    metrics.metrics("test /healthcheck")
    health = audit.health(full=True)
    audit.audit_done(result=json.dumps(health))

    _LOGGER.info("healthcheck: %s", json.dumps(health))
    assert bool(health)
Exemplo n.º 7
0
    def rediscover_config(updated_config=None):
        """rediscover the config"""
        if updated_config is not None:
            MockSettings.mock_config = copy.deepcopy(updated_config)

        audit = Audit(req_message="rediscover_config")

        Config.discover(audit)
        ServiceActivator.determine_mode_of_operation(audit)

        _LOGGER.info("testing policy_handler with config: %s", Config.discovered_config)

        audit.audit_done(" -- started")
Exemplo n.º 8
0
def test_get_policy_latest():
    """test /policy_latest/<policy-id>"""
    policy_id, expected_policy = MockPolicyEngine2018.gen_policy_latest(3)

    audit = Audit(job_name="test_get_policy_latest",
                  req_message="get /policy_latest/{}".format(policy_id or ""))

    policy_latest = pdp_client.PolicyRest.get_latest_policy((audit, policy_id, None, None)) or {}
    audit.audit_done(result=json.dumps(policy_latest))

    _LOGGER.info("expected_policy: %s", json.dumps(expected_policy))
    _LOGGER.info("policy_latest: %s", json.dumps(policy_latest))
    assert Utils.are_the_same(policy_latest, expected_policy)

    Tracker.validate()
Exemplo n.º 9
0
    def test_zzz_catch_up_on_deploy_handler_changed(self):
        """test run policy handler with deployment-handler changed underneath"""
        _LOGGER.info("start zzz_catch_up_on_deploy_handler_changed")
        assert not PolicyReceiver.is_running()
        audit = Audit(
            job_name="test_zzz_catch_up_on_deploy_handler_changed",
            req_message="start zzz_catch_up_on_deploy_handler_changed")
        PolicyReceiver.run(audit)

        _LOGGER.info("sleep before send_notification...")
        time.sleep(2)

        MockWebSocket.send_notification([1])
        _LOGGER.info("sleep after send_notification...")
        time.sleep(3)

        MockSettings.deploy_handler_instance_uuid = str(uuid.uuid4())
        _LOGGER.info("new deploy-handler uuid=%s",
                     MockSettings.deploy_handler_instance_uuid)

        MockWebSocket.send_notification([2, 4])
        _LOGGER.info("sleep after send_notification...")
        time.sleep(3)

        _LOGGER.info("sleep 3 before shutdown...")
        time.sleep(3)

        result = self.getPage("/healthcheck")
        _LOGGER.info("healthcheck result: %s", result)

        PolicyReceiver.shutdown(audit)
        time.sleep(1)
        assert not PolicyReceiver.is_running()

        Tracker.validate()
Exemplo n.º 10
0
    def test_zzz_policy_updates_and_catch_ups(self):
        """test run policy handler with policy updates and catchups"""
        _LOGGER.info("start policy_updates_and_catch_ups")
        assert not PolicyReceiver.is_running()

        audit = Audit(job_name="test_zzz_policy_updates_and_catch_ups",
                      req_message="start policy_updates_and_catch_ups")
        PolicyReceiver.run(audit)

        _LOGGER.info("sleep before send_notification...")
        time.sleep(2)

        MockWebSocket.send_notification([1, 3, 5])
        _LOGGER.info("sleep after send_notification...")
        time.sleep(3)

        _LOGGER.info("sleep 10 before shutdown...")
        time.sleep(10)

        result = self.getPage("/healthcheck")
        _LOGGER.info("healthcheck result: %s", result)

        PolicyReceiver.shutdown(audit)
        time.sleep(1)
        assert not PolicyReceiver.is_running()

        Tracker.validate()
Exemplo n.º 11
0
    def init_mock_config():
        """init configs"""
        if MockSettings._loaded:
            _LOGGER.info("testing policy_handler with config: %s", Config.discovered_config)
            return
        MockSettings._loaded = True

        _LOGGER.info("init MockSettings")

        MockSettings.reinit_mock_config()

        with open("tests/mock_config.json", 'r') as config_json:
            MockSettings.mock_config = json.load(config_json)

        sys.stdout = LogWriter(_LOGGER.info)
        sys.stderr = LogWriter(_LOGGER.error)

        print("print is expected to be in the log")
        _LOGGER.info("========== run_policy_handler ==========")
        Audit.init(Config.system_name, Config.LOGGER_CONFIG_FILE_PATH)
        MockSettings.rediscover_config()
Exemplo n.º 12
0
def test_catch_up_dh_404():
    """test run policy handler with catchups and failed deployment-handler"""
    _LOGGER.info("start test_catch_up_dh_404")
    assert not PolicyReceiver.is_running()
    audit = Audit(job_name="test_catch_up_dh_404",
                  req_message="start test_catch_up_dh_404")
    PolicyReceiver.run(audit)

    _LOGGER.info("sleep 12 before shutdown...")
    time.sleep(12)

    health = audit.health(full=True)
    audit.audit_done(result=json.dumps(health))

    _LOGGER.info("healthcheck: %s", json.dumps(health))
    assert bool(health)

    PolicyReceiver.shutdown(audit)
    time.sleep(1)
    assert not PolicyReceiver.is_running()

    health = audit.health(full=True)
    _LOGGER.info("healthcheck: %s", json.dumps(health))

    Tracker.validate()
    def test_zzzzz_shutdown(self):
        """test shutdown"""
        _LOGGER.info("testing the shutdown")
        assert not PolicyReceiver.is_running()
        audit = Audit(job_name="test_zzzzz_shutdown", req_message="testing the shutdown")
        PolicyReceiver.run(audit)

        result = self.getPage("/healthcheck")
        _LOGGER.info("healthcheck result: %s", result)

        time.sleep(1)

        WebServerTest.do_gc_test = False
        _LOGGER.info("shutdown...")
        audit.audit_done("shutdown")
        result = self.getPage("/shutdown")
        _LOGGER.info("shutdown result: %s", result)
        self.assertStatus('200 OK')
        _LOGGER.info("got shutdown: %s", self.body)
        time.sleep(5)
        assert not PolicyReceiver.is_running()

        Tracker.validate()
Exemplo n.º 14
0
    def test_zzz_get_catch_up(self):
        """test /catch_up"""
        _LOGGER.info("start /catch_up")
        assert not PolicyReceiver.is_running()
        audit = Audit(job_name="test_zzz_get_catch_up", req_message="start /catch_up")
        PolicyReceiver.run(audit)
        time.sleep(5)
        result = self.getPage("/catch_up")
        _LOGGER.info("catch_up result: %s", result)
        self.assertStatus('200 OK')
        _LOGGER.info("got catch_up: %s", self.body)

        _LOGGER.info("sleep 5 before shutdown...")
        time.sleep(5)

        result = self.getPage("/healthcheck")
        _LOGGER.info("healthcheck result: %s", result)

        PolicyReceiver.shutdown(audit)
        time.sleep(1)
        assert not PolicyReceiver.is_running()

        Tracker.validate()
def test_healthcheck_with_garbage():
    """test /healthcheck"""
    gc_found = gc.collect()
    gc.set_debug(gc.DEBUG_LEAK)

    node1 = Node("one")
    node2 = Node("two")
    node3 = Node("three")
    node1.next = node2
    node2.next = node3
    node3.next = node1
    node1 = node2 = node3 = None
    gc_found = gc.collect()

    audit = Audit(job_name="test_healthcheck_with_garbage",
                  req_message="get /test_healthcheck_with_garbage")
    health = audit.health(full=True)
    audit.audit_done(result=json.dumps(health))

    _LOGGER.info("test_healthcheck_with_garbage[%s]: %s", gc_found,
                 json.dumps(health))
    assert bool(health)
    assert bool(health.get("runtime", {}).get("gc", {}).get("gc_garbage"))

    _LOGGER.info("clearing up garbage...")
    for obj in gc.garbage:
        if isinstance(obj, Node):
            _LOGGER.info("in garbage: %s 0x%x", obj, id(obj))
            obj.next = None

    gc_found = gc.collect()
    _LOGGER.info("after clear test_healthcheck_with_garbage[%s]: %s", gc_found,
                 json.dumps(audit.health(full=True)))

    gc.set_debug(False)

    gc_found = gc.collect()
    _LOGGER.info(
        "after turned off gc debug test_healthcheck_with_garbage[%s]: %s",
        gc_found, json.dumps(audit.health(full=True)))
def test_healthcheck_with_error():
    """test /healthcheck"""
    audit = Audit(job_name="test_healthcheck_with_error",
                  req_message="get /healthcheck")
    metrics = Metrics(aud_parent=audit,
                      targetEntity="test_healthcheck_with_error")
    metrics.metrics_start("test /healthcheck")
    time.sleep(0.2)
    audit.error("error from test_healthcheck_with_error")
    audit.fatal("fatal from test_healthcheck_with_error")
    audit.debug("debug from test_healthcheck_with_error")
    audit.warn("debug from test_healthcheck_with_error")
    audit.info_requested("debug from test_healthcheck_with_error")
    if audit.is_success():
        audit.set_http_status_code(AuditHttpCode.DATA_NOT_FOUND_OK.value)
    audit.set_http_status_code(AuditHttpCode.SERVER_INTERNAL_ERROR.value)
    metrics.metrics("test /healthcheck")

    health = audit.health(full=True)
    audit.audit_done(result=json.dumps(health))

    _LOGGER.info("healthcheck: %s", json.dumps(health))
    assert bool(health)