func (s *cmdSuite) TestHookCommandOutput(c *gc.C) { var CommandOutput = (*exec.Cmd).CombinedOutput cmdChan, cleanup := testing.HookCommandOutput(&CommandOutput, []byte{1, 2, 3, 4}, nil) defer cleanup() testCmd := exec.Command("fake-command", "arg1", "arg2") out, err := CommandOutput(testCmd) c.Assert(err, gc.IsNil) cmd := <-cmdChan c.Assert(out, gc.DeepEquals, []byte{1, 2, 3, 4}) c.Assert(cmd.Args, gc.DeepEquals, []string{"fake-command", "arg1", "arg2"}) }
func (s *prepareSuite) TestPrepareCapturesEnvironment(c *gc.C) { baseConfig, err := config.New(config.UseDefaults, map[string]interface{}{ "type": provider.Local, "name": "test", }) c.Assert(err, gc.IsNil) provider, err := environs.Provider(provider.Local) c.Assert(err, gc.IsNil) for i, test := range []struct { message string extraConfig map[string]interface{} env map[string]string aptOutput string expectedProxy proxy.Settings expectedAptProxy proxy.Settings }{{ message: "nothing set", }, { message: "grabs proxy from environment", env: map[string]string{ "http_proxy": "http://[email protected]", "HTTPS_PROXY": "https://[email protected]", "ftp_proxy": "ftp://[email protected]", "no_proxy": "localhost,10.0.3.1", }, expectedProxy: proxy.Settings{ Http: "http://[email protected]", Https: "https://[email protected]", Ftp: "ftp://[email protected]", NoProxy: "localhost,10.0.3.1", }, expectedAptProxy: proxy.Settings{ Http: "http://[email protected]", Https: "https://[email protected]", Ftp: "ftp://[email protected]", }, }, { message: "skips proxy from environment if http-proxy set", extraConfig: map[string]interface{}{ "http-proxy": "http://[email protected]", }, env: map[string]string{ "http_proxy": "http://[email protected]", "HTTPS_PROXY": "https://[email protected]", "ftp_proxy": "ftp://[email protected]", }, expectedProxy: proxy.Settings{ Http: "http://[email protected]", }, expectedAptProxy: proxy.Settings{ Http: "http://[email protected]", }, }, { message: "skips proxy from environment if https-proxy set", extraConfig: map[string]interface{}{ "https-proxy": "https://[email protected]", }, env: map[string]string{ "http_proxy": "http://[email protected]", "HTTPS_PROXY": "https://[email protected]", "ftp_proxy": "ftp://[email protected]", }, expectedProxy: proxy.Settings{ Https: "https://[email protected]", }, expectedAptProxy: proxy.Settings{ Https: "https://[email protected]", }, }, { message: "skips proxy from environment if ftp-proxy set", extraConfig: map[string]interface{}{ "ftp-proxy": "ftp://[email protected]", }, env: map[string]string{ "http_proxy": "http://[email protected]", "HTTPS_PROXY": "https://[email protected]", "ftp_proxy": "ftp://[email protected]", }, expectedProxy: proxy.Settings{ Ftp: "ftp://[email protected]", }, expectedAptProxy: proxy.Settings{ Ftp: "ftp://[email protected]", }, }, { message: "skips proxy from environment if no-proxy set", extraConfig: map[string]interface{}{ "no-proxy": "localhost,10.0.3.1", }, env: map[string]string{ "http_proxy": "http://[email protected]", "HTTPS_PROXY": "https://[email protected]", "ftp_proxy": "ftp://[email protected]", }, expectedProxy: proxy.Settings{ NoProxy: "localhost,10.0.3.1", }, }, { message: "apt-proxies detected", aptOutput: `CommandLine::AsString "apt-config dump"; Acquire::http::Proxy "10.0.3.1:3142"; Acquire::https::Proxy "false"; Acquire::ftp::Proxy "none"; Acquire::magic::Proxy "none"; `, expectedAptProxy: proxy.Settings{ Http: "10.0.3.1:3142", Https: "false", Ftp: "none", }, }, { message: "apt-proxies not used if apt-http-proxy set", extraConfig: map[string]interface{}{ "apt-http-proxy": "value-set", }, aptOutput: `CommandLine::AsString "apt-config dump"; Acquire::http::Proxy "10.0.3.1:3142"; Acquire::https::Proxy "false"; Acquire::ftp::Proxy "none"; Acquire::magic::Proxy "none"; `, expectedAptProxy: proxy.Settings{ Http: "value-set", }, }, { message: "apt-proxies not used if apt-https-proxy set", extraConfig: map[string]interface{}{ "apt-https-proxy": "value-set", }, aptOutput: `CommandLine::AsString "apt-config dump"; Acquire::http::Proxy "10.0.3.1:3142"; Acquire::https::Proxy "false"; Acquire::ftp::Proxy "none"; Acquire::magic::Proxy "none"; `, expectedAptProxy: proxy.Settings{ Https: "value-set", }, }, { message: "apt-proxies not used if apt-ftp-proxy set", extraConfig: map[string]interface{}{ "apt-ftp-proxy": "value-set", }, aptOutput: `CommandLine::AsString "apt-config dump"; Acquire::http::Proxy "10.0.3.1:3142"; Acquire::https::Proxy "false"; Acquire::ftp::Proxy "none"; Acquire::magic::Proxy "none"; `, expectedAptProxy: proxy.Settings{ Ftp: "value-set", }, }} { c.Logf("\n%v: %s", i, test.message) cleanup := []func(){} for key, value := range test.env { restore := testing.PatchEnvironment(key, value) cleanup = append(cleanup, restore) } _, restore := testing.HookCommandOutput(&apt.CommandOutput, []byte(test.aptOutput), nil) cleanup = append(cleanup, restore) testConfig := baseConfig if test.extraConfig != nil { testConfig, err = baseConfig.Apply(test.extraConfig) c.Assert(err, gc.IsNil) } env, err := provider.Prepare(coretesting.Context(c), testConfig) c.Assert(err, gc.IsNil) envConfig := env.Config() c.Assert(envConfig.HttpProxy(), gc.Equals, test.expectedProxy.Http) c.Assert(envConfig.HttpsProxy(), gc.Equals, test.expectedProxy.Https) c.Assert(envConfig.FtpProxy(), gc.Equals, test.expectedProxy.Ftp) c.Assert(envConfig.NoProxy(), gc.Equals, test.expectedProxy.NoProxy) c.Assert(envConfig.AptHttpProxy(), gc.Equals, test.expectedAptProxy.Http) c.Assert(envConfig.AptHttpsProxy(), gc.Equals, test.expectedAptProxy.Https) c.Assert(envConfig.AptFtpProxy(), gc.Equals, test.expectedAptProxy.Ftp) for _, clean := range cleanup { clean() } } }