github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/cleaner/cleaner_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cleaner_test
     5  
     6  import (
     7  	"errors"
     8  	"time"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/api/base"
    14  	apitesting "github.com/juju/juju/api/base/testing"
    15  	"github.com/juju/juju/api/cleaner"
    16  	"github.com/juju/juju/apiserver/params"
    17  	coretesting "github.com/juju/juju/testing"
    18  )
    19  
    20  type CleanerSuite struct {
    21  	coretesting.BaseSuite
    22  }
    23  
    24  var _ = gc.Suite(&CleanerSuite{})
    25  
    26  type TestCommon struct {
    27  	apiCaller base.APICaller
    28  	called    chan struct{}
    29  	api       *cleaner.API
    30  }
    31  
    32  // Init returns a new, initialised instance of TestCommon.
    33  func Init(c *gc.C, facade, method string, expectArgs, useResults interface{}, err error) (t *TestCommon) {
    34  	t = &TestCommon{}
    35  	caller := apitesting.APICallChecker(c, apitesting.APICall{
    36  		Facade:        facade,
    37  		VersionIsZero: true,
    38  		IdIsEmpty:     true,
    39  		Method:        method,
    40  		Args:          expectArgs,
    41  		Results:       useResults,
    42  		Error:         err,
    43  	})
    44  	t.called = make(chan struct{}, 100)
    45  	t.apiCaller = apitesting.NotifyingAPICaller(c, t.called, caller)
    46  	t.api = cleaner.NewAPI(t.apiCaller)
    47  
    48  	c.Check(t.api, gc.NotNil)
    49  	return
    50  }
    51  
    52  // AssertNumReceives checks that the watched channel receives "expected" messages
    53  // within a LongWait, but returns as soon as possible.
    54  func AssertNumReceives(c *gc.C, watched chan struct{}, expected uint32) {
    55  	var receives uint32
    56  
    57  	for receives < expected {
    58  		select {
    59  		case <-watched:
    60  			receives++
    61  		case <-time.After(coretesting.LongWait):
    62  			c.Errorf("timeout while waiting for a call")
    63  		}
    64  	}
    65  	select {
    66  	case <-watched:
    67  		c.Fatalf("unexpected event received")
    68  	case <-time.After(coretesting.ShortWait):
    69  	}
    70  }
    71  
    72  func (s *CleanerSuite) TestNewAPI(c *gc.C) {
    73  	Init(c, "Cleaner", "", nil, nil, nil)
    74  }
    75  
    76  func (s *CleanerSuite) TestWatchCleanups(c *gc.C) {
    77  	// Multiple facades are called, so pass an empty string for the facade.
    78  	t := Init(c, "", "", nil, nil, nil)
    79  	m, err := t.api.WatchCleanups()
    80  	AssertNumReceives(c, t.called, 2)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	c.Assert(m, gc.NotNil)
    83  }
    84  
    85  func (s *CleanerSuite) TestCleanup(c *gc.C) {
    86  	t := Init(c, "Cleaner", "Cleanup", nil, nil, nil)
    87  	err := t.api.Cleanup()
    88  	AssertNumReceives(c, t.called, 1)
    89  	c.Assert(err, jc.ErrorIsNil)
    90  }
    91  
    92  func (s *CleanerSuite) TestWatchCleanupsFailFacadeCall(c *gc.C) {
    93  	t := Init(c, "Cleaner", "WatchCleanups", nil, nil, errors.New("client error!"))
    94  	m, err := t.api.WatchCleanups()
    95  	c.Assert(err, gc.ErrorMatches, "client error!")
    96  	AssertNumReceives(c, t.called, 1)
    97  	c.Assert(m, gc.IsNil)
    98  }
    99  
   100  func (s *CleanerSuite) TestWatchCleanupsFailFacadeResult(c *gc.C) {
   101  	e := params.Error{
   102  		Message: "Server Error",
   103  	}
   104  	p := params.NotifyWatchResult{
   105  		Error: &e,
   106  	}
   107  	t := Init(c, "Cleaner", "WatchCleanups", nil, p, nil)
   108  	m, err := t.api.WatchCleanups()
   109  	AssertNumReceives(c, t.called, 1)
   110  	c.Assert(err, gc.ErrorMatches, e.Message)
   111  	c.Assert(m, gc.IsNil)
   112  }