Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(flagd): migrate file to own provider type #1173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class Config {

static final String RESOLVER_RPC = "rpc";
static final String RESOLVER_IN_PROCESS = "in-process";
static final String RESOLVER_FILE = "file";

public static final String STATIC_REASON = "STATIC";
public static final String CACHED_REASON = "CACHED";
Expand Down Expand Up @@ -87,6 +88,8 @@ static Resolver fromValueProvider(Function<String, String> provider) {
return Resolver.IN_PROCESS;
case "rpc":
return Resolver.RPC;
case "file":
return Resolver.FILE;
default:
log.warn("Unsupported resolver variable: {}", resolverVar);
return DEFAULT_RESOLVER_TYPE;
Expand Down Expand Up @@ -143,6 +146,11 @@ public String asString() {
public String asString() {
return RESOLVER_IN_PROCESS;
}
},
FILE {
public String asString() {
return RESOLVER_FILE;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.function.Function;
import lombok.Builder;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
* FlagdOptions is a builder to build flagd provider options.
Expand Down Expand Up @@ -119,8 +120,7 @@ public class FlagdOptions {
* File source of flags to be used by offline mode.
* Setting this enables the offline mode of the in-process provider.
*/
@Builder.Default
private String offlineFlagSourcePath = fallBackToEnvOrDefault(Config.OFFLINE_SOURCE_PATH, null);
private String offlineFlagSourcePath;

/**
* gRPC custom target string.
Expand Down Expand Up @@ -193,7 +193,20 @@ void prebuild() {
resolverType = fromValueProvider(System::getenv);
}

if (port == 0) {
if (StringUtils.isEmpty(offlineFlagSourcePath)) {
offlineFlagSourcePath = fallBackToEnvOrDefault(Config.OFFLINE_SOURCE_PATH, null);
}

if (!StringUtils.isEmpty(offlineFlagSourcePath) && resolverType == Config.Resolver.IN_PROCESS) {
resolverType = Config.Resolver.FILE;
}

// We need a file path for FILE Provider
if (StringUtils.isEmpty(offlineFlagSourcePath) && resolverType == Config.Resolver.FILE) {
throw new IllegalArgumentException("Resolver Type 'FILE' requires a offlineFlagSourcePath");
}

if (port == 0 && resolverType != Config.Resolver.FILE) {
port = Integer.parseInt(
fallBackToEnvOrDefault(Config.PORT_ENV_VAR_NAME, determineDefaultPortForResolver()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public FlagdProvider() {
*/
public FlagdProvider(final FlagdOptions options) {
switch (options.getResolverType().asString()) {
case Config.RESOLVER_FILE:
case Config.RESOLVER_IN_PROCESS:
this.flagResolver = new InProcessResolver(options, this::onProviderEvent);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ void TestBuilderOptions() {
.cacheType("lru")
.maxCacheSize(100)
.selector("app=weatherApp")
.offlineFlagSourcePath("some-path")
.openTelemetry(openTelemetry)
.customConnector(connector)
.resolverType(Resolver.IN_PROCESS)
Expand All @@ -76,7 +75,6 @@ void TestBuilderOptions() {
assertEquals("lru", flagdOptions.getCacheType());
assertEquals(100, flagdOptions.getMaxCacheSize());
assertEquals("app=weatherApp", flagdOptions.getSelector());
assertEquals("some-path", flagdOptions.getOfflineFlagSourcePath());
assertEquals(openTelemetry, flagdOptions.getOpenTelemetry());
assertEquals(connector, flagdOptions.getCustomConnector());
assertEquals(Resolver.IN_PROCESS, flagdOptions.getResolverType());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.openfeature.contrib.providers.flagd.e2e;
package dev.openfeature.contrib.providers.flagd;

import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.openfeature.contrib.providers.flagd.e2e;

import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
import static io.cucumber.junit.platform.engine.Constants.OBJECT_FACTORY_PROPERTY_NAME;
import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;

import dev.openfeature.contrib.providers.flagd.Config;
import org.apache.logging.log4j.core.config.Order;
import org.junit.platform.suite.api.BeforeSuite;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.ExcludeTags;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectDirectories;
import org.junit.platform.suite.api.Suite;
import org.testcontainers.junit.jupiter.Testcontainers;

/**
* Class for running the reconnection tests for the RPC provider
*/
@Order(value = Integer.MAX_VALUE)
@Suite
@IncludeEngines("cucumber")
@SelectDirectories("test-harness/gherkin")
// if you want to run just one feature file, use the following line instead of @SelectDirectories
// @SelectFile("test-harness/gherkin/connection.feature")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "dev.openfeature.contrib.providers.flagd.e2e.steps")
@ConfigurationParameter(key = OBJECT_FACTORY_PROPERTY_NAME, value = "io.cucumber.picocontainer.PicoFactory")
@IncludeTags("file")
@ExcludeTags({"unixsocket", "targetURI", "reconnect", "customCert", "events"})
@Testcontainers
public class RunFileTest {

@BeforeSuite
public static void before() {
State.resolverType = Config.Resolver.FILE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public class State {
public FlagdOptions options;
public FlagdOptions.FlagdOptionsBuilder builder = FlagdOptions.builder();
public static Config.Resolver resolverType;
public boolean hasError;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import dev.openfeature.contrib.providers.flagd.e2e.State;

abstract class AbstractSteps {
State state;
public abstract class AbstractSteps {
protected State state;

public AbstractSteps(State state) {
protected AbstractSteps(State state) {
this.state = state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public ConfigSteps(State state) {

@When("a config was initialized")
public void we_initialize_a_config() {
state.options = state.builder.build();
try {
state.options = state.builder.build();
} catch (IllegalArgumentException e) {
state.options = null;
state.hasError = true;
}
}

@When("a config was initialized for {string}")
Expand Down Expand Up @@ -87,19 +92,25 @@ public void the_option_of_type_should_have_the_value(String option, String type,
}

option = mapOptionNames(option);

assertThat(state.options).hasFieldOrPropertyWithValue(option, convert);

// Resetting env vars
for (Map.Entry<String, String> envVar : envVarsSet.entrySet()) {
if (envVar.getValue() == null) {
EnvironmentVariableUtils.clear(envVar.getKey());
} else {
EnvironmentVariableUtils.set(envVar.getKey(), envVar.getValue());
try {
assertThat(state.options).hasFieldOrPropertyWithValue(option, convert);
} finally {
// Resetting env vars
for (Map.Entry<String, String> envVar : envVarsSet.entrySet()) {
if (envVar.getValue() == null) {
EnvironmentVariableUtils.clear(envVar.getKey());
} else {
EnvironmentVariableUtils.set(envVar.getKey(), envVar.getValue());
}
}
}
}

@Then("we should have an error")
public void we_should_have_an_error() {
assertThat(state.hasError).isTrue();
}

private static String mapOptionNames(String option) {
Map<String, String> propertyMapper = new HashMap<>();
propertyMapper.put("resolver", "resolverType");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* This class modifies the internals of the environment variables map with reflection. Warning: If
* your {@link SecurityManager} does not allow modifications, it fails.
*/
class EnvironmentVariableUtils {
public class EnvironmentVariableUtils {

private EnvironmentVariableUtils() {
// private constructor to prevent instantiation of utility class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public int getPort(Config.Resolver resolver, ProviderType providerType) {
case SSL:
return toxiproxy.getMappedPort(8669);
}
case FILE:
return 0;
default:
throw new IllegalArgumentException("Unsupported resolver: " + resolver);
}
Expand All @@ -143,10 +145,23 @@ public int getPort(Config.Resolver resolver, ProviderType providerType) {
public void setupProvider(String providerType) throws IOException {
state.builder.deadline(500).keepAlive(0).retryGracePeriod(3);
boolean wait = true;
File flags = new File("test-harness/flags");
ObjectMapper objectMapper = new ObjectMapper();
Object merged = new Object();
for (File listFile : Objects.requireNonNull(flags.listFiles())) {
ObjectReader updater = objectMapper.readerForUpdating(merged);
merged = updater.readValue(listFile, Object.class);
}
Path offlinePath = Files.createTempFile("flags", ".json");
objectMapper.writeValue(offlinePath.toFile(), merged);
switch (providerType) {
case "unavailable":
this.state.providerType = ProviderType.SOCKET;
state.builder.port(UNAVAILABLE_PORT);
if (State.resolverType == Config.Resolver.FILE) {

state.builder.offlineFlagSourcePath("not-existing");
}
wait = false;
break;
case "socket":
Expand All @@ -167,25 +182,17 @@ public void setupProvider(String providerType) throws IOException {
.tls(true)
.certPath(absolutePath);
break;
case "offline":
File flags = new File("test-harness/flags");
ObjectMapper objectMapper = new ObjectMapper();
Object merged = new Object();
for (File listFile : Objects.requireNonNull(flags.listFiles())) {
ObjectReader updater = objectMapper.readerForUpdating(merged);
merged = updater.readValue(listFile, Object.class);
}
Path offlinePath = Files.createTempFile("flags", ".json");
objectMapper.writeValue(offlinePath.toFile(), merged);

state.builder
.port(UNAVAILABLE_PORT)
.offlineFlagSourcePath(offlinePath.toAbsolutePath().toString());
break;

default:
this.state.providerType = ProviderType.DEFAULT;
state.builder.port(getPort(State.resolverType, state.providerType));
if (State.resolverType == Config.Resolver.FILE) {

state.builder
.port(UNAVAILABLE_PORT)
.offlineFlagSourcePath(offlinePath.toAbsolutePath().toString());
} else {
state.builder.port(getPort(State.resolverType, state.providerType));
}
break;
}
FeatureProvider provider =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static Object convert(String value, String type) throws ClassNotFoundExce
return Config.Resolver.IN_PROCESS;
case "rpc":
return Config.Resolver.RPC;
case "file":
return Config.Resolver.FILE;
default:
throw new RuntimeException("Unknown resolver type: " + value);
}
Expand Down
2 changes: 1 addition & 1 deletion providers/flagd/test-harness
Loading