func (s *APIEndpointForEnvSuite) TestAPIEndpointNotMachineLocal(c *gc.C) { store := newConfigStore("env-name", dummyStoreInfo) called := 0 hostPorts := [][]network.HostPort{ network.AddressesWithPort([]network.Address{ network.NewAddress("1.0.0.1", network.ScopePublic), network.NewAddress("192.0.0.1", network.ScopeCloudLocal), network.NewAddress("127.0.0.1", network.ScopeMachineLocal), network.NewAddress("localhost", network.ScopeMachineLocal), }, 1234), network.AddressesWithPort([]network.Address{ network.NewAddress("1.0.0.2", network.ScopeUnknown), network.NewAddress("2002:0:0:0:0:0:100:2", network.ScopeUnknown), network.NewAddress("::1", network.ScopeMachineLocal), network.NewAddress("127.0.0.1", network.ScopeMachineLocal), network.NewAddress("localhost", network.ScopeMachineLocal), }, 1235), } expectState := &mockAPIState{apiHostPorts: hostPorts} apiOpen := func(_ *api.Info, _ api.DialOpts) (juju.APIState, error) { called++ return expectState, nil } endpoint, err := juju.APIEndpointInStore("env-name", true, store, apiOpen) c.Assert(err, gc.IsNil) c.Check(called, gc.Equals, 1) c.Check(endpoint.Addresses, gc.DeepEquals, []string{ "1.0.0.1:1234", "192.0.0.1:1234", "1.0.0.2:1235", }) }
func (s *APIEndpointForEnvSuite) TestAPIEndpointInStoreCached(c *gc.C) { store := newConfigStore("env-name", dummyStoreInfo) apiOpen := func(apiInfo *api.Info, opts api.DialOpts) (juju.APIState, error) { return nil, nil } endpoint, err := juju.APIEndpointInStore("env-name", false, store, apiOpen) c.Assert(err, gc.IsNil) c.Check(endpoint, gc.DeepEquals, dummyStoreInfo.endpoint) }
func (s *APIEndpointForEnvSuite) TestAPIEndpointRefresh(c *gc.C) { store := newConfigStore("env-name", dummyStoreInfo) called := 0 expectState := mockedAPIState(true, false) apiOpen := func(apiInfo *api.Info, opts api.DialOpts) (juju.APIState, error) { checkCommonAPIInfoAttrs(c, apiInfo, opts) called++ return expectState, nil } endpoint, err := juju.APIEndpointInStore("env-name", false, store, apiOpen) c.Assert(err, gc.IsNil) c.Assert(called, gc.Equals, 0) c.Check(endpoint.Addresses, gc.DeepEquals, []string{"foo.invalid"}) // However, if we ask to refresh them, we'll connect to the API and get // the freshest set endpoint, err = juju.APIEndpointInStore("env-name", true, store, apiOpen) c.Assert(err, gc.IsNil) c.Check(called, gc.Equals, 1) // This refresh now gives us the values return by APIHostPorts c.Check(endpoint.Addresses, gc.DeepEquals, []string{"0.1.2.3:1234"}) }
func (s *APIEndpointForEnvSuite) TestAPIEndpointNotCached(c *gc.C) { coretesting.WriteEnvironments(c, coretesting.MultipleEnvConfig) store, err := configstore.Default() c.Assert(err, gc.IsNil) ctx := coretesting.Context(c) env, err := environs.PrepareFromName("erewhemos", ctx, store) c.Assert(err, gc.IsNil) defer dummy.Reset() envtesting.UploadFakeTools(c, env.Storage()) err = bootstrap.Bootstrap(ctx, env, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) // Note: if we get Bootstrap to start caching the API endpoint // immediately, we'll still want to have this test for compatibility. // We can just write blank info instead of reading and checking it is empty. savedInfo, err := store.ReadInfo("erewhemos") c.Assert(err, gc.IsNil) // Ensure that the data isn't cached c.Check(savedInfo.APIEndpoint().Addresses, gc.HasLen, 0) called := 0 expectState := mockedAPIState(true, true) apiOpen := func(apiInfo *api.Info, opts api.DialOpts) (juju.APIState, error) { c.Check(apiInfo.Tag, gc.Equals, "user-admin") c.Check(string(apiInfo.CACert), gc.Equals, coretesting.CACert) c.Check(apiInfo.Password, gc.Equals, coretesting.DefaultMongoPassword) c.Check(opts, gc.DeepEquals, api.DefaultDialOpts()) // we didn't know about it when connecting c.Check(apiInfo.EnvironTag, gc.Equals, "") called++ return expectState, nil } endpoint, err := juju.APIEndpointInStore("erewhemos", false, store, apiOpen) c.Assert(err, gc.IsNil) c.Assert(called, gc.Equals, 1) c.Check(endpoint.Addresses, gc.DeepEquals, []string{"0.1.2.3:1234"}) c.Check(endpoint.EnvironUUID, gc.Equals, "fake-uuid") }