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
// AddStateServerMachine adds a "state server" 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 state servers.
24
func AddStateServerMachine(c *gc.C, st *state.State) *state.Machine {
25
machine, err := st.AddMachine("quantal", state.JobManageEnviron)
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-{{.}}",
51
// AllocatableIPLow: "{{if (gt . 0)}}10.10.{{.}}.5{{end}}",
52
// AllocatableIPHigh: "{{if (gt . 0)}}10.10.{{.}}.254{{end}}",
56
// This is equivalent to the following calls:
58
// _, err := st.AddSubnet(state.SubnetInfo{
59
// CIDR: "10.10.0.0/24",
60
// ProviderId: "subnet-0",
61
// SpaceName: "space1",
62
// AvailabilityZone: "zone-0",
65
// c.Assert(err, jc.ErrorIsNil)
66
// _, err = st.AddSubnet(state.SubnetInfo{
67
// CIDR: "10.10.1.0/24",
68
// ProviderId: "subnet-1",
69
// SpaceName: "space1",
70
// AvailabilityZone: "zone-1",
71
// AllocatableIPLow: "10.10.1.5",
72
// AllocatableIPHigh: "10.10.1.254",
75
func AddSubnetsWithTemplate(c *gc.C, st *state.State, numSubnets uint, infoTemplate state.SubnetInfo) {
76
for subnetIndex := 0; subnetIndex < int(numSubnets); subnetIndex++ {
77
info := infoTemplate // make a copy each time.
79
// permute replaces the contents of *s with the result of interpreting
81
permute := func(s string) string {
82
t, err := template.New("").Parse(s)
83
c.Assert(err, jc.ErrorIsNil)
86
err = t.Execute(&buf, subnetIndex)
87
c.Assert(err, jc.ErrorIsNil)
91
info.ProviderId = permute(info.ProviderId)
92
info.CIDR = permute(info.CIDR)
93
info.AllocatableIPHigh = permute(info.AllocatableIPHigh)
94
info.AllocatableIPLow = permute(info.AllocatableIPLow)
95
info.AvailabilityZone = permute(info.AvailabilityZone)
96
info.SpaceName = permute(info.SpaceName)
98
_, err := st.AddSubnet(info)
99
c.Assert(err, jc.ErrorIsNil)