Ejemplo n.º 1
0
func (*NetworkSuite) TestInitializeFromConfig(c *gc.C) {
	c.Check(network.GetPreferIPv6(), jc.IsFalse)

	envConfig := testing.CustomEnvironConfig(c, testing.Attrs{
		"prefer-ipv6": true,
	})
	network.InitializeFromConfig(envConfig)
	c.Check(network.GetPreferIPv6(), jc.IsTrue)

	envConfig = testing.CustomEnvironConfig(c, testing.Attrs{
		"prefer-ipv6": false,
	})
	network.InitializeFromConfig(envConfig)
	c.Check(network.GetPreferIPv6(), jc.IsFalse)
}
Ejemplo n.º 2
0
func (s *EnvironSuite) TestNewEnvironmentSameUserSameNameFails(c *gc.C) {
	cfg, _ := s.createTestEnvConfig(c)
	owner := s.factory.MakeUser(c, nil).UserTag()

	// Create the first environment.
	_, st1, err := s.State.NewEnvironment(cfg, owner)
	c.Assert(err, jc.ErrorIsNil)
	defer st1.Close()

	// Attempt to create another environment with a different UUID but the
	// same owner and name as the first.
	newUUID, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	cfg2 := testing.CustomEnvironConfig(c, testing.Attrs{
		"name": cfg.Name(),
		"uuid": newUUID.String(),
	})
	_, _, err = s.State.NewEnvironment(cfg2, owner)
	errMsg := fmt.Sprintf("environment %q for %s already exists", cfg2.Name(), owner.Username())
	c.Assert(err, gc.ErrorMatches, errMsg)
	c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)

	// Remove the first environment.
	err = st1.RemoveAllEnvironDocs()
	c.Assert(err, jc.ErrorIsNil)

	// We should now be able to create the other environment.
	env2, st2, err := s.State.NewEnvironment(cfg2, owner)
	c.Assert(err, jc.ErrorIsNil)
	defer st2.Close()
	c.Assert(env2, gc.NotNil)
	c.Assert(st2, gc.NotNil)
}
Ejemplo n.º 3
0
func (s *tagsSuite) TestAddInstanceTagsDoesNotSupportTagging(c *gc.C) {
	env := &testEnviron{cfg: testing.CustomEnvironConfig(c, nil)}
	err := upgrades.AddInstanceTags(env, []*state.Machine{
		s.stateServer, s.unprovisioned, s.provisioned, s.container,
	})
	c.Assert(err, jc.ErrorIsNil)
}
Ejemplo n.º 4
0
func (s *tagsSuite) TestAddInstanceTagsSupportsTagging(c *gc.C) {
	env := &testEnvironWithTagging{
		testEnviron: testEnviron{
			cfg: testing.CustomEnvironConfig(c, testing.Attrs{
				"resource-tags": "abc=123",
			}),
		},
	}
	err := upgrades.AddInstanceTags(env, []*state.Machine{
		s.stateServer, s.unprovisioned, s.provisioned, s.container,
	})
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(env.calls, jc.DeepEquals, []tagInstanceArgs{{
		"inst-0", map[string]string{
			"juju-is-state": "true",
			"juju-env-uuid": testing.EnvironmentTag.Id(),
			"abc":           "123",
		},
	}, {
		"inst-1", map[string]string{
			"juju-env-uuid": testing.EnvironmentTag.Id(),
			"abc":           "123",
		},
	}})
}
Ejemplo n.º 5
0
// createTestEnvConfig returns a new environment config and its UUID for testing.
func (s *EnvironSuite) createTestEnvConfig(c *gc.C) (*config.Config, string) {
	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	return testing.CustomEnvironConfig(c, testing.Attrs{
		"name": "testing",
		"uuid": uuid.String(),
	}), uuid.String()
}
Ejemplo n.º 6
0
// newEnvironConfig returns an environment config map with the supplied attrs
// (on top of some default set), or fails the test.
func newEnvironConfig(c *gc.C, extraAttrs coretesting.Attrs) map[string]interface{} {
	attrs := dummy.SampleConfig()
	attrs["broken"] = ""
	attrs["state-id"] = "42"
	for k, v := range extraAttrs {
		attrs[k] = v
	}
	return coretesting.CustomEnvironConfig(c, attrs).AllAttrs()
}
Ejemplo n.º 7
0
func (*instancecfgSuite) TestInstanceTagsUserSpecified(c *gc.C) {
	cfg := testing.CustomEnvironConfig(c, testing.Attrs{
		"resource-tags": "a=b c=",
	})
	testInstanceTags(c, cfg, nil, map[string]string{
		"juju-env-uuid": testing.EnvironmentTag.Id(),
		"a":             "b",
		"c":             "",
	})
}
Ejemplo n.º 8
0
func (*instancecfgSuite) TestInstanceTagsStateServer(c *gc.C) {
	cfg := testing.CustomEnvironConfig(c, testing.Attrs{})
	stateServerJobs := []multiwatcher.MachineJob{multiwatcher.JobManageEnviron}
	nonStateServerJobs := []multiwatcher.MachineJob{multiwatcher.JobHostUnits}
	testInstanceTags(c, cfg, stateServerJobs, map[string]string{
		"juju-env-uuid": testing.EnvironmentTag.Id(),
		"juju-is-state": "true",
	})
	testInstanceTags(c, cfg, nonStateServerJobs, map[string]string{
		"juju-env-uuid": testing.EnvironmentTag.Id(),
	})
}
Ejemplo n.º 9
0
func (s *blockSuite) createTestEnv(c *gc.C) (*state.Environment, *state.State) {
	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	cfg := testing.CustomEnvironConfig(c, testing.Attrs{
		"name": "testing",
		"uuid": uuid.String(),
	})
	owner := names.NewUserTag("test@remote")
	env, st, err := s.State.NewEnvironment(cfg, owner)
	c.Assert(err, jc.ErrorIsNil)
	return env, st
}
Ejemplo n.º 10
0
func (s *EnvUserSuite) newEnvWithOwner(c *gc.C, name string, owner names.UserTag) *state.Environment {
	// Don't use the factory to call MakeEnvironment because it may at some
	// time in the future be modified to do additional things.  Instead call
	// the state method directly to create an environment to make sure that
	// the owner is able to access the environment.
	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	cfg := testing.CustomEnvironConfig(c, testing.Attrs{
		"name": name,
		"uuid": uuid.String(),
	})
	env, st, err := s.State.NewEnvironment(cfg, owner)
	c.Assert(err, jc.ErrorIsNil)
	defer st.Close()
	return env
}
Ejemplo n.º 11
0
// SetConfig changes the stored environment config with the given
// extraAttrs and triggers a change for the watcher.
func (s *fakeState) SetConfig(c *gc.C, extraAttrs coretesting.Attrs) {
	s.mu.Lock()
	defer s.mu.Unlock()

	attrs := dummy.SampleConfig()
	for k, v := range extraAttrs {
		attrs[k] = v
	}

	// Simulate it's prepared.
	attrs["broken"] = ""
	attrs["state-id"] = "42"

	s.config = coretesting.CustomEnvironConfig(c, attrs).AllAttrs()
	s.changes <- struct{}{}
}
Ejemplo n.º 12
0
func (s *EnvironSuite) TestNewEnvironmentSameUserSameNameFails(c *gc.C) {
	cfg, _ := s.createTestEnvConfig(c)
	owner := s.Factory.MakeUser(c, nil).UserTag()

	// Create the first environment.
	_, st1, err := s.State.NewEnvironment(cfg, owner)
	c.Assert(err, jc.ErrorIsNil)
	defer st1.Close()

	// Attempt to create another environment with a different UUID but the
	// same owner and name as the first.
	newUUID, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	cfg2 := testing.CustomEnvironConfig(c, testing.Attrs{
		"name": cfg.Name(),
		"uuid": newUUID.String(),
	})
	_, _, err = s.State.NewEnvironment(cfg2, owner)
	errMsg := fmt.Sprintf("environment %q for %s already exists", cfg2.Name(), owner.Canonical())
	c.Assert(err, gc.ErrorMatches, errMsg)
	c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)

	// Remove the first environment.
	env1, err := st1.Environment()
	c.Assert(err, jc.ErrorIsNil)
	err = env1.Destroy()
	c.Assert(err, jc.ErrorIsNil)
	// Destroy only sets the environment to dying and RemoveAllEnvironDocs can
	// only be called on a dead environment. Normally, the environ's lifecycle
	// would be set to dead after machines and services have been cleaned up.
	err = state.SetEnvLifeDead(st1, env1.EnvironTag().Id())
	c.Assert(err, jc.ErrorIsNil)
	err = st1.RemoveAllEnvironDocs()
	c.Assert(err, jc.ErrorIsNil)

	// We should now be able to create the other environment.
	env2, st2, err := s.State.NewEnvironment(cfg2, owner)
	c.Assert(err, jc.ErrorIsNil)
	defer st2.Close()
	c.Assert(env2, gc.NotNil)
	c.Assert(st2, gc.NotNil)
}
Ejemplo n.º 13
0
// MakeEnvironment creates an environment with specified params,
// filling in sane defaults for missing values. If params is nil,
// defaults are used for all values.
//
// By default the new enviroment shares the same owner as the calling
// Factory's environment.
func (factory *Factory) MakeEnvironment(c *gc.C, params *EnvParams) *state.State {
	if params == nil {
		params = new(EnvParams)
	}
	if params.Name == "" {
		params.Name = uniqueString("testenv")
	}
	if params.Owner == nil {
		origEnv, err := factory.st.Environment()
		c.Assert(err, jc.ErrorIsNil)
		params.Owner = origEnv.Owner()
	}
	// It only makes sense to make an environment with the same provider
	// as the initial environment, or things will break elsewhere.
	currentCfg, err := factory.st.EnvironConfig()
	c.Assert(err, jc.ErrorIsNil)

	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	cfg := testing.CustomEnvironConfig(c, testing.Attrs{
		"name":       params.Name,
		"uuid":       uuid.String(),
		"type":       currentCfg.Type(),
		"state-port": currentCfg.StatePort(),
		"api-port":   currentCfg.APIPort(),
	}.Merge(params.ConfigAttrs))
	_, st, err := factory.st.NewEnvironment(cfg, params.Owner.(names.UserTag))
	c.Assert(err, jc.ErrorIsNil)
	if params.Prepare {
		// Prepare the environment.
		provider, err := environs.Provider(cfg.Type())
		c.Assert(err, jc.ErrorIsNil)
		env, err := provider.PrepareForBootstrap(envtesting.BootstrapContext(c), cfg)
		c.Assert(err, jc.ErrorIsNil)
		// Now save the config back.
		err = st.UpdateEnvironConfig(env.Config().AllAttrs(), nil, nil)
		c.Assert(err, jc.ErrorIsNil)
	}
	return st
}
Ejemplo n.º 14
0
func (*volumesSuite) TestVolumeParamsStorageTags(c *gc.C) {
	volumeTag := names.NewVolumeTag("100")
	storageTag := names.NewStorageTag("mystore/0")
	unitTag := names.NewUnitTag("mysql/123")
	p, err := storagecommon.VolumeParams(
		&fakeVolume{tag: volumeTag},
		&fakeStorageInstance{tag: storageTag, owner: unitTag},
		testing.CustomEnvironConfig(c, nil),
		&fakePoolManager{},
	)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(p, jc.DeepEquals, params.VolumeParams{
		VolumeTag: "volume-100",
		Provider:  "loop",
		Size:      1024,
		Tags: map[string]string{
			tags.JujuEnv:             testing.EnvironmentTag.Id(),
			tags.JujuStorageInstance: "mystore/0",
			tags.JujuStorageOwner:    "mysql/123",
		},
	})
}
Ejemplo n.º 15
0
func (*volumesSuite) testVolumeParams(c *gc.C, provisioned bool) {
	tag := names.NewVolumeTag("100")
	p, err := storagecommon.VolumeParams(
		&fakeVolume{tag: tag, provisioned: provisioned},
		nil, // StorageInstance
		testing.CustomEnvironConfig(c, testing.Attrs{
			"resource-tags": "a=b c=",
		}),
		&fakePoolManager{},
	)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(p, jc.DeepEquals, params.VolumeParams{
		VolumeTag: "volume-100",
		Provider:  "loop",
		Size:      1024,
		Tags: map[string]string{
			tags.JujuEnv: testing.EnvironmentTag.Id(),
			"a":          "b",
			"c":          "",
		},
	})
}
Ejemplo n.º 16
0
func (sb *StubBacking) SetUp(c *gc.C, envName string, withZones, withSpaces, withSubnets SetUpFlag) {
	// This method must be called at the beginning of each test, which
	// needs access to any of the mocks, to reset the recorded calls
	// and errors, as well as to initialize the mocks as needed.
	ResetStub(sb.Stub)

	// Make sure we use the stub provider.
	extraAttrs := coretesting.Attrs{
		"uuid": utils.MustNewUUID().String(),
		"type": StubProviderType,
		"name": envName,
	}
	sb.EnvConfig = coretesting.CustomEnvironConfig(c, extraAttrs)
	sb.Zones = []providercommon.AvailabilityZone{}
	if withZones {
		sb.Zones = make([]providercommon.AvailabilityZone, len(ProviderInstance.Zones))
		copy(sb.Zones, ProviderInstance.Zones)
	}
	sb.Spaces = []common.BackingSpace{}
	if withSpaces {
		// Note that full subnet data is generated from the SubnetIds in
		// FakeSpace.Subnets().
		sb.Spaces = []common.BackingSpace{
			&FakeSpace{
				SpaceName: "default",
				SubnetIds: []string{"192.168.0.0/24", "192.168.3.0/24"},
				NextErr:   sb.NextErr},
			&FakeSpace{
				SpaceName: "dmz",
				SubnetIds: []string{"192.168.1.0/24"},
				NextErr:   sb.NextErr},
			&FakeSpace{
				SpaceName: "private",
				SubnetIds: []string{"192.168.2.0/24"},
				NextErr:   sb.NextErr},
			&FakeSpace{
				SpaceName: "private",
				SubnetIds: []string{"192.168.2.0/24"},
				NextErr:   sb.NextErr}, // duplicates are ignored when caching spaces.
		}
	}
	sb.Subnets = []common.BackingSubnet{}
	if withSubnets {
		info0 := common.BackingSubnetInfo{
			CIDR:              ProviderInstance.Subnets[0].CIDR,
			ProviderId:        string(ProviderInstance.Subnets[0].ProviderId),
			AllocatableIPLow:  ProviderInstance.Subnets[0].AllocatableIPLow.String(),
			AllocatableIPHigh: ProviderInstance.Subnets[0].AllocatableIPHigh.String(),
			AvailabilityZones: ProviderInstance.Subnets[0].AvailabilityZones,
			SpaceName:         "private",
		}
		info1 := common.BackingSubnetInfo{
			CIDR:              ProviderInstance.Subnets[1].CIDR,
			ProviderId:        string(ProviderInstance.Subnets[1].ProviderId),
			AvailabilityZones: ProviderInstance.Subnets[1].AvailabilityZones,
			SpaceName:         "dmz",
		}

		sb.Subnets = []common.BackingSubnet{
			&FakeSubnet{info0},
			&FakeSubnet{info1},
		}
	}
}