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