github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/apiserver/common/life_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	"launchpad.net/juju-core/state"
    12  	"launchpad.net/juju-core/state/api/params"
    13  	"launchpad.net/juju-core/state/apiserver/common"
    14  	apiservertesting "launchpad.net/juju-core/state/apiserver/testing"
    15  )
    16  
    17  type lifeSuite struct{}
    18  
    19  var _ = gc.Suite(&lifeSuite{})
    20  
    21  type fakeLifer struct {
    22  	state.Entity
    23  	life state.Life
    24  	fetchError
    25  }
    26  
    27  func (l *fakeLifer) Life() state.Life {
    28  	return l.life
    29  }
    30  
    31  func (*lifeSuite) TestLife(c *gc.C) {
    32  	st := &fakeState{
    33  		entities: map[string]entityWithError{
    34  			"x0": &fakeLifer{life: state.Alive},
    35  			"x1": &fakeLifer{life: state.Dying},
    36  			"x2": &fakeLifer{life: state.Dead},
    37  			"x3": &fakeLifer{fetchError: "x3 error"},
    38  		},
    39  	}
    40  	getCanRead := func() (common.AuthFunc, error) {
    41  		return func(tag string) bool {
    42  			switch tag {
    43  			case "x0", "x2", "x3":
    44  				return true
    45  			}
    46  			return false
    47  		}, nil
    48  	}
    49  	lg := common.NewLifeGetter(st, getCanRead)
    50  	entities := params.Entities{[]params.Entity{
    51  		{"x0"}, {"x1"}, {"x2"}, {"x3"}, {"x4"},
    52  	}}
    53  	results, err := lg.Life(entities)
    54  	c.Assert(err, gc.IsNil)
    55  	c.Assert(results, gc.DeepEquals, params.LifeResults{
    56  		Results: []params.LifeResult{
    57  			{Life: params.Alive},
    58  			{Error: apiservertesting.ErrUnauthorized},
    59  			{Life: params.Dead},
    60  			{Error: &params.Error{Message: "x3 error"}},
    61  			{Error: apiservertesting.ErrUnauthorized},
    62  		},
    63  	})
    64  }
    65  
    66  func (*lifeSuite) TestLifeError(c *gc.C) {
    67  	getCanRead := func() (common.AuthFunc, error) {
    68  		return nil, fmt.Errorf("pow")
    69  	}
    70  	lg := common.NewLifeGetter(&fakeState{}, getCanRead)
    71  	_, err := lg.Life(params.Entities{[]params.Entity{{"x0"}}})
    72  	c.Assert(err, gc.ErrorMatches, "pow")
    73  }
    74  
    75  func (*lifeSuite) TestLifeNoArgsNoError(c *gc.C) {
    76  	getCanRead := func() (common.AuthFunc, error) {
    77  		return nil, fmt.Errorf("pow")
    78  	}
    79  	lg := common.NewLifeGetter(&fakeState{}, getCanRead)
    80  	result, err := lg.Life(params.Entities{})
    81  	c.Assert(err, gc.IsNil)
    82  	c.Assert(result.Results, gc.HasLen, 0)
    83  }