github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/juju/testing/utils.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "bytes" 8 "text/template" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/network" 14 "github.com/juju/juju/state" 15 ) 16 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) 29 30 hostPorts := [][]network.HostPort{ 31 network.NewHostPorts(1234, "0.1.2.3"), 32 } 33 err = st.SetAPIHostPorts(hostPorts) 34 c.Assert(err, jc.ErrorIsNil) 35 36 return machine 37 } 38 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). 43 // 44 // Example: 45 // 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}}", 53 // VLANTag: 42, 54 // }) 55 // 56 // This is equivalent to the following calls: 57 // 58 // _, err := st.AddSubnet(state.SubnetInfo{ 59 // CIDR: "10.10.0.0/24", 60 // ProviderId: "subnet-0", 61 // SpaceName: "space1", 62 // AvailabilityZone: "zone-0", 63 // VLANTag: 42, 64 // }) 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", 73 // VLANTag: 42, 74 // }) 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. 78 79 // permute replaces the contents of *s with the result of interpreting 80 // *s as a template. 81 permute := func(s string) string { 82 t, err := template.New("").Parse(s) 83 c.Assert(err, jc.ErrorIsNil) 84 85 var buf bytes.Buffer 86 err = t.Execute(&buf, subnetIndex) 87 c.Assert(err, jc.ErrorIsNil) 88 return buf.String() 89 } 90 91 info.ProviderId = network.Id(permute(string(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) 97 98 _, err := st.AddSubnet(info) 99 c.Assert(err, jc.ErrorIsNil) 100 } 101 }