launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/juju/testing/instance.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  	"fmt"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	"launchpad.net/juju-core/constraints"
    12  	"launchpad.net/juju-core/environs"
    13  	"launchpad.net/juju-core/environs/tools"
    14  	"launchpad.net/juju-core/instance"
    15  	"launchpad.net/juju-core/names"
    16  	"launchpad.net/juju-core/state"
    17  	"launchpad.net/juju-core/state/api"
    18  	"launchpad.net/juju-core/testing"
    19  )
    20  
    21  // FakeStateInfo holds information about no state - it will always
    22  // give an error when connected to.  The machine id gives the machine id
    23  // of the machine to be started.
    24  func FakeStateInfo(machineId string) *state.Info {
    25  	return &state.Info{
    26  		Addrs:    []string{"0.1.2.3:1234"},
    27  		Tag:      names.MachineTag(machineId),
    28  		Password: "unimportant",
    29  		CACert:   []byte(testing.CACert),
    30  	}
    31  }
    32  
    33  // FakeAPIInfo holds information about no state - it will always
    34  // give an error when connected to.  The machine id gives the machine id
    35  // of the machine to be started.
    36  func FakeAPIInfo(machineId string) *api.Info {
    37  	return &api.Info{
    38  		Addrs:    []string{"0.1.2.3:1234"},
    39  		Tag:      names.MachineTag(machineId),
    40  		Password: "unimportant",
    41  		CACert:   []byte(testing.CACert),
    42  	}
    43  }
    44  
    45  // AssertStartInstance is a test helper function that starts an instance with a
    46  // plausible but invalid configuration, and checks that it succeeds.
    47  func AssertStartInstance(
    48  	c *gc.C, env environs.Environ, machineId string,
    49  ) (
    50  	instance.Instance, *instance.HardwareCharacteristics,
    51  ) {
    52  	inst, hc, err := StartInstance(env, machineId)
    53  	c.Assert(err, gc.IsNil)
    54  	return inst, hc
    55  }
    56  
    57  // StartInstance is a test helper function that starts an instance with a plausible
    58  // but invalid configuration, and returns the result of Environ.StartInstance.
    59  func StartInstance(
    60  	env environs.Environ, machineId string,
    61  ) (
    62  	instance.Instance, *instance.HardwareCharacteristics, error,
    63  ) {
    64  	return StartInstanceWithConstraints(env, machineId, constraints.Value{})
    65  }
    66  
    67  // AssertStartInstanceWithConstraints is a test helper function that starts an instance
    68  // with the given constraints, and a plausible but invalid configuration, and returns
    69  // the result of Environ.StartInstance.
    70  func AssertStartInstanceWithConstraints(
    71  	c *gc.C, env environs.Environ, machineId string, cons constraints.Value,
    72  ) (
    73  	instance.Instance, *instance.HardwareCharacteristics,
    74  ) {
    75  	inst, hc, err := StartInstanceWithConstraints(env, machineId, cons)
    76  	c.Assert(err, gc.IsNil)
    77  	return inst, hc
    78  }
    79  
    80  // StartInstanceWithConstraints is a test helper function that starts an instance
    81  // with the given constraints, and a plausible but invalid configuration, and returns
    82  // the result of Environ.StartInstance.
    83  func StartInstanceWithConstraints(
    84  	env environs.Environ, machineId string, cons constraints.Value,
    85  ) (
    86  	instance.Instance, *instance.HardwareCharacteristics, error,
    87  ) {
    88  	series := env.Config().DefaultSeries()
    89  	agentVersion, ok := env.Config().AgentVersion()
    90  	if !ok {
    91  		return nil, nil, fmt.Errorf("missing agent version in environment config")
    92  	}
    93  	possibleTools, err := tools.FindInstanceTools(env, agentVersion, series, cons.Arch)
    94  	if err != nil {
    95  		return nil, nil, err
    96  	}
    97  	machineNonce := "fake_nonce"
    98  	stateInfo := FakeStateInfo(machineId)
    99  	apiInfo := FakeAPIInfo(machineId)
   100  	machineConfig := environs.NewMachineConfig(machineId, machineNonce, stateInfo, apiInfo)
   101  	return env.StartInstance(cons, possibleTools, machineConfig)
   102  }