github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/resumer/manifold_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resumer_test
     5  
     6  import (
     7  	"github.com/juju/names"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/agent"
    13  	"github.com/juju/juju/api"
    14  	apiagent "github.com/juju/juju/api/agent"
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/state/multiwatcher"
    17  	"github.com/juju/juju/worker"
    18  	"github.com/juju/juju/worker/dependency"
    19  	resumer "github.com/juju/juju/worker/resumer"
    20  	workertesting "github.com/juju/juju/worker/testing"
    21  )
    22  
    23  type ManifoldSuite struct {
    24  	testing.IsolationSuite
    25  	newCalled bool
    26  }
    27  
    28  var _ = gc.Suite(&ManifoldSuite{})
    29  
    30  func (s *ManifoldSuite) SetUpTest(c *gc.C) {
    31  	s.newCalled = false
    32  	s.PatchValue(&resumer.NewResumer,
    33  		func(tr resumer.TransactionResumer) worker.Worker {
    34  			s.newCalled = true
    35  			return nil
    36  		},
    37  	)
    38  }
    39  
    40  func (s *ManifoldSuite) TestMachine(c *gc.C) {
    41  	config := resumer.ManifoldConfig(workertesting.AgentApiManifoldTestConfig())
    42  	_, err := workertesting.RunAgentApiManifold(
    43  		resumer.Manifold(config),
    44  		&fakeAgent{tag: names.NewMachineTag("42")},
    45  		&fakeAPIConn{machineJob: multiwatcher.JobManageModel})
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	c.Assert(s.newCalled, jc.IsTrue)
    48  }
    49  
    50  func (s *ManifoldSuite) TestMachineNonManagerErrors(c *gc.C) {
    51  	config := resumer.ManifoldConfig(workertesting.AgentApiManifoldTestConfig())
    52  	_, err := workertesting.RunAgentApiManifold(
    53  		resumer.Manifold(config),
    54  		&fakeAgent{tag: names.NewMachineTag("42")},
    55  		&fakeAPIConn{machineJob: multiwatcher.JobHostUnits})
    56  	c.Assert(err, gc.Equals, dependency.ErrMissing)
    57  	c.Assert(s.newCalled, jc.IsFalse)
    58  }
    59  
    60  func (s *ManifoldSuite) TestUnitErrors(c *gc.C) {
    61  	config := resumer.ManifoldConfig(workertesting.AgentApiManifoldTestConfig())
    62  	_, err := workertesting.RunAgentApiManifold(
    63  		resumer.Manifold(config),
    64  		&fakeAgent{tag: names.NewUnitTag("foo/0")},
    65  		&fakeAPIConn{})
    66  	c.Assert(err, gc.ErrorMatches, "this manifold may only be used inside a machine agent")
    67  	c.Assert(s.newCalled, jc.IsFalse)
    68  }
    69  
    70  func (s *ManifoldSuite) TestNonAgentErrors(c *gc.C) {
    71  	config := resumer.ManifoldConfig(workertesting.AgentApiManifoldTestConfig())
    72  	_, err := workertesting.RunAgentApiManifold(
    73  		resumer.Manifold(config),
    74  		&fakeAgent{tag: names.NewUserTag("foo")},
    75  		&fakeAPIConn{})
    76  	c.Assert(err, gc.ErrorMatches, "this manifold may only be used inside a machine agent")
    77  	c.Assert(s.newCalled, jc.IsFalse)
    78  }
    79  
    80  type fakeAgent struct {
    81  	agent.Agent
    82  	tag names.Tag
    83  }
    84  
    85  func (a *fakeAgent) CurrentConfig() agent.Config {
    86  	return &fakeConfig{tag: a.tag}
    87  }
    88  
    89  type fakeConfig struct {
    90  	agent.Config
    91  	tag names.Tag
    92  }
    93  
    94  func (c *fakeConfig) Tag() names.Tag {
    95  	return c.tag
    96  }
    97  
    98  type fakeAPIConn struct {
    99  	api.Connection
   100  	machineJob multiwatcher.MachineJob
   101  }
   102  
   103  func (f *fakeAPIConn) APICall(objType string, version int, id, request string, args interface{}, response interface{}) error {
   104  	if res, ok := response.(*params.AgentGetEntitiesResults); ok {
   105  		res.Entities = []params.AgentGetEntitiesResult{
   106  			{Jobs: []multiwatcher.MachineJob{f.machineJob}},
   107  		}
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  func (*fakeAPIConn) BestFacadeVersion(facade string) int {
   114  	return 42
   115  }
   116  
   117  func (f *fakeAPIConn) Agent() *apiagent.State {
   118  	return apiagent.NewState(f)
   119  }