github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/jujud/agent/agent_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	jc "github.com/juju/testing/checkers"
     9  	"github.com/juju/utils/series"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	agenttesting "github.com/juju/juju/cmd/jujud/agent/testing"
    13  	cmdutil "github.com/juju/juju/cmd/jujud/util"
    14  	imagetesting "github.com/juju/juju/environs/imagemetadata/testing"
    15  	"github.com/juju/juju/juju/paths"
    16  	"github.com/juju/juju/mongo"
    17  	"github.com/juju/juju/network"
    18  	coretesting "github.com/juju/juju/testing"
    19  	"github.com/juju/juju/worker"
    20  	"github.com/juju/juju/worker/proxyupdater"
    21  )
    22  
    23  type acCreator func() (cmd.Command, AgentConf)
    24  
    25  // CheckAgentCommand is a utility function for verifying that common agent
    26  // options are handled by a Command; it returns an instance of that
    27  // command pre-parsed, with any mandatory flags added.
    28  func CheckAgentCommand(c *gc.C, create acCreator, args []string) cmd.Command {
    29  	com, conf := create()
    30  	err := coretesting.InitCommand(com, args)
    31  	dataDir, err := paths.DataDir(series.HostSeries())
    32  	c.Assert(err, jc.ErrorIsNil)
    33  	c.Assert(conf.DataDir(), gc.Equals, dataDir)
    34  	badArgs := append(args, "--data-dir", "")
    35  	com, _ = create()
    36  	err = coretesting.InitCommand(com, badArgs)
    37  	c.Assert(err, gc.ErrorMatches, "--data-dir option must be set")
    38  
    39  	args = append(args, "--data-dir", "jd")
    40  	com, conf = create()
    41  	c.Assert(coretesting.InitCommand(com, args), gc.IsNil)
    42  	c.Assert(conf.DataDir(), gc.Equals, "jd")
    43  	return com
    44  }
    45  
    46  // ParseAgentCommand is a utility function that inserts the always-required args
    47  // before parsing an agent command and returning the result.
    48  func ParseAgentCommand(ac cmd.Command, args []string) error {
    49  	common := []string{
    50  		"--data-dir", "jd",
    51  	}
    52  	return coretesting.InitCommand(ac, append(common, args...))
    53  }
    54  
    55  // AgentSuite is a fixture to be used by agent test suites.
    56  type AgentSuite struct {
    57  	agenttesting.AgentSuite
    58  }
    59  
    60  func (s *AgentSuite) SetUpSuite(c *gc.C) {
    61  	s.JujuConnSuite.SetUpSuite(c)
    62  
    63  	s.PatchValue(&cmdutil.EnsureMongoServer, func(mongo.EnsureServerParams) error {
    64  		return nil
    65  	})
    66  }
    67  
    68  func (s *AgentSuite) SetUpTest(c *gc.C) {
    69  	s.JujuConnSuite.SetUpTest(c)
    70  	// Set API host ports so FindTools/Tools API calls succeed.
    71  	hostPorts := [][]network.HostPort{
    72  		network.NewHostPorts(1234, "0.1.2.3"),
    73  	}
    74  	err := s.State.SetAPIHostPorts(hostPorts)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	s.PatchValue(&proxyupdater.NewWorker, func(proxyupdater.Config) (worker.Worker, error) {
    77  		return newDummyWorker(), nil
    78  	})
    79  
    80  	// Tests should not try to use internet. Ensure base url is empty.
    81  	imagetesting.PatchOfficialDataSources(&s.CleanupSuite, "")
    82  }