1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
jc "github.com/juju/testing/checkers"
11
gc "gopkg.in/check.v1"
13
"github.com/juju/juju/network"
14
"github.com/juju/juju/state"
17
// AddControllerMachine adds a "controller" machine to the state so
18
// that State.Addresses and State.APIAddresses will work. It returns the
19
// added machine. The addresses that those methods will return bear no
20
// relation to the addresses actually used by the state and API servers.
21
// It returns the addresses that will be returned by the State.Addresses
22
// and State.APIAddresses methods, which will not bear any relation to
23
// the be the addresses used by the controllers.
24
func AddControllerMachine(c *gc.C, st *state.State) *state.Machine {
25
machine, err := st.AddMachine("quantal", state.JobManageModel)
26
c.Assert(err, jc.ErrorIsNil)
27
err = machine.SetProviderAddresses(network.NewAddress("0.1.2.3"))
28
c.Assert(err, jc.ErrorIsNil)
30
hostPorts := [][]network.HostPort{
31
network.NewHostPorts(1234, "0.1.2.3"),
33
err = st.SetAPIHostPorts(hostPorts)
34
c.Assert(err, jc.ErrorIsNil)
39
// AddSubnetsWithTemplate adds numSubnets subnets, using the given
40
// infoTemplate. Any string field in the infoTemplate can be specified
41
// as a text/template string containing {{.}}, which is the current
42
// index of the subnet-to-add (between 0 and numSubnets-1).
46
// AddSubnetsWithTemplate(c, st, 2, state.SubnetInfo{
47
// CIDR: "10.10.{{.}}.0/24",
48
// ProviderId: "subnet-{{.}}",
49
// SpaceName: "space1",
50
// AvailabilityZone: "zone-{{.}}",
54
// This is equivalent to the following calls:
56
// _, err := st.AddSubnet(state.SubnetInfo{
57
// CIDR: "10.10.0.0/24",
58
// ProviderId: "subnet-0",
59
// SpaceName: "space1",
60
// AvailabilityZone: "zone-0",
63
// c.Assert(err, jc.ErrorIsNil)
64
// _, err = st.AddSubnet(state.SubnetInfo{
65
// CIDR: "10.10.1.0/24",
66
// ProviderId: "subnet-1",
67
// SpaceName: "space1",
68
// AvailabilityZone: "zone-1",
71
func AddSubnetsWithTemplate(c *gc.C, st *state.State, numSubnets uint, infoTemplate state.SubnetInfo) {
72
for subnetIndex := 0; subnetIndex < int(numSubnets); subnetIndex++ {
73
info := infoTemplate // make a copy each time.
75
// permute replaces the contents of *s with the result of interpreting
77
permute := func(s string) string {
78
t, err := template.New("").Parse(s)
79
c.Assert(err, jc.ErrorIsNil)
82
err = t.Execute(&buf, subnetIndex)
83
c.Assert(err, jc.ErrorIsNil)
87
info.ProviderId = network.Id(permute(string(info.ProviderId)))
88
info.CIDR = permute(info.CIDR)
89
info.AvailabilityZone = permute(info.AvailabilityZone)
90
info.SpaceName = permute(info.SpaceName)
92
_, err := st.AddSubnet(info)
93
c.Assert(err, jc.ErrorIsNil)