コード例 #1
0
def fetch_and_load_features(url: str,
                            app_name: str,
                            instance_id: str,
                            custom_headers: dict,
                            custom_options: dict,
                            cache: BaseCache,
                            features: dict,
                            strategy_mapping: dict,
                            project: str = None) -> None:
    (feature_provisioning, etag) = get_feature_toggles(
        url,
        app_name,
        instance_id,
        custom_headers,
        custom_options,
        project,
        cache.get(ETAG)
    )

    if feature_provisioning:
        cache.set(FEATURES_URL, feature_provisioning)
    else:
        LOGGER.debug("No feature provisioning returned from server, using cached provisioning.")

    if etag:
        cache.set(ETAG, etag)

    load_features(cache, features, strategy_mapping)
コード例 #2
0
def test_get_feature_toggle_failure():
    responses.add(responses.GET, FULL_FEATURE_URL, json={}, status=500)

    result = get_feature_toggles(URL,
                                 APP_NAME,
                                 INSTANCE_ID,
                                 CUSTOM_HEADERS)

    assert len(responses.calls) == 1
    assert not result
コード例 #3
0
def test_get_feature_toggle_success():
    responses.add(responses.GET, FULL_FEATURE_URL, json=MOCK_FEATURE_RESPONSE, status=200)

    result = get_feature_toggles(URL,
                                 APP_NAME,
                                 INSTANCE_ID,
                                 CUSTOM_HEADERS)

    assert len(responses.calls) == 1
    assert result["version"] == 1
コード例 #4
0
def test_get_feature_toggle_project():
    responses.add(responses.GET,
                  PROJECT_URL,
                  json=MOCK_FEATURE_RESPONSE_PROJECT,
                  status=200)

    result = get_feature_toggles(URL, APP_NAME, INSTANCE_ID, CUSTOM_HEADERS,
                                 CUSTOM_OPTIONS, PROJECT_NAME)

    assert len(responses.calls) == 1
    assert len(result["features"]) == 1
コード例 #5
0
def test_get_feature_toggle(response, status, expected):
    responses.add(responses.GET,
                  FULL_FEATURE_URL,
                  json=response,
                  status=status)

    result = get_feature_toggles(URL, APP_NAME, INSTANCE_ID, CUSTOM_HEADERS,
                                 CUSTOM_OPTIONS)

    assert len(responses.calls) == 1
    assert expected(result)
コード例 #6
0
def fetch_and_load_features(url, app_name, instance_id, custom_headers, cache,
                            features, strategy_mapping):
    feature_provisioning = get_feature_toggles(url, app_name, instance_id,
                                               custom_headers)

    if feature_provisioning:
        cache[FEATURES_URL] = feature_provisioning
        cache.sync()
    else:
        LOGGER.info("Unable to get feature flag toggles, using cached values.")

    load_features(cache, features, strategy_mapping)
コード例 #7
0
def fetch_and_load_features(url: str, app_name: str, instance_id: str,
                            custom_headers: dict, cache: FileCache,
                            strategies: dict) -> None:
    feature_provisioning = get_feature_toggles(url, app_name, instance_id,
                                               custom_headers)

    if feature_provisioning:
        cache[FEATURES_URL] = feature_provisioning
        cache.sync()
    else:
        LOGGER.info("Unable to get feature flag toggles, using cached values.")

    load_features(cache, strategies)
コード例 #8
0
def test_get_feature_toggle_failed_etag():
    responses.add(responses.GET,
                  PROJECT_URL,
                  json={},
                  status=500,
                  headers={'etag': ETAG_VALUE})

    (result, etag) = get_feature_toggles(URL, APP_NAME, INSTANCE_ID,
                                         CUSTOM_HEADERS, CUSTOM_OPTIONS,
                                         PROJECT_NAME)

    assert len(responses.calls) == 1
    assert etag == ''
コード例 #9
0
def test_get_feature_toggle_project():
    responses.add(responses.GET,
                  PROJECT_URL,
                  json=MOCK_FEATURE_RESPONSE_PROJECT,
                  status=200,
                  headers={'etag': ETAG_VALUE})

    (result, etag) = get_feature_toggles(URL, APP_NAME, INSTANCE_ID,
                                         CUSTOM_HEADERS, CUSTOM_OPTIONS,
                                         PROJECT_NAME)

    assert len(responses.calls) == 1
    assert len(result["features"]) == 1
    assert etag == ETAG_VALUE
コード例 #10
0
def test_get_feature_toggle_etag_present():
    responses.add(responses.GET,
                  PROJECT_URL,
                  json={},
                  status=304,
                  headers={'etag': ETAG_VALUE})

    (result, etag) = get_feature_toggles(URL, APP_NAME, INSTANCE_ID,
                                         CUSTOM_HEADERS, CUSTOM_OPTIONS,
                                         PROJECT_NAME, ETAG_VALUE)

    assert len(responses.calls) == 1
    assert not result
    assert responses.calls[0].request.headers['If-None-Match'] == ETAG_VALUE
    assert etag == ETAG_VALUE
コード例 #11
0
def fetch_and_load_features(url: str,
                            app_name: str,
                            instance_id: str,
                            custom_headers: dict,
                            custom_options: dict,
                            cache: FileCache,
                            features: dict,
                            strategy_mapping: dict) -> None:
    feature_provisioning = get_feature_toggles(url, app_name, instance_id, custom_headers, custom_options)

    if feature_provisioning:
        cache[FEATURES_URL] = feature_provisioning
        cache.sync()
    else:
        LOGGER.warning("Unable to get feature flag toggles, using cached provisioning.")

    load_features(cache, features, strategy_mapping)
コード例 #12
0
def fetch_and_load_features(url: str, app_name: str, instance_id: str,
                            custom_headers: dict, custom_options: dict,
                            cache: redis.Redis, features: dict,
                            strategy_mapping: dict) -> None:
    feature_provisioning = get_feature_toggles(url, app_name, instance_id,
                                               custom_headers, custom_options)

    if feature_provisioning:
        # Sample data we're writing into cache
        # {
        #   "features": [{
        #     "name": "haptik.development.enable_smart_skills",
        #     "description": "Feature to enable smart skills on dev servers",
        #     "type": "release",
        #     "project": "default",
        #     "enabled": true,
        #     "stale": false,
        #     "strategies": [
        #         {
        #         "name": "EnableForPartners",
        #         "parameters": {
        #             "partner_names": "Platform Demo,haptik,demo,aksc"
        #         }
        #         }
        #     ],
        #     "variants": [],
        #     "createdAt": "2021-03-08T09:14:41.828Z"
        #   }]
        # }
        features = feature_provisioning.get('features', [])
        if not features:
            LOGGER.warning("Features are empty")
        cache.set(FEATURES_URL, pickle.dumps(features))
    else:
        LOGGER.warning(
            "Unable to get feature flag toggles, using cached provisioning.")

    load_features(cache, features, strategy_mapping)