github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/juju/osenv/proxy_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package osenv_test 5 6 import ( 7 "os" 8 9 gc "launchpad.net/gocheck" 10 11 "launchpad.net/juju-core/juju/osenv" 12 "launchpad.net/juju-core/testing/testbase" 13 ) 14 15 type proxySuite struct { 16 testbase.LoggingSuite 17 } 18 19 var _ = gc.Suite(&proxySuite{}) 20 21 func (s *proxySuite) TestDetectNoSettings(c *gc.C) { 22 // Patch all of the environment variables we check out just in case the 23 // user has one set. 24 s.PatchEnvironment("http_proxy", "") 25 s.PatchEnvironment("HTTP_PROXY", "") 26 s.PatchEnvironment("https_proxy", "") 27 s.PatchEnvironment("HTTPS_PROXY", "") 28 s.PatchEnvironment("ftp_proxy", "") 29 s.PatchEnvironment("FTP_PROXY", "") 30 s.PatchEnvironment("no_proxy", "") 31 s.PatchEnvironment("NO_PROXY", "") 32 33 proxies := osenv.DetectProxies() 34 35 c.Assert(proxies, gc.DeepEquals, osenv.ProxySettings{}) 36 } 37 38 func (s *proxySuite) TestDetectPrimary(c *gc.C) { 39 // Patch all of the environment variables we check out just in case the 40 // user has one set. 41 s.PatchEnvironment("http_proxy", "http://user@10.0.0.1") 42 s.PatchEnvironment("HTTP_PROXY", "") 43 s.PatchEnvironment("https_proxy", "https://user@10.0.0.1") 44 s.PatchEnvironment("HTTPS_PROXY", "") 45 s.PatchEnvironment("ftp_proxy", "ftp://user@10.0.0.1") 46 s.PatchEnvironment("FTP_PROXY", "") 47 s.PatchEnvironment("no_proxy", "10.0.3.1,localhost") 48 s.PatchEnvironment("NO_PROXY", "") 49 50 proxies := osenv.DetectProxies() 51 52 c.Assert(proxies, gc.DeepEquals, osenv.ProxySettings{ 53 Http: "http://user@10.0.0.1", 54 Https: "https://user@10.0.0.1", 55 Ftp: "ftp://user@10.0.0.1", 56 NoProxy: "10.0.3.1,localhost", 57 }) 58 } 59 60 func (s *proxySuite) TestDetectFallback(c *gc.C) { 61 // Patch all of the environment variables we check out just in case the 62 // user has one set. 63 s.PatchEnvironment("http_proxy", "") 64 s.PatchEnvironment("HTTP_PROXY", "http://user@10.0.0.2") 65 s.PatchEnvironment("https_proxy", "") 66 s.PatchEnvironment("HTTPS_PROXY", "https://user@10.0.0.2") 67 s.PatchEnvironment("ftp_proxy", "") 68 s.PatchEnvironment("FTP_PROXY", "ftp://user@10.0.0.2") 69 s.PatchEnvironment("no_proxy", "") 70 s.PatchEnvironment("NO_PROXY", "10.0.3.1,localhost") 71 72 proxies := osenv.DetectProxies() 73 74 c.Assert(proxies, gc.DeepEquals, osenv.ProxySettings{ 75 Http: "http://user@10.0.0.2", 76 Https: "https://user@10.0.0.2", 77 Ftp: "ftp://user@10.0.0.2", 78 NoProxy: "10.0.3.1,localhost", 79 }) 80 } 81 82 func (s *proxySuite) TestDetectPrimaryPreference(c *gc.C) { 83 // Patch all of the environment variables we check out just in case the 84 // user has one set. 85 s.PatchEnvironment("http_proxy", "http://user@10.0.0.1") 86 s.PatchEnvironment("https_proxy", "https://user@10.0.0.1") 87 s.PatchEnvironment("ftp_proxy", "ftp://user@10.0.0.1") 88 s.PatchEnvironment("no_proxy", "10.0.3.1,localhost") 89 s.PatchEnvironment("HTTP_PROXY", "http://user@10.0.0.2") 90 s.PatchEnvironment("HTTPS_PROXY", "https://user@10.0.0.2") 91 s.PatchEnvironment("FTP_PROXY", "ftp://user@10.0.0.2") 92 s.PatchEnvironment("NO_PROXY", "localhost") 93 94 proxies := osenv.DetectProxies() 95 96 c.Assert(proxies, gc.DeepEquals, osenv.ProxySettings{ 97 Http: "http://user@10.0.0.1", 98 Https: "https://user@10.0.0.1", 99 Ftp: "ftp://user@10.0.0.1", 100 NoProxy: "10.0.3.1,localhost", 101 }) 102 } 103 104 func (s *proxySuite) TestAsScriptEnvironmentEmpty(c *gc.C) { 105 proxies := osenv.ProxySettings{} 106 c.Assert(proxies.AsScriptEnvironment(), gc.Equals, "") 107 } 108 109 func (s *proxySuite) TestAsScriptEnvironmentOneValue(c *gc.C) { 110 proxies := osenv.ProxySettings{ 111 Http: "some-value", 112 } 113 expected := ` 114 export http_proxy=some-value 115 export HTTP_PROXY=some-value`[1:] 116 c.Assert(proxies.AsScriptEnvironment(), gc.Equals, expected) 117 } 118 119 func (s *proxySuite) TestAsScriptEnvironmentAllValue(c *gc.C) { 120 proxies := osenv.ProxySettings{ 121 Http: "some-value", 122 Https: "special", 123 Ftp: "who uses this?", 124 NoProxy: "10.0.3.1,localhost", 125 } 126 expected := ` 127 export http_proxy=some-value 128 export HTTP_PROXY=some-value 129 export https_proxy=special 130 export HTTPS_PROXY=special 131 export ftp_proxy=who uses this? 132 export FTP_PROXY=who uses this? 133 export no_proxy=10.0.3.1,localhost 134 export NO_PROXY=10.0.3.1,localhost`[1:] 135 c.Assert(proxies.AsScriptEnvironment(), gc.Equals, expected) 136 } 137 138 func (s *proxySuite) TestAsEnvironmentValuesEmpty(c *gc.C) { 139 proxies := osenv.ProxySettings{} 140 c.Assert(proxies.AsEnvironmentValues(), gc.HasLen, 0) 141 } 142 143 func (s *proxySuite) TestAsEnvironmentValuesOneValue(c *gc.C) { 144 proxies := osenv.ProxySettings{ 145 Http: "some-value", 146 } 147 expected := []string{ 148 "http_proxy=some-value", 149 "HTTP_PROXY=some-value", 150 } 151 c.Assert(proxies.AsEnvironmentValues(), gc.DeepEquals, expected) 152 } 153 154 func (s *proxySuite) TestAsEnvironmentValuesAllValue(c *gc.C) { 155 proxies := osenv.ProxySettings{ 156 Http: "some-value", 157 Https: "special", 158 Ftp: "who uses this?", 159 NoProxy: "10.0.3.1,localhost", 160 } 161 expected := []string{ 162 "http_proxy=some-value", 163 "HTTP_PROXY=some-value", 164 "https_proxy=special", 165 "HTTPS_PROXY=special", 166 "ftp_proxy=who uses this?", 167 "FTP_PROXY=who uses this?", 168 "no_proxy=10.0.3.1,localhost", 169 "NO_PROXY=10.0.3.1,localhost", 170 } 171 c.Assert(proxies.AsEnvironmentValues(), gc.DeepEquals, expected) 172 } 173 174 func (s *proxySuite) TestSetEnvironmentValues(c *gc.C) { 175 s.PatchEnvironment("http_proxy", "initial") 176 s.PatchEnvironment("HTTP_PROXY", "initial") 177 s.PatchEnvironment("https_proxy", "initial") 178 s.PatchEnvironment("HTTPS_PROXY", "initial") 179 s.PatchEnvironment("ftp_proxy", "initial") 180 s.PatchEnvironment("FTP_PROXY", "initial") 181 s.PatchEnvironment("no_proxy", "initial") 182 s.PatchEnvironment("NO_PROXY", "initial") 183 184 proxy := osenv.ProxySettings{ 185 Http: "http proxy", 186 Https: "https proxy", 187 // Ftp left blank to show clearing env. 188 NoProxy: "10.0.3.1,localhost", 189 } 190 proxy.SetEnvironmentValues() 191 192 obtained := osenv.DetectProxies() 193 194 c.Assert(obtained, gc.DeepEquals, proxy) 195 196 c.Assert(os.Getenv("http_proxy"), gc.Equals, "http proxy") 197 c.Assert(os.Getenv("HTTP_PROXY"), gc.Equals, "http proxy") 198 c.Assert(os.Getenv("https_proxy"), gc.Equals, "https proxy") 199 c.Assert(os.Getenv("HTTPS_PROXY"), gc.Equals, "https proxy") 200 c.Assert(os.Getenv("ftp_proxy"), gc.Equals, "") 201 c.Assert(os.Getenv("FTP_PROXY"), gc.Equals, "") 202 c.Assert(os.Getenv("no_proxy"), gc.Equals, "10.0.3.1,localhost") 203 c.Assert(os.Getenv("NO_PROXY"), gc.Equals, "10.0.3.1,localhost") 204 }