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 default :
@@ -131,11 +131,20 @@ public void setupProvider(String providerType) throws InterruptedException {
131131 .toAbsolutePath ()
132132 .toString ());
133133 } else {
134- state .builder .port (container .getPort (State .resolverType ));
134+ state .builder .port (ContainerUtil .getPort (container , State .resolverType ));
135135 }
136136 break ;
137137 }
138- when ().post ("http://" + container .getLaunchpadUrl () + "/start?config={config}" , flagdConfig )
138+
139+ // Setting TargetUri if this setting is set
140+ FlagdOptions tempBuild = state .builder .build ();
141+ if (!StringUtils .isEmpty (tempBuild .getTargetUri ())) {
142+ String replace = tempBuild .getTargetUri ().replace ("<port>" , "" + container .getServicePort ("envoy" , 9211 ));
143+ state .builder .targetUri (replace );
144+ state .builder .port (UNAVAILABLE_PORT );
145+ }
146+
147+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/start?config={config}" , flagdConfig )
139148 .then ()
140149 .statusCode (200 );
141150
@@ -156,18 +165,22 @@ public void setupProvider(String providerType) throws InterruptedException {
156165
157166 @ When ("the connection is lost" )
158167 public void the_connection_is_lost () {
159- when ().post ("http://" + container .getLaunchpadUrl () + "/stop" ).then ().statusCode (200 );
168+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/stop" )
169+ .then ()
170+ .statusCode (200 );
160171 }
161172
162173 @ When ("the connection is lost for {int}s" )
163174 public void the_connection_is_lost_for (int seconds ) {
164- when ().post ("http://" + container .getLaunchpadUrl () + "/restart?seconds={seconds}" , seconds )
175+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/restart?seconds={seconds}" , seconds )
165176 .then ()
166177 .statusCode (200 );
167178 }
168179
169180 @ When ("the flag was modified" )
170181 public void the_flag_was_modded () {
171- when ().post ("http://" + container .getLaunchpadUrl () + "/change" ).then ().statusCode (200 );
182+ when ().post ("http://" + ContainerUtil .getLaunchpadUrl (container ) + "/change" )
183+ .then ()
184+ .statusCode (200 );
172185 }
173186}
0 commit comments