func (*EnvironsCertSuite) TestEnsureCertificate(c *C) { defer testing.MakeFakeHome(c, testing.SingleEnvConfig).Restore() env, err := environs.NewFromName(testing.SampleEnvName) c.Assert(err, IsNil) writeCalled := false created, err := environs.EnsureCertificate(env, func(name string, cert, key []byte) error { writeCalled = true return nil }) c.Assert(err, IsNil) c.Assert(created, Equals, environs.CertCreated) c.Assert(writeCalled, Equals, true) }
// Run connects to the environment specified on the command line and bootstraps // a juju in that environment if none already exists. If there is as yet no environments.yaml file, // the user is informed how to create one. func (c *BootstrapCommand) Run(context *cmd.Context) error { environ, err := environs.NewFromName(c.EnvName) if err != nil { if os.IsNotExist(err) { out := context.Stderr fmt.Fprintln(out, "No juju environment configuration file exists.") fmt.Fprintln(out, "Please create a configuration by running:") fmt.Fprintln(out, " juju init -w") fmt.Fprintln(out, "then edit the file to configure your juju environment.") fmt.Fprintln(out, "You can then re-run bootstrap.") } return err } // TODO: if in verbose mode, write out to Stdout if a new cert was created. _, err = environs.EnsureCertificate(environ, environs.WriteCertAndKey) if err != nil { return err } // If we are using a local provider, always upload tools. if environ.Config().Type() == provider.Local { c.UploadTools = true } if c.UploadTools { // Force version.Current, for consistency with subsequent upgrade-juju // (see UpgradeJujuCommand). forceVersion := uploadVersion(version.Current.Number, nil) cfg := environ.Config() series := getUploadSeries(cfg, c.Series) tools, err := uploadTools(environ.Storage(), &forceVersion, series...) if err != nil { return err } cfg, err = cfg.Apply(map[string]interface{}{ "agent-version": tools.Version.Number.String(), }) if err == nil { err = environ.SetConfig(cfg) } if err != nil { return fmt.Errorf("failed to update environment configuration: %v", err) } } err = c.ensureToolsAvailability(environ, context) if err != nil { return err } return environs.Bootstrap(environ, c.Constraints) }
func (*EnvironsCertSuite) TestEnsureCertificateMissingKey(c *C) { defer testing.MakeFakeHome(c, testing.SingleEnvConfig).Restore() envName := testing.SampleEnvName keyPath := testing.HomePath(".juju", envName+"-cert.pem") ioutil.WriteFile(keyPath, []byte(testing.CACert), 0600) // Need to create the environment after the cert has been written. env, err := environs.NewFromName(envName) c.Assert(err, IsNil) writeCalled := false _, err = environs.EnsureCertificate(env, func(name string, cert, key []byte) error { writeCalled = true return nil }) c.Assert(err, ErrorMatches, "environment configuration with a certificate but no CA private key") c.Assert(writeCalled, Equals, false) }