Exemplo n.º 1
0
func (s *DeployLocalSuite) TestDeployForceMachineIdWithContainer(c *C) {
	machine, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)
	c.Assert(machine.Id(), Equals, "0")
	cons := constraints.MustParse("mem=2G")
	err = s.State.SetEnvironConstraints(cons)
	c.Assert(err, IsNil)
	serviceCons := constraints.MustParse("cpu-cores=2")
	service, err := s.Conn.DeployService(juju.DeployServiceParams{
		ServiceName:   "bob",
		Charm:         s.charm,
		Constraints:   serviceCons,
		NumUnits:      1,
		ToMachineSpec: fmt.Sprintf("%s:0", instance.LXC),
	})
	c.Assert(err, IsNil)
	s.assertConstraints(c, service, serviceCons)
	units, err := service.AllUnits()
	c.Assert(err, IsNil)
	c.Assert(units, HasLen, 1)

	// The newly created container will use the constraints.
	id, err := units[0].AssignedMachineId()
	c.Assert(err, IsNil)
	machine, err = s.State.Machine(id)
	c.Assert(err, IsNil)
	expectedCons, err := machine.Constraints()
	c.Assert(err, IsNil)
	c.Assert(cons, DeepEquals, expectedCons)
}
Exemplo n.º 2
0
func (s *AssignSuite) TestAssignUnitToNewMachineSetsConstraints(c *C) {
	// Set up constraints.
	scons := constraints.MustParse("mem=2G cpu-power=400")
	err := s.wordpress.SetConstraints(scons)
	c.Assert(err, IsNil)
	econs := constraints.MustParse("mem=4G cpu-cores=2")
	err = s.State.SetEnvironConstraints(econs)
	c.Assert(err, IsNil)

	// Unit will take combined service/environ constraints on creation.
	unit, err := s.wordpress.AddUnit()
	c.Assert(err, IsNil)

	// Change service/env constraints before assigning, to verify this.
	scons = constraints.MustParse("mem=6G cpu-power=800")
	err = s.wordpress.SetConstraints(scons)
	c.Assert(err, IsNil)
	econs = constraints.MustParse("cpu-cores=4")
	err = s.State.SetEnvironConstraints(econs)
	c.Assert(err, IsNil)

	// The new machine takes the original combined unit constraints.
	err = unit.AssignToNewMachine()
	c.Assert(err, IsNil)
	err = unit.Refresh()
	c.Assert(err, IsNil)
	mid, err := unit.AssignedMachineId()
	c.Assert(err, IsNil)
	machine, err := s.State.Machine(mid)
	c.Assert(err, IsNil)
	mcons, err := machine.Constraints()
	c.Assert(err, IsNil)
	expect := constraints.MustParse("mem=2G cpu-cores=2 cpu-power=400")
	c.Assert(mcons, DeepEquals, expect)
}
Exemplo n.º 3
0
func (s *MachineSuite) TestConstraintsFromEnvironment(c *C) {
	econs1 := constraints.MustParse("mem=1G")
	econs2 := constraints.MustParse("mem=2G")

	// A newly-created machine gets a copy of the environment constraints.
	err := s.State.SetEnvironConstraints(econs1)
	c.Assert(err, IsNil)
	machine1, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)
	mcons1, err := machine1.Constraints()
	c.Assert(err, IsNil)
	c.Assert(mcons1, DeepEquals, econs1)

	// Change environment constraints and add a new machine.
	err = s.State.SetEnvironConstraints(econs2)
	c.Assert(err, IsNil)
	machine2, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)
	mcons2, err := machine2.Constraints()
	c.Assert(err, IsNil)
	c.Assert(mcons2, DeepEquals, econs2)

	// Check the original machine has its original constraints.
	mcons1, err = machine1.Constraints()
	c.Assert(err, IsNil)
	c.Assert(mcons1, DeepEquals, econs1)
}
Exemplo n.º 4
0
func (s *ConstraintsSuite) TestWithFallbacks(c *C) {
	for i, t := range withFallbacksTests {
		c.Logf("test %d", i)
		initial := constraints.MustParse(t.initial)
		fallbacks := constraints.MustParse(t.fallbacks)
		final := constraints.MustParse(t.final)
		c.Assert(initial.WithFallbacks(fallbacks), DeepEquals, final)
	}
}
Exemplo n.º 5
0
func (s *DeployLocalSuite) TestDeployConstraints(c *C) {
	err := s.State.SetEnvironConstraints(constraints.MustParse("mem=2G"))
	c.Assert(err, IsNil)
	serviceCons := constraints.MustParse("cpu-cores=2")
	service, err := s.Conn.DeployService(juju.DeployServiceParams{
		ServiceName: "bob",
		Charm:       s.charm,
		Constraints: serviceCons,
	})
	c.Assert(err, IsNil)
	s.assertConstraints(c, service, serviceCons)
}
Exemplo n.º 6
0
func (s *AssignSuite) TestAssignUnitToNewMachineContainerConstraint(c *C) {
	// Set up service constraints.
	scons := constraints.MustParse("container=lxc")
	err := s.wordpress.SetConstraints(scons)
	c.Assert(err, IsNil)
	s.assertAssignUnitToNewMachineContainerConstraint(c)
}
Exemplo n.º 7
0
func (s *ConstraintsSuite) TestHasContainer(c *C) {
	for i, t := range hasContainerTests {
		c.Logf("test %d", i)
		cons := constraints.MustParse(t.constraints)
		c.Assert(cons.HasContainer(), Equals, t.hasContainer)
	}
}
Exemplo n.º 8
0
func (s *clientSuite) TestClientServiceDeployPrincipal(c *C) {
	// TODO(fwereade): test ToMachineSpec directly on srvClient, when we
	// manage to extract it as a package and can thus do it conveniently.
	store, restore := makeMockCharmStore()
	defer restore()
	curl, bundle := addCharm(c, store, "dummy")
	mem4g := constraints.MustParse("mem=4G")
	err := s.APIState.Client().ServiceDeploy(
		curl.String(), "service", 3, "", mem4g,
	)
	c.Assert(err, IsNil)
	service, err := s.State.Service("service")
	c.Assert(err, IsNil)
	charm, force, err := service.Charm()
	c.Assert(err, IsNil)
	c.Assert(force, Equals, false)
	c.Assert(charm.URL(), DeepEquals, curl)
	c.Assert(charm.Meta(), DeepEquals, bundle.Meta())
	c.Assert(charm.Config(), DeepEquals, bundle.Config())

	cons, err := service.Constraints()
	c.Assert(err, IsNil)
	c.Assert(cons, DeepEquals, mem4g)
	units, err := service.AllUnits()
	c.Assert(err, IsNil)
	for _, unit := range units {
		mid, err := unit.AssignedMachineId()
		c.Assert(err, IsNil)
		machine, err := s.State.Machine(mid)
		c.Assert(err, IsNil)
		cons, err := machine.Constraints()
		c.Assert(err, IsNil)
		c.Assert(cons, DeepEquals, mem4g)
	}
}
Exemplo n.º 9
0
func (s *ToolsSuite) TestFindInstanceTools(c *C) {
	for i, test := range findInstanceToolsTests {
		c.Logf("\ntest %d: %s", i, test.info)
		s.Reset(c, map[string]interface{}{
			"agent-version": test.agentVersion.String(),
		})
		available := s.uploadPrivate(c, test.available...)
		if len(available) > 0 {
			// These should never be chosen.
			s.uploadPublic(c, vAll...)
		}

		cons := constraints.MustParse(test.constraints)
		actual, err := environs.FindInstanceTools(s.env, test.series, cons)
		if test.err != nil {
			if len(actual) > 0 {
				c.Logf(actual.String())
			}
			c.Check(err, DeepEquals, &errors.NotFoundError{test.err, ""})
			continue
		}
		expect := map[version.Binary]string{}
		for _, expected := range test.expect {
			expect[expected] = available[expected]
		}
		c.Check(actual.URLs(), DeepEquals, expect)
	}
}
Exemplo n.º 10
0
func (s *assignCleanSuite) TestAssignUsingConstraintsToMachine(c *C) {
	for i, t := range assignUsingConstraintsTests {
		c.Logf("test %d", i)
		cons := constraints.MustParse(t.unitConstraints)
		err := s.State.SetEnvironConstraints(cons)
		c.Assert(err, IsNil)

		unit, err := s.wordpress.AddUnit()
		c.Assert(err, IsNil)

		m, err := s.State.AddMachine("series", state.JobHostUnits)
		c.Assert(err, IsNil)
		if t.hardwareCharacteristics != "none" {
			hc := instance.MustParseHardware(t.hardwareCharacteristics)
			err = m.SetProvisioned("inst-id", "fake_nonce", &hc)
			c.Assert(err, IsNil)
		}

		um, err := s.assignUnit(unit)
		if t.assignOk {
			c.Assert(err, IsNil)
			c.Assert(um.Id(), Equals, m.Id())
		} else {
			c.Assert(um, IsNil)
			c.Assert(err, ErrorMatches, eligibleMachinesInUse)
			// Destroy the machine so it can't be used for the next test.
			err = m.Destroy()
			c.Assert(err, IsNil)
		}
	}
}
Exemplo n.º 11
0
func (s *AssignSuite) TestAssignUnitToNewMachineBecomesDirty(c *C) {
	_, err := s.State.AddMachine("series", state.JobManageEnviron) // bootstrap machine
	c.Assert(err, IsNil)

	// Set up constraints to specify we want to install into a container.
	econs := constraints.MustParse("container=lxc")
	err = s.State.SetEnvironConstraints(econs)
	c.Assert(err, IsNil)

	// Create some units and a clean machine.
	unit, err := s.wordpress.AddUnit()
	c.Assert(err, IsNil)
	anotherUnit, err := s.wordpress.AddUnit()
	c.Assert(err, IsNil)
	machine, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)

	makeDirty := state.TransactionHook{
		Before: func() { c.Assert(unit.AssignToMachine(machine), IsNil) },
	}
	defer state.SetTransactionHooks(
		c, s.State, makeDirty,
	).Check()

	err = anotherUnit.AssignToNewMachineOrContainer()
	c.Assert(err, IsNil)

	mid, err := unit.AssignedMachineId()
	c.Assert(err, IsNil)
	c.Assert(mid, Equals, "1")

	mid, err = anotherUnit.AssignedMachineId()
	c.Assert(err, IsNil)
	c.Assert(mid, Equals, "2/lxc/0")
}
Exemplo n.º 12
0
func (s *AssignSuite) TestAssignUnitToNewMachineDefaultContainerConstraint(c *C) {
	// Set up env constraints.
	econs := constraints.MustParse("container=lxc")
	err := s.State.SetEnvironConstraints(econs)
	c.Assert(err, IsNil)
	s.assertAssignUnitToNewMachineContainerConstraint(c)
}
Exemplo n.º 13
0
func (s *ConfigSuite) TestServiceGet(c *C) {
	for i, t := range getTests {
		c.Logf("test %d. %s", i, t.about)
		ch := s.AddTestingCharm(c, t.charm)
		svc, err := s.State.AddService(fmt.Sprintf("test%d", i), ch)
		c.Assert(err, IsNil)

		var constraintsv constraints.Value
		if t.constraints != "" {
			constraintsv = constraints.MustParse(t.constraints)
			err = svc.SetConstraints(constraintsv)
			c.Assert(err, IsNil)
		}
		if t.config != nil {
			settings, err := ch.Config().ParseSettingsStrings(t.config)
			c.Assert(err, IsNil)
			err = svc.UpdateConfigSettings(settings)
			c.Assert(err, IsNil)
		}
		expect := t.expect
		expect.Constraints = constraintsv
		expect.Service = svc.Name()
		expect.Charm = ch.Meta().Name
		got, err := statecmd.ServiceGet(s.State, params.ServiceGet{svc.Name()})
		c.Assert(err, IsNil)
		c.Assert(got, DeepEquals, expect)
	}
}
Exemplo n.º 14
0
func (s *bootstrapSuite) TestBootstrapSpecifiedConstraints(c *gc.C) {
	env := newEnviron("foo", useDefaultKeys)
	cons := constraints.MustParse("cpu-cores=2 mem=4G")
	err := environs.Bootstrap(env, cons)
	c.Assert(err, gc.IsNil)
	c.Assert(env.bootstrapCount, gc.Equals, 1)
	c.Assert(env.constraints, gc.DeepEquals, cons)
}
Exemplo n.º 15
0
func (s *AssignSuite) TestAssignUnitNewPolicyWithDefaultContainerConstraint(c *C) {
	_, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)
	// Set up env constraints.
	econs := constraints.MustParse("container=lxc")
	err = s.State.SetEnvironConstraints(econs)
	c.Assert(err, IsNil)
	s.assertAssignUnitNewPolicyWithContainerConstraint(c)
}
Exemplo n.º 16
0
func (s *AssignSuite) TestAssignUnitNewPolicyWithContainerConstraint(c *C) {
	_, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)
	// Set up service constraints.
	scons := constraints.MustParse("container=lxc")
	err = s.wordpress.SetConstraints(scons)
	c.Assert(err, IsNil)
	s.assertAssignUnitNewPolicyWithContainerConstraint(c)
}
Exemplo n.º 17
0
func (s *DeployLocalSuite) TestDeployForceMachineId(c *C) {
	machine, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)
	c.Assert(machine.Id(), Equals, "0")
	err = s.State.SetEnvironConstraints(constraints.MustParse("mem=2G"))
	c.Assert(err, IsNil)
	serviceCons := constraints.MustParse("cpu-cores=2")
	service, err := s.Conn.DeployService(juju.DeployServiceParams{
		ServiceName:   "bob",
		Charm:         s.charm,
		Constraints:   serviceCons,
		NumUnits:      1,
		ToMachineSpec: "0",
	})
	c.Assert(err, IsNil)
	s.assertConstraints(c, service, serviceCons)
	s.assertMachines(c, service, constraints.Value{}, "0")
}
Exemplo n.º 18
0
func (s *localServerSuite) TestStartInstanceHardwareCharacteristics(c *C) {
	err := environs.Bootstrap(s.Env, constraints.Value{})
	c.Assert(err, IsNil)
	_, hc := testing.StartInstanceWithConstraints(c, s.Env, "100", constraints.MustParse("mem=1024"))
	c.Check(*hc.Arch, Equals, "amd64")
	c.Check(*hc.Mem, Equals, uint64(2048))
	c.Check(*hc.CpuCores, Equals, uint64(1))
	c.Assert(hc.CpuPower, IsNil)
}
Exemplo n.º 19
0
func (s *DeploySuite) TestConstraints(c *C) {
	coretesting.Charms.BundlePath(s.SeriesPath, "dummy")
	err := runDeploy(c, "local:dummy", "--constraints", "mem=2G cpu-cores=2")
	c.Assert(err, IsNil)
	curl := charm.MustParseURL("local:precise/dummy-1")
	service, _ := s.AssertService(c, "dummy", curl, 1, 0)
	cons, err := service.Constraints()
	c.Assert(err, IsNil)
	c.Assert(cons, DeepEquals, constraints.MustParse("mem=2G cpu-cores=2"))
}
Exemplo n.º 20
0
func FindInstanceSpec(e environs.Environ, series, arch, cons string) (spec *instances.InstanceSpec, err error) {
	env := e.(*environ)
	spec, err = findInstanceSpec(env, &instances.InstanceConstraint{
		Series:      series,
		Arches:      []string{arch},
		Region:      env.ecfg().region(),
		Constraints: constraints.MustParse(cons),
	})
	return
}
Exemplo n.º 21
0
func (s *AddMachineSuite) TestAddMachineWithConstraints(c *C) {
	err := runAddMachine(c, "--constraints", "mem=4G")
	c.Assert(err, IsNil)
	m, err := s.State.Machine("0")
	c.Assert(err, IsNil)
	mcons, err := m.Constraints()
	c.Assert(err, IsNil)
	expectedCons := constraints.MustParse("mem=4G")
	c.Assert(mcons, DeepEquals, expectedCons)
}
Exemplo n.º 22
0
func (t *LiveTests) TestStartInstanceConstraints(c *C) {
	cons := constraints.MustParse("mem=2G")
	inst, hc, err := t.Env.StartInstance("31", "fake_nonce", config.DefaultSeries, cons, testing.FakeStateInfo("31"), testing.FakeAPIInfo("31"))
	c.Assert(err, IsNil)
	defer t.Env.StopInstances([]instance.Instance{inst})
	ec2inst := ec2.InstanceEC2(inst)
	c.Assert(ec2inst.InstanceType, Equals, "m1.medium")
	c.Assert(*hc.Arch, Equals, "amd64")
	c.Assert(*hc.Mem, Equals, uint64(3840))
	c.Assert(*hc.CpuCores, Equals, uint64(1))
	c.Assert(*hc.CpuPower, Equals, uint64(200))
}
Exemplo n.º 23
0
func (s *specSuite) TestFindInstanceSpecErrors(c *C) {
	for i, t := range findInstanceSpecErrorTests {
		c.Logf("test %d", i)
		_, err := findInstanceSpec([]string{"test:"}, &instances.InstanceConstraint{
			Region:      "test",
			Series:      t.series,
			Arches:      t.arches,
			Constraints: constraints.MustParse(t.cons),
		})
		c.Check(err, ErrorMatches, t.err)
	}
}
Exemplo n.º 24
0
func (s *ProvisionerSuite) TestConstraints(c *C) {
	// Create a machine with non-standard constraints.
	m, err := s.addMachine()
	c.Assert(err, IsNil)
	cons := constraints.MustParse("mem=8G arch=amd64 cpu-cores=2")
	err = m.SetConstraints(cons)
	c.Assert(err, IsNil)

	// Start a provisioner and check those constraints are used.
	p := s.newEnvironProvisioner("0")
	defer stop(c, p)
	s.checkStartInstanceCustom(c, m, "pork", cons)
}
Exemplo n.º 25
0
func (s *ProvisionerSuite) TestConstraints(c *C) {
	// Create a machine with non-standard constraints.
	m, err := s.State.AddMachine(config.DefaultSeries, state.JobHostUnits)
	c.Assert(err, IsNil)
	cons := constraints.MustParse("mem=4G arch=amd64")
	err = m.SetConstraints(cons)
	c.Assert(err, IsNil)

	// Start a provisioner and check those constraints are used.
	p := provisioner.NewProvisioner(s.State, "0")
	defer stop(c, p)
	s.checkStartInstanceCustom(c, m, "pork", cons)
}
Exemplo n.º 26
0
func (s *MachineSuite) TestSetConstraints(c *C) {
	machine, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)

	// Constraints can be set...
	cons1 := constraints.MustParse("mem=1G")
	err = machine.SetConstraints(cons1)
	c.Assert(err, IsNil)
	mcons, err := machine.Constraints()
	c.Assert(err, IsNil)
	c.Assert(mcons, DeepEquals, cons1)

	// ...until the machine is provisioned, at which point they stick.
	err = machine.SetProvisioned("i-mstuck", "fake_nonce", nil)
	c.Assert(err, IsNil)
	cons2 := constraints.MustParse("mem=2G")
	err = machine.SetConstraints(cons2)
	c.Assert(err, ErrorMatches, "cannot set constraints: machine is already provisioned")

	// Check the failed set had no effect.
	mcons, err = machine.Constraints()
	c.Assert(err, IsNil)
	c.Assert(mcons, DeepEquals, cons1)
}
Exemplo n.º 27
0
func (t *LiveTests) BootstrapOnce(c *C) {
	if t.bootstrapped {
		return
	}
	// We only build and upload tools if there will be a state agent that
	// we could connect to (actual live tests, rather than local-only)
	cons := constraints.MustParse("mem=2G")
	if t.CanOpenState {
		_, err := tools.Upload(t.Env.Storage(), nil, config.DefaultSeries)
		c.Assert(err, IsNil)
	}
	err := environs.Bootstrap(t.Env, cons)
	c.Assert(err, IsNil)
	t.bootstrapped = true
}
Exemplo n.º 28
0
func (s *AssignSuite) TestDirectAssignIgnoresConstraints(c *C) {
	// Set up constraints.
	scons := constraints.MustParse("mem=2G cpu-power=400")
	err := s.wordpress.SetConstraints(scons)
	c.Assert(err, IsNil)
	econs := constraints.MustParse("mem=4G cpu-cores=2")
	err = s.State.SetEnvironConstraints(econs)
	c.Assert(err, IsNil)

	// Machine will take environment constraints on creation.
	machine, err := s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)

	// Unit will take combined service/environ constraints on creation.
	unit, err := s.wordpress.AddUnit()
	c.Assert(err, IsNil)

	// Machine keeps its original constraints on direct assignment.
	err = unit.AssignToMachine(machine)
	c.Assert(err, IsNil)
	mcons, err := machine.Constraints()
	c.Assert(err, IsNil)
	c.Assert(mcons, DeepEquals, econs)
}
Exemplo n.º 29
0
func (s *CloudInitSuite) TestFinishBootstrapConfig(c *C) {
	cfg, err := config.New(map[string]interface{}{
		"name":            "barbara",
		"type":            "dummy",
		"admin-secret":    "lisboan-pork",
		"authorized-keys": "we-are-the-keys",
		"agent-version":   "1.2.3",
		"ca-cert":         testing.CACert,
		"ca-private-key":  testing.CAKey,
		"state-server":    false,
		"secret":          "british-horse",
	})
	c.Assert(err, IsNil)
	oldAttrs := cfg.AllAttrs()
	mcfg := &cloudinit.MachineConfig{
		StateServer: true,
	}
	cons := constraints.MustParse("mem=1T cpu-power=999999999")
	err = environs.FinishMachineConfig(mcfg, cfg, cons)
	c.Check(err, IsNil)
	c.Check(mcfg.AuthorizedKeys, Equals, "we-are-the-keys")
	password := utils.PasswordHash("lisboan-pork")
	c.Check(mcfg.APIInfo, DeepEquals, &api.Info{
		Password: password, CACert: []byte(testing.CACert),
	})
	c.Check(mcfg.StateInfo, DeepEquals, &state.Info{
		Password: password, CACert: []byte(testing.CACert),
	})
	c.Check(mcfg.StatePort, Equals, cfg.StatePort())
	c.Check(mcfg.APIPort, Equals, cfg.APIPort())
	c.Check(mcfg.Constraints, DeepEquals, cons)

	oldAttrs["ca-private-key"] = ""
	oldAttrs["admin-secret"] = ""
	delete(oldAttrs, "secret")
	c.Check(mcfg.Config.AllAttrs(), DeepEquals, oldAttrs)
	srvCertPEM := mcfg.StateServerCert
	srvKeyPEM := mcfg.StateServerKey
	_, _, err = cert.ParseCertAndKey(srvCertPEM, srvKeyPEM)
	c.Check(err, IsNil)

	err = cert.Verify(srvCertPEM, []byte(testing.CACert), time.Now())
	c.Assert(err, IsNil)
	err = cert.Verify(srvCertPEM, []byte(testing.CACert), time.Now().AddDate(9, 0, 0))
	c.Assert(err, IsNil)
	err = cert.Verify(srvCertPEM, []byte(testing.CACert), time.Now().AddDate(10, 0, 1))
	c.Assert(err, NotNil)
}
Exemplo n.º 30
0
func (s *specSuite) TestFindInstanceSpec(c *C) {
	for i, t := range findInstanceSpecTests {
		c.Logf("test %d", i)
		storage := ebsStorage
		spec, err := findInstanceSpec([]string{"test:"}, &instances.InstanceConstraint{
			Region:      "test",
			Series:      t.series,
			Arches:      t.arches,
			Constraints: constraints.MustParse(t.cons),
			Storage:     &storage,
		})
		c.Assert(err, IsNil)
		c.Check(spec.InstanceType.Name, Equals, t.itype)
		c.Check(spec.Image.Id, Equals, t.image)
	}
}