|
| 1 | +package io.kubernetes.client.util; |
| 2 | + |
| 3 | +import io.kubernetes.client.ApiClient; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileWriter; |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import static org.junit.Assert.*; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Ignore; |
| 13 | +import org.junit.Rule; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.contrib.java.lang.system.EnvironmentVariables; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for the Config helper class |
| 19 | + */ |
| 20 | +public class ConfigTest { |
| 21 | + @Rule |
| 22 | + public final EnvironmentVariables environmentVariables |
| 23 | + = new EnvironmentVariables(); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testDefaultClientNothingPresent() { |
| 27 | + environmentVariables.set("HOME", "/non-existent"); |
| 28 | + try { |
| 29 | + ApiClient client = Config.defaultClient(); |
| 30 | + assertEquals("http://localhost:8080", client.getBasePath()); |
| 31 | + } catch (IOException ex) { |
| 32 | + fail("Unexpected exception: " + ex); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static String HOME_CONFIG = |
| 37 | + "apiVersion: v1\n" + |
| 38 | + "clusters:\n" + |
| 39 | + "- cluster:\n" + |
| 40 | + " server: http://home.dir.com\n" + |
| 41 | + " name: foo\n" + |
| 42 | + "contexts:\n" + |
| 43 | + "- context:\n" + |
| 44 | + " cluster: foo\n" + |
| 45 | + " name: foo-context\n" + |
| 46 | + "current-context: foo-context\n"; |
| 47 | + |
| 48 | + public static String KUBECONFIG = |
| 49 | + "apiVersion: v1\n" + |
| 50 | + "clusters:\n" + |
| 51 | + "- cluster:\n" + |
| 52 | + " server: http://kubeconfig.dir.com\n" + |
| 53 | + " name: foo\n" + |
| 54 | + "contexts:\n" + |
| 55 | + "- context:\n" + |
| 56 | + " cluster: foo\n" + |
| 57 | + " name: foo-context\n" + |
| 58 | + "current-context: foo-context\n"; |
| 59 | + |
| 60 | + File config = null; |
| 61 | + File dir = null; |
| 62 | + File kubedir = null; |
| 63 | + File configFile = null; |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setUp() throws IOException { |
| 67 | + dir = new File("/tmp/tester"); |
| 68 | + dir.mkdir(); |
| 69 | + kubedir = new File(dir, ".kube"); |
| 70 | + kubedir.mkdir(); |
| 71 | + config = new File(kubedir, "config"); |
| 72 | + FileWriter writer = new FileWriter(config); |
| 73 | + writer.write(HOME_CONFIG); |
| 74 | + writer.flush(); |
| 75 | + writer.close(); |
| 76 | + |
| 77 | + configFile = File.createTempFile("config", ""); |
| 78 | + writer = new FileWriter(configFile); |
| 79 | + writer.write(KUBECONFIG); |
| 80 | + writer.flush(); |
| 81 | + writer.close(); |
| 82 | + } |
| 83 | + |
| 84 | + @After |
| 85 | + public void tearDown() throws IOException { |
| 86 | + if (config != null) { |
| 87 | + config.delete(); |
| 88 | + } |
| 89 | + if (kubedir != null) { |
| 90 | + kubedir.delete(); |
| 91 | + } |
| 92 | + if (dir != null) { |
| 93 | + dir.delete(); |
| 94 | + } |
| 95 | + if (configFile != null) { |
| 96 | + configFile.delete(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testDefaultClientHomeDir() { |
| 102 | + try { |
| 103 | + environmentVariables.set("HOME", dir.getCanonicalPath()); |
| 104 | + ApiClient client = Config.defaultClient(); |
| 105 | + assertEquals("http://home.dir.com", client.getBasePath()); |
| 106 | + } catch (Exception ex) { |
| 107 | + ex.printStackTrace(); |
| 108 | + fail("Unexpected exception: " + ex); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testDefaultClientKubeConfig() { |
| 114 | + try { |
| 115 | + environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); |
| 116 | + ApiClient client = Config.defaultClient(); |
| 117 | + assertEquals("http://kubeconfig.dir.com", client.getBasePath()); |
| 118 | + } catch (Exception ex) { |
| 119 | + ex.printStackTrace(); |
| 120 | + fail("Unexpected exception: " + ex); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testDefaultClientPrecedence() { |
| 126 | + try { |
| 127 | + environmentVariables.set("HOME", dir.getCanonicalPath()); |
| 128 | + environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); |
| 129 | + ApiClient client = Config.defaultClient(); |
| 130 | + // $KUBECONFIG should take precedence over $HOME/.kube/config |
| 131 | + assertEquals("http://kubeconfig.dir.com", client.getBasePath()); |
| 132 | + } catch (Exception ex) { |
| 133 | + ex.printStackTrace(); |
| 134 | + fail("Unexpected exception: " + ex); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments