func (s *destroyEnvSuite) TestDestroyEnvironmentCommandConfirmation(c *gc.C) { var stdin, stdout bytes.Buffer ctx, err := cmd.DefaultContext() c.Assert(err, gc.IsNil) ctx.Stdout = &stdout ctx.Stdin = &stdin // Prepare the environment so we can destroy it. env, err := environs.PrepareFromName("dummyenv", nullContext(c), s.ConfigStore) c.Assert(err, gc.IsNil) assertEnvironNotDestroyed(c, env, s.ConfigStore) // Ensure confirmation is requested if "-y" is not specified. stdin.WriteString("n") opc, errc := runCommand(ctx, new(DestroyEnvironmentCommand), "dummyenv") c.Check(<-errc, gc.ErrorMatches, "environment destruction aborted") c.Check(<-opc, gc.IsNil) c.Check(stdout.String(), gc.Matches, "WARNING!.*dummyenv.*\\(type: dummy\\)(.|\n)*") assertEnvironNotDestroyed(c, env, s.ConfigStore) // EOF on stdin: equivalent to answering no. stdin.Reset() stdout.Reset() opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand), "dummyenv") c.Check(<-opc, gc.IsNil) c.Check(<-errc, gc.ErrorMatches, "environment destruction aborted") assertEnvironNotDestroyed(c, env, s.ConfigStore) // "--yes" passed: no confirmation request. stdin.Reset() stdout.Reset() opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand), "dummyenv", "--yes") c.Check(<-errc, gc.IsNil) c.Check((<-opc).(dummy.OpDestroy).Env, gc.Equals, "dummyenv") c.Check(stdout.String(), gc.Equals, "") assertEnvironDestroyed(c, env, s.ConfigStore) // Any of casing of "y" and "yes" will confirm. for _, answer := range []string{"y", "Y", "yes", "YES"} { // Prepare the environment so we can destroy it. s.Reset(c) env, err := environs.PrepareFromName("dummyenv", nullContext(c), s.ConfigStore) c.Assert(err, gc.IsNil) stdin.Reset() stdout.Reset() stdin.WriteString(answer) opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand), "dummyenv") c.Check(<-errc, gc.IsNil) c.Check((<-opc).(dummy.OpDestroy).Env, gc.Equals, "dummyenv") c.Check(stdout.String(), gc.Matches, "WARNING!.*dummyenv.*\\(type: dummy\\)(.|\n)*") assertEnvironDestroyed(c, env, s.ConfigStore) } }
func (s *syncToolsSuite) TestSyncToolsCommand(c *gc.C) { for i, test := range syncToolsCommandTests { c.Logf("test %d: %s", i, test.description) targetEnv, err := environs.PrepareFromName("test-target", nullContext(c), s.configStore) c.Assert(err, gc.IsNil) called := false syncTools = func(sctx *sync.SyncContext) error { c.Assert(sctx.AllVersions, gc.Equals, test.sctx.AllVersions) c.Assert(sctx.MajorVersion, gc.Equals, test.sctx.MajorVersion) c.Assert(sctx.MinorVersion, gc.Equals, test.sctx.MinorVersion) c.Assert(sctx.DryRun, gc.Equals, test.sctx.DryRun) c.Assert(sctx.Dev, gc.Equals, test.sctx.Dev) c.Assert(sctx.Public, gc.Equals, test.sctx.Public) c.Assert(sctx.Source, gc.Equals, test.sctx.Source) c.Assert(dummy.IsSameStorage(sctx.Target, targetEnv.Storage()), jc.IsTrue) called = true return nil } ctx, err := runSyncToolsCommand(c, test.args...) c.Assert(err, gc.IsNil) c.Assert(ctx, gc.NotNil) c.Assert(called, jc.IsTrue) s.Reset(c) } }
func (s *BootstrapSuite) TestBootstrapJenvWarning(c *gc.C) { env, fake := makeEmptyFakeHome(c) defer fake.Restore() defaultSeriesVersion := version.Current defaultSeriesVersion.Series = config.PreferredSeries(env.Config()) // Force a dev version by having an odd minor version number. // This is because we have not uploaded any tools and auto // upload is only enabled for dev versions. defaultSeriesVersion.Minor = 11 s.PatchValue(&version.Current, defaultSeriesVersion) store, err := configstore.Default() c.Assert(err, gc.IsNil) ctx := coretesting.Context(c) environs.PrepareFromName("peckham", ctx, store) logger := "jenv.warning.test" testWriter := &loggo.TestWriter{} loggo.RegisterWriter(logger, testWriter, loggo.WARNING) defer loggo.RemoveWriter(logger) _, errc := runCommand(ctx, new(BootstrapCommand), "-e", "peckham") c.Assert(<-errc, gc.IsNil) c.Assert(testWriter.Log, jc.LogMatches, []string{"ignoring environments.yaml: using bootstrap config in .*"}) }
func (s *destroyEnvSuite) TestDestroyEnvironmentCommandEFlag(c *gc.C) { // Prepare the environment so we can destroy it. _, err := environs.PrepareFromName("dummyenv", nullContext(c), s.ConfigStore) c.Assert(err, gc.IsNil) // check that either environment or the flag is mandatory opc, errc := runCommand(nullContext(c), new(DestroyEnvironmentCommand)) c.Check(<-errc, gc.Equals, NoEnvironmentError) // We don't allow them to supply both entries at the same time opc, errc = runCommand(nullContext(c), new(DestroyEnvironmentCommand), "-e", "dummyenv", "dummyenv", "--yes") c.Check(<-errc, gc.Equals, DoubleEnvironmentError) // We treat --environment the same way opc, errc = runCommand(nullContext(c), new(DestroyEnvironmentCommand), "--environment", "dummyenv", "dummyenv", "--yes") c.Check(<-errc, gc.Equals, DoubleEnvironmentError) // destroy using the -e flag opc, errc = runCommand(nullContext(c), new(DestroyEnvironmentCommand), "-e", "dummyenv", "--yes") c.Check(<-errc, gc.IsNil) c.Check((<-opc).(dummy.OpDestroy).Env, gc.Equals, "dummyenv") // Verify that the environment information has been removed. _, err = s.ConfigStore.ReadInfo("dummyenv") c.Assert(err, jc.Satisfies, errors.IsNotFound) }
func (s *destroyEnvSuite) TestDestroyEnvironmentCommandBroken(c *gc.C) { oldinfo, err := s.ConfigStore.ReadInfo("dummyenv") c.Assert(err, gc.IsNil) bootstrapConfig := oldinfo.BootstrapConfig() apiEndpoint := oldinfo.APIEndpoint() apiCredentials := oldinfo.APICredentials() err = oldinfo.Destroy() c.Assert(err, gc.IsNil) newinfo, err := s.ConfigStore.CreateInfo("dummyenv") c.Assert(err, gc.IsNil) bootstrapConfig["broken"] = "Destroy" newinfo.SetBootstrapConfig(bootstrapConfig) newinfo.SetAPIEndpoint(apiEndpoint) newinfo.SetAPICredentials(apiCredentials) err = newinfo.Write() c.Assert(err, gc.IsNil) // Prepare the environment so we can destroy it. _, err = environs.PrepareFromName("dummyenv", nullContext(c), s.ConfigStore) c.Assert(err, gc.IsNil) // destroy with broken environment opc, errc := runCommand(nullContext(c), new(DestroyEnvironmentCommand), "dummyenv", "--yes") op, ok := (<-opc).(dummy.OpDestroy) c.Assert(ok, jc.IsTrue) c.Assert(op.Error, gc.ErrorMatches, "dummy.Destroy is broken") c.Check(<-errc, gc.Equals, op.Error) c.Check(<-opc, gc.IsNil) }
func (s *datasourceSuite) SetUpTest(c *gc.C) { s.home = testing.MakeFakeHome(c, existingEnv, "existing") environ, err := environs.PrepareFromName("test", testing.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) s.stor = environ.Storage() s.baseURL, err = s.stor.URL("") c.Assert(err, gc.IsNil) }
func (*OpenSuite) TestPrepareFromName(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfigNoDefault, testing.SampleCertName).Restore() ctx := testing.Context(c) e, err := environs.PrepareFromName("erewhemos", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) c.Assert(e.Name(), gc.Equals, "erewhemos") // Check we can access storage ok, which implies the environment has been prepared. c.Assert(e.Storage(), gc.NotNil) }
// makeEmptyFakeHome creates a faked home without envtools. func makeEmptyFakeHome(c *gc.C) (environs.Environ, *coretesting.FakeHome) { fake := coretesting.MakeFakeHome(c, envConfig) dummy.Reset() store, err := configstore.Default() c.Assert(err, gc.IsNil) env, err := environs.PrepareFromName("peckham", nullContext(c), store) c.Assert(err, gc.IsNil) envtesting.RemoveAllTools(c, env) return env, fake }
func (*OpenSuite) TestNewFromName(c *gc.C) { defer testing.MakeFakeHome(c, testing.MultipleEnvConfigNoDefault, testing.SampleCertName).Restore() 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.Name(), gc.Equals, "erewhemos") }
func bootstrapEnv(c *gc.C, envName string, store configstore.Storage) { if store == nil { store = configstore.NewMem() } ctx := coretesting.Context(c) env, err := environs.PrepareFromName(envName, ctx, store) c.Assert(err, gc.IsNil) envtesting.UploadFakeTools(c, env.Storage()) err = bootstrap.Bootstrap(ctx, env, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) }
func (s *verifyStorageSuite) TestVerifyStorageFails(c *gc.C) { defer testing.MakeFakeHome(c, existingEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) stor := environ.Storage() someError := errors.Unauthorizedf("you shall not pass") dummy.Poison(stor, environs.VerificationFilename, someError) err = environs.VerifyStorage(stor) c.Assert(err, gc.Equals, environs.VerifyStorageError) }
func (s *checkEnvironmentSuite) TestCheckEnvironment(c *gc.C) { defer testing.MakeFakeHome(c, checkEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) // VerifyStorage is sufficient for our tests and much simpler // than Bootstrap which calls it. stor := environ.Storage() err = environs.VerifyStorage(stor) c.Assert(err, gc.IsNil) err = environs.CheckEnvironment(environ) c.Assert(err, gc.IsNil) }
func (s *verifyStorageSuite) TestVerifyStorage(c *gc.C) { defer testing.MakeFakeHome(c, existingEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) stor := environ.Storage() err = environs.VerifyStorage(stor) c.Assert(err, gc.IsNil) reader, err := storage.Get(stor, environs.VerificationFilename) c.Assert(err, gc.IsNil) defer reader.Close() contents, err := ioutil.ReadAll(reader) c.Assert(err, gc.IsNil) c.Check(string(contents), gc.Equals, "juju-core storage writing verified: ok\n") }
func (s *destroyEnvSuite) TestDestroyEnvironmentCommand(c *gc.C) { // Prepare the environment so we can destroy it. _, err := environs.PrepareFromName("dummyenv", nullContext(c), s.ConfigStore) c.Assert(err, gc.IsNil) // check environment is mandatory opc, errc := runCommand(nullContext(c), new(DestroyEnvironmentCommand)) c.Check(<-errc, gc.Equals, NoEnvironmentError) // normal destroy opc, errc = runCommand(nullContext(c), new(DestroyEnvironmentCommand), "dummyenv", "--yes") c.Check(<-errc, gc.IsNil) c.Check((<-opc).(dummy.OpDestroy).Env, gc.Equals, "dummyenv") // Verify that the environment information has been removed. _, err = s.ConfigStore.ReadInfo("dummyenv") c.Assert(err, jc.Satisfies, errors.IsNotFound) }
func (s *ToolsMetadataSuite) SetUpTest(c *gc.C) { s.LoggingSuite.SetUpTest(c) s.home = coretesting.MakeSampleHome(c) s.AddCleanup(func(*gc.C) { s.home.Restore() dummy.Reset() loggo.ResetLoggers() }) env, err := environs.PrepareFromName("erewhemos", coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) s.env = env envtesting.RemoveAllTools(c, s.env) loggo.GetLogger("").SetLogLevel(loggo.INFO) // Switch the default tools location. s.publicStorageDir = c.MkDir() s.PatchValue(&tools.DefaultBaseURL, s.publicStorageDir) }
func (s *syncSuite) setUpTest(c *gc.C) { s.LoggingSuite.SetUpTest(c) s.ToolsFixture.SetUpTest(c) s.origVersion = version.Current // It's important that this be v1.8.x to match the test data. version.Current.Number = version.MustParse("1.8.3") // Create a target environments.yaml. fakeHome := coretesting.MakeFakeHome(c, ` environments: test-target: type: dummy state-server: false authorized-keys: "not-really-one" `) s.AddCleanup(func(*gc.C) { fakeHome.Restore() }) var err error s.targetEnv, err = environs.PrepareFromName("test-target", coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) envtesting.RemoveAllTools(c, s.targetEnv) // Create a source storage. baseDir := c.MkDir() stor, err := filestorage.NewFileStorageWriter(baseDir) c.Assert(err, gc.IsNil) s.storage = stor // Create a local tools directory. s.localStorage = c.MkDir() // Populate both local and default tools locations with the public tools. versionStrings := make([]string, len(vAll)) for i, vers := range vAll { versionStrings[i] = vers.String() } ttesting.MakeTools(c, baseDir, "releases", versionStrings) ttesting.MakeTools(c, s.localStorage, "releases", versionStrings) // Switch the default tools location. baseURL, err := s.storage.URL(storage.BaseToolsPath) c.Assert(err, gc.IsNil) s.PatchValue(&envtools.DefaultBaseURL, baseURL) }
func (s *checkEnvironmentSuite) TestCheckEnvironmentBadContent(c *gc.C) { defer testing.MakeFakeHome(c, checkEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) // We mock a bad (eg. from a Python-juju environment) bootstrap-verify. stor := environ.Storage() content := "bad verification content" reader := strings.NewReader(content) err = stor.Put(environs.VerificationFilename, reader, int64(len(content))) c.Assert(err, gc.IsNil) // When the bootstrap-verify file contains unexpected content, // we get an InvalidEnvironmentError. err = environs.CheckEnvironment(environ) c.Assert(err, gc.Equals, environs.InvalidEnvironmentError) }
func (s *checkEnvironmentSuite) TestCheckEnvironmentGetFails(c *gc.C) { defer testing.MakeFakeHome(c, checkEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) // VerifyStorage is sufficient for our tests and much simpler // than Bootstrap which calls it. stor := environ.Storage() err = environs.VerifyStorage(stor) c.Assert(err, gc.IsNil) // When fetching the verification file from storage fails, // we get an InvalidEnvironmentError. someError := errors.Unauthorizedf("you shall not pass") dummy.Poison(stor, environs.VerificationFilename, someError) err = environs.CheckEnvironment(environ) c.Assert(err, gc.Equals, someError) }
func (s *checkEnvironmentSuite) TestCheckEnvironmentFileNotFound(c *gc.C) { defer testing.MakeFakeHome(c, checkEnv, "existing").Restore() ctx := testing.Context(c) environ, err := environs.PrepareFromName("test", ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) // VerifyStorage is sufficient for our tests and much simpler // than Bootstrap which calls it. stor := environ.Storage() err = environs.VerifyStorage(stor) c.Assert(err, gc.IsNil) // When the bootstrap-verify file does not exist, it still believes // the environment is a juju-core one because earlier versions // did not create that file. err = stor.Remove(environs.VerificationFilename) c.Assert(err, gc.IsNil) err = environs.CheckEnvironment(environ) c.Assert(err, gc.IsNil) }
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"}) }
// environFromName loads an existing environment or prepares a new one. func environFromName( ctx *cmd.Context, envName string, resultErr *error, action string) (environs.Environ, func(), error) { store, err := configstore.Default() if err != nil { return nil, nil, err } var existing bool if environInfo, err := store.ReadInfo(envName); !errors.IsNotFound(err) { existing = true logger.Warningf("ignoring environments.yaml: using bootstrap config in %s", environInfo.Location()) } environ, err := environs.PrepareFromName(envName, ctx, store) if err != nil { return nil, nil, err } cleanup := func() { if !existing { destroyPreparedEnviron(ctx, environ, store, resultErr, action) } } return environ, cleanup, nil }
func (c *ValidateImageMetadataCommand) Run(context *cmd.Context) error { var params *simplestreams.MetadataLookupParams if c.providerType == "" { store, err := configstore.Default() if err != nil { return err } environ, err := environs.PrepareFromName(c.EnvName, context, store) if err != nil { return err } mdLookup, ok := environ.(simplestreams.MetadataValidator) if !ok { return fmt.Errorf("%s provider does not support image metadata validation", environ.Config().Type()) } params, err = mdLookup.MetadataLookupParams(c.region) if err != nil { return err } oes := &overrideEnvStream{environ, c.stream} params.Sources, err = imagemetadata.GetMetadataSources(oes) if err != nil { return err } } else { prov, err := environs.Provider(c.providerType) if err != nil { return err } mdLookup, ok := prov.(simplestreams.MetadataValidator) if !ok { return fmt.Errorf("%s provider does not support image metadata validation", c.providerType) } params, err = mdLookup.MetadataLookupParams(c.region) if err != nil { return err } } if c.series != "" { params.Series = c.series } if c.region != "" { params.Region = c.region } if c.endpoint != "" { params.Endpoint = c.endpoint } if c.metadataDir != "" { dir := filepath.Join(c.metadataDir, "images") if _, err := os.Stat(dir); err != nil { return err } params.Sources = []simplestreams.DataSource{ simplestreams.NewURLDataSource( "local metadata directory", "file://"+dir, utils.VerifySSLHostnames), } } params.Stream = c.stream image_ids, resolveInfo, err := imagemetadata.ValidateImageMetadata(params) if err != nil { if resolveInfo != nil { metadata := map[string]interface{}{ "Resolve Metadata": *resolveInfo, } if metadataYaml, yamlErr := cmd.FormatYaml(metadata); yamlErr == nil { err = fmt.Errorf("%v\n%v", err, string(metadataYaml)) } } return err } if len(image_ids) > 0 { metadata := map[string]interface{}{ "ImageIds": image_ids, "Region": params.Region, "Resolve Metadata": *resolveInfo, } c.out.Write(context, metadata) } else { var sources []string for _, s := range params.Sources { url, err := s.URL("") if err == nil { sources = append(sources, fmt.Sprintf("- %s (%s)", s.Description(), url)) } } return fmt.Errorf( "no matching image ids for region %s using sources:\n%s", params.Region, strings.Join(sources, "\n")) } return nil }
func (c *ValidateToolsMetadataCommand) Run(context *cmd.Context) error { var params *simplestreams.MetadataLookupParams if c.providerType == "" { store, err := configstore.Default() if err != nil { return err } environ, err := environs.PrepareFromName(c.EnvName, context, store) if err == nil { mdLookup, ok := environ.(simplestreams.MetadataValidator) if !ok { return fmt.Errorf("%s provider does not support tools metadata validation", environ.Config().Type()) } params, err = mdLookup.MetadataLookupParams(c.region) if err != nil { return err } params.Sources, err = tools.GetMetadataSources(environ) if err != nil { return err } } else { if c.metadataDir == "" { return err } params = &simplestreams.MetadataLookupParams{ Architectures: arch.AllSupportedArches, } } } else { prov, err := environs.Provider(c.providerType) if err != nil { return err } mdLookup, ok := prov.(simplestreams.MetadataValidator) if !ok { return fmt.Errorf("%s provider does not support tools metadata validation", c.providerType) } params, err = mdLookup.MetadataLookupParams(c.region) if err != nil { return err } } if c.series != "" { params.Series = c.series } if c.region != "" { params.Region = c.region } if c.endpoint != "" { params.Endpoint = c.endpoint } if c.metadataDir != "" { if _, err := os.Stat(c.metadataDir); err != nil { return err } toolsURL, err := tools.ToolsURL(c.metadataDir) if err != nil { return err } params.Sources = []simplestreams.DataSource{simplestreams.NewURLDataSource( "local metadata directory", toolsURL, utils.VerifySSLHostnames), } } versions, resolveInfo, err := tools.ValidateToolsMetadata(&tools.ToolsMetadataLookupParams{ MetadataLookupParams: *params, Version: c.exactVersion, Major: c.major, Minor: c.minor, }) if err != nil { if resolveInfo != nil { metadata := map[string]interface{}{ "Resolve Metadata": *resolveInfo, } if metadataYaml, yamlErr := cmd.FormatYaml(metadata); yamlErr == nil { err = fmt.Errorf("%v\n%v", err, string(metadataYaml)) } } return err } if len(versions) > 0 { metadata := map[string]interface{}{ "Matching Tools Versions": versions, "Resolve Metadata": *resolveInfo, } c.out.Write(context, metadata) } else { var sources []string for _, s := range params.Sources { url, err := s.URL("") if err == nil { sources = append(sources, fmt.Sprintf("- %s (%s)", s.Description(), url)) } } return fmt.Errorf("no matching tools using sources:\n%s", strings.Join(sources, "\n")) } return nil }
// setParams sets parameters based on the environment configuration // for those which have not been explicitly specified. func (c *ImageMetadataCommand) setParams(context *cmd.Context) error { c.privateStorage = "<private storage name>" var environ environs.Environ if store, err := configstore.Default(); err == nil { if environ, err = environs.PrepareFromName(c.EnvName, context, store); err == nil { logger.Infof("creating image metadata for environment %q", environ.Name()) // If the user has not specified region and endpoint, try and get it from the environment. if c.Region == "" || c.Endpoint == "" { var cloudSpec simplestreams.CloudSpec if inst, ok := environ.(simplestreams.HasRegion); ok { if cloudSpec, err = inst.Region(); err != nil { return err } } else { return fmt.Errorf("environment %q cannot provide region and endpoint", environ.Name()) } // If only one of region or endpoint is provided, that is a problem. if cloudSpec.Region != cloudSpec.Endpoint && (cloudSpec.Region == "" || cloudSpec.Endpoint == "") { return fmt.Errorf("cannot generate metadata without a complete cloud configuration") } if c.Region == "" { c.Region = cloudSpec.Region } if c.Endpoint == "" { c.Endpoint = cloudSpec.Endpoint } } cfg := environ.Config() if c.Series == "" { c.Series = config.PreferredSeries(cfg) } if v, ok := cfg.AllAttrs()["control-bucket"]; ok { c.privateStorage = v.(string) } } else { logger.Warningf("environment %q could not be opened: %v", c.EnvName, err) } } if environ == nil { logger.Infof("no environment found, creating image metadata using user supplied data") } if c.Series == "" { c.Series = config.LatestLtsSeries() } if c.ImageId == "" { return fmt.Errorf("image id must be specified") } if c.Region == "" { return fmt.Errorf("image region must be specified") } if c.Endpoint == "" { return fmt.Errorf("cloud endpoint URL must be specified") } if c.Dir == "" { logger.Infof("no destination directory specified, using current directory") var err error if c.Dir, err = os.Getwd(); err != nil { return err } } return nil }
func (s *JujuConnSuite) setUpConn(c *gc.C) { if s.RootDir != "" { panic("JujuConnSuite.setUpConn without teardown") } s.RootDir = c.MkDir() s.oldHome = osenv.Home() home := filepath.Join(s.RootDir, "/home/ubuntu") err := os.MkdirAll(home, 0777) c.Assert(err, gc.IsNil) osenv.SetHome(home) s.oldJujuHome = osenv.SetJujuHome(filepath.Join(home, ".juju")) err = os.Mkdir(osenv.JujuHome(), 0777) c.Assert(err, gc.IsNil) err = os.MkdirAll(s.DataDir(), 0777) c.Assert(err, gc.IsNil) s.PatchEnvironment(osenv.JujuEnvEnvKey, "") // TODO(rog) remove these files and add them only when // the tests specifically need them (in cmd/juju for example) s.writeSampleConfig(c, osenv.JujuHomePath("environments.yaml")) err = ioutil.WriteFile(osenv.JujuHomePath("dummyenv-cert.pem"), []byte(testing.CACert), 0666) c.Assert(err, gc.IsNil) err = ioutil.WriteFile(osenv.JujuHomePath("dummyenv-private-key.pem"), []byte(testing.CAKey), 0600) c.Assert(err, gc.IsNil) store, err := configstore.Default() c.Assert(err, gc.IsNil) s.ConfigStore = store ctx := testing.Context(c) environ, err := environs.PrepareFromName("dummyenv", ctx, s.ConfigStore) c.Assert(err, gc.IsNil) // sanity check we've got the correct environment. c.Assert(environ.Name(), gc.Equals, "dummyenv") s.PatchValue(&dummy.DataDir, s.DataDir()) s.LogDir = c.MkDir() s.PatchValue(&dummy.LogDir, s.LogDir) versions := PreferredDefaultVersions(environ.Config(), version.Binary{Number: version.Current.Number, Series: "precise", Arch: "amd64"}) versions = append(versions, version.Current) // Upload tools for both preferred and fake default series envtesting.MustUploadFakeToolsVersions(environ.Storage(), versions...) c.Assert(bootstrap.Bootstrap(ctx, environ, environs.BootstrapParams{}), gc.IsNil) s.BackingState = environ.(GetStater).GetStateInAPIServer() conn, err := juju.NewConn(environ) c.Assert(err, gc.IsNil) s.Conn = conn s.State = conn.State apiConn, err := juju.NewAPIConn(environ, api.DialOpts{}) c.Assert(err, gc.IsNil) s.APIConn = apiConn s.APIState = apiConn.State s.environ = environ }