func (*NewAPIClientSuite) TestNameNotDefault(c *gc.C) { defer coretesting.MakeMultipleEnvHome(c).Restore() envName := coretesting.SampleCertName + "-2" bootstrapEnv(c, envName, defaultConfigStore(c)) apiclient, err := juju.NewAPIClientFromName(envName) c.Assert(err, gc.IsNil) defer apiclient.Close() assertEnvironmentName(c, apiclient, envName) }
func (*NewConnSuite) TestNewConnFromNameNotDefault(c *gc.C) { defer coretesting.MakeMultipleEnvHome(c).Restore() // The default environment is "erewhemos", so make sure we get what we ask for. const envName = "erewhemos-2" bootstrapEnv(c, envName, defaultConfigStore(c)) conn, err := juju.NewConnFromName(envName) c.Assert(err, gc.IsNil) defer assertClose(c, conn) c.Assert(conn.Environ.Name(), gc.Equals, envName) }
func (s *NewAPIClientSuite) TestNameDefault(c *gc.C) { defer coretesting.MakeMultipleEnvHome(c).Restore() // The connection logic should not delay the config connection // at all when there is no environment info available. // Make sure of that by providing a suitably long delay // and checking that the connection happens within that // time. s.PatchValue(juju.ProviderConnectDelay, coretesting.LongWait) bootstrapEnv(c, coretesting.SampleEnvName, defaultConfigStore(c)) startTime := time.Now() apiclient, err := juju.NewAPIClientFromName("") c.Assert(err, gc.IsNil) defer apiclient.Close() c.Assert(time.Since(startTime), jc.LessThan, coretesting.LongWait) // We should get the default sample environment if we ask for "" assertEnvironmentName(c, apiclient, coretesting.SampleEnvName) }
func (*NewAPIClientSuite) TestWithBootstrapConfigTakesPrecedence(c *gc.C) { // We want to make sure that the code is using the bootstrap // config rather than information from environments.yaml, // even when there is an entry in environments.yaml // We can do that by changing the info bootstrap config // so it has a different environment name. defer coretesting.MakeMultipleEnvHome(c).Restore() store := configstore.NewMem() bootstrapEnv(c, coretesting.SampleEnvName, store) info, err := store.ReadInfo(coretesting.SampleEnvName) c.Assert(err, gc.IsNil) envName2 := coretesting.SampleCertName + "-2" info2, err := store.CreateInfo(envName2) c.Assert(err, gc.IsNil) info2.SetBootstrapConfig(info.BootstrapConfig()) err = info2.Write() c.Assert(err, gc.IsNil) // Now we have info for envName2 which will actually // cause a connection to the originally bootstrapped // state. apiOpen := func(*api.Info, api.DialOpts) (juju.APIState, error) { return &mockAPIState{}, nil } st, err := juju.NewAPIFromStore(envName2, store, apiOpen) c.Check(err, gc.IsNil) st.Close() // Sanity check that connecting to the envName2 // but with no info fails. // Currently this panics with an "environment not prepared" error. // Disable for now until an upcoming branch fixes it. // err = info2.Destroy() // c.Assert(err, gc.IsNil) // st, err = juju.NewAPIFromStore(envName2, store) // if err == nil { // st.Close() // } // c.Assert(err, gc.ErrorMatches, "fooobie") }
func (s *APIEndpointForEnvSuite) TestAPIEndpointNotCached(c *gc.C) { defer coretesting.MakeMultipleEnvHome(c).Restore() 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 := &mockAPIState{ apiHostPorts: [][]instance.HostPort{ instance.AddressesWithPort([]instance.Address{instance.NewAddress("0.1.2.3", instance.NetworkUnknown)}, 1234), }, } 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()) 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"}) }
func (s *APIEndpointForEnvSuite) TestAPIEndpointForEnvSuchName(c *gc.C) { defer coretesting.MakeMultipleEnvHome(c).Restore() _, err := juju.APIEndpointForEnv("no-such-env", false) c.Check(err, jc.Satisfies, errors.IsNotFound) c.Check(err, gc.ErrorMatches, `environment "no-such-env" not found`) }