github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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 "fmt" 9 "reflect" 10 "text/template" 11 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/network" 16 "github.com/juju/juju/state" 17 ) 18 19 // AddStateServerMachine adds a "state server" machine to the state so 20 // that State.Addresses and State.APIAddresses will work. It returns the 21 // added machine. The addresses that those methods will return bear no 22 // relation to the addresses actually used by the state and API servers. 23 // It returns the addresses that will be returned by the State.Addresses 24 // and State.APIAddresses methods, which will not bear any relation to 25 // the be the addresses used by the state servers. 26 func AddStateServerMachine(c *gc.C, st *state.State) *state.Machine { 27 machine, err := st.AddMachine("quantal", state.JobManageEnviron) 28 c.Assert(err, jc.ErrorIsNil) 29 err = machine.SetProviderAddresses(network.NewAddress("0.1.2.3")) 30 c.Assert(err, jc.ErrorIsNil) 31 32 hostPorts := [][]network.HostPort{ 33 network.NewHostPorts(1234, "0.1.2.3"), 34 } 35 err = st.SetAPIHostPorts(hostPorts) 36 c.Assert(err, jc.ErrorIsNil) 37 38 return machine 39 } 40 41 // AddSubnetsWithTemplate adds numSubnets subnets, using the given 42 // infoTemplate. Any string field in the infoTemplate can be specified 43 // as a text/template string containing {{.}}, which is the current 44 // index of the subnet-to-add (between 0 and numSubnets-1). 45 // 46 // Example: 47 // 48 // AddSubnetsWithTemplate(c, st, 2, state.SubnetInfo{ 49 // CIDR: "10.10.{{.}}.0/24", 50 // ProviderId: "subnet-{{.}}", 51 // SpaceName: "space1", 52 // AvailabilityZone: "zone-{{.}}", 53 // AllocatableIPLow: "{{if (gt . 0)}}10.10.{{.}}.5{{end}}", 54 // AllocatableIPHigh: "{{if (gt . 0)}}10.10.{{.}}.254{{end}}", 55 // VLANTag: 42, 56 // }) 57 // 58 // This is equivalent to the following calls: 59 // 60 // _, err := st.AddSubnet(state.SubnetInfo{ 61 // CIDR: "10.10.0.0/24", 62 // ProviderId: "subnet-0", 63 // SpaceName: "space1", 64 // AvailabilityZone: "zone-0", 65 // VLANTag: 42, 66 // }) 67 // c.Assert(err, jc.ErrorIsNil) 68 // _, err = st.AddSubnet(state.SubnetInfo{ 69 // CIDR: "10.10.1.0/24", 70 // ProviderId: "subnet-1", 71 // SpaceName: "space1", 72 // AvailabilityZone: "zone-1", 73 // AllocatableIPLow: "10.10.1.5", 74 // AllocatableIPHigh: "10.10.1.254", 75 // VLANTag: 42, 76 // }) 77 func AddSubnetsWithTemplate(c *gc.C, st *state.State, numSubnets uint, infoTemplate state.SubnetInfo) { 78 infot := reflect.TypeOf(infoTemplate) 79 for subnetIndex := 0; subnetIndex < int(numSubnets); subnetIndex++ { 80 info := infoTemplate // make a copy each time. 81 82 for fieldIndex := 0; fieldIndex < infot.NumField(); fieldIndex++ { 83 fieldv := reflect.ValueOf(&info).Elem().Field(fieldIndex) 84 85 if fieldv.Kind() != reflect.String { 86 // Skip non string fields. 87 continue 88 } 89 90 text := fmt.Sprint(fieldv.Interface()) 91 t, err := template.New("").Parse(text) 92 c.Assert(err, jc.ErrorIsNil) 93 94 var buf bytes.Buffer 95 err = t.Execute(&buf, subnetIndex) 96 c.Assert(err, jc.ErrorIsNil) 97 fieldv.SetString(buf.String()) 98 } 99 _, err := st.AddSubnet(info) 100 c.Assert(err, jc.ErrorIsNil) 101 } 102 }