func (t *ProviderSuite) assertGetImageMetadataSources(c *gc.C, stream, officialSourcePath string) { // Make an env configured with the stream. envAttrs := localConfigAttrs if stream != "" { envAttrs = envAttrs.Merge(coretesting.Attrs{ "image-stream": stream, }) } cfg, err := config.New(config.NoDefaults, envAttrs) c.Assert(err, gc.IsNil) env, err := environs.Prepare(cfg, coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) c.Assert(env, gc.NotNil) sources, err := imagemetadata.GetMetadataSources(env) c.Assert(err, gc.IsNil) c.Assert(len(sources), gc.Equals, 2) var urls = make([]string, len(sources)) for i, source := range sources { url, err := source.URL("") c.Assert(err, gc.IsNil) urls[i] = url } // The control bucket URL contains the bucket name. c.Check(strings.Contains(urls[0], ec2.ControlBucketName(env)+"/images"), jc.IsTrue) c.Assert(urls[1], gc.Equals, fmt.Sprintf("http://cloud-images.ubuntu.com/%s/", officialSourcePath)) }
func (*NewAPIConnSuite) TestNewConn(c *gc.C) { cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) c.Assert(err, gc.IsNil) ctx := coretesting.Context(c) env, err := environs.Prepare(cfg, ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) envtesting.UploadFakeTools(c, env.Storage()) err = bootstrap.Bootstrap(ctx, env, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) cfg = env.Config() cfg, err = cfg.Apply(map[string]interface{}{ "secret": "fnord", }) c.Assert(err, gc.IsNil) err = env.SetConfig(cfg) c.Assert(err, gc.IsNil) conn, err := juju.NewAPIConn(env, api.DefaultDialOpts()) c.Assert(err, gc.IsNil) c.Assert(conn.Environ, gc.Equals, env) c.Assert(conn.State, gc.NotNil) // the secrets will not be updated, as they already exist attrs, err := conn.State.Client().EnvironmentGet() c.Assert(attrs["secret"], gc.Equals, "pork") c.Assert(conn.Close(), gc.IsNil) }
func (s *ConfigSuite) TestFirewallMode(c *gc.C) { for i, test := range firewallModeTests { c.Logf("test %d: %s", i, test.configFirewallMode) attrs := dummy.SampleConfig() if test.configFirewallMode != "" { attrs = attrs.Merge(testing.Attrs{ "firewall-mode": test.configFirewallMode, }) } cfg, err := config.New(config.NoDefaults, attrs) if err != nil { c.Assert(err, gc.ErrorMatches, test.errorMsg) continue } ctx := testing.Context(c) env, err := environs.Prepare(cfg, ctx, configstore.NewMem()) if test.errorMsg != "" { c.Assert(err, gc.ErrorMatches, test.errorMsg) continue } c.Assert(err, gc.IsNil) defer env.Destroy() firewallMode := env.Config().FirewallMode() c.Assert(firewallMode, gc.Equals, test.firewallMode) s.TearDownTest(c) } }
func (s *StorageSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) cfg, err := config.New(config.NoDefaults, dummy.SampleConfig()) c.Assert(err, gc.IsNil) s.env, err = environs.Prepare(cfg, testing.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) s.dataDir = c.MkDir() }
func (s *verifyStorageSuite) TestVerifyStorageFails(c *gc.C) { 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 (t *localNonUSEastSuite) SetUpTest(c *gc.C) { t.BaseSuite.SetUpTest(c) t.srv.config = &s3test.Config{ Send409Conflict: true, } t.srv.startServer(c) cfg, err := config.New(config.NoDefaults, localConfigAttrs) c.Assert(err, gc.IsNil) env, err := environs.Prepare(cfg, coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) t.env = env }
func (s *NewAPIClientSuite) TestWithSlowInfoConnect(c *gc.C) { coretesting.MakeSampleJujuHome(c) store := configstore.NewMem() bootstrapEnv(c, coretesting.SampleEnvName, store) setEndpointAddress(c, store, coretesting.SampleEnvName, "infoapi.invalid") infoOpenedState := &mockAPIState{} infoEndpointOpened := make(chan struct{}) cfgOpenedState := &mockAPIState{} // On a sample run with no delay, the logic took 45ms to run, so // we make the delay slightly more than that, so that if the // logic doesn't delay at all, the test will fail reasonably consistently. s.PatchValue(juju.ProviderConnectDelay, 50*time.Millisecond) apiOpen := func(info *api.Info, opts api.DialOpts) (juju.APIState, error) { if info.Addrs[0] == "infoapi.invalid" { infoEndpointOpened <- struct{}{} return infoOpenedState, nil } return cfgOpenedState, nil } stateClosed := make(chan juju.APIState) infoOpenedState.close = func(st juju.APIState) error { stateClosed <- st return nil } cfgOpenedState.close = infoOpenedState.close startTime := time.Now() st, err := juju.NewAPIFromStore(coretesting.SampleEnvName, store, apiOpen) c.Assert(err, gc.IsNil) // The connection logic should wait for some time before opening // the API from the configuration. c.Assert(time.Since(startTime), jc.GreaterThan, *juju.ProviderConnectDelay) c.Assert(st, gc.Equals, cfgOpenedState) select { case <-infoEndpointOpened: case <-time.After(coretesting.LongWait): c.Errorf("api never opened via info") } // Check that the ignored state was closed. select { case st := <-stateClosed: c.Assert(st, gc.Equals, infoOpenedState) case <-time.After(coretesting.LongWait): c.Errorf("timed out waiting for state to be closed") } }
// newConfigStore returns a storage that contains information // for the environment name. func newConfigStore(envName string, info *environInfo) configstore.Storage { store := configstore.NewMem() newInfo, err := store.CreateInfo(envName) if err != nil { panic(err) } newInfo.SetAPICredentials(info.creds) newInfo.SetAPIEndpoint(info.endpoint) newInfo.SetBootstrapConfig(info.bootstrapConfig) err = newInfo.Write() if err != nil { panic(err) } return store }
func (*ConfigSuite) TestSecretAttrs(c *gc.C) { attrs := dummy.SampleConfig().Delete("secret") cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) ctx := testing.Context(c) env, err := environs.Prepare(cfg, ctx, configstore.NewMem()) c.Assert(err, gc.IsNil) defer env.Destroy() expected := map[string]string{ "secret": "pork", } actual, err := env.Provider().SecretAttrs(cfg) c.Assert(err, gc.IsNil) c.Assert(actual, gc.DeepEquals, expected) }
func (s *verifyStorageSuite) TestVerifyStorage(c *gc.C) { 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 *NewAPIClientSuite) TestBothError(c *gc.C) { coretesting.MakeSampleJujuHome(c) store := configstore.NewMem() bootstrapEnv(c, coretesting.SampleEnvName, store) setEndpointAddress(c, store, coretesting.SampleEnvName, "infoapi.invalid") s.PatchValue(juju.ProviderConnectDelay, 0*time.Second) apiOpen := func(info *api.Info, opts api.DialOpts) (juju.APIState, error) { if info.Addrs[0] == "infoapi.invalid" { return nil, fmt.Errorf("info connect failed") } return nil, fmt.Errorf("config connect failed") } st, err := juju.NewAPIFromStore(coretesting.SampleEnvName, store, apiOpen) c.Check(err, gc.ErrorMatches, "config connect failed") c.Check(st, gc.IsNil) }
func (s *NewAPIClientSuite) TestWithBootstrapConfigAndNoEnvironmentsFile(c *gc.C) { coretesting.MakeSampleJujuHome(c) store := configstore.NewMem() bootstrapEnv(c, coretesting.SampleEnvName, store) info, err := store.ReadInfo(coretesting.SampleEnvName) c.Assert(err, gc.IsNil) c.Assert(info.BootstrapConfig(), gc.NotNil) c.Assert(info.APIEndpoint().Addresses, gc.HasLen, 0) err = os.Remove(osenv.JujuHomePath("environments.yaml")) c.Assert(err, gc.IsNil) apiOpen := func(*api.Info, api.DialOpts) (juju.APIState, error) { return &mockAPIState{}, nil } st, err := juju.NewAPIFromStore(coretesting.SampleEnvName, store, apiOpen) c.Check(err, gc.IsNil) st.Close() }
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. coretesting.WriteEnvironments(c, coretesting.MultipleEnvConfig) 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 *NewAPIClientSuite) TestWithSlowConfigConnect(c *gc.C) { coretesting.MakeSampleJujuHome(c) store := configstore.NewMem() bootstrapEnv(c, coretesting.SampleEnvName, store) setEndpointAddress(c, store, coretesting.SampleEnvName, "infoapi.invalid") infoOpenedState := &mockAPIState{} infoEndpointOpened := make(chan struct{}) cfgOpenedState := &mockAPIState{} cfgEndpointOpened := make(chan struct{}) s.PatchValue(juju.ProviderConnectDelay, 0*time.Second) apiOpen := func(info *api.Info, opts api.DialOpts) (juju.APIState, error) { if info.Addrs[0] == "infoapi.invalid" { infoEndpointOpened <- struct{}{} <-infoEndpointOpened return infoOpenedState, nil } cfgEndpointOpened <- struct{}{} <-cfgEndpointOpened return cfgOpenedState, nil } stateClosed := make(chan juju.APIState) infoOpenedState.close = func(st juju.APIState) error { stateClosed <- st return nil } cfgOpenedState.close = infoOpenedState.close done := make(chan struct{}) go func() { st, err := juju.NewAPIFromStore(coretesting.SampleEnvName, store, apiOpen) c.Check(err, gc.IsNil) c.Check(st, gc.Equals, infoOpenedState) close(done) }() // Check that we're trying to connect to both endpoints: select { case <-infoEndpointOpened: case <-time.After(coretesting.LongWait): c.Fatalf("api never opened via info") } select { case <-cfgEndpointOpened: case <-time.After(coretesting.LongWait): c.Fatalf("api never opened via config") } // Let the info endpoint open go ahead and // check that the NewAPIFromStore call returns. infoEndpointOpened <- struct{}{} select { case <-done: case <-time.After(coretesting.LongWait): c.Errorf("timed out opening API") } // Let the config endpoint open go ahead and // check that its state is closed. cfgEndpointOpened <- struct{}{} select { case st := <-stateClosed: c.Assert(st, gc.Equals, cfgOpenedState) case <-time.After(coretesting.LongWait): c.Errorf("timed out waiting for state to be closed") } }
func (t *Tests) SetUpTest(c *gc.C) { t.ToolsFixture.SetUpTest(c) t.ConfigStore = configstore.NewMem() }
func (t *LiveTests) TestBootstrapWithDefaultSeries(c *gc.C) { if !t.HasProvisioner { c.Skip("HasProvisioner is false; cannot test deployment") } current := version.Current other := current other.Series = "quantal" if current == other { other.Series = "precise" } dummyCfg, err := config.New(config.NoDefaults, dummy.SampleConfig().Merge(coretesting.Attrs{ "state-server": false, "name": "dummy storage", })) dummyenv, err := environs.Prepare(dummyCfg, coretesting.Context(c), configstore.NewMem()) c.Assert(err, gc.IsNil) defer dummyenv.Destroy() t.Destroy(c) attrs := t.TestConfig.Merge(coretesting.Attrs{"default-series": other.Series}) cfg, err := config.New(config.NoDefaults, attrs) c.Assert(err, gc.IsNil) env, err := environs.Prepare(cfg, coretesting.Context(c), t.ConfigStore) c.Assert(err, gc.IsNil) defer environs.Destroy(env, t.ConfigStore) currentName := envtools.StorageName(current) otherName := envtools.StorageName(other) envStorage := env.Storage() dummyStorage := dummyenv.Storage() defer envStorage.Remove(otherName) _, err = sync.Upload(dummyStorage, ¤t.Number) c.Assert(err, gc.IsNil) // This will only work while cross-compiling across releases is safe, // which depends on external elements. Tends to be safe for the last // few releases, but we may have to refactor some day. err = storageCopy(dummyStorage, currentName, envStorage, otherName) c.Assert(err, gc.IsNil) err = bootstrap.Bootstrap(coretesting.Context(c), env, environs.BootstrapParams{}) c.Assert(err, gc.IsNil) conn, err := juju.NewConn(env) c.Assert(err, gc.IsNil) defer conn.Close() // Wait for machine agent to come up on the bootstrap // machine and ensure it deployed the proper series. m0, err := conn.State.Machine("0") c.Assert(err, gc.IsNil) mw0 := newMachineToolWaiter(m0) defer mw0.Stop() waitAgentTools(c, mw0, other) }
func (t *LiveTests) SetUpSuite(c *gc.C) { t.ConfigStore = configstore.NewMem() }
// newConfigStoreWithError that will return the given // error from ReadInfo. func newConfigStoreWithError(err error) configstore.Storage { return &errorConfigStorage{ Storage: configstore.NewMem(), err: err, } }