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