Repository navigation
feat(flagd-testcontainer): add flagd Testcontainers module #1993
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
Open
joe-chambers
wants to merge
7
commits into
open-feature:main
Choose a base branch
from
joe-chambers:feat/flagd-testcontainer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e3164e0
feat(flagd-testcontainer): add flagd Testcontainers module
joe-chambers f88cf04
fix(flagd-testcontainer): address review feedback
joe-chambers adc5a8b
fix(flagd-testcontainer): encode context entries the way flagd parses…
joe-chambers 03dee86
fix(flagd-testcontainer): reject context entries flagd re-reads lossily
joe-chambers b5a1362
docs(flagd-testcontainer): use the generated certificate path in the …
joe-chambers 27b23de
Merge remote-tracking branch 'upstream/main' into feat/flagd-testcont…
joe-chambers 1629179
docs(flagd-testcontainer): cover remote Docker hosts and create the c…
joe-chambers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| # flagd Testcontainer | ||
|
|
||
| A [Testcontainers](https://java.testcontainers.org/) module that runs [flagd](https://flagd.dev) in Docker for integration tests. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>dev.openfeature.contrib.tools</groupId> | ||
| <artifactId>flagd-testcontainer</artifactId> | ||
| <version>0.0.1</version> <!--x-release-please-version --> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Give the container at least one flag source, start it, and point your provider at the mapped ports. | ||
|
|
||
| ```java | ||
| try (FlagdContainer flagd = new FlagdContainer().withFlagsFromClasspath("flags.json")) { | ||
| flagd.start(); | ||
|
|
||
| FlagdProvider provider = new FlagdProvider(FlagdOptions.builder() | ||
| .host(flagd.getHost()) | ||
| .port(flagd.getEvaluationPort()) | ||
| .build()); | ||
| } | ||
| ``` | ||
|
|
||
| With JUnit 5 and `org.testcontainers:testcontainers-junit-jupiter`: | ||
|
|
||
| ```java | ||
| @Testcontainers | ||
| class MyTest { | ||
| @Container | ||
| static FlagdContainer flagd = new FlagdContainer().withFlagsFromClasspath("flags.json"); | ||
| } | ||
| ``` | ||
|
|
||
| ### Flag sources | ||
|
|
||
| | Method | Source | | ||
| | --- | --- | | ||
| | `withFlagsFromClasspath(String)` | a classpath resource | | ||
| | `withFlagsFromFile(Path)` | a file on the host | | ||
| | `withFlags(String)` | a flag definition JSON string | | ||
| | `withFlagSource(String)` | any flagd `--uri`, e.g. `https://...` or `grpc://...` | | ||
|
|
||
| Sources can be combined. File-based sources are copied into the container, not bind-mounted. | ||
|
|
||
| ### Changing flags at runtime | ||
|
|
||
| With exactly one file-based source, `updateFlags(String)` replaces the flag definition in the running container. | ||
| flagd picks the change up through its file watcher, so wait for it (for example with Awaitility) rather than asserting immediately. | ||
|
|
||
| ```java | ||
| flagd.updateFlags(newFlagsJson); | ||
| ``` | ||
|
|
||
| ### Ports | ||
|
|
||
| | Service | Container port | Accessor | | ||
| | --- | --- | --- | | ||
| | Evaluation (gRPC / Connect), for RPC providers | 8013 | `getEvaluationPort()` | | ||
| | Management (health, metrics) | 8014 | `getManagementPort()` | | ||
| | Sync, for in-process providers | 8015 | `getSyncPort()` | | ||
| | OFREP | 8016 | `getOfrepPort()`, `getOfrepEndpoint()` | | ||
|
|
||
| The container is ready once flagd's `/readyz` endpoint answers on the management port. | ||
|
|
||
| ### Connecting an application | ||
|
|
||
| `FlagdConnection` describes where flagd is reachable from, and how, so an application can be configured from it rather than from hard-coded ports. | ||
|
|
||
| | Accessor | Vantage point | Host and ports | | ||
| | --- | --- | --- | | ||
| | `getConnection()` | the test host (the test JVM, a process it launches) | `getHost()` and mapped ports | | ||
| | `getNetworkConnection()` | another container on the same Docker network | the last network alias and container ports | | ||
|
|
||
| `toEnv(FlagdResolver)` returns the environment variables the flagd provider reads: | ||
|
|
||
| - `FLAGD_RESOLVER`, `FLAGD_HOST` and `FLAGD_PORT`: the evaluation port for `RPC`, the sync port for `IN_PROCESS` | ||
| - `FLAGD_SYNC_PORT` for `IN_PROCESS`, which the provider prefers over `FLAGD_PORT` | ||
| - `FLAGD_TLS`, set to `true` or `false` so that it overrides any value already in the application's environment | ||
| - `FLAGD_SERVER_CERT_PATH` when TLS is enabled and the path is known | ||
| - `FLAGD_SOURCE_SELECTOR` when a selector is set | ||
|
|
||
| ```java | ||
| Network network = Network.newNetwork(); | ||
| FlagdContainer flagd = new FlagdContainer() | ||
| .withFlagsFromClasspath("flags.json") | ||
| .withNetwork(network) | ||
| .withNetworkAliases("flagd"); | ||
|
|
||
| GenericContainer<?> app = new GenericContainer<>("my-app:latest") | ||
| .withNetwork(network) | ||
| .withEnv(flagd.getNetworkConnection().toEnv(FlagdResolver.IN_PROCESS)) | ||
| .dependsOn(flagd); | ||
| ``` | ||
|
|
||
| `getEvaluationEndpoint()` and `getOfrepEndpoint()` return base URLs with the right scheme, and `getFlagSources()`, `isTlsEnabled()`, `getContextValues()` and `getContextFromHeaders()` report how the container was configured. | ||
|
|
||
| ### Selecting a flag source | ||
|
|
||
| With several sources, an in-process provider can sync just one of them. | ||
| `getSourceSelectors()` returns the selector of each source, in the order they were added. flagd names a file source by its path without `file:`, so these differ from `getFlagSources()`. | ||
|
|
||
| ```java | ||
| String selector = flagd.getSourceSelectors().get(1); | ||
| flagd.getConnection().withSourceSelector(selector).toEnv(FlagdResolver.IN_PROCESS); // adds FLAGD_SOURCE_SELECTOR | ||
| ``` | ||
|
|
||
| ### TLS | ||
|
|
||
| ```java | ||
| new FlagdContainer() | ||
| .withFlagsFromClasspath("flags.json") | ||
| .withTls(Paths.get("server-cert.pem"), Paths.get("server-key.pem")); | ||
| ``` | ||
|
|
||
| flagd serves the evaluation and sync services over TLS; the management and OFREP services stay on plain HTTP. | ||
| The certificate must be valid for the name clients use: `getHost()` from the test host, the network alias from other containers. | ||
| `getHost()` is usually `localhost`, but with a remote Docker host, as in some CI environments, it is that host's name or IP address. | ||
|
|
||
| From the test host, `getConnection()` sets `FLAGD_SERVER_CERT_PATH` to the certificate you passed in. | ||
| From another container, only you know where that container has the certificate, so add it: | ||
|
|
||
| ```java | ||
| flagd.getNetworkConnection().withServerCertPath("/certs/ca.pem").toEnv(FlagdResolver.RPC); | ||
| ``` | ||
|
|
||
| #### Creating a test certificate and key | ||
|
|
||
| A self-signed certificate is enough for tests. The client trusts the certificate itself, so no separate CA is needed. | ||
| List every name clients connect with as a subject alternative name. Hostname verification checks these, not the common name. | ||
|
|
||
| ```bash | ||
| mkdir -p src/test/resources/tls | ||
| openssl req -x509 -newkey rsa:2048 -nodes -days 36500 \ | ||
| -keyout src/test/resources/tls/server-key.pem \ | ||
| -out src/test/resources/tls/server-cert.pem \ | ||
| -subj "/CN=flagd-test" \ | ||
| -addext "subjectAltName=DNS:localhost,DNS:flagd,IP:127.0.0.1" | ||
| ``` | ||
|
|
||
| - `-nodes` leaves the key unencrypted, which flagd requires. | ||
| - `DNS:flagd` must match the alias given to `withNetworkAliases`; add one entry per alias. | ||
| - If `getHost()` is not `localhost` in your environment, add its value too, as `DNS:` for a name or `IP:` for an address. | ||
| - Requires OpenSSL 1.1.1 or later for `-addext`. Run `openssl x509 -in src/test/resources/tls/server-cert.pem -noout -ext subjectAltName` to check the names. | ||
| - The long validity keeps committed test fixtures from expiring. Use this key for tests only. | ||
|
|
||
| Then point flagd and the provider at the files: | ||
|
|
||
| ```java | ||
| Path cert = Paths.get("src/test/resources/tls/server-cert.pem"); | ||
| Path key = Paths.get("src/test/resources/tls/server-key.pem"); | ||
|
|
||
| FlagdContainer flagd = new FlagdContainer() | ||
| .withFlagsFromClasspath("flags.json") | ||
| .withTls(cert, key); | ||
| flagd.start(); | ||
|
|
||
| FlagdProvider provider = new FlagdProvider(FlagdOptions.builder() | ||
| .host(flagd.getHost()) | ||
| .port(flagd.getEvaluationPort()) | ||
| .tls(true) | ||
| .certPath(cert.toAbsolutePath().toString()) | ||
| .build()); | ||
| ``` | ||
|
|
||
| ### Evaluation context | ||
|
|
||
| ```java | ||
| new FlagdContainer() | ||
| .withFlagsFromClasspath("flags.json") | ||
| .withContextValue("env", "test") // --context-value, added to every evaluation | ||
| .withContextFromHeader("X-Tier", "tier"); // --context-from-header, request header to context key | ||
| ``` | ||
|
|
||
| Values are encoded so that flagd receives them unchanged, including commas, quotes and line breaks. | ||
| flagd cannot take some entries, so these are rejected with an `IllegalArgumentException`: | ||
|
|
||
| - a key or header that contains `=` or starts with `[` | ||
| - a value that ends with whitespace or `]` | ||
| - a value with no `=` in it whose key starts with `"`, or which itself ends with `"` | ||
| - an entry containing a CRLF line break | ||
|
|
||
| ### Other flagd options | ||
|
|
||
| `withFlagdArgs(String...)` appends raw arguments to `flagd start`, for options without a dedicated method: | ||
|
|
||
| ```java | ||
| new FlagdContainer().withFlagsFromClasspath("flags.json").withFlagdArgs("--stream-deadline", "30s"); | ||
| ``` | ||
|
|
||
| Don't use it to change the service ports, because the accessors assume flagd's defaults. | ||
|
|
||
| ### Image | ||
|
|
||
| The default image is `ghcr.io/open-feature/flagd` at the tag in `FlagdContainer.DEFAULT_TAG`. | ||
| Pass another tag, or a compatible image, to the constructor: | ||
|
|
||
| ```java | ||
| new FlagdContainer("ghcr.io/open-feature/flagd:v0.18.1"); | ||
| new FlagdContainer(DockerImageName.parse("my-registry/flagd:dev").asCompatibleSubstituteFor("ghcr.io/open-feature/flagd")); | ||
| ``` | ||
|
|
||
| ## Development | ||
|
|
||
| Tests start real containers, so a running Docker daemon is required. | ||
|
|
||
| ```bash | ||
| mvn --projects tools/flagd-testcontainer clean verify | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>dev.openfeature.contrib</groupId> | ||
| <artifactId>parent</artifactId> | ||
| <version>[1.0,2.0)</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
| <groupId>dev.openfeature.contrib.tools</groupId> | ||
| <artifactId>flagd-testcontainer</artifactId> | ||
| <version>0.0.1</version> <!--x-release-please-version --> | ||
|
|
||
| <properties> | ||
| <module-name>${groupId}.flagdtestcontainer</module-name> | ||
| <testcontainers.version>2.0.5</testcontainers.version> | ||
| </properties> | ||
|
|
||
| <name>flagd-testcontainer</name> | ||
| <description>Testcontainers module for running flagd in integration tests</description> | ||
| <url>https://openfeature.dev</url> | ||
|
|
||
| <developers> | ||
| <developer> | ||
| <id>joe-chambers</id> | ||
| <name>Joe Chambers</name> | ||
| <organization>OpenFeature</organization> | ||
| <url>https://openfeature.dev/</url> | ||
| </developer> | ||
| </developers> | ||
|
|
||
| <dependencies> | ||
| <!-- compile scope: consumed as a test library alongside the adopter's own testcontainers setup --> | ||
| <dependency> | ||
| <groupId>org.testcontainers</groupId> | ||
| <artifactId>testcontainers</artifactId> | ||
| <version>${testcontainers.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.