github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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  	"github.com/juju/names"
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"github.com/juju/juju/constraints"
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/config"
    15  	"github.com/juju/juju/environs/network"
    16  	"github.com/juju/juju/environs/tools"
    17  	"github.com/juju/juju/instance"
    18  	"github.com/juju/juju/state"
    19  	"github.com/juju/juju/state/api"
    20  	"github.com/juju/juju/testing"
    21  )
    22  
    23  // FakeStateInfo holds information about no state - it will always
    24  // give an error when connected to.  The machine id gives the machine id
    25  // of the machine to be started.
    26  func FakeStateInfo(machineId string) *state.Info {
    27  	return &state.Info{
    28  		Addrs:    []string{"0.1.2.3:1234"},
    29  		Tag:      names.MachineTag(machineId),
    30  		Password: "unimportant",
    31  		CACert:   testing.CACert,
    32  	}
    33  }
    34  
    35  // FakeAPIInfo holds information about no state - it will always
    36  // give an error when connected to.  The machine id gives the machine id
    37  // of the machine to be started.
    38  func FakeAPIInfo(machineId string) *api.Info {
    39  	return &api.Info{
    40  		Addrs:    []string{"0.1.2.3:1234"},
    41  		Tag:      names.MachineTag(machineId),
    42  		Password: "unimportant",
    43  		CACert:   testing.CACert,
    44  	}
    45  }
    46  
    47  // WaitAddresses waits until the specified instance has addresses, and returns them.
    48  func WaitInstanceAddresses(env environs.Environ, instId instance.Id) ([]instance.Address, error) {
    49  	for a := testing.LongAttempt.Start(); a.Next(); {
    50  		insts, err := env.Instances([]instance.Id{instId})
    51  		if err != nil {
    52  			return nil, err
    53  		}
    54  		addresses, err := insts[0].Addresses()
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  		if len(addresses) > 0 {
    59  			return addresses, nil
    60  		}
    61  	}
    62  	return nil, fmt.Errorf("timed out trying to get addresses for %v", instId)
    63  }
    64  
    65  // AssertStartInstance is a test helper function that starts an instance with a
    66  // plausible but invalid configuration, and checks that it succeeds.
    67  func AssertStartInstance(
    68  	c *gc.C, env environs.Environ, machineId string,
    69  ) (
    70  	instance.Instance, *instance.HardwareCharacteristics,
    71  ) {
    72  	inst, hc, _, err := StartInstance(env, machineId)
    73  	c.Assert(err, gc.IsNil)
    74  	return inst, hc
    75  }
    76  
    77  // StartInstance is a test helper function that starts an instance with a plausible
    78  // but invalid configuration, and returns the result of Environ.StartInstance.
    79  func StartInstance(
    80  	env environs.Environ, machineId string,
    81  ) (
    82  	instance.Instance, *instance.HardwareCharacteristics, []network.Info, error,
    83  ) {
    84  	return StartInstanceWithConstraints(env, machineId, constraints.Value{})
    85  }
    86  
    87  // AssertStartInstanceWithConstraints is a test helper function that starts an instance
    88  // with the given constraints, and a plausible but invalid configuration, and returns
    89  // the result of Environ.StartInstance.
    90  func AssertStartInstanceWithConstraints(
    91  	c *gc.C, env environs.Environ, machineId string, cons constraints.Value,
    92  ) (
    93  	instance.Instance, *instance.HardwareCharacteristics,
    94  ) {
    95  	inst, hc, _, err := StartInstanceWithConstraints(env, machineId, cons)
    96  	c.Assert(err, gc.IsNil)
    97  	return inst, hc
    98  }
    99  
   100  // StartInstanceWithConstraints is a test helper function that starts an instance
   101  // with the given constraints, and a plausible but invalid configuration, and returns
   102  // the result of Environ.StartInstance.
   103  func StartInstanceWithConstraints(
   104  	env environs.Environ, machineId string, cons constraints.Value,
   105  ) (
   106  	instance.Instance, *instance.HardwareCharacteristics, []network.Info, error,
   107  ) {
   108  	return StartInstanceWithConstraintsAndNetworks(env, machineId, cons, nil)
   109  }
   110  
   111  // AssertStartInstanceWithNetworks is a test helper function that starts an
   112  // instance with the given networks, and a plausible but invalid
   113  // configuration, and returns the result of Environ.StartInstance.
   114  func AssertStartInstanceWithNetworks(
   115  	c *gc.C, env environs.Environ, machineId string, cons constraints.Value,
   116  	networks []string,
   117  ) (
   118  	instance.Instance, *instance.HardwareCharacteristics,
   119  ) {
   120  	inst, hc, _, err := StartInstanceWithConstraintsAndNetworks(
   121  		env, machineId, cons, networks)
   122  	c.Assert(err, gc.IsNil)
   123  	return inst, hc
   124  }
   125  
   126  // StartInstanceWithConstraintsAndNetworks is a test helper function that
   127  // starts an instance with the given networks, and a plausible but invalid
   128  // configuration, and returns the result of Environ.StartInstance.
   129  func StartInstanceWithConstraintsAndNetworks(
   130  	env environs.Environ, machineId string, cons constraints.Value,
   131  	networks []string,
   132  ) (
   133  	instance.Instance, *instance.HardwareCharacteristics, []network.Info, error,
   134  ) {
   135  	params := environs.StartInstanceParams{Constraints: cons}
   136  	return StartInstanceWithParams(
   137  		env, machineId, params, networks)
   138  }
   139  
   140  // StartInstanceWithParams is a test helper function that starts an instance
   141  // with the given parameters, and a plausible but invalid configuration, and
   142  // returns the result of Environ.StartInstance. The provided params's
   143  // MachineConfig and Tools field values will be ignored.
   144  func StartInstanceWithParams(
   145  	env environs.Environ, machineId string,
   146  	params environs.StartInstanceParams,
   147  	networks []string,
   148  ) (
   149  	instance.Instance, *instance.HardwareCharacteristics, []network.Info, error,
   150  ) {
   151  	series := config.PreferredSeries(env.Config())
   152  	agentVersion, ok := env.Config().AgentVersion()
   153  	if !ok {
   154  		return nil, nil, nil, fmt.Errorf("missing agent version in environment config")
   155  	}
   156  	possibleTools, err := tools.FindInstanceTools(
   157  		env, agentVersion, series, params.Constraints.Arch,
   158  	)
   159  	if err != nil {
   160  		return nil, nil, nil, err
   161  	}
   162  	machineNonce := "fake_nonce"
   163  	stateInfo := FakeStateInfo(machineId)
   164  	apiInfo := FakeAPIInfo(machineId)
   165  	machineConfig := environs.NewMachineConfig(
   166  		machineId, machineNonce,
   167  		networks,
   168  		stateInfo, apiInfo)
   169  	params.Tools = possibleTools
   170  	params.MachineConfig = machineConfig
   171  	return env.StartInstance(params)
   172  }