1
1
package dev .openfeature .contrib .providers .flagd .e2e .steps ;
2
2
3
+ import dev .openfeature .contrib .providers .flagd .Config ;
3
4
import dev .openfeature .contrib .providers .flagd .FlagdProvider ;
4
5
import dev .openfeature .contrib .providers .flagd .e2e .FlagdContainer ;
5
6
import dev .openfeature .contrib .providers .flagd .e2e .State ;
6
- import dev .openfeature .sdk .Client ;
7
7
import dev .openfeature .sdk .FeatureProvider ;
8
8
import dev .openfeature .sdk .OpenFeatureAPI ;
9
+ import eu .rekawek .toxiproxy .Proxy ;
10
+ import eu .rekawek .toxiproxy .ToxiproxyClient ;
11
+ import eu .rekawek .toxiproxy .model .ToxicDirection ;
12
+ import eu .rekawek .toxiproxy .model .toxic .Timeout ;
9
13
import io .cucumber .java .After ;
10
14
import io .cucumber .java .AfterAll ;
11
15
import io .cucumber .java .Before ;
17
21
import org .slf4j .Logger ;
18
22
import org .slf4j .LoggerFactory ;
19
23
import org .testcontainers .containers .BindMode ;
24
+ import org .testcontainers .containers .Network ;
25
+ import org .testcontainers .containers .ToxiproxyContainer ;
20
26
import org .testcontainers .shaded .org .apache .commons .io .FileUtils ;
21
27
22
28
import java .io .File ;
@@ -36,6 +42,11 @@ public class ProviderSteps extends AbstractSteps {
36
42
37
43
public static final int UNAVAILABLE_PORT = 9999 ;
38
44
static Map <ProviderType , FlagdContainer > containers = new HashMap <>();
45
+ static Map <ProviderType , Map <Config .Resolver , String >> proxyports = new HashMap <>();
46
+ public static Network network = Network .newNetwork ();
47
+ public static ToxiproxyContainer toxiproxy = new ToxiproxyContainer ("ghcr.io/shopify/toxiproxy:2.5.0" )
48
+ .withNetwork (network ).withCreateContainerCmdModifier ((cmd -> cmd .withName ("toxiproxy" )));
49
+ public static ToxiproxyClient toxiproxyClient ;
39
50
40
51
static Path sharedTempDir ;
41
52
@@ -52,13 +63,32 @@ public ProviderSteps(State state) {
52
63
super (state );
53
64
}
54
65
66
+ static String generateProxyName (Config .Resolver resolver , ProviderType providerType ) {
67
+ return providerType + "-" + resolver ;
68
+ }
69
+
55
70
@ BeforeAll
56
71
public static void beforeAll () throws IOException {
57
- containers .put (ProviderType .DEFAULT , new FlagdContainer ());
58
- containers .put (ProviderType .SSL , new FlagdContainer ("ssl" ));
72
+ toxiproxy .start ();
73
+ toxiproxyClient = new ToxiproxyClient (toxiproxy .getHost (), toxiproxy .getControlPort ());
74
+ toxiproxyClient .createProxy (
75
+ generateProxyName (Config .Resolver .RPC , ProviderType .DEFAULT ),
76
+ "0.0.0.0:8666" , "default:8013" );
77
+
78
+ toxiproxyClient .createProxy (generateProxyName (Config .Resolver .IN_PROCESS , ProviderType .DEFAULT ), "0.0.0.0:8667" , "default:8015" );
79
+ toxiproxyClient .createProxy (generateProxyName (Config .Resolver .RPC , ProviderType .SSL ), "0.0.0.0:8668" , "ssl:8013" );
80
+ toxiproxyClient .createProxy (generateProxyName (Config .Resolver .IN_PROCESS , ProviderType .SSL ), "0.0.0.0:8669" , "ssl:8015" );
81
+
82
+ containers .put (ProviderType .DEFAULT ,
83
+ new FlagdContainer ().withNetwork (network ).withNetworkAliases ("default" )
84
+ );
85
+ containers .put (ProviderType .SSL ,
86
+ new FlagdContainer ("ssl" ).withNetwork (network ).withNetworkAliases ("ssl" )
87
+ );
59
88
containers .put (ProviderType .SOCKET , new FlagdContainer ("socket" )
60
89
.withFileSystemBind (sharedTempDir .toAbsolutePath ().toString (), "/tmp" , BindMode .READ_WRITE ));
61
90
91
+
62
92
}
63
93
64
94
@ AfterAll
@@ -69,16 +99,19 @@ public static void afterAll() throws IOException {
69
99
}
70
100
71
101
@ Before
72
- public void before () {
102
+ public void before () throws IOException {
103
+
73
104
containers .values ().stream ().filter (containers -> !containers .isRunning ())
74
105
.forEach (FlagdContainer ::start );
75
106
}
107
+
76
108
@ After
77
109
public void tearDown () {
78
110
OpenFeatureAPI .getInstance ().shutdown ();
79
111
}
80
112
81
113
114
+
82
115
@ Given ("a {} flagd provider" )
83
116
public void setupProvider (String providerType ) {
84
117
state .builder
@@ -106,14 +139,14 @@ public void setupProvider(String providerType) {
106
139
this .state .providerType = ProviderType .SSL ;
107
140
state
108
141
.builder
109
- .port (getContainer ().getPort (State .resolverType ))
142
+ .port (getContainer (state . providerType ).getPort (State .resolverType ))
110
143
.tls (true )
111
144
.certPath (absolutePath );
112
145
break ;
113
146
114
147
default :
115
148
this .state .providerType = ProviderType .DEFAULT ;
116
- state .builder .port (getContainer (). getPort ( State . resolverType ));
149
+ state .builder .port (toxiproxy . getMappedPort ( 8666 ));
117
150
break ;
118
151
}
119
152
FeatureProvider provider = new FlagdProvider (state .builder
@@ -130,30 +163,30 @@ public void setupProvider(String providerType) {
130
163
}
131
164
132
165
@ When ("the connection is lost for {int}s" )
133
- public void the_connection_is_lost_for (int seconds ) throws InterruptedException {
134
- FlagdContainer container = getContainer ();
135
-
136
- /* TimerTask task = new TimerTask() {
166
+ public void the_connection_is_lost_for (int seconds ) throws InterruptedException , IOException {
167
+ LOG .info ("Timeout and wait for {} seconds" , seconds );
168
+ Proxy proxy = toxiproxyClient .getProxy (generateProxyName (State .resolverType , state .providerType ));
169
+ Timeout restart = proxy
170
+ .toxics ()
171
+ .timeout ("restart" , ToxicDirection .UPSTREAM , seconds );
172
+
173
+ TimerTask task = new TimerTask () {
137
174
public void run () {
138
- container.start();
139
- int port = container.getPort(State.resolverType);
175
+ try {
176
+ proxy .toxics ().get ("restart" ).remove ();
177
+ } catch (IOException e ) {
178
+ throw new RuntimeException (e );
179
+ }
140
180
}
141
181
};
142
- Timer timer = new Timer("Timer");*/
143
-
144
- LOG .info ("stopping container for {}" , state .providerType );
145
- container .stop ();
182
+ Timer timer = new Timer ("Timer" );
146
183
147
- //timer.schedule(task, seconds * 1000L);
148
- Thread .sleep (seconds * 1000L );
184
+ timer .schedule (task , seconds * 1000L );
149
185
150
- LOG .info ("starting container for {}" , state .providerType );
151
- container .start ();
152
186
}
153
187
154
- private FlagdContainer getContainer () {
155
- LOG .info ("getting container for {}" , state .providerType );
156
- System .out .println ("getting container for " + state .providerType );
157
- return containers .getOrDefault (state .providerType , containers .get (ProviderType .DEFAULT ));
188
+ static FlagdContainer getContainer (ProviderType providerType ) {
189
+ LOG .info ("getting container for {}" , providerType );
190
+ return containers .getOrDefault (providerType , containers .get (ProviderType .DEFAULT ));
158
191
}
159
192
}
0 commit comments