github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/apiserver/common/instanceidgetter_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/instance"
    12  	"launchpad.net/juju-core/state"
    13  	"launchpad.net/juju-core/state/api/params"
    14  	"launchpad.net/juju-core/state/apiserver/common"
    15  	apiservertesting "launchpad.net/juju-core/state/apiserver/testing"
    16  	jc "launchpad.net/juju-core/testing/checkers"
    17  )
    18  
    19  type instanceIdGetterSuite struct{}
    20  
    21  var _ = gc.Suite(&instanceIdGetterSuite{})
    22  
    23  type fakeInstanceIdGetter struct {
    24  	state.Entity
    25  	instanceId string
    26  	err        string
    27  	fetchError
    28  }
    29  
    30  func (f *fakeInstanceIdGetter) InstanceId() (instance.Id, error) {
    31  	if f.err != "" {
    32  		return "", fmt.Errorf(f.err)
    33  	}
    34  	return instance.Id(f.instanceId), nil
    35  }
    36  
    37  func (*instanceIdGetterSuite) TestInstanceId(c *gc.C) {
    38  	st := &fakeState{
    39  		entities: map[string]entityWithError{
    40  			"x0": &fakeInstanceIdGetter{instanceId: "foo"},
    41  			"x1": &fakeInstanceIdGetter{instanceId: "bar"},
    42  			"x2": &fakeInstanceIdGetter{instanceId: "baz", err: "x2 error"},
    43  			"x3": &fakeInstanceIdGetter{fetchError: "x3 error"},
    44  		},
    45  	}
    46  	getCanRead := func() (common.AuthFunc, error) {
    47  		return func(tag string) bool {
    48  			switch tag {
    49  			case "x0", "x2", "x3":
    50  				return true
    51  			}
    52  			return false
    53  		}, nil
    54  	}
    55  	ig := common.NewInstanceIdGetter(st, getCanRead)
    56  	entities := params.Entities{[]params.Entity{
    57  		{"x0"}, {"x1"}, {"x2"}, {"x3"}, {"x4"},
    58  	}}
    59  	results, err := ig.InstanceId(entities)
    60  	c.Assert(err, gc.IsNil)
    61  	c.Assert(results, jc.DeepEquals, params.StringResults{
    62  		Results: []params.StringResult{
    63  			{Result: "foo"},
    64  			{Error: apiservertesting.ErrUnauthorized},
    65  			{Error: &params.Error{Message: "x2 error"}},
    66  			{Error: &params.Error{Message: "x3 error"}},
    67  			{Error: apiservertesting.ErrUnauthorized},
    68  		},
    69  	})
    70  }
    71  
    72  func (*instanceIdGetterSuite) TestInstanceIdError(c *gc.C) {
    73  	getCanRead := func() (common.AuthFunc, error) {
    74  		return nil, fmt.Errorf("pow")
    75  	}
    76  	ig := common.NewInstanceIdGetter(&fakeState{}, getCanRead)
    77  	_, err := ig.InstanceId(params.Entities{[]params.Entity{{"x0"}}})
    78  	c.Assert(err, gc.ErrorMatches, "pow")
    79  }