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,13 @@ 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+ .withExposedService ("flagd" , 8013 , Wait .forListeningPort ())
50+ .withExposedService ("flagd" , 8015 , Wait .forListeningPort ())
51+ .withExposedService ("flagd" , 8080 , Wait .forListeningPort ())
52+ .withExposedService ("envoy" , 9211 , Wait .forListeningPort ())
53+ .withStartupTimeout (Duration .ofSeconds (45 ));
54+ container .start ();
4855 }
4956
5057 @ AfterAll
@@ -53,17 +60,10 @@ public static void afterAll() throws IOException {
5360 FileUtils .deleteDirectory (sharedTempDir .toFile ());
5461 }
5562
56- @ Before
57- public void before () {
58- if (!container .isRunning ()) {
59- container .start ();
60- }
61- }
62-
6363 @ After
6464 public void tearDown () {
6565 if (state .client != null ) {
66- when ().post ("http://" + container .getLaunchpadUrl () + "/stop" )
66+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/stop" )
6767 .then ()
6868 .statusCode (200 );
6969 }
@@ -100,7 +100,7 @@ public void setupProvider(String providerType) throws InterruptedException {
100100 String absolutePath = file .getAbsolutePath ();
101101 this .state .providerType = ProviderType .SSL ;
102102 state .builder
103- .port (container .getPort (State .resolverType ))
103+ .port (ContainerUtil .getPort (container , State .resolverType ))
104104 .tls (true )
105105 .certPath (absolutePath );
106106 flagdConfig = "ssl" ;
@@ -117,7 +117,7 @@ public void setupProvider(String providerType) throws InterruptedException {
117117 .port (UNAVAILABLE_PORT )
118118 .offlineFlagSourcePath (new File ("test-harness/flags/" + replace ).getAbsolutePath ());
119119 } else {
120- state .builder .port (container .getPort (State .resolverType ));
120+ state .builder .port (ContainerUtil .getPort (container , State .resolverType ));
121121 }
122122 break ;
123123 case "syncpayload" :
@@ -135,13 +135,22 @@ public void setupProvider(String providerType) throws InterruptedException {
135135 .toAbsolutePath ()
136136 .toString ());
137137 } else {
138- state .builder .port (container .getPort (State .resolverType ));
138+ state .builder .port (ContainerUtil .getPort (container , State .resolverType ));
139139 }
140140 break ;
141141 default :
142142 throw new IllegalStateException ();
143143 }
144- when ().post ("http://" + container .getLaunchpadUrl () + "/start?config={config}" , flagdConfig )
144+
145+ // Setting TargetUri if this setting is set
146+ FlagdOptions tempBuild = state .builder .build ();
147+ if (!StringUtils .isEmpty (tempBuild .getTargetUri ())) {
148+ String replace = tempBuild .getTargetUri ().replace ("<port>" , "" + container .getServicePort ("envoy" , 9211 ));
149+ state .builder .targetUri (replace );
150+ state .builder .port (UNAVAILABLE_PORT );
151+ }
152+
153+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/start?config={config}" , flagdConfig )
145154 .then ()
146155 .statusCode (200 );
147156
@@ -162,18 +171,22 @@ public void setupProvider(String providerType) throws InterruptedException {
162171
163172 @ When ("the connection is lost" )
164173 public void the_connection_is_lost () {
165- when ().post ("http://" + container .getLaunchpadUrl () + "/stop" ).then ().statusCode (200 );
174+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/stop" )
175+ .then ()
176+ .statusCode (200 );
166177 }
167178
168179 @ When ("the connection is lost for {int}s" )
169180 public void the_connection_is_lost_for (int seconds ) {
170- when ().post ("http://" + container .getLaunchpadUrl () + "/restart?seconds={seconds}" , seconds )
181+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/restart?seconds={seconds}" , seconds )
171182 .then ()
172183 .statusCode (200 );
173184 }
174185
175186 @ When ("the flag was modified" )
176187 public void the_flag_was_modded () {
177- when ().post ("http://" + container .getLaunchpadUrl () + "/change" ).then ().statusCode (200 );
188+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/change" )
189+ .then ()
190+ .statusCode (200 );
178191 }
179192}
0 commit comments