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