github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"github.com/juju/cmd/cmdtesting"
     9  	"github.com/juju/loggo"
    10  	"github.com/juju/os/series"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	"gopkg.in/juju/worker.v1"
    15  
    16  	"github.com/juju/juju/agent"
    17  	"github.com/juju/juju/cmd/jujud/agent/agenttest"
    18  	imagetesting "github.com/juju/juju/environs/imagemetadata/testing"
    19  	"github.com/juju/juju/juju/paths"
    20  	"github.com/juju/juju/network"
    21  	"github.com/juju/juju/worker/proxyupdater"
    22  )
    23  
    24  type acCreator func() (cmd.Command, AgentConf)
    25  
    26  // CheckAgentCommand is a utility function for verifying that common agent
    27  // options are handled by a Command; it returns an instance of that
    28  // command pre-parsed, with any mandatory flags added.
    29  func CheckAgentCommand(c *gc.C, create acCreator, args []string) cmd.Command {
    30  	com, conf := create()
    31  	err := cmdtesting.InitCommand(com, args)
    32  	dataDir, err := paths.DataDir(series.MustHostSeries())
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	c.Assert(conf.DataDir(), gc.Equals, dataDir)
    35  	badArgs := append(args, "--data-dir", "")
    36  	com, _ = create()
    37  	err = cmdtesting.InitCommand(com, badArgs)
    38  	c.Assert(err, gc.ErrorMatches, "--data-dir option must be set")
    39  
    40  	args = append(args, "--data-dir", "jd")
    41  	com, conf = create()
    42  	c.Assert(cmdtesting.InitCommand(com, args), gc.IsNil)
    43  	c.Assert(conf.DataDir(), gc.Equals, "jd")
    44  	return com
    45  }
    46  
    47  // ParseAgentCommand is a utility function that inserts the always-required args
    48  // before parsing an agent command and returning the result.
    49  func ParseAgentCommand(ac cmd.Command, args []string) error {
    50  	common := []string{
    51  		"--data-dir", "jd",
    52  	}
    53  	return cmdtesting.InitCommand(ac, append(common, args...))
    54  }
    55  
    56  // AgentSuite is a fixture to be used by agent test suites.
    57  type AgentSuite struct {
    58  	agenttest.AgentSuite
    59  }
    60  
    61  func (s *AgentSuite) SetUpSuite(c *gc.C) {
    62  	s.JujuConnSuite.SetUpSuite(c)
    63  
    64  	agenttest.InstallFakeEnsureMongo(s)
    65  }
    66  
    67  func (s *AgentSuite) SetUpTest(c *gc.C) {
    68  	s.JujuConnSuite.SetUpTest(c)
    69  	// Set API host ports so FindTools/Tools API calls succeed.
    70  	hostPorts := [][]network.HostPort{
    71  		network.NewHostPorts(1234, "0.1.2.3"),
    72  	}
    73  	err := s.State.SetAPIHostPorts(hostPorts)
    74  	c.Assert(err, jc.ErrorIsNil)
    75  	s.PatchValue(&proxyupdater.NewWorker, func(proxyupdater.Config) (worker.Worker, error) {
    76  		return newDummyWorker(), nil
    77  	})
    78  
    79  	// Tests should not try to use internet. Ensure base url is empty.
    80  	imagetesting.PatchOfficialDataSources(&s.CleanupSuite, "")
    81  }
    82  
    83  type agentLoggingSuite struct {
    84  	testing.IsolationSuite
    85  }
    86  
    87  var _ = gc.Suite(&agentLoggingSuite{})
    88  
    89  func (*agentLoggingSuite) TestNoLoggingConfig(c *gc.C) {
    90  	f := &fakeLoggingConfig{}
    91  	initial := loggo.LoggerInfo()
    92  
    93  	setupAgentLogging(f)
    94  
    95  	c.Assert(loggo.LoggerInfo(), gc.Equals, initial)
    96  }
    97  
    98  func (*agentLoggingSuite) TestLoggingOverride(c *gc.C) {
    99  	f := &fakeLoggingConfig{
   100  		loggingOverride: "test=INFO",
   101  	}
   102  
   103  	setupAgentLogging(f)
   104  
   105  	c.Assert(loggo.LoggerInfo(), gc.Equals, "<root>=WARNING;test=INFO")
   106  }
   107  
   108  func (*agentLoggingSuite) TestLoggingConfig(c *gc.C) {
   109  	f := &fakeLoggingConfig{
   110  		loggingConfig: "test=INFO",
   111  	}
   112  
   113  	setupAgentLogging(f)
   114  
   115  	c.Assert(loggo.LoggerInfo(), gc.Equals, "<root>=WARNING;test=INFO")
   116  }
   117  
   118  type fakeLoggingConfig struct {
   119  	agent.Config
   120  
   121  	loggingConfig   string
   122  	loggingOverride string
   123  }
   124  
   125  func (f *fakeLoggingConfig) LoggingConfig() string {
   126  	return f.loggingConfig
   127  }
   128  
   129  func (f *fakeLoggingConfig) Value(key string) string {
   130  	if key == agent.LoggingOverride {
   131  		return f.loggingOverride
   132  	}
   133  	return ""
   134  }