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

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasunitprovisioner_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/worker.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/api/base"
    17  	"github.com/juju/juju/worker/caasunitprovisioner"
    18  )
    19  
    20  type ManifoldSuite struct {
    21  	testing.IsolationSuite
    22  	testing.Stub
    23  	manifold dependency.Manifold
    24  	context  dependency.Context
    25  
    26  	apiCaller fakeAPICaller
    27  	broker    fakeBroker
    28  	client    fakeClient
    29  }
    30  
    31  var _ = gc.Suite(&ManifoldSuite{})
    32  
    33  func (s *ManifoldSuite) SetUpTest(c *gc.C) {
    34  	s.IsolationSuite.SetUpTest(c)
    35  	s.ResetCalls()
    36  
    37  	s.context = s.newContext(nil)
    38  	s.manifold = caasunitprovisioner.Manifold(s.validConfig())
    39  }
    40  
    41  func (s *ManifoldSuite) validConfig() caasunitprovisioner.ManifoldConfig {
    42  	return caasunitprovisioner.ManifoldConfig{
    43  		APICallerName: "api-caller",
    44  		BrokerName:    "broker",
    45  		NewClient:     s.newClient,
    46  		NewWorker:     s.newWorker,
    47  	}
    48  }
    49  
    50  func (s *ManifoldSuite) newClient(apiCaller base.APICaller) caasunitprovisioner.Client {
    51  	s.MethodCall(s, "NewClient", apiCaller)
    52  	return &s.client
    53  }
    54  
    55  func (s *ManifoldSuite) newWorker(config caasunitprovisioner.Config) (worker.Worker, error) {
    56  	s.MethodCall(s, "NewWorker", config)
    57  	if err := s.NextErr(); err != nil {
    58  		return nil, err
    59  	}
    60  	w := worker.NewRunner(worker.RunnerParams{})
    61  	s.AddCleanup(func(c *gc.C) { workertest.DirtyKill(c, w) })
    62  	return w, nil
    63  }
    64  
    65  func (s *ManifoldSuite) newContext(overlay map[string]interface{}) dependency.Context {
    66  	resources := map[string]interface{}{
    67  		"api-caller": &s.apiCaller,
    68  		"broker":     &s.broker,
    69  	}
    70  	for k, v := range overlay {
    71  		resources[k] = v
    72  	}
    73  	return dt.StubContext(nil, resources)
    74  }
    75  
    76  func (s *ManifoldSuite) TestMissingAPICallerName(c *gc.C) {
    77  	config := s.validConfig()
    78  	config.APICallerName = ""
    79  	s.checkConfigInvalid(c, config, "empty APICallerName not valid")
    80  }
    81  
    82  func (s *ManifoldSuite) TestMissingBrokerName(c *gc.C) {
    83  	config := s.validConfig()
    84  	config.BrokerName = ""
    85  	s.checkConfigInvalid(c, config, "empty BrokerName not valid")
    86  }
    87  
    88  func (s *ManifoldSuite) TestMissingNewWorker(c *gc.C) {
    89  	config := s.validConfig()
    90  	config.NewWorker = nil
    91  	s.checkConfigInvalid(c, config, "nil NewWorker not valid")
    92  }
    93  
    94  func (s *ManifoldSuite) checkConfigInvalid(c *gc.C, config caasunitprovisioner.ManifoldConfig, expect string) {
    95  	err := config.Validate()
    96  	c.Check(err, gc.ErrorMatches, expect)
    97  	c.Check(err, jc.Satisfies, errors.IsNotValid)
    98  }
    99  
   100  var expectedInputs = []string{"api-caller", "broker"}
   101  
   102  func (s *ManifoldSuite) TestInputs(c *gc.C) {
   103  	c.Assert(s.manifold.Inputs, jc.SameContents, expectedInputs)
   104  }
   105  
   106  func (s *ManifoldSuite) TestMissingInputs(c *gc.C) {
   107  	for _, input := range expectedInputs {
   108  		context := s.newContext(map[string]interface{}{
   109  			input: dependency.ErrMissing,
   110  		})
   111  		_, err := s.manifold.Start(context)
   112  		c.Assert(errors.Cause(err), gc.Equals, dependency.ErrMissing)
   113  	}
   114  }
   115  
   116  func (s *ManifoldSuite) TestStart(c *gc.C) {
   117  	w, err := s.manifold.Start(s.context)
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	workertest.CleanKill(c, w)
   120  
   121  	s.CheckCallNames(c, "NewClient", "NewWorker")
   122  	s.CheckCall(c, 0, "NewClient", &s.apiCaller)
   123  
   124  	args := s.Calls()[1].Args
   125  	c.Assert(args, gc.HasLen, 1)
   126  	c.Assert(args[0], gc.FitsTypeOf, caasunitprovisioner.Config{})
   127  	config := args[0].(caasunitprovisioner.Config)
   128  
   129  	c.Assert(config, jc.DeepEquals, caasunitprovisioner.Config{
   130  		ApplicationGetter:        &s.client,
   131  		ApplicationUpdater:       &s.client,
   132  		ServiceBroker:            &s.broker,
   133  		ContainerBroker:          &s.broker,
   134  		ProvisioningInfoGetter:   &s.client,
   135  		ProvisioningStatusSetter: &s.client,
   136  		LifeGetter:               &s.client,
   137  		UnitUpdater:              &s.client,
   138  	})
   139  }