2424
2525package org .csanchez .jenkins .plugins .kubernetes ;
2626
27+ import com .cloudbees .plugins .credentials .CredentialsScope ;
28+ import com .cloudbees .plugins .credentials .SystemCredentialsProvider ;
2729import hudson .ProxyConfiguration ;
30+ import hudson .util .Secret ;
2831import io .fabric8 .kubernetes .client .KubernetesClient ;
2932import io .fabric8 .kubernetes .client .utils .HttpClientUtils ;
3033import org .jenkinsci .plugins .kubernetes .auth .KubernetesAuthException ;
34+ import org .jenkinsci .plugins .plaincredentials .impl .StringCredentialsImpl ;
3135import org .junit .After ;
3236import org .junit .Before ;
3337import org .junit .Rule ;
4650import static io .fabric8 .kubernetes .client .Config .KUBERNETES_NO_PROXY ;
4751import static io .fabric8 .kubernetes .client .Config .KUBERNETES_PROXY_PASSWORD ;
4852import static io .fabric8 .kubernetes .client .Config .KUBERNETES_PROXY_USERNAME ;
53+ import static io .fabric8 .kubernetes .client .Config .KUBERNETES_SERVICE_HOST_PROPERTY ;
54+ import static io .fabric8 .kubernetes .client .Config .KUBERNETES_SERVICE_PORT_PROPERTY ;
4955import static org .junit .Assert .assertArrayEquals ;
5056import static org .junit .Assert .assertEquals ;
57+ import static org .junit .Assert .assertFalse ;
5158import static org .junit .Assert .assertNotNull ;
5259import static org .junit .Assert .assertNull ;
60+ import static org .junit .Assert .assertTrue ;
5361
5462/**
5563 * Test the creation of clients
@@ -71,6 +79,9 @@ public class KubernetesFactoryAdapterTest {
7179 private static final String NO_PROXY = "noproxy" ;
7280 private static final String PROXY_USERNAME = "proxy_username" ;
7381 private static final String PROXY_PASSWORD = "proxy_password" ;
82+ private static final String KUBERNETES_HOST = "kubernetes.example" ;
83+ private static final String KUBERNETES_PORT = "443" ;
84+ private static final String KUBERNETES_MASTER_URL = "https://" + KUBERNETES_HOST + ":" + KUBERNETES_PORT + "/" ;
7485
7586 private Map <String , String > systemProperties = new HashMap <>();
7687
@@ -91,6 +102,8 @@ public void saveSystemProperties() {
91102 System .setProperty (KUBERNETES_NO_PROXY , NO_PROXY );
92103 System .setProperty (KUBERNETES_PROXY_USERNAME , PROXY_USERNAME );
93104 System .setProperty (KUBERNETES_PROXY_PASSWORD , PROXY_PASSWORD );
105+ System .setProperty (KUBERNETES_SERVICE_HOST_PROPERTY , KUBERNETES_HOST );
106+ System .setProperty (KUBERNETES_SERVICE_PORT_PROPERTY , KUBERNETES_PORT );
94107 }
95108
96109 @ After
@@ -110,6 +123,8 @@ public void defaultNamespace() throws Exception {
110123 KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter (null , null , null , false );
111124 KubernetesClient client = factory .createClient ();
112125 assertEquals ("default" , client .getNamespace ());
126+ assertEquals (KUBERNETES_MASTER_URL , client .getConfiguration ().getMasterUrl ());
127+ assertTrue (client .getConfiguration ().getAutoConfigure ());
113128 }
114129
115130 @ Test
@@ -123,6 +138,8 @@ public void autoConfig() throws Exception {
123138 assertArrayEquals (new String [] { NO_PROXY }, client .getConfiguration ().getNoProxy ());
124139 assertEquals (PROXY_USERNAME , client .getConfiguration ().getProxyUsername ());
125140 assertEquals (PROXY_PASSWORD , client .getConfiguration ().getProxyPassword ());
141+ assertEquals (KUBERNETES_MASTER_URL , client .getConfiguration ().getMasterUrl ());
142+ assertTrue (client .getConfiguration ().getAutoConfigure ());
126143 }
127144
128145 @ Test
@@ -134,6 +151,27 @@ public void autoConfigWithMasterUrl() throws Exception {
134151 assertArrayEquals (new String [] { NO_PROXY }, client .getConfiguration ().getNoProxy ());
135152 assertEquals (PROXY_USERNAME , client .getConfiguration ().getProxyUsername ());
136153 assertEquals (PROXY_PASSWORD , client .getConfiguration ().getProxyPassword ());
154+ assertTrue (client .getConfiguration ().getAutoConfigure ());
155+ assertEquals ("http://example.com/" , client .getConfiguration ().getMasterUrl ());
156+ }
157+
158+ @ Test
159+ @ Issue ("JENKINS-70416" )
160+ public void autoConfigWithAuth () throws Exception {
161+ System .setProperty (KUBERNETES_NAMESPACE_FILE , "src/test/resources/kubenamespace" );
162+ StringCredentialsImpl tokenCredential = new StringCredentialsImpl (CredentialsScope .GLOBAL , "sa-token" , "some credentials" , Secret .fromString ("sa-token" ));
163+ SystemCredentialsProvider .getInstance ().getCredentials ().add (tokenCredential );
164+ KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter (null , null , "sa-token" , false );
165+ KubernetesClient client = factory .createClient ();
166+ assertEquals ("test-namespace" , client .getNamespace ());
167+ assertEquals (HTTP_PROXY , client .getConfiguration ().getHttpProxy ());
168+ assertEquals (HTTPS_PROXY , client .getConfiguration ().getHttpsProxy ());
169+ assertArrayEquals (new String [] { NO_PROXY }, client .getConfiguration ().getNoProxy ());
170+ assertEquals (PROXY_USERNAME , client .getConfiguration ().getProxyUsername ());
171+ assertEquals (PROXY_PASSWORD , client .getConfiguration ().getProxyPassword ());
172+ assertFalse (client .getConfiguration ().getAutoConfigure ());
173+ assertEquals (KUBERNETES_MASTER_URL , client .getConfiguration ().getMasterUrl ());
174+ assertEquals ("sa-token" , client .getConfiguration ().getOauthToken ());
137175 }
138176
139177 @ Test
0 commit comments