github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/lifeflag/facade_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lifeflag_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/api/lifeflag"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/core/life"
    18  	"github.com/juju/juju/watcher"
    19  )
    20  
    21  type FacadeSuite struct {
    22  	testing.IsolationSuite
    23  }
    24  
    25  var _ = gc.Suite(&FacadeSuite{})
    26  
    27  func (*FacadeSuite) TestLifeCall(c *gc.C) {
    28  	var called bool
    29  	caller := apiCaller(c, func(request string, args, _ interface{}) error {
    30  		called = true
    31  		c.Check(request, gc.Equals, "Life")
    32  		c.Check(args, jc.DeepEquals, params.Entities{
    33  			Entities: []params.Entity{{Tag: "application-blah"}},
    34  		})
    35  		return nil
    36  	})
    37  	facade := lifeflag.NewFacade(caller, nil)
    38  
    39  	facade.Life(names.NewApplicationTag("blah"))
    40  	c.Check(called, jc.IsTrue)
    41  }
    42  
    43  func (*FacadeSuite) TestLifeCallError(c *gc.C) {
    44  	caller := apiCaller(c, func(_ string, _, _ interface{}) error {
    45  		return errors.New("crunch belch")
    46  	})
    47  	facade := lifeflag.NewFacade(caller, nil)
    48  
    49  	result, err := facade.Life(names.NewApplicationTag("blah"))
    50  	c.Check(err, gc.ErrorMatches, "crunch belch")
    51  	c.Check(result, gc.Equals, life.Value(""))
    52  }
    53  
    54  func (*FacadeSuite) TestLifeNoResultsError(c *gc.C) {
    55  	caller := apiCaller(c, func(_ string, _, _ interface{}) error {
    56  		return nil
    57  	})
    58  	facade := lifeflag.NewFacade(caller, nil)
    59  
    60  	result, err := facade.Life(names.NewApplicationTag("blah"))
    61  	c.Check(err, gc.ErrorMatches, "expected 1 Life result, got 0")
    62  	c.Check(result, gc.Equals, life.Value(""))
    63  }
    64  
    65  func (*FacadeSuite) TestLifeExtraResultsError(c *gc.C) {
    66  	caller := apiCaller(c, func(_ string, _, results interface{}) error {
    67  		typed, ok := results.(*params.LifeResults)
    68  		c.Assert(ok, jc.IsTrue)
    69  		*typed = params.LifeResults{
    70  			Results: make([]params.LifeResult, 2),
    71  		}
    72  		return nil
    73  	})
    74  	facade := lifeflag.NewFacade(caller, nil)
    75  
    76  	result, err := facade.Life(names.NewApplicationTag("blah"))
    77  	c.Check(err, gc.ErrorMatches, "expected 1 Life result, got 2")
    78  	c.Check(result, gc.Equals, life.Value(""))
    79  }
    80  
    81  func (*FacadeSuite) TestLifeNotFoundError(c *gc.C) {
    82  	caller := apiCaller(c, func(_ string, _, results interface{}) error {
    83  		typed, ok := results.(*params.LifeResults)
    84  		c.Assert(ok, jc.IsTrue)
    85  		*typed = params.LifeResults{
    86  			Results: []params.LifeResult{{
    87  				Error: &params.Error{Code: params.CodeNotFound},
    88  			}},
    89  		}
    90  		return nil
    91  	})
    92  	facade := lifeflag.NewFacade(caller, nil)
    93  
    94  	result, err := facade.Life(names.NewApplicationTag("blah"))
    95  	c.Check(err, gc.Equals, lifeflag.ErrNotFound)
    96  	c.Check(result, gc.Equals, life.Value(""))
    97  }
    98  
    99  func (*FacadeSuite) TestLifeInvalidResultError(c *gc.C) {
   100  	caller := apiCaller(c, func(_ string, _, results interface{}) error {
   101  		typed, ok := results.(*params.LifeResults)
   102  		c.Assert(ok, jc.IsTrue)
   103  		*typed = params.LifeResults{
   104  			Results: []params.LifeResult{{Life: "decomposed"}},
   105  		}
   106  		return nil
   107  	})
   108  	facade := lifeflag.NewFacade(caller, nil)
   109  
   110  	result, err := facade.Life(names.NewApplicationTag("blah"))
   111  	c.Check(err, gc.ErrorMatches, `life value "decomposed" not valid`)
   112  	c.Check(result, gc.Equals, life.Value(""))
   113  }
   114  
   115  func (*FacadeSuite) TestLifeSuccess(c *gc.C) {
   116  	caller := apiCaller(c, func(_ string, _, results interface{}) error {
   117  		typed, ok := results.(*params.LifeResults)
   118  		c.Assert(ok, jc.IsTrue)
   119  		*typed = params.LifeResults{
   120  			Results: []params.LifeResult{{Life: "dying"}},
   121  		}
   122  		return nil
   123  	})
   124  	facade := lifeflag.NewFacade(caller, nil)
   125  
   126  	result, err := facade.Life(names.NewApplicationTag("blah"))
   127  	c.Check(err, jc.ErrorIsNil)
   128  	c.Check(result, gc.Equals, life.Dying)
   129  }
   130  
   131  func (*FacadeSuite) TestWatchCall(c *gc.C) {
   132  	var called bool
   133  	caller := apiCaller(c, func(request string, args, _ interface{}) error {
   134  		called = true
   135  		c.Check(request, gc.Equals, "Watch")
   136  		c.Check(args, jc.DeepEquals, params.Entities{
   137  			Entities: []params.Entity{{Tag: "application-blah"}},
   138  		})
   139  		return nil
   140  	})
   141  	facade := lifeflag.NewFacade(caller, nil)
   142  
   143  	facade.Watch(names.NewApplicationTag("blah"))
   144  	c.Check(called, jc.IsTrue)
   145  }
   146  
   147  func (*FacadeSuite) TestWatchCallError(c *gc.C) {
   148  	caller := apiCaller(c, func(_ string, _, _ interface{}) error {
   149  		return errors.New("crunch belch")
   150  	})
   151  	facade := lifeflag.NewFacade(caller, nil)
   152  
   153  	watcher, err := facade.Watch(names.NewApplicationTag("blah"))
   154  	c.Check(err, gc.ErrorMatches, "crunch belch")
   155  	c.Check(watcher, gc.IsNil)
   156  }
   157  
   158  func (*FacadeSuite) TestWatchNoResultsError(c *gc.C) {
   159  	caller := apiCaller(c, func(_ string, _, _ interface{}) error {
   160  		return nil
   161  	})
   162  	facade := lifeflag.NewFacade(caller, nil)
   163  
   164  	watcher, err := facade.Watch(names.NewApplicationTag("blah"))
   165  	c.Check(err, gc.ErrorMatches, "expected 1 Watch result, got 0")
   166  	c.Check(watcher, gc.IsNil)
   167  }
   168  
   169  func (*FacadeSuite) TestWatchExtraResultsError(c *gc.C) {
   170  	caller := apiCaller(c, func(_ string, _, results interface{}) error {
   171  		typed, ok := results.(*params.NotifyWatchResults)
   172  		c.Assert(ok, jc.IsTrue)
   173  		*typed = params.NotifyWatchResults{
   174  			Results: make([]params.NotifyWatchResult, 2),
   175  		}
   176  		return nil
   177  	})
   178  	facade := lifeflag.NewFacade(caller, nil)
   179  
   180  	watcher, err := facade.Watch(names.NewApplicationTag("blah"))
   181  	c.Check(err, gc.ErrorMatches, "expected 1 Watch result, got 2")
   182  	c.Check(watcher, gc.IsNil)
   183  }
   184  
   185  func (*FacadeSuite) TestWatchNotFoundError(c *gc.C) {
   186  	caller := apiCaller(c, func(_ string, _, results interface{}) error {
   187  		typed, ok := results.(*params.NotifyWatchResults)
   188  		c.Assert(ok, jc.IsTrue)
   189  		*typed = params.NotifyWatchResults{
   190  			Results: []params.NotifyWatchResult{{
   191  				Error: &params.Error{Code: params.CodeNotFound},
   192  			}},
   193  		}
   194  		return nil
   195  	})
   196  	facade := lifeflag.NewFacade(caller, nil)
   197  
   198  	watcher, err := facade.Watch(names.NewApplicationTag("blah"))
   199  	c.Check(err, gc.Equals, lifeflag.ErrNotFound)
   200  	c.Check(watcher, gc.IsNil)
   201  }
   202  
   203  func (*FacadeSuite) TestWatchSuccess(c *gc.C) {
   204  	caller := apiCaller(c, func(_ string, _, results interface{}) error {
   205  		typed, ok := results.(*params.NotifyWatchResults)
   206  		c.Assert(ok, jc.IsTrue)
   207  		*typed = params.NotifyWatchResults{
   208  			Results: []params.NotifyWatchResult{{
   209  				NotifyWatcherId: "123",
   210  			}},
   211  		}
   212  		return nil
   213  	})
   214  	expectWatcher := &struct{ watcher.NotifyWatcher }{}
   215  	newWatcher := func(apiCaller base.APICaller, result params.NotifyWatchResult) watcher.NotifyWatcher {
   216  		c.Check(apiCaller, gc.NotNil) // uncomparable
   217  		c.Check(result, jc.DeepEquals, params.NotifyWatchResult{
   218  			NotifyWatcherId: "123",
   219  		})
   220  		return expectWatcher
   221  	}
   222  	facade := lifeflag.NewFacade(caller, newWatcher)
   223  
   224  	watcher, err := facade.Watch(names.NewApplicationTag("blah"))
   225  	c.Check(err, jc.ErrorIsNil)
   226  	c.Check(watcher, gc.Equals, expectWatcher)
   227  }
   228  
   229  func apiCaller(c *gc.C, check func(request string, arg, result interface{}) error) base.APICaller {
   230  	return apitesting.APICallerFunc(func(facade string, version int, id, request string, arg, result interface{}) error {
   231  		c.Check(facade, gc.Equals, "LifeFlag")
   232  		c.Check(version, gc.Equals, 0)
   233  		c.Check(id, gc.Equals, "")
   234  		return check(request, arg, result)
   235  	})
   236  }