3535import hudson .slaves .ComputerLauncher ;
3636import hudson .util .StreamTaskListener ;
3737import io .fabric8 .kubernetes .api .model .*;
38+ import io .fabric8 .kubernetes .client .KubernetesClient ;
3839import io .fabric8 .kubernetes .client .Watcher ;
39- import io .fabric8 .kubernetes .client .server .mock .KubernetesServer ;
40+ import io .fabric8 .kubernetes .client .server .mock .KubernetesMockServer ;
4041import io .fabric8 .kubernetes .client .utils .Utils ;
42+ import io .fabric8 .mockwebserver .http .RecordedRequest ;
4143import java .io .IOException ;
4244import java .net .HttpURLConnection ;
45+ import java .net .InetAddress ;
4346import java .util .LinkedList ;
4447import java .util .List ;
4548import java .util .Map ;
4851import java .util .concurrent .TimeUnit ;
4952import java .util .stream .Collectors ;
5053import jenkins .model .Jenkins ;
51- import okhttp3 .mockwebserver .RecordedRequest ;
5254import org .csanchez .jenkins .plugins .kubernetes .*;
5355import org .csanchez .jenkins .plugins .kubernetes .PodTemplate ;
5456import org .junit .After ;
57+ import org .junit .Before ;
5558import org .junit .Rule ;
5659import org .junit .Test ;
5760import org .junit .rules .ExternalResource ;
@@ -64,15 +67,25 @@ public class ReaperTest {
6467 @ Rule
6568 public JenkinsRule j = new JenkinsRule ();
6669
67- @ Rule
68- public KubernetesServer server = new KubernetesServer ();
69-
7070 @ Rule
7171 public CapturingReaperListener listener = new CapturingReaperListener ();
7272
73+ private KubernetesMockServer server ;
74+ private KubernetesClient client ;
75+
76+ @ Before
77+ public void setUp () {
78+ // TODO: remove when moving to junit 5
79+ server = new KubernetesMockServer ();
80+ server .init (InetAddress .getLoopbackAddress (), 0 );
81+ client = server .createClient ();
82+ }
83+
7384 @ After
7485 public void tearDown () {
7586 KubernetesClientProvider .invalidateAll ();
87+ server .destroy ();
88+ client .close ();
7689 }
7790
7891 @ Test
@@ -561,7 +574,7 @@ public void testTerminateAgentOnContainerTerminated() throws IOException, Interr
561574
562575 @ Test (timeout = 10_000 )
563576 public void testTerminateAgentOnPodFailed () throws IOException , InterruptedException {
564- System .out .println (server .getKubernetesMockServer (). getPort ());
577+ System .out .println (server .getPort ());
565578 KubernetesCloud cloud = addCloud ("k8s" , "foo" );
566579 KubernetesSlave node = addNode (cloud , "node-123" , "node" );
567580 Pod node123 = createPod (node );
@@ -716,7 +729,7 @@ private KubernetesSlave addNode(KubernetesCloud cld, String podName, String node
716729
717730 private KubernetesCloud addCloud (String name , String namespace ) {
718731 KubernetesCloud c = new KubernetesCloud (name );
719- c .setServerUrl (server . getClient () .getMasterUrl ().toString ());
732+ c .setServerUrl (client .getMasterUrl ().toString ());
720733 c .setNamespace (namespace );
721734 c .setSkipTlsVerify (true );
722735 j .jenkins .clouds .add (c );
@@ -730,10 +743,10 @@ private KubernetesCloud addCloud(String name, String namespace) {
730743 * @throws InterruptedException interrupted exception
731744 */
732745 private CapturedRequests kubeClientRequests () throws InterruptedException {
733- int count = server .getKubernetesMockServer (). getRequestCount ();
746+ int count = server .getRequestCount ();
734747 List <RecordedRequest > requests = new LinkedList <>();
735748 while (count -- > 0 ) {
736- RecordedRequest rr = server .getKubernetesMockServer (). takeRequest (1 , TimeUnit .SECONDS );
749+ RecordedRequest rr = server .takeRequest (1 , TimeUnit .SECONDS );
737750 if (rr != null ) {
738751 requests .add (rr );
739752 }
@@ -750,7 +763,7 @@ private CapturedRequests kubeClientRequests() throws InterruptedException {
750763 private CapturedRequests waitForKubeClientRequests (int count ) throws InterruptedException {
751764 List <RecordedRequest > requests = new LinkedList <>();
752765 while (count -- > 0 ) {
753- requests .add (server .getKubernetesMockServer (). takeRequest ());
766+ requests .add (server .takeRequest ());
754767 }
755768 return new CapturedRequests (requests );
756769 }
@@ -764,7 +777,7 @@ private CapturedRequests waitForKubeClientRequests(int count) throws Interrupted
764777 private CapturedRequests waitForKubeClientRequests (String path ) throws InterruptedException {
765778 List <RecordedRequest > requests = new LinkedList <>();
766779 while (true ) {
767- RecordedRequest rr = server .getKubernetesMockServer (). takeRequest ();
780+ RecordedRequest rr = server .takeRequest ();
768781 requests .add (rr );
769782 if (rr .getPath ().equals (path )) {
770783 return new CapturedRequests (requests );
@@ -878,21 +891,22 @@ public String toString() {
878891 private static WatchEvent outdatedEvent () {
879892 return new WatchEventBuilder ()
880893 .withType (Watcher .Action .ERROR .name ())
881- .withNewStatusObject ()
882- .withCode (HttpURLConnection .HTTP_GONE )
883- .withMessage (
884- "410: The event in requested index is outdated and cleared (the requested history has been cleared [3/1]) [2]" )
885- .endStatusObject ()
894+ .withType (Watcher .Action .ERROR .name ())
895+ .withObject (new StatusBuilder ()
896+ .withCode (HttpURLConnection .HTTP_GONE )
897+ .withMessage (
898+ "410: The event in requested index is outdated and cleared (the requested history has been cleared [3/1]) [2]" )
899+ .build ())
886900 .build ();
887901 }
888902
889903 private static WatchEvent errorEvent () {
890904 return new WatchEventBuilder ()
891905 .withType (Watcher .Action .ERROR .name ())
892- .withNewStatusObject ()
893- .withCode (HttpURLConnection .HTTP_INTERNAL_ERROR )
894- .withMessage ("500: Internal error" )
895- . endStatusObject ( )
906+ .withObject ( new StatusBuilder ()
907+ .withCode (HttpURLConnection .HTTP_INTERNAL_ERROR )
908+ .withMessage ("500: Internal error" )
909+ . build () )
896910 .build ();
897911 }
898912
0 commit comments