/*
 * The MIT License
 *
 * Copyright (c) 2016, CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.csanchez.jenkins.plugins.kubernetes;

import static org.hamcrest.Matchers.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import org.junit.Assume;

import hudson.Launcher;
import hudson.util.StreamTaskListener;

public class KubernetesTestUtil {

    private static String ip = null;

    public static String miniKubeIp() {
        if (ip == null) {
            Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                localLauncher.launch().cmds("minikube", "ip").stdout(baos).join();
                String stdout = baos.toString().trim();

                // leave last line only, ie. when a new version is available it will print some info message
                int i = stdout.lastIndexOf("\n");
                String s;
                if (i > 0) {
                    s = stdout.substring(i);
                } else {
                    s = stdout;
                }
                URL url = new URL(s);
                ip = url.toString();

            } catch (InterruptedException | IOException x) {
                ip = "";
            }
        }
        return ip;
    }

    public static void assumeMiniKube() throws Exception {
        Assume.assumeThat("MiniKube working", ip, not(isEmptyOrNullString()));
    }

}

@@ -1312,16 +1312,81 @@
 net.URL;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -1405,16 +1405,39 @@
 ssume;%0A%0A
+import hudson.EnvVars;%0A
 import h
@@ -1550,201 +1550,858 @@
 tic 
-String ip = null;%0A%0A    public static String miniKubeIp() %7B%0A        if (ip == null) %7B%0A            Launcher.LocalLauncher localLauncher = new Launcher.LocalLauncher(StreamTaskListener.NULL);%0A    
+final Logger LOGGER = Logger.getLogger(KubernetesTestUtil.class.getName());%0A%0A    private static final String%5B%5D MINIKUBE_COMMANDS = new String%5B%5D %7B %22minikube%22, %22/usr/local/bin/minikube%22 %7D;%0A    private static String ip = null;%0A%0A    public static String miniKubeIp() %7B%0A        if (ip == null) %7B%0A            for (String cmd : MINIKUBE_COMMANDS) %7B%0A                String s = miniKubeIp(new Launcher.LocalLauncher(StreamTaskListener.NULL), cmd);%0A                if (s != null) %7B%0A                    ip = s;%0A                    LOGGER.log(Level.INFO, %22Using minikube at %7B0%7D%22, ip);%0A                    return ip;%0A                %7D%0A            %7D%0A            LOGGER.warning(%22Minikube was not found or is stopped%22);%0A            ip = %22%22;%0A        %7D%0A        return ip;%0A    %7D%0A%0A    private static String miniKubeIp(Launcher.LocalLauncher localLauncher, String cmd) %7B%0A
     
@@ -2474,22 +2474,14 @@
     
-    
 try %7B%0A
-    
     
@@ -2506,32 +2506,124 @@
 her.
-launch().cmds(%22minikube%22
+decorateByEnv(new EnvVars(%22PATH%22, System.getenv(%22PATH%22) + %22:/usr/local/bin%22)).launch()%0A                    .cmds(cmd
 , %22i
@@ -2651,36 +2651,32 @@
 ();%0A            
-  
-  
 String stdout = 
@@ -2696,28 +2696,24 @@
 ().trim();%0A%0A
-    
             
@@ -2817,20 +2817,16 @@
         
-    
 int i = 
@@ -2855,36 +2855,32 @@
 %22);%0A            
-    
 String s;%0A      
@@ -2881,28 +2881,24 @@
             
-  
-  
 if (i %3E 0) %7B
@@ -2894,28 +2894,24 @@
 f (i %3E 0) %7B%0A
-    
             
@@ -2951,20 +2951,16 @@
         
-  
-  
 %7D else %7B
@@ -2960,20 +2960,16 @@
  else %7B%0A
-    
         
@@ -2996,38 +2996,38 @@
             
+%7D%0A
     
-%7D%0A
         
         URL 
@@ -3018,34 +3018,80 @@
     
-        URL url = new URL(
+// check that we got an ip and is valid%0A            new URL(%22http://%22 + 
 s);%0A
@@ -3106,38 +3106,19 @@
     
-   
- ip = url.toString();%0A%0A    
+return s;%0A%0A
     
@@ -3178,35 +3178,35 @@
             
-    ip = %22%22
+return null
 ;%0A          
@@ -3207,42 +3207,9 @@
     
- 
-   %7D%0A        %7D%0A        return ip;
+%7D
 %0A   
@@ -3317,18 +3317,28 @@
 rking%22, 
-ip
+miniKubeIp()
 , not(is

Fix minikube test execution in Eclipse