github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/jujud/agent/testing/agent.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  	"net"
     9  	"strconv"
    10  	"time"
    11  
    12  	"github.com/juju/names"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/cmd"
    17  	"github.com/juju/juju/agent"
    18  
    19  	agenttools "github.com/juju/juju/agent/tools"
    20  	"github.com/juju/juju/environs"
    21  	"github.com/juju/juju/environs/filestorage"
    22  	envtesting "github.com/juju/juju/environs/testing"
    23  	envtools "github.com/juju/juju/environs/tools"
    24  	"github.com/juju/juju/juju/testing"
    25  	"github.com/juju/juju/mongo"
    26  	"github.com/juju/juju/network"
    27  	"github.com/juju/juju/state"
    28  	coretesting "github.com/juju/juju/testing"
    29  	coretools "github.com/juju/juju/tools"
    30  	"github.com/juju/juju/version"
    31  	"github.com/juju/juju/worker/peergrouper"
    32  )
    33  
    34  type FakeEnsure struct {
    35  	EnsureCount    int
    36  	InitiateCount  int
    37  	DataDir        string
    38  	Namespace      string
    39  	OplogSize      int
    40  	Info           state.StateServingInfo
    41  	InitiateParams peergrouper.InitiateMongoParams
    42  	Err            error
    43  }
    44  
    45  func (f *FakeEnsure) FakeEnsureMongo(args mongo.EnsureServerParams) error {
    46  	f.EnsureCount++
    47  	f.DataDir, f.Namespace, f.OplogSize = args.DataDir, args.Namespace, args.OplogSize
    48  	f.Info = state.StateServingInfo{
    49  		APIPort:        args.APIPort,
    50  		StatePort:      args.StatePort,
    51  		Cert:           args.Cert,
    52  		PrivateKey:     args.PrivateKey,
    53  		CAPrivateKey:   args.CAPrivateKey,
    54  		SharedSecret:   args.SharedSecret,
    55  		SystemIdentity: args.SystemIdentity,
    56  	}
    57  	return f.Err
    58  }
    59  
    60  func (f *FakeEnsure) FakeInitiateMongo(p peergrouper.InitiateMongoParams) error {
    61  	f.InitiateCount++
    62  	f.InitiateParams = p
    63  	return nil
    64  }
    65  
    66  // agentSuite is a fixture to be used by agent test suites.
    67  type AgentSuite struct {
    68  	oldRestartDelay time.Duration
    69  	testing.JujuConnSuite
    70  }
    71  
    72  // PrimeAgent writes the configuration file and tools with version vers
    73  // for an agent with the given entity name.  It returns the agent's
    74  // configuration and the current tools.
    75  func (s *AgentSuite) PrimeAgent(c *gc.C, tag names.Tag, password string, vers version.Binary) (agent.ConfigSetterWriter, *coretools.Tools) {
    76  	c.Logf("priming agent %s", tag.String())
    77  	stor, err := filestorage.NewFileStorageWriter(c.MkDir())
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	agentTools := envtesting.PrimeTools(c, stor, s.DataDir(), "released", vers)
    80  	err = envtools.MergeAndWriteMetadata(stor, "released", "released", coretools.List{agentTools}, envtools.DoNotWriteMirrors)
    81  	tools1, err := agenttools.ChangeAgentTools(s.DataDir(), tag.String(), vers)
    82  	c.Assert(err, jc.ErrorIsNil)
    83  	c.Assert(tools1, gc.DeepEquals, agentTools)
    84  
    85  	stateInfo := s.MongoInfo(c)
    86  	apiInfo := s.APIInfo(c)
    87  	conf, err := agent.NewAgentConfig(
    88  		agent.AgentConfigParams{
    89  			DataDir:           s.DataDir(),
    90  			Tag:               tag,
    91  			UpgradedToVersion: vers.Number,
    92  			Password:          password,
    93  			Nonce:             agent.BootstrapNonce,
    94  			StateAddresses:    stateInfo.Addrs,
    95  			APIAddresses:      apiInfo.Addrs,
    96  			CACert:            stateInfo.CACert,
    97  			Environment:       apiInfo.EnvironTag,
    98  		})
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	conf.SetPassword(password)
   101  	c.Assert(conf.Write(), gc.IsNil)
   102  	s.primeAPIHostPorts(c)
   103  	return conf, agentTools
   104  }
   105  
   106  func (s *AgentSuite) primeAPIHostPorts(c *gc.C) {
   107  	apiInfo := s.APIInfo(c)
   108  
   109  	c.Assert(apiInfo.Addrs, gc.HasLen, 1)
   110  	hostPort, err := ParseHostPort(apiInfo.Addrs[0])
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	err = s.State.SetAPIHostPorts([][]network.HostPort{{hostPort}})
   114  	c.Assert(err, jc.ErrorIsNil)
   115  
   116  	c.Logf("api host ports primed %#v", hostPort)
   117  }
   118  
   119  func ParseHostPort(s string) (network.HostPort, error) {
   120  	addr, port, err := net.SplitHostPort(s)
   121  	if err != nil {
   122  		return network.HostPort{}, err
   123  	}
   124  	portNum, err := strconv.Atoi(port)
   125  	if err != nil {
   126  		return network.HostPort{}, fmt.Errorf("bad port number %q", port)
   127  	}
   128  	addrs := network.NewAddresses(addr)
   129  	hostPorts := network.AddressesWithPort(addrs, portNum)
   130  	return hostPorts[0], nil
   131  }
   132  
   133  // InitAgent initialises the given agent command with additional
   134  // arguments as provided.
   135  func (s *AgentSuite) InitAgent(c *gc.C, a cmd.Command, args ...string) {
   136  	args = append([]string{"--data-dir", s.DataDir()}, args...)
   137  	err := coretesting.InitCommand(a, args)
   138  	c.Assert(err, jc.ErrorIsNil)
   139  }
   140  
   141  func (s *AgentSuite) AssertCanOpenState(c *gc.C, tag names.Tag, dataDir string) {
   142  	config, err := agent.ReadConfig(agent.ConfigPath(dataDir, tag))
   143  	c.Assert(err, jc.ErrorIsNil)
   144  	info, ok := config.MongoInfo()
   145  	c.Assert(ok, jc.IsTrue)
   146  	st, err := state.Open(info, mongo.DialOpts{}, environs.NewStatePolicy())
   147  	c.Assert(err, jc.ErrorIsNil)
   148  	st.Close()
   149  }
   150  
   151  func (s *AgentSuite) AssertCannotOpenState(c *gc.C, tag names.Tag, dataDir string) {
   152  	config, err := agent.ReadConfig(agent.ConfigPath(dataDir, tag))
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	_, ok := config.MongoInfo()
   155  	c.Assert(ok, jc.IsFalse)
   156  }