github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/machineactions/machineactions_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Copyright 2016 Cloudbase Solutions 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package machineactions_test 6 7 import ( 8 "errors" 9 10 "github.com/juju/names" 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/apiserver/common" 16 "github.com/juju/juju/apiserver/machineactions" 17 "github.com/juju/juju/apiserver/params" 18 "github.com/juju/juju/state" 19 ) 20 21 type FacadeSuite struct { 22 testing.IsolationSuite 23 } 24 25 var _ = gc.Suite(&FacadeSuite{}) 26 27 func (*FacadeSuite) TestAcceptsMachineAgent(c *gc.C) { 28 facade, err := machineactions.NewFacade(nil, nil, agentAuth{machine: true}) 29 c.Check(err, jc.ErrorIsNil) 30 c.Check(facade, gc.NotNil) 31 } 32 33 func (*FacadeSuite) TestOtherAgent(c *gc.C) { 34 facade, err := machineactions.NewFacade(nil, nil, agentAuth{}) 35 c.Check(err, gc.Equals, common.ErrPerm) 36 c.Check(facade, gc.IsNil) 37 } 38 39 func (*FacadeSuite) TestRunningActions(c *gc.C) { 40 stub := &testing.Stub{} 41 auth := agentAuth{ 42 machine: true, 43 } 44 backend := &mockBackend{ 45 stub: stub, 46 } 47 48 facade, err := machineactions.NewFacade(backend, nil, auth) 49 c.Assert(err, jc.ErrorIsNil) 50 51 stub.SetErrors(errors.New("boom")) 52 results := facade.RunningActions(entities( 53 "valid", // we will cause this one to err out 54 "valid", 55 "invalid", 56 "unauthorized", 57 )) 58 59 c.Assert(results, gc.DeepEquals, params.ActionsByReceivers{ 60 Actions: []params.ActionsByReceiver{{ 61 Receiver: "valid", 62 Error: common.ServerError(errors.New("boom")), 63 }, { 64 Receiver: "valid", 65 Actions: actions, 66 }, { 67 Error: common.ServerError(common.ErrBadId), 68 }, { 69 Receiver: "unauthorized", 70 Error: common.ServerError(common.ErrPerm), 71 }}, 72 }) 73 stub.CheckCallNames(c, "TagToActionReceiverFn", "ConvertActions", "ConvertActions") 74 } 75 76 // entities is a convenience constructor for params.Entities. 77 func entities(tags ...string) params.Entities { 78 entities := params.Entities{ 79 Entities: make([]params.Entity, len(tags)), 80 } 81 for i, tag := range tags { 82 entities.Entities[i].Tag = tag 83 } 84 return entities 85 } 86 87 // agentAuth implements common.Authorizer for use in the tests. 88 type agentAuth struct { 89 common.Authorizer 90 machine bool 91 } 92 93 // AuthMachineAgent is part of the common.Authorizer interface. 94 func (auth agentAuth) AuthMachineAgent() bool { 95 return auth.machine 96 } 97 98 func (auth agentAuth) AuthOwner(tag names.Tag) bool { 99 if tag.String() == "valid" { 100 return true 101 } 102 return false 103 } 104 105 // mockBackend implements machineactions.Backend for use in the tests. 106 type mockBackend struct { 107 machineactions.Backend 108 stub *testing.Stub 109 } 110 111 func (mock *mockBackend) TagToActionReceiverFn(findEntity func(names.Tag) (state.Entity, error)) func(string) (state.ActionReceiver, error) { 112 mock.stub.AddCall("TagToActionReceiverFn", findEntity) 113 return tagToActionReceiver 114 } 115 116 func tagToActionReceiver(tag string) (state.ActionReceiver, error) { 117 switch tag { 118 case "valid": 119 return validReceiver, nil 120 case "unauthorized": 121 return unauthorizedReceiver, nil 122 default: 123 return nil, errors.New("invalid actionReceiver tag") 124 } 125 } 126 127 var validReceiver = fakeActionReceiver{tag: validTag} 128 var unauthorizedReceiver = fakeActionReceiver{tag: unauthorizedTag} 129 var validTag = fakeTag{s: "valid"} 130 var unauthorizedTag = fakeTag{s: "unauthorized"} 131 132 type fakeActionReceiver struct { 133 state.ActionReceiver 134 tag fakeTag 135 } 136 137 func (mock fakeActionReceiver) Tag() names.Tag { 138 return mock.tag 139 } 140 141 type fakeTag struct { 142 names.Tag 143 s string 144 } 145 146 func (mock fakeTag) String() string { 147 return mock.s 148 } 149 150 func (mock *mockBackend) ConvertActions(ar state.ActionReceiver, fn common.GetActionsFn) ([]params.ActionResult, error) { 151 mock.stub.AddCall("ConvertActions", ar, fn) 152 if err := mock.stub.NextErr(); err != nil { 153 return nil, err 154 } 155 return actions, nil 156 } 157 158 var actions = []params.ActionResult{params.ActionResult{Action: ¶ms.Action{Name: "foo"}}}