17
17
18
18
import java .io .File ;
19
19
import java .io .FileReader ;
20
+ import java .io .IOException ;
20
21
import java .util .Collections ;
21
22
import java .util .LinkedList ;
22
23
import java .util .List ;
43
44
import io .fabric8 .openshift .api .model .ImageStreamBuilder ;
44
45
import io .fabric8 .openshift .api .model .ImageStreamStatusBuilder ;
45
46
import io .fabric8 .openshift .api .model .NamedTagEventListBuilder ;
47
+ import io .fabric8 .openshift .client .DefaultOpenShiftClient ;
46
48
import io .fabric8 .openshift .client .OpenShiftClient ;
47
49
import io .fabric8 .openshift .client .server .mock .OpenShiftMockServer ;
48
50
@@ -69,6 +71,8 @@ public class OpenshiftBuildServiceTest {
69
71
70
72
private static final Logger LOG = LoggerFactory .getLogger (OpenshiftBuildServiceTest .class );
71
73
74
+ private static final int MAX_TIMEOUT_RETRIES = 5 ;
75
+
72
76
private String baseDir = "target/test-files/openshift-build-service" ;
73
77
74
78
private String projectName = "myapp" ;
@@ -139,18 +143,37 @@ public void init() throws Exception {
139
143
140
144
@ Test
141
145
public void testSuccessfulBuild () throws Exception {
142
- BuildService .BuildServiceConfig config = defaultConfig .build ();
143
- WebServerEventCollector <OpenShiftMockServer > collector = createMockServer (config , true , 50 , false , false );
144
- OpenShiftMockServer mockServer = collector .getMockServer ();
145
-
146
- OpenShiftClient client = mockServer .createOpenShiftClient ();
147
- OpenshiftBuildService service = new OpenshiftBuildService (client , logger , dockerServiceHub , config );
148
- service .build (image );
149
-
150
- // we should Foadd a better way to assert that a certain call has been made
151
- assertTrue (mockServer .getRequestCount () > 8 );
152
- collector .assertEventsRecordedInOrder ("build-config-check" , "new-build-config" , "pushed" );
153
- collector .assertEventsNotRecorded ("patch-build-config" );
146
+ int nTries = 0 ;
147
+ boolean bTestComplete = false ;
148
+ do {
149
+ try {
150
+ nTries ++;
151
+ BuildService .BuildServiceConfig config = defaultConfig .build ();
152
+ WebServerEventCollector <OpenShiftMockServer > collector = createMockServer (config , true , 50 , false , false );
153
+ OpenShiftMockServer mockServer = collector .getMockServer ();
154
+
155
+ DefaultOpenShiftClient client = (DefaultOpenShiftClient )mockServer .createOpenShiftClient ();
156
+ LOG .info ("Current write timeout is : {}" , client .getHttpClient ().writeTimeoutMillis ());
157
+ LOG .info ("Current read timeout is : {}" , client .getHttpClient ().readTimeoutMillis ());
158
+ LOG .info ("Retry on failure : {}" , client .getHttpClient ().retryOnConnectionFailure ());
159
+ OpenshiftBuildService service = new OpenshiftBuildService (client , logger , dockerServiceHub , config );
160
+ service .build (image );
161
+
162
+ // we should Foadd a better way to assert that a certain call has been made
163
+ assertTrue (mockServer .getRequestCount () > 8 );
164
+ collector .assertEventsRecordedInOrder ("build-config-check" , "new-build-config" , "pushed" );
165
+ collector .assertEventsNotRecorded ("patch-build-config" );
166
+ bTestComplete = true ;
167
+ } catch (Fabric8ServiceException exception ) {
168
+ Throwable rootCause = getRootCause (exception );
169
+ logger .warn ("A problem encountered while running test {}, retrying.." , exception .getMessage ());
170
+ // Let's wait for a while, and then retry again
171
+ if (rootCause != null && rootCause instanceof IOException ) {
172
+ Thread .sleep (5 * 1000 );
173
+ continue ;
174
+ }
175
+ }
176
+ } while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete );
154
177
}
155
178
156
179
@ Test (expected = Fabric8ServiceException .class )
@@ -302,4 +325,19 @@ protected WebServerEventCollector<OpenShiftMockServer> createMockServer(BuildSer
302
325
return collector ;
303
326
}
304
327
328
+ /**
329
+ * Helper method to get root cause, pretty much like Apache's ExceptionUtils
330
+ *
331
+ * @param aThrowable
332
+ * @return
333
+ */
334
+ private Throwable getRootCause (Throwable aThrowable ) {
335
+ Throwable cause , result = aThrowable ;
336
+
337
+ while ((cause = result .getCause ()) != null && cause != result ) {
338
+ result = cause ;
339
+ }
340
+ return result ;
341
+ }
342
+
305
343
}
0 commit comments