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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package centralhub_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/pubsub"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/worker.v1/dependency"
    13  	dt "gopkg.in/juju/worker.v1/dependency/testing"
    14  	"gopkg.in/juju/worker.v1/workertest"
    15  
    16  	"github.com/juju/juju/worker/centralhub"
    17  )
    18  
    19  type ManifoldSuite struct {
    20  	testing.IsolationSuite
    21  	hub    *pubsub.StructuredHub
    22  	config centralhub.ManifoldConfig
    23  }
    24  
    25  var _ = gc.Suite(&ManifoldSuite{})
    26  
    27  func (s *ManifoldSuite) SetUpTest(c *gc.C) {
    28  	s.IsolationSuite.SetUpTest(c)
    29  	s.hub = pubsub.NewStructuredHub(nil)
    30  	s.config = centralhub.ManifoldConfig{
    31  		StateConfigWatcherName: "state-config-watcher",
    32  		Hub:                    s.hub,
    33  	}
    34  }
    35  
    36  func (s *ManifoldSuite) manifold() dependency.Manifold {
    37  	return centralhub.Manifold(s.config)
    38  }
    39  
    40  func (s *ManifoldSuite) TestInputs(c *gc.C) {
    41  	c.Check(s.manifold().Inputs, jc.DeepEquals, []string{"state-config-watcher"})
    42  }
    43  
    44  func (s *ManifoldSuite) TestStateConfigWatcherMissing(c *gc.C) {
    45  	context := dt.StubContext(nil, map[string]interface{}{
    46  		"state-config-watcher": dependency.ErrMissing,
    47  	})
    48  
    49  	worker, err := s.manifold().Start(context)
    50  	c.Check(worker, gc.IsNil)
    51  	c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing)
    52  }
    53  
    54  func (s *ManifoldSuite) TestStateConfigWatcherNotAStateServer(c *gc.C) {
    55  	context := dt.StubContext(nil, map[string]interface{}{
    56  		"state-config-watcher": false,
    57  	})
    58  
    59  	worker, err := s.manifold().Start(context)
    60  	c.Check(worker, gc.IsNil)
    61  	c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing)
    62  }
    63  
    64  func (s *ManifoldSuite) TestMissingHub(c *gc.C) {
    65  	s.config.Hub = nil
    66  	context := dt.StubContext(nil, map[string]interface{}{
    67  		"state-config-watcher": true,
    68  	})
    69  
    70  	worker, err := s.manifold().Start(context)
    71  	c.Check(worker, gc.IsNil)
    72  	c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
    73  }
    74  
    75  func (s *ManifoldSuite) TestHubOutput(c *gc.C) {
    76  	context := dt.StubContext(nil, map[string]interface{}{
    77  		"state-config-watcher": true,
    78  	})
    79  
    80  	manifold := s.manifold()
    81  	worker, err := manifold.Start(context)
    82  	c.Check(err, jc.ErrorIsNil)
    83  	c.Assert(worker, gc.NotNil)
    84  	defer workertest.CleanKill(c, worker)
    85  
    86  	var hub *pubsub.StructuredHub
    87  	err = manifold.Output(worker, &hub)
    88  	c.Check(err, jc.ErrorIsNil)
    89  	c.Check(hub, gc.Equals, s.hub)
    90  }