launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  
    31  	proxies := osenv.DetectProxies()
    32  
    33  	c.Assert(proxies, gc.DeepEquals, osenv.ProxySettings{})
    34  }
    35  
    36  func (s *proxySuite) TestDetectPrimary(c *gc.C) {
    37  	// Patch all of the environment variables we check out just in case the
    38  	// user has one set.
    39  	s.PatchEnvironment("http_proxy", "http://user@10.0.0.1")
    40  	s.PatchEnvironment("HTTP_PROXY", "")
    41  	s.PatchEnvironment("https_proxy", "https://user@10.0.0.1")
    42  	s.PatchEnvironment("HTTPS_PROXY", "")
    43  	s.PatchEnvironment("ftp_proxy", "ftp://user@10.0.0.1")
    44  	s.PatchEnvironment("FTP_PROXY", "")
    45  
    46  	proxies := osenv.DetectProxies()
    47  
    48  	c.Assert(proxies, gc.DeepEquals, osenv.ProxySettings{
    49  		Http:  "http://user@10.0.0.1",
    50  		Https: "https://user@10.0.0.1",
    51  		Ftp:   "ftp://user@10.0.0.1",
    52  	})
    53  }
    54  
    55  func (s *proxySuite) TestDetectFallback(c *gc.C) {
    56  	// Patch all of the environment variables we check out just in case the
    57  	// user has one set.
    58  	s.PatchEnvironment("http_proxy", "")
    59  	s.PatchEnvironment("HTTP_PROXY", "http://user@10.0.0.2")
    60  	s.PatchEnvironment("https_proxy", "")
    61  	s.PatchEnvironment("HTTPS_PROXY", "https://user@10.0.0.2")
    62  	s.PatchEnvironment("ftp_proxy", "")
    63  	s.PatchEnvironment("FTP_PROXY", "ftp://user@10.0.0.2")
    64  
    65  	proxies := osenv.DetectProxies()
    66  
    67  	c.Assert(proxies, gc.DeepEquals, osenv.ProxySettings{
    68  		Http:  "http://user@10.0.0.2",
    69  		Https: "https://user@10.0.0.2",
    70  		Ftp:   "ftp://user@10.0.0.2",
    71  	})
    72  }
    73  
    74  func (s *proxySuite) TestDetectPrimaryPreference(c *gc.C) {
    75  	// Patch all of the environment variables we check out just in case the
    76  	// user has one set.
    77  	s.PatchEnvironment("http_proxy", "http://user@10.0.0.1")
    78  	s.PatchEnvironment("https_proxy", "https://user@10.0.0.1")
    79  	s.PatchEnvironment("ftp_proxy", "ftp://user@10.0.0.1")
    80  	s.PatchEnvironment("HTTP_PROXY", "http://user@10.0.0.2")
    81  	s.PatchEnvironment("HTTPS_PROXY", "https://user@10.0.0.2")
    82  	s.PatchEnvironment("FTP_PROXY", "ftp://user@10.0.0.2")
    83  
    84  	proxies := osenv.DetectProxies()
    85  
    86  	c.Assert(proxies, gc.DeepEquals, osenv.ProxySettings{
    87  		Http:  "http://user@10.0.0.1",
    88  		Https: "https://user@10.0.0.1",
    89  		Ftp:   "ftp://user@10.0.0.1",
    90  	})
    91  }
    92  
    93  func (s *proxySuite) TestAsScriptEnvironmentEmpty(c *gc.C) {
    94  	proxies := osenv.ProxySettings{}
    95  	c.Assert(proxies.AsScriptEnvironment(), gc.Equals, "")
    96  }
    97  
    98  func (s *proxySuite) TestAsScriptEnvironmentOneValue(c *gc.C) {
    99  	proxies := osenv.ProxySettings{
   100  		Http: "some-value",
   101  	}
   102  	expected := `
   103  export http_proxy=some-value
   104  export HTTP_PROXY=some-value`[1:]
   105  	c.Assert(proxies.AsScriptEnvironment(), gc.Equals, expected)
   106  }
   107  
   108  func (s *proxySuite) TestAsScriptEnvironmentAllValue(c *gc.C) {
   109  	proxies := osenv.ProxySettings{
   110  		Http:  "some-value",
   111  		Https: "special",
   112  		Ftp:   "who uses this?",
   113  	}
   114  	expected := `
   115  export http_proxy=some-value
   116  export HTTP_PROXY=some-value
   117  export https_proxy=special
   118  export HTTPS_PROXY=special
   119  export ftp_proxy=who uses this?
   120  export FTP_PROXY=who uses this?`[1:]
   121  	c.Assert(proxies.AsScriptEnvironment(), gc.Equals, expected)
   122  }
   123  
   124  func (s *proxySuite) TestAsEnvironmentValuesEmpty(c *gc.C) {
   125  	proxies := osenv.ProxySettings{}
   126  	c.Assert(proxies.AsEnvironmentValues(), gc.HasLen, 0)
   127  }
   128  
   129  func (s *proxySuite) TestAsEnvironmentValuesOneValue(c *gc.C) {
   130  	proxies := osenv.ProxySettings{
   131  		Http: "some-value",
   132  	}
   133  	expected := []string{
   134  		"http_proxy=some-value",
   135  		"HTTP_PROXY=some-value",
   136  	}
   137  	c.Assert(proxies.AsEnvironmentValues(), gc.DeepEquals, expected)
   138  }
   139  
   140  func (s *proxySuite) TestAsEnvironmentValuesAllValue(c *gc.C) {
   141  	proxies := osenv.ProxySettings{
   142  		Http:  "some-value",
   143  		Https: "special",
   144  		Ftp:   "who uses this?",
   145  	}
   146  	expected := []string{
   147  		"http_proxy=some-value",
   148  		"HTTP_PROXY=some-value",
   149  		"https_proxy=special",
   150  		"HTTPS_PROXY=special",
   151  		"ftp_proxy=who uses this?",
   152  		"FTP_PROXY=who uses this?",
   153  	}
   154  	c.Assert(proxies.AsEnvironmentValues(), gc.DeepEquals, expected)
   155  }
   156  
   157  func (s *proxySuite) TestSetEnvironmentValues(c *gc.C) {
   158  	s.PatchEnvironment("http_proxy", "initial")
   159  	s.PatchEnvironment("HTTP_PROXY", "initial")
   160  	s.PatchEnvironment("https_proxy", "initial")
   161  	s.PatchEnvironment("HTTPS_PROXY", "initial")
   162  	s.PatchEnvironment("ftp_proxy", "initial")
   163  	s.PatchEnvironment("FTP_PROXY", "initial")
   164  
   165  	proxy := osenv.ProxySettings{
   166  		Http:  "http proxy",
   167  		Https: "https proxy",
   168  		// Ftp left blank to show clearing env.
   169  	}
   170  	proxy.SetEnvironmentValues()
   171  
   172  	obtained := osenv.DetectProxies()
   173  
   174  	c.Assert(obtained, gc.DeepEquals, proxy)
   175  
   176  	c.Assert(os.Getenv("http_proxy"), gc.Equals, "http proxy")
   177  	c.Assert(os.Getenv("HTTP_PROXY"), gc.Equals, "http proxy")
   178  	c.Assert(os.Getenv("https_proxy"), gc.Equals, "https proxy")
   179  	c.Assert(os.Getenv("HTTPS_PROXY"), gc.Equals, "https proxy")
   180  	c.Assert(os.Getenv("ftp_proxy"), gc.Equals, "")
   181  	c.Assert(os.Getenv("FTP_PROXY"), gc.Equals, "")
   182  }