55import dev .openfeature .contrib .providers .flagd .Config ;
66import dev .openfeature .contrib .providers .flagd .FlagdOptions ;
77import dev .openfeature .contrib .providers .flagd .FlagdProvider ;
8- import dev .openfeature .contrib .providers .flagd .e2e .FlagdContainer ;
8+ import dev .openfeature .contrib .providers .flagd .e2e .ContainerUtil ;
99import dev .openfeature .contrib .providers .flagd .e2e .State ;
1010import dev .openfeature .sdk .FeatureProvider ;
1111import dev .openfeature .sdk .OpenFeatureAPI ;
1212import io .cucumber .java .After ;
1313import io .cucumber .java .AfterAll ;
14- import io .cucumber .java .Before ;
1514import io .cucumber .java .BeforeAll ;
1615import io .cucumber .java .en .Given ;
1716import io .cucumber .java .en .When ;
2019import java .nio .file .Files ;
2120import java .nio .file .Path ;
2221import java .nio .file .Paths ;
22+ import java .time .Duration ;
2323import lombok .extern .slf4j .Slf4j ;
2424import org .apache .commons .lang3 .RandomStringUtils ;
25+ import org .apache .commons .lang3 .StringUtils ;
2526import org .junit .jupiter .api .parallel .Isolated ;
26- import org .testcontainers .containers .BindMode ;
27+ import org .testcontainers .containers .ComposeContainer ;
28+ import org .testcontainers .containers .wait .strategy .Wait ;
2729import org .testcontainers .shaded .org .apache .commons .io .FileUtils ;
2830
2931@ Isolated ()
3032@ Slf4j
3133public class ProviderSteps extends AbstractSteps {
3234
3335 public static final int UNAVAILABLE_PORT = 9999 ;
34- static FlagdContainer container ;
36+ static ComposeContainer container ;
3537
3638 static Path sharedTempDir ;
3739
@@ -43,8 +45,14 @@ public ProviderSteps(State state) {
4345 public static void beforeAll () throws IOException {
4446 sharedTempDir = Files .createDirectories (
4547 Paths .get ("tmp/" + RandomStringUtils .randomAlphanumeric (8 ).toLowerCase () + "/" ));
46- container = new FlagdContainer ()
47- .withFileSystemBind (sharedTempDir .toAbsolutePath ().toString (), "/flags" , BindMode .READ_WRITE );
48+ container = new ComposeContainer (new File ("test-harness/docker-compose.yaml" ))
49+ .withEnv ("FLAGS_DIR" , sharedTempDir .toAbsolutePath ().toString ())
50+ .withExposedService ("flagd" , 8013 , Wait .forListeningPort ())
51+ .withExposedService ("flagd" , 8015 , Wait .forListeningPort ())
52+ .withExposedService ("flagd" , 8080 , Wait .forListeningPort ())
53+ .withExposedService ("envoy" , 9211 , Wait .forListeningPort ())
54+ .withStartupTimeout (Duration .ofSeconds (45 ));
55+ container .start ();
4856 }
4957
5058 @ AfterAll
@@ -53,17 +61,10 @@ public static void afterAll() throws IOException {
5361 FileUtils .deleteDirectory (sharedTempDir .toFile ());
5462 }
5563
56- @ Before
57- public void before () {
58- if (!container .isRunning ()) {
59- container .start ();
60- }
61- }
62-
6364 @ After
6465 public void tearDown () {
6566 if (state .client != null ) {
66- when ().post ("http://" + container .getLaunchpadUrl () + "/stop" )
67+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/stop" )
6768 .then ()
6869 .statusCode (200 );
6970 }
@@ -100,7 +101,7 @@ public void setupProvider(String providerType) throws InterruptedException {
100101 String absolutePath = file .getAbsolutePath ();
101102 this .state .providerType = ProviderType .SSL ;
102103 state .builder
103- .port (container .getPort (State .resolverType ))
104+ .port (ContainerUtil .getPort (container , State .resolverType ))
104105 .tls (true )
105106 .certPath (absolutePath );
106107 flagdConfig = "ssl" ;
@@ -117,12 +118,12 @@ public void setupProvider(String providerType) throws InterruptedException {
117118 .port (UNAVAILABLE_PORT )
118119 .offlineFlagSourcePath (new File ("test-harness/flags/" + replace ).getAbsolutePath ());
119120 } else {
120- state .builder .port (container .getPort (State .resolverType ));
121+ state .builder .port (ContainerUtil .getPort (container , State .resolverType ));
121122 }
122123 break ;
123124 case "syncpayload" :
124125 flagdConfig = "sync-payload" ;
125- state .builder .port (container .getPort (State .resolverType ));
126+ state .builder .port (ContainerUtil .getPort (container , State .resolverType ));
126127 break ;
127128 case "stable" :
128129 this .state .providerType = ProviderType .DEFAULT ;
@@ -135,13 +136,22 @@ public void setupProvider(String providerType) throws InterruptedException {
135136 .toAbsolutePath ()
136137 .toString ());
137138 } else {
138- state .builder .port (container .getPort (State .resolverType ));
139+ state .builder .port (ContainerUtil .getPort (container , State .resolverType ));
139140 }
140141 break ;
141142 default :
142143 throw new IllegalStateException ();
143144 }
144- when ().post ("http://" + container .getLaunchpadUrl () + "/start?config={config}" , flagdConfig )
145+
146+ // Setting TargetUri if this setting is set
147+ FlagdOptions tempBuild = state .builder .build ();
148+ if (!StringUtils .isEmpty (tempBuild .getTargetUri ())) {
149+ String replace = tempBuild .getTargetUri ().replace ("<port>" , "" + container .getServicePort ("envoy" , 9211 ));
150+ state .builder .targetUri (replace );
151+ state .builder .port (UNAVAILABLE_PORT );
152+ }
153+
154+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/start?config={config}" , flagdConfig )
145155 .then ()
146156 .statusCode (200 );
147157
@@ -162,18 +172,22 @@ public void setupProvider(String providerType) throws InterruptedException {
162172
163173 @ When ("the connection is lost" )
164174 public void the_connection_is_lost () {
165- when ().post ("http://" + container .getLaunchpadUrl () + "/stop" ).then ().statusCode (200 );
175+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/stop" )
176+ .then ()
177+ .statusCode (200 );
166178 }
167179
168180 @ When ("the connection is lost for {int}s" )
169181 public void the_connection_is_lost_for (int seconds ) {
170- when ().post ("http://" + container .getLaunchpadUrl () + "/restart?seconds={seconds}" , seconds )
182+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/restart?seconds={seconds}" , seconds )
171183 .then ()
172184 .statusCode (200 );
173185 }
174186
175187 @ When ("the flag was modified" )
176188 public void the_flag_was_modded () {
177- when ().post ("http://" + container .getLaunchpadUrl () + "/change" ).then ().statusCode (200 );
189+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/change" )
190+ .then ()
191+ .statusCode (200 );
178192 }
179193}
0 commit comments