github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/resumer/config_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resumer_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/worker/resumer"
    15  	"github.com/juju/juju/worker/workertest"
    16  )
    17  
    18  type ConfigSuite struct {
    19  	testing.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&ConfigSuite{})
    23  
    24  func (*ConfigSuite) TestValid(c *gc.C) {
    25  	config := validConfig()
    26  	err := config.Validate()
    27  	c.Check(err, jc.ErrorIsNil)
    28  }
    29  
    30  func (*ConfigSuite) TestNilFacade(c *gc.C) {
    31  	config := validConfig()
    32  	config.Facade = nil
    33  	checkInvalid(c, config, "nil Facade not valid")
    34  }
    35  
    36  func (*ConfigSuite) TestNilClock(c *gc.C) {
    37  	config := validConfig()
    38  	config.Clock = nil
    39  	checkInvalid(c, config, "nil Clock not valid")
    40  }
    41  
    42  func (*ConfigSuite) TestZeroInterval(c *gc.C) {
    43  	config := validConfig()
    44  	config.Interval = 0
    45  	checkInvalid(c, config, "non-positive Interval not valid")
    46  }
    47  
    48  func (*ConfigSuite) TestNegativeInterval(c *gc.C) {
    49  	config := validConfig()
    50  	config.Interval = -time.Minute
    51  	checkInvalid(c, config, "non-positive Interval not valid")
    52  }
    53  
    54  func validConfig() resumer.Config {
    55  	return resumer.Config{
    56  		Facade:   &fakeFacade{},
    57  		Clock:    &fakeClock{},
    58  		Interval: time.Minute,
    59  	}
    60  }
    61  
    62  func checkInvalid(c *gc.C, config resumer.Config, match string) {
    63  	check := func(err error) {
    64  		c.Check(err, jc.Satisfies, errors.IsNotValid)
    65  		c.Check(err, gc.ErrorMatches, match)
    66  	}
    67  	check(config.Validate())
    68  
    69  	worker, err := resumer.NewResumer(config)
    70  	workertest.CheckNilOrKill(c, worker)
    71  	check(err)
    72  }