github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/storageprovisioner/manifold_machine_test.go (about)

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