github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/proxyupdater/proxyupdater_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package proxyupdater_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path" 10 "strings" 11 "time" 12 13 jc "github.com/juju/testing/checkers" 14 "github.com/juju/utils/apt" 15 "github.com/juju/utils/proxy" 16 gc "gopkg.in/check.v1" 17 18 "github.com/juju/juju/api" 19 "github.com/juju/juju/api/environment" 20 "github.com/juju/juju/environs/config" 21 jujutesting "github.com/juju/juju/juju/testing" 22 "github.com/juju/juju/state" 23 "github.com/juju/juju/testing" 24 "github.com/juju/juju/worker" 25 "github.com/juju/juju/worker/proxyupdater" 26 ) 27 28 type ProxyUpdaterSuite struct { 29 jujutesting.JujuConnSuite 30 31 apiRoot *api.State 32 environmentAPI *environment.Facade 33 machine *state.Machine 34 35 proxyFile string 36 started chan struct{} 37 } 38 39 var _ = gc.Suite(&ProxyUpdaterSuite{}) 40 41 func (s *ProxyUpdaterSuite) setStarted() { 42 select { 43 case <-s.started: 44 default: 45 close(s.started) 46 } 47 } 48 49 func (s *ProxyUpdaterSuite) SetUpTest(c *gc.C) { 50 s.JujuConnSuite.SetUpTest(c) 51 s.apiRoot, s.machine = s.OpenAPIAsNewMachine(c) 52 // Create the environment API facade. 53 s.environmentAPI = s.apiRoot.Environment() 54 c.Assert(s.environmentAPI, gc.NotNil) 55 56 proxyDir := c.MkDir() 57 s.PatchValue(&proxyupdater.ProxyDirectory, proxyDir) 58 s.started = make(chan struct{}) 59 s.PatchValue(&proxyupdater.Started, s.setStarted) 60 s.PatchValue(&apt.ConfFile, path.Join(proxyDir, "juju-apt-proxy")) 61 s.proxyFile = path.Join(proxyDir, proxyupdater.ProxyFile) 62 } 63 64 func (s *ProxyUpdaterSuite) waitForPostSetup(c *gc.C) { 65 select { 66 case <-time.After(testing.LongWait): 67 c.Fatalf("timeout while waiting for setup") 68 case <-s.started: 69 } 70 } 71 72 func (s *ProxyUpdaterSuite) waitProxySettings(c *gc.C, expected proxy.Settings) { 73 for { 74 select { 75 case <-time.After(testing.LongWait): 76 c.Fatalf("timeout while waiting for proxy settings to change") 77 case <-time.After(10 * time.Millisecond): 78 obtained := proxy.DetectProxies() 79 if obtained != expected { 80 c.Logf("proxy settings are %#v, still waiting", obtained) 81 continue 82 } 83 return 84 } 85 } 86 } 87 88 func (s *ProxyUpdaterSuite) waitForFile(c *gc.C, filename, expected string) { 89 for { 90 select { 91 case <-time.After(testing.LongWait): 92 c.Fatalf("timeout while waiting for proxy settings to change") 93 case <-time.After(10 * time.Millisecond): 94 fileContent, err := ioutil.ReadFile(filename) 95 if os.IsNotExist(err) { 96 continue 97 } 98 c.Assert(err, jc.ErrorIsNil) 99 if string(fileContent) != expected { 100 c.Logf("file content not matching, still waiting") 101 continue 102 } 103 return 104 } 105 } 106 } 107 108 func (s *ProxyUpdaterSuite) TestRunStop(c *gc.C) { 109 updater := proxyupdater.New(s.environmentAPI, false) 110 c.Assert(worker.Stop(updater), gc.IsNil) 111 } 112 113 func (s *ProxyUpdaterSuite) updateConfig(c *gc.C) (proxy.Settings, proxy.Settings) { 114 115 proxySettings := proxy.Settings{ 116 Http: "http proxy", 117 Https: "https proxy", 118 Ftp: "ftp proxy", 119 NoProxy: "no proxy", 120 } 121 attrs := map[string]interface{}{} 122 for k, v := range config.ProxyConfigMap(proxySettings) { 123 attrs[k] = v 124 } 125 126 // We explicitly set apt proxy settings as well to show that it is the apt 127 // settings that are used for the apt config, and not just the normal 128 // proxy settings which is what we would get if we don't explicitly set 129 // apt values. 130 aptProxySettings := proxy.Settings{ 131 Http: "apt http proxy", 132 Https: "apt https proxy", 133 Ftp: "apt ftp proxy", 134 } 135 for k, v := range config.AptProxyConfigMap(aptProxySettings) { 136 attrs[k] = v 137 } 138 139 err := s.State.UpdateEnvironConfig(attrs, nil, nil) 140 c.Assert(err, jc.ErrorIsNil) 141 142 return proxySettings, aptProxySettings 143 } 144 145 func (s *ProxyUpdaterSuite) TestInitialState(c *gc.C) { 146 proxySettings, aptProxySettings := s.updateConfig(c) 147 148 updater := proxyupdater.New(s.environmentAPI, true) 149 defer worker.Stop(updater) 150 151 s.waitProxySettings(c, proxySettings) 152 s.waitForFile(c, s.proxyFile, proxySettings.AsScriptEnvironment()+"\n") 153 s.waitForFile(c, apt.ConfFile, apt.ProxyContent(aptProxySettings)+"\n") 154 } 155 156 func (s *ProxyUpdaterSuite) TestWriteSystemFiles(c *gc.C) { 157 proxySettings, aptProxySettings := s.updateConfig(c) 158 159 updater := proxyupdater.New(s.environmentAPI, true) 160 defer worker.Stop(updater) 161 s.waitForPostSetup(c) 162 163 s.waitProxySettings(c, proxySettings) 164 s.waitForFile(c, s.proxyFile, proxySettings.AsScriptEnvironment()+"\n") 165 s.waitForFile(c, apt.ConfFile, apt.ProxyContent(aptProxySettings)+"\n") 166 } 167 168 func (s *ProxyUpdaterSuite) TestEnvironmentVariables(c *gc.C) { 169 setenv := func(proxy, value string) { 170 os.Setenv(proxy, value) 171 os.Setenv(strings.ToUpper(proxy), value) 172 } 173 setenv("http_proxy", "foo") 174 setenv("https_proxy", "foo") 175 setenv("ftp_proxy", "foo") 176 setenv("no_proxy", "foo") 177 178 proxySettings, _ := s.updateConfig(c) 179 180 updater := proxyupdater.New(s.environmentAPI, true) 181 defer worker.Stop(updater) 182 s.waitForPostSetup(c) 183 s.waitProxySettings(c, proxySettings) 184 185 assertEnv := func(proxy, value string) { 186 c.Assert(os.Getenv(proxy), gc.Equals, value) 187 c.Assert(os.Getenv(strings.ToUpper(proxy)), gc.Equals, value) 188 } 189 assertEnv("http_proxy", proxySettings.Http) 190 assertEnv("https_proxy", proxySettings.Https) 191 assertEnv("ftp_proxy", proxySettings.Ftp) 192 assertEnv("no_proxy", proxySettings.NoProxy) 193 } 194 195 func (s *ProxyUpdaterSuite) TestDontWriteSystemFiles(c *gc.C) { 196 proxySettings, _ := s.updateConfig(c) 197 198 updater := proxyupdater.New(s.environmentAPI, false) 199 defer worker.Stop(updater) 200 s.waitForPostSetup(c) 201 202 s.waitProxySettings(c, proxySettings) 203 c.Assert(apt.ConfFile, jc.DoesNotExist) 204 c.Assert(s.proxyFile, jc.DoesNotExist) 205 }