github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/toolsversionchecker/manifold_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package toolsversionchecker_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/agent"
    13  	apitesting "github.com/juju/juju/api/base/testing"
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/cmd/jujud/agent/engine/enginetest"
    16  	"github.com/juju/juju/state/multiwatcher"
    17  	"github.com/juju/juju/worker"
    18  	"github.com/juju/juju/worker/dependency"
    19  	"github.com/juju/juju/worker/toolsversionchecker"
    20  )
    21  
    22  type ManifoldSuite struct {
    23  	testing.IsolationSuite
    24  	newCalled bool
    25  }
    26  
    27  var _ = gc.Suite(&ManifoldSuite{})
    28  
    29  func (s *ManifoldSuite) SetUpTest(c *gc.C) {
    30  	s.newCalled = false
    31  	s.PatchValue(&toolsversionchecker.New,
    32  		func(api toolsversionchecker.Facade, params *toolsversionchecker.VersionCheckerParams) worker.Worker {
    33  			s.newCalled = true
    34  			return nil
    35  		},
    36  	)
    37  }
    38  
    39  func (s *ManifoldSuite) TestMachine(c *gc.C) {
    40  	config := toolsversionchecker.ManifoldConfig(enginetest.AgentAPIManifoldTestConfig())
    41  	_, err := enginetest.RunAgentAPIManifold(
    42  		toolsversionchecker.Manifold(config),
    43  		&fakeAgent{tag: names.NewMachineTag("42")},
    44  		mockAPICaller(multiwatcher.JobManageModel))
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	c.Assert(s.newCalled, jc.IsTrue)
    47  }
    48  
    49  func (s *ManifoldSuite) TestMachineNotModelManagerErrors(c *gc.C) {
    50  	config := toolsversionchecker.ManifoldConfig(enginetest.AgentAPIManifoldTestConfig())
    51  	_, err := enginetest.RunAgentAPIManifold(
    52  		toolsversionchecker.Manifold(config),
    53  		&fakeAgent{tag: names.NewMachineTag("42")},
    54  		mockAPICaller(multiwatcher.JobHostUnits))
    55  	c.Assert(err, gc.Equals, dependency.ErrMissing)
    56  	c.Assert(s.newCalled, jc.IsFalse)
    57  }
    58  
    59  func (s *ManifoldSuite) TestNonMachineAgent(c *gc.C) {
    60  	config := toolsversionchecker.ManifoldConfig(enginetest.AgentAPIManifoldTestConfig())
    61  	_, err := enginetest.RunAgentAPIManifold(
    62  		toolsversionchecker.Manifold(config),
    63  		&fakeAgent{tag: names.NewUnitTag("foo/0")},
    64  		mockAPICaller(""))
    65  	c.Assert(err, gc.ErrorMatches, "this manifold may only be used inside a machine agent")
    66  	c.Assert(s.newCalled, jc.IsFalse)
    67  }
    68  
    69  type fakeAgent struct {
    70  	agent.Agent
    71  	tag names.Tag
    72  }
    73  
    74  func (a *fakeAgent) CurrentConfig() agent.Config {
    75  	return &fakeConfig{tag: a.tag}
    76  }
    77  
    78  type fakeConfig struct {
    79  	agent.Config
    80  	tag names.Tag
    81  }
    82  
    83  func (c *fakeConfig) Tag() names.Tag {
    84  	return c.tag
    85  }
    86  
    87  func mockAPICaller(job multiwatcher.MachineJob) apitesting.APICallerFunc {
    88  	return apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    89  		if res, ok := result.(*params.AgentGetEntitiesResults); ok {
    90  			res.Entities = []params.AgentGetEntitiesResult{
    91  				{Jobs: []multiwatcher.MachineJob{
    92  					job,
    93  				}}}
    94  		}
    95  		return nil
    96  	})
    97  }