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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machineundertaker_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    12  	"gopkg.in/juju/worker.v1"
    13  	"gopkg.in/juju/worker.v1/dependency"
    14  	dt "gopkg.in/juju/worker.v1/dependency/testing"
    15  
    16  	"github.com/juju/juju/api/base"
    17  	apitesting "github.com/juju/juju/api/base/testing"
    18  	"github.com/juju/juju/environs"
    19  	"github.com/juju/juju/worker/common"
    20  	"github.com/juju/juju/worker/machineundertaker"
    21  )
    22  
    23  type manifoldSuite struct {
    24  	testing.IsolationSuite
    25  }
    26  
    27  var _ = gc.Suite(&manifoldSuite{})
    28  
    29  func (*manifoldSuite) TestMissingCaller(c *gc.C) {
    30  	manifold := makeManifold(nil, nil)
    31  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    32  		"the-caller":  dependency.ErrMissing,
    33  		"the-environ": &fakeEnviron{},
    34  	}))
    35  	c.Assert(result, gc.IsNil)
    36  	c.Assert(errors.Cause(err), gc.Equals, dependency.ErrMissing)
    37  }
    38  
    39  func (*manifoldSuite) TestMissingEnviron(c *gc.C) {
    40  	manifold := makeManifold(nil, nil)
    41  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    42  		"the-caller":  &fakeAPICaller{},
    43  		"the-environ": dependency.ErrMissing,
    44  	}))
    45  	c.Assert(result, gc.IsNil)
    46  	c.Assert(errors.Cause(err), gc.Equals, dependency.ErrMissing)
    47  }
    48  
    49  func (*manifoldSuite) TestAPIError(c *gc.C) {
    50  	manifold := makeManifold(nil, nil)
    51  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    52  		"the-caller":  &fakeAPICaller{},
    53  		"the-environ": &fakeEnviron{},
    54  	}))
    55  	c.Assert(result, gc.IsNil)
    56  	c.Assert(err, gc.ErrorMatches, "machine undertaker client requires a model API connection")
    57  }
    58  
    59  func (*manifoldSuite) TestWorkerError(c *gc.C) {
    60  	manifold := makeManifold(nil, errors.New("boglodite"))
    61  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    62  		"the-caller":  apitesting.APICallerFunc(nil),
    63  		"the-environ": &fakeEnviron{},
    64  	}))
    65  	c.Assert(result, gc.IsNil)
    66  	c.Assert(err, gc.ErrorMatches, "boglodite")
    67  }
    68  
    69  func (*manifoldSuite) TestSuccess(c *gc.C) {
    70  	w := fakeWorker{name: "Boris"}
    71  	manifold := makeManifold(&w, nil)
    72  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    73  		"the-caller":  apitesting.APICallerFunc(nil),
    74  		"the-environ": &fakeEnviron{},
    75  	}))
    76  	c.Assert(err, jc.ErrorIsNil)
    77  	c.Assert(result, gc.DeepEquals, &w)
    78  }
    79  
    80  func makeManifold(workerResult worker.Worker, workerError error) dependency.Manifold {
    81  	return machineundertaker.Manifold(machineundertaker.ManifoldConfig{
    82  		APICallerName: "the-caller",
    83  		EnvironName:   "the-environ",
    84  		NewWorker: func(machineundertaker.Facade, environs.Environ, common.CredentialAPI) (worker.Worker, error) {
    85  			return workerResult, workerError
    86  		},
    87  		NewCredentialValidatorFacade: func(base.APICaller) (common.CredentialAPI, error) {
    88  			return &fakeCredentialAPI{}, nil
    89  		},
    90  	})
    91  }
    92  
    93  type fakeAPICaller struct {
    94  	base.APICaller
    95  }
    96  
    97  func (c *fakeAPICaller) ModelTag() (names.ModelTag, bool) {
    98  	return names.ModelTag{}, false
    99  }
   100  
   101  type fakeWorker struct {
   102  	worker.Worker
   103  	name string
   104  }