github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 // VLANTag: 42, 52 // }) 53 // 54 // This is equivalent to the following calls: 55 // 56 // _, err := st.AddSubnet(state.SubnetInfo{ 57 // CIDR: "10.10.0.0/24", 58 // ProviderId: "subnet-0", 59 // SpaceName: "space1", 60 // AvailabilityZone: "zone-0", 61 // VLANTag: 42, 62 // }) 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", 69 // VLANTag: 42, 70 // }) 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. 74 75 // permute replaces the contents of *s with the result of interpreting 76 // *s as a template. 77 permute := func(s string) string { 78 t, err := template.New("").Parse(s) 79 c.Assert(err, jc.ErrorIsNil) 80 81 var buf bytes.Buffer 82 err = t.Execute(&buf, subnetIndex) 83 c.Assert(err, jc.ErrorIsNil) 84 return buf.String() 85 } 86 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) 91 92 _, err := st.AddSubnet(info) 93 c.Assert(err, jc.ErrorIsNil) 94 } 95 }