func (s *ConfigSuite) SetUpSuite(c *gc.C) { s.FakeJujuXDGDataHomeSuite.SetUpSuite(c) restoreSdcAccount := testing.PatchEnvironment(jp.SdcAccount, "tester") s.AddCleanup(func(*gc.C) { restoreSdcAccount() }) restoreSdcKeyId := testing.PatchEnvironment(jp.SdcKeyId, "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00") s.AddCleanup(func(*gc.C) { restoreSdcKeyId() }) s.privateKeyData = generatePrivateKey(c) jp.RegisterMachinesEndpoint() s.AddCleanup(func(*gc.C) { jp.UnregisterMachinesEndpoint() }) }
func (s *ConfigSuite) SetUpSuite(c *gc.C) { s.FakeHomeSuite.SetUpSuite(c) restoreSdcAccount := testing.PatchEnvironment(jp.SdcAccount, "tester") s.AddSuiteCleanup(func(*gc.C) { restoreSdcAccount() }) restoreSdcKeyId := testing.PatchEnvironment(jp.SdcKeyId, "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00") s.AddSuiteCleanup(func(*gc.C) { restoreSdcKeyId() }) restoreMantaUser := testing.PatchEnvironment(jp.MantaUser, "tester") s.AddSuiteCleanup(func(*gc.C) { restoreMantaUser() }) restoreMantaKeyId := testing.PatchEnvironment(jp.MantaKeyId, "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00") s.AddSuiteCleanup(func(*gc.C) { restoreMantaKeyId() }) }
func (s *apiSuite) TestUnsignedTermsEnvTermsURL(c *gc.C) { cleanup := testing.PatchEnvironment("JUJU_TERMS", "http://example.com") defer cleanup() s.httpClient.status = http.StatusOK s.httpClient.SetBody(c, []terms.GetTermsResponse{ { Name: "hello-world-terms", Revision: 1, Content: "terms doc content", }, { Name: "hello-universe-terms", Revision: 1, Content: "universal terms doc content", }, }) _, err := s.client.GetUnsignedTerms(&terms.CheckAgreementsRequest{ Terms: []string{ "hello-world-terms/1", "hello-universe-terms/1", }, }) c.Assert(err, jc.ErrorIsNil) c.Assert(s.httpClient.Calls(), gc.HasLen, 1) s.httpClient.CheckCall(c, 0, "Do", "http://example.com/agreement?Terms=hello-world-terms%2F1&Terms=hello-universe-terms%2F1") s.httpClient.ResetCalls() }
func (s *apiSuite) TestSignedAgreementsEnvTermsURL(c *gc.C) { cleanup := testing.PatchEnvironment("JUJU_TERMS", "http://example.com") defer cleanup() t := time.Now().UTC() s.httpClient.status = http.StatusOK s.httpClient.SetBody(c, []terms.AgreementResponse{ { User: "******", Term: "hello-world-terms", Revision: 1, CreatedOn: t, }, { User: "******", Term: "hello-universe-terms", Revision: 42, CreatedOn: t, }, }) _, err := s.client.GetUsersAgreements() c.Assert(err, jc.ErrorIsNil) c.Assert(s.httpClient.Calls(), gc.HasLen, 1) s.httpClient.CheckCall(c, 0, "Do", "http://example.com/agreements") }
func (s *formSuite) TestIOPrompter(c *gc.C) { for i, test := range ioPrompterTests { func() { c.Logf("%d. %s", i, test.about) for k, v := range test.environment { defer testing.PatchEnvironment(k, v)() } outBuf := new(bytes.Buffer) prompter := form.IOPrompter{ In: strings.NewReader(test.input), Out: outBuf, } if test.title != "" { err := prompter.ShowTitle(test.title) c.Assert(err, gc.IsNil) } for j, p := range test.prompts { result, err := prompter.Prompt(p.name, p.attr) if err != nil { c.Assert(err, gc.ErrorMatches, test.expectError) c.Assert(outBuf.String(), gc.Equals, test.expectOutput) return } c.Assert(result, gc.Equals, test.expect[j]) } c.Assert(test.expectError, gc.Equals, "", gc.Commentf("did not reveive expected error")) c.Assert(outBuf.String(), gc.Equals, test.expectOutput) }() } }
func (*PatchEnvironmentSuite) TestPatchEnvironment(c *gc.C) { const envName = "TESTING_PATCH_ENVIRONMENT" // remember the old value, and set it to something we can check oldValue := os.Getenv(envName) os.Setenv(envName, "initial") restore := testing.PatchEnvironment(envName, "new value") // Using check to make sure the environment gets set back properly in the test. c.Check(os.Getenv(envName), gc.Equals, "new value") restore() c.Check(os.Getenv(envName), gc.Equals, "initial") os.Setenv(envName, oldValue) }
func (s *formSuite) TestDefaultFromEnv(c *gc.C) { for i, test := range defaultFromEnvTests { c.Logf("%d. %s", i, test.about) func() { for k, v := range test.environment { defer testing.PatchEnvironment(k, v)() } result := form.DefaultFromEnv(test.attr) c.Assert(result, gc.Equals, test.expect) }() } }
func (s *apiSuite) TestSaveAgreementEnvTermsURL(c *gc.C) { cleanup := testing.PatchEnvironment("JUJU_TERMS", "http://example.com") defer cleanup() s.httpClient.status = http.StatusOK s.httpClient.SetBody(c, terms.SaveAgreementResponses{}) p1 := &terms.SaveAgreements{ Agreements: []terms.SaveAgreement{{ TermName: "hello-world-terms", TermRevision: 1, }}} _, err := s.client.SaveAgreement(p1) c.Assert(err, jc.ErrorIsNil) c.Assert(s.httpClient.Calls(), gc.HasLen, 1) s.httpClient.CheckCall(c, 0, "DoWithBody", "http://example.com/agreement") }
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() } } }