func (*OpenSuite) TestNewFromNameWithInvalidEnvironConfig(c *gc.C) { store := configstore.NewMem() e, err := environs.NewFromName("erewhemos", store) c.Assert(err, gc.Equals, environs.ErrNotBootstrapped) c.Assert(e, gc.IsNil) }
func (c *DestroyEnvironmentCommand) Run(ctx *cmd.Context) (result error) { store, err := configstore.Default() if err != nil { return fmt.Errorf("cannot open environment info storage: %v", err) } environ, err := environs.NewFromName(c.envName, store) if err != nil { if environs.IsEmptyConfig(err) { // Delete the .jenv file and call it done. ctx.Infof("removing empty environment file") return environs.DestroyInfo(c.envName, store) } return err } if !c.assumeYes { fmt.Fprintf(ctx.Stdout, destroyEnvMsg, environ.Name(), environ.Config().Type()) scanner := bufio.NewScanner(ctx.Stdin) scanner.Scan() err := scanner.Err() if err != nil && err != io.EOF { return fmt.Errorf("Environment destruction aborted: %s", err) } answer := strings.ToLower(scanner.Text()) if answer != "y" && answer != "yes" { return errors.New("environment destruction aborted") } } // If --force is supplied, then don't attempt to use the API. // This is necessary to destroy broken environments, where the // API server is inaccessible or faulty. if !c.force { defer func() { if result == nil { return } logger.Errorf(`failed to destroy environment %q If the environment is unusable, then you may run juju destroy-environment --force to forcefully destroy the environment. Upon doing so, review your environment provider console for any resources that need to be cleaned up. `, c.envName) }() apiclient, err := juju.NewAPIClientFromName(c.envName) if err != nil { return fmt.Errorf("cannot connect to API: %v", err) } defer apiclient.Close() err = apiclient.DestroyEnvironment() if err != nil && !params.IsCodeNotImplemented(err) { return fmt.Errorf("destroying environment: %v", err) } } return environs.Destroy(environ, store) }
func assertEnvironNotDestroyed(c *gc.C, env environs.Environ, store configstore.Storage) { info, err := store.ReadInfo(env.Config().Name()) c.Assert(err, gc.IsNil) c.Assert(info.Initialized(), jc.IsTrue) _, err = environs.NewFromName(env.Config().Name(), store) c.Assert(err, gc.IsNil) }
func (*OpenSuite) TestNewFromName(c *gc.C) { store := configstore.NewMem() ctx := testing.Context(c) e, err := environs.PrepareFromName("erewhemos", ctx, store) c.Assert(err, gc.IsNil) e, err = environs.NewFromName("erewhemos", store) c.Assert(err, gc.IsNil) c.Assert(e.Config().Name(), gc.Equals, "erewhemos") }
// NewConnFromName returns a Conn pointing at the environName environment, or the // default environment if not specified. func NewConnFromName(environName string) (*Conn, error) { store, err := configstore.Default() if err != nil { return nil, err } environ, err := environs.NewFromName(environName, store) if err != nil { return nil, err } return NewConn(environ) }
func (*OpenSuite) TestNewFromNameWithInvalidInfo(c *gc.C) { store := configstore.NewMem() cfg, _, err := environs.ConfigForName("erewhemos", store) c.Assert(err, gc.IsNil) info := store.CreateInfo("erewhemos") // The configuration from environments.yaml is invalid // because it doesn't contain the state-id attribute which // the dummy environment adds at Prepare time. info.SetBootstrapConfig(cfg.AllAttrs()) err = info.Write() c.Assert(err, gc.IsNil) e, err := environs.NewFromName("erewhemos", store) c.Assert(err, gc.ErrorMatches, "environment is not prepared") c.Assert(e, gc.IsNil) }
func (s *BootstrapSuite) run(c *gc.C, test bootstrapTest) (restore gitjujutesting.Restorer) { // Create home with dummy provider and remove all // of its envtools. env := resetJujuHome(c, "peckham") // Although we're testing PrepareEndpointsForCaching interactions // separately in the juju package, here we just ensure it gets // called with the right arguments. prepareCalled := false addrConnectedTo := "localhost:17070" restore = gitjujutesting.PatchValue( &prepareEndpointsForCaching, func(info configstore.EnvironInfo, hps [][]network.HostPort, addr network.HostPort) (_, _ []string, _ bool) { prepareCalled = true addrs, hosts, changed := juju.PrepareEndpointsForCaching(info, hps, addr) // Because we're bootstrapping the addresses will always // change, as there's no .jenv file saved yet. c.Assert(changed, jc.IsTrue) return addrs, hosts, changed }, ) if test.version != "" { useVersion := strings.Replace(test.version, "%LTS%", config.LatestLtsSeries(), 1) origVersion := version.Current version.Current = version.MustParseBinary(useVersion) restore = restore.Add(func() { version.Current = origVersion }) } if test.hostArch != "" { origArch := arch.HostArch arch.HostArch = func() string { return test.hostArch } restore = restore.Add(func() { arch.HostArch = origArch }) } // Run command and check for uploads. opc, errc := cmdtesting.RunCommand(cmdtesting.NullContext(c), envcmd.Wrap(new(BootstrapCommand)), test.args...) // Check for remaining operations/errors. if test.err != "" { err := <-errc c.Assert(err, gc.NotNil) stripped := strings.Replace(err.Error(), "\n", "", -1) c.Check(stripped, gc.Matches, test.err) return restore } if !c.Check(<-errc, gc.IsNil) { return restore } opBootstrap := (<-opc).(dummy.OpBootstrap) c.Check(opBootstrap.Env, gc.Equals, "peckham") c.Check(opBootstrap.Args.Constraints, gc.DeepEquals, test.constraints) c.Check(opBootstrap.Args.Placement, gc.Equals, test.placement) opFinalizeBootstrap := (<-opc).(dummy.OpFinalizeBootstrap) c.Check(opFinalizeBootstrap.Env, gc.Equals, "peckham") c.Check(opFinalizeBootstrap.InstanceConfig.Tools, gc.NotNil) if test.upload != "" { c.Check(opFinalizeBootstrap.InstanceConfig.Tools.Version.String(), gc.Equals, test.upload) } store, err := configstore.Default() c.Assert(err, jc.ErrorIsNil) // Check a CA cert/key was generated by reloading the environment. env, err = environs.NewFromName("peckham", store) c.Assert(err, jc.ErrorIsNil) _, hasCert := env.Config().CACert() c.Check(hasCert, jc.IsTrue) _, hasKey := env.Config().CAPrivateKey() c.Check(hasKey, jc.IsTrue) info, err := store.ReadInfo("peckham") c.Assert(err, jc.ErrorIsNil) c.Assert(info, gc.NotNil) c.Assert(prepareCalled, jc.IsTrue) c.Assert(info.APIEndpoint().Addresses, gc.DeepEquals, []string{addrConnectedTo}) return restore }
func (test bootstrapTest) run(c *gc.C) { // Create home with dummy provider and remove all // of its envtools. env := resetJujuHome(c) if test.version != "" { useVersion := strings.Replace(test.version, "%LTS%", config.LatestLtsSeries(), 1) origVersion := version.Current version.Current = version.MustParseBinary(useVersion) defer func() { version.Current = origVersion }() } if test.hostArch != "" { origVersion := arch.HostArch arch.HostArch = func() string { return test.hostArch } defer func() { arch.HostArch = origVersion }() } if test.upload == "" { usefulVersion := version.Current usefulVersion.Series = config.PreferredSeries(env.Config()) envtesting.AssertUploadFakeToolsVersions(c, env.Storage(), usefulVersion) } // Run command and check for uploads. opc, errc := cmdtesting.RunCommand(cmdtesting.NullContext(c), envcmd.Wrap(new(BootstrapCommand)), test.args...) // Check for remaining operations/errors. if test.err != "" { err := <-errc stripped := strings.Replace(err.Error(), "\n", "", -1) c.Check(stripped, gc.Matches, test.err) return } if !c.Check(<-errc, gc.IsNil) { return } opPutBootstrapVerifyFile := (<-opc).(dummy.OpPutFile) c.Check(opPutBootstrapVerifyFile.Env, gc.Equals, "peckham") c.Check(opPutBootstrapVerifyFile.FileName, gc.Equals, environs.VerificationFilename) opBootstrap := (<-opc).(dummy.OpBootstrap) c.Check(opBootstrap.Env, gc.Equals, "peckham") c.Check(opBootstrap.Args.Constraints, gc.DeepEquals, test.constraints) c.Check(opBootstrap.Args.Placement, gc.Equals, test.placement) c.Check(opBootstrap.Args.KeepBroken, gc.Equals, test.keepBroken) opFinalizeBootstrap := (<-opc).(dummy.OpFinalizeBootstrap) c.Check(opFinalizeBootstrap.Env, gc.Equals, "peckham") c.Check(opFinalizeBootstrap.MachineConfig.Tools, gc.NotNil) if test.upload != "" { c.Check(opFinalizeBootstrap.MachineConfig.Tools.Version.String(), gc.Equals, test.upload) } store, err := configstore.Default() c.Assert(err, gc.IsNil) // Check a CA cert/key was generated by reloading the environment. env, err = environs.NewFromName("peckham", store) c.Assert(err, gc.IsNil) _, hasCert := env.Config().CACert() c.Check(hasCert, gc.Equals, true) _, hasKey := env.Config().CAPrivateKey() c.Check(hasKey, gc.Equals, true) info, err := store.ReadInfo("peckham") c.Assert(err, gc.IsNil) c.Assert(info, gc.NotNil) c.Assert(info.APIEndpoint().Addresses, gc.DeepEquals, []string{"localhost:17070"}) }
func (test bootstrapTest) run(c *gc.C) { // Create home with dummy provider and remove all // of its envtools. env := resetJujuHome(c) if test.version != "" { useVersion := strings.Replace(test.version, "%LTS%", config.LatestLtsSeries(), 1) origVersion := version.Current version.Current = version.MustParseBinary(useVersion) defer func() { version.Current = origVersion }() } if test.hostArch != "" { origVersion := arch.HostArch arch.HostArch = func() string { return test.hostArch } defer func() { arch.HostArch = origVersion }() } uploadCount := len(test.uploads) if uploadCount == 0 { usefulVersion := version.Current usefulVersion.Series = config.PreferredSeries(env.Config()) envtesting.AssertUploadFakeToolsVersions(c, env.Storage(), usefulVersion) } // Run command and check for uploads. opc, errc := runCommand(nullContext(c), envcmd.Wrap(new(BootstrapCommand)), test.args...) // Check for remaining operations/errors. if test.err != "" { err := <-errc stripped := strings.Replace(err.Error(), "\n", "", -1) c.Check(stripped, gc.Matches, test.err) return } if !c.Check(<-errc, gc.IsNil) { return } if uploadCount > 0 { for i := 0; i < uploadCount; i++ { c.Check((<-opc).(dummy.OpPutFile).Env, gc.Equals, "peckham") } list, err := envtools.FindTools( env, version.Current.Major, version.Current.Minor, coretools.Filter{}, envtools.DoNotAllowRetry) c.Check(err, gc.IsNil) c.Logf("found: " + list.String()) urls := list.URLs() c.Check(urls, gc.HasLen, len(test.uploads)) for _, v := range test.uploads { v := strings.Replace(v, "%LTS%", config.LatestLtsSeries(), 1) c.Logf("seeking: " + v) vers := version.MustParseBinary(v) _, found := urls[vers] c.Check(found, gc.Equals, true) } } if len(test.uploads) > 0 { indexFile := (<-opc).(dummy.OpPutFile) c.Check(indexFile.FileName, gc.Equals, "tools/streams/v1/index.json") productFile := (<-opc).(dummy.OpPutFile) c.Check(productFile.FileName, gc.Equals, "tools/streams/v1/com.ubuntu.juju:released:tools.json") } opPutBootstrapVerifyFile := (<-opc).(dummy.OpPutFile) c.Check(opPutBootstrapVerifyFile.Env, gc.Equals, "peckham") c.Check(opPutBootstrapVerifyFile.FileName, gc.Equals, environs.VerificationFilename) opPutBootstrapInitFile := (<-opc).(dummy.OpPutFile) c.Check(opPutBootstrapInitFile.Env, gc.Equals, "peckham") c.Check(opPutBootstrapInitFile.FileName, gc.Equals, "provider-state") opBootstrap := (<-opc).(dummy.OpBootstrap) c.Check(opBootstrap.Env, gc.Equals, "peckham") c.Check(opBootstrap.Args.Constraints, gc.DeepEquals, test.constraints) c.Check(opBootstrap.Args.Placement, gc.Equals, test.placement) store, err := configstore.Default() c.Assert(err, gc.IsNil) // Check a CA cert/key was generated by reloading the environment. env, err = environs.NewFromName("peckham", store) c.Assert(err, gc.IsNil) _, hasCert := env.Config().CACert() c.Check(hasCert, gc.Equals, true) _, hasKey := env.Config().CAPrivateKey() c.Check(hasKey, gc.Equals, true) }