github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  
    13  	"github.com/juju/juju/api/base"
    14  	apitesting "github.com/juju/juju/api/base/testing"
    15  	"github.com/juju/juju/environs"
    16  	"github.com/juju/juju/worker"
    17  	"github.com/juju/juju/worker/dependency"
    18  	dt "github.com/juju/juju/worker/dependency/testing"
    19  	"github.com/juju/juju/worker/machineundertaker"
    20  )
    21  
    22  type manifoldSuite struct {
    23  	testing.IsolationSuite
    24  }
    25  
    26  var _ = gc.Suite(&manifoldSuite{})
    27  
    28  func (*manifoldSuite) TestMissingCaller(c *gc.C) {
    29  	manifold := makeManifold(nil, nil)
    30  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    31  		"the-caller":  dependency.ErrMissing,
    32  		"the-environ": &fakeEnviron{},
    33  	}))
    34  	c.Assert(result, gc.IsNil)
    35  	c.Assert(errors.Cause(err), gc.Equals, dependency.ErrMissing)
    36  }
    37  
    38  func (*manifoldSuite) TestMissingEnviron(c *gc.C) {
    39  	manifold := makeManifold(nil, nil)
    40  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    41  		"the-caller":  &fakeAPICaller{},
    42  		"the-environ": dependency.ErrMissing,
    43  	}))
    44  	c.Assert(result, gc.IsNil)
    45  	c.Assert(errors.Cause(err), gc.Equals, dependency.ErrMissing)
    46  }
    47  
    48  func (*manifoldSuite) TestAPIError(c *gc.C) {
    49  	manifold := makeManifold(nil, nil)
    50  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    51  		"the-caller":  &fakeAPICaller{},
    52  		"the-environ": &fakeEnviron{},
    53  	}))
    54  	c.Assert(result, gc.IsNil)
    55  	c.Assert(err, gc.ErrorMatches, "machine undertaker client requires a model API connection")
    56  }
    57  
    58  func (*manifoldSuite) TestWorkerError(c *gc.C) {
    59  	manifold := makeManifold(nil, errors.New("boglodite"))
    60  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    61  		"the-caller":  apitesting.APICallerFunc(nil),
    62  		"the-environ": &fakeEnviron{},
    63  	}))
    64  	c.Assert(result, gc.IsNil)
    65  	c.Assert(err, gc.ErrorMatches, "boglodite")
    66  }
    67  
    68  func (*manifoldSuite) TestSuccess(c *gc.C) {
    69  	w := fakeWorker{name: "Boris"}
    70  	manifold := makeManifold(&w, nil)
    71  	result, err := manifold.Start(dt.StubContext(nil, map[string]interface{}{
    72  		"the-caller":  apitesting.APICallerFunc(nil),
    73  		"the-environ": &fakeEnviron{},
    74  	}))
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(result, gc.DeepEquals, &w)
    77  }
    78  
    79  func makeManifold(workerResult worker.Worker, workerError error) dependency.Manifold {
    80  	return machineundertaker.Manifold(machineundertaker.ManifoldConfig{
    81  		APICallerName: "the-caller",
    82  		EnvironName:   "the-environ",
    83  		NewWorker: func(machineundertaker.Facade, environs.Environ) (worker.Worker, error) {
    84  			return workerResult, workerError
    85  		},
    86  	})
    87  }
    88  
    89  type fakeAPICaller struct {
    90  	base.APICaller
    91  }
    92  
    93  func (c *fakeAPICaller) ModelTag() (names.ModelTag, bool) {
    94  	return names.ModelTag{}, false
    95  }
    96  
    97  type fakeWorker struct {
    98  	worker.Worker
    99  	name string
   100  }