github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/common/unitstate_test.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names/v5" 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 apitesting "github.com/juju/juju/api/base/testing" 14 "github.com/juju/juju/api/common" 15 apiservererrors "github.com/juju/juju/apiserver/errors" 16 "github.com/juju/juju/rpc/params" 17 ) 18 19 type unitStateSuite struct { 20 testing.IsolationSuite 21 tag names.UnitTag 22 } 23 24 var _ = gc.Suite(&unitStateSuite{}) 25 26 func (s *unitStateSuite) SetUpTest(c *gc.C) { 27 s.tag = names.NewUnitTag("test-unit/0") 28 } 29 30 func (s *unitStateSuite) TestSetStateSingleResult(c *gc.C) { 31 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 32 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 33 c.Assert(name, gc.Equals, "SetState") 34 c.Assert(args.(params.SetUnitStateArgs).Args, gc.HasLen, 1) 35 c.Assert(args.(params.SetUnitStateArgs).Args[0].Tag, gc.Equals, s.tag.String()) 36 c.Assert(*args.(params.SetUnitStateArgs).Args[0].CharmState, jc.DeepEquals, map[string]string{"one": "two"}) 37 *(response.(*params.ErrorResults)) = params.ErrorResults{ 38 Results: []params.ErrorResult{{ 39 Error: nil, 40 }}, 41 } 42 return nil 43 } 44 api := common.NewUniterStateAPI(&facadeCaller, s.tag) 45 err := api.SetState(params.SetUnitStateArg{ 46 CharmState: &map[string]string{"one": "two"}, 47 }) 48 c.Assert(err, jc.ErrorIsNil) 49 } 50 51 func (s *unitStateSuite) TestSetStateReturnsQuotaExceededError(c *gc.C) { 52 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 53 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 54 result := response.(*params.ErrorResults) 55 result.Results = []params.ErrorResult{{ 56 Error: apiservererrors.ServerError(errors.NewQuotaLimitExceeded(nil, "cake slice limit exceeded; try again later")), 57 }} 58 return nil 59 } 60 61 // The client should reconstruct the quota error from the server response 62 api := common.NewUniterStateAPI(&facadeCaller, s.tag) 63 err := api.SetState(params.SetUnitStateArg{ 64 CharmState: &map[string]string{"one": "two"}, 65 }) 66 c.Assert(err, jc.Satisfies, errors.IsQuotaLimitExceeded, gc.Commentf("expected the client to reconstruct QuotaLimitExceeded error from server response")) 67 } 68 69 func (s *unitStateSuite) TestSetStateMultipleReturnsError(c *gc.C) { 70 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 71 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 72 c.Assert(name, gc.Equals, "SetState") 73 c.Assert(args.(params.SetUnitStateArgs).Args, gc.HasLen, 1) 74 c.Assert(args.(params.SetUnitStateArgs).Args[0].Tag, gc.Equals, s.tag.String()) 75 c.Assert(*args.(params.SetUnitStateArgs).Args[0].CharmState, jc.DeepEquals, map[string]string{"one": "two"}) 76 *(response.(*params.ErrorResults)) = params.ErrorResults{ 77 Results: []params.ErrorResult{ 78 {Error: nil}, 79 {Error: nil}, 80 }, 81 } 82 return nil 83 } 84 85 api := common.NewUniterStateAPI(&facadeCaller, s.tag) 86 err := api.SetState(params.SetUnitStateArg{ 87 CharmState: &map[string]string{"one": "two"}, 88 }) 89 c.Assert(err, gc.ErrorMatches, "expected 1 result, got 2") 90 } 91 92 func (s *unitStateSuite) TestStateSingleResult(c *gc.C) { 93 expectedCharmState := map[string]string{ 94 "one": "two", 95 "three": "four", 96 } 97 expectedUniterState := "testing" 98 99 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 100 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 101 c.Assert(name, gc.Equals, "State") 102 *(response.(*params.UnitStateResults)) = params.UnitStateResults{ 103 Results: []params.UnitStateResult{{ 104 UniterState: expectedUniterState, 105 CharmState: expectedCharmState, 106 }}} 107 return nil 108 } 109 110 api := common.NewUniterStateAPI(&facadeCaller, s.tag) 111 obtainedUnitState, err := api.State() 112 c.Assert(err, jc.ErrorIsNil) 113 c.Assert(expectedCharmState, gc.DeepEquals, obtainedUnitState.CharmState) 114 c.Assert(expectedUniterState, gc.DeepEquals, obtainedUnitState.UniterState) 115 } 116 117 func (s *unitStateSuite) TestStateMultipleReturnsError(c *gc.C) { 118 facadeCaller := apitesting.StubFacadeCaller{Stub: &testing.Stub{}} 119 facadeCaller.FacadeCallFn = func(name string, args, response interface{}) error { 120 c.Assert(name, gc.Equals, "State") 121 *(response.(*params.UnitStateResults)) = params.UnitStateResults{ 122 Results: []params.UnitStateResult{ 123 {Error: ¶ms.Error{Code: params.CodeNotFound, Message: `testing`}}, 124 {Error: ¶ms.Error{Code: params.CodeNotFound, Message: `other`}}, 125 }} 126 return nil 127 } 128 129 api := common.NewUniterStateAPI(&facadeCaller, s.tag) 130 _, err := api.State() 131 c.Assert(err, gc.ErrorMatches, "expected 1 result, got 2") 132 }