github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/romulus/updateallocation/updateallocation_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package updateallocation_test 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/cmd/cmdtesting" 9 "github.com/juju/errors" 10 "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/cmd/juju/romulus/updateallocation" 15 "github.com/juju/juju/jujuclient" 16 "github.com/juju/juju/jujuclient/jujuclienttesting" 17 coretesting "github.com/juju/juju/testing" 18 ) 19 20 var _ = gc.Suite(&updateAllocationSuite{}) 21 22 type updateAllocationSuite struct { 23 coretesting.FakeJujuXDGDataHomeSuite 24 stub *testing.Stub 25 mockAPI *mockapi 26 store jujuclient.ClientStore 27 } 28 29 func (s *updateAllocationSuite) SetUpTest(c *gc.C) { 30 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 31 s.store = &jujuclienttesting.MemStore{ 32 Controllers: map[string]jujuclient.ControllerDetails{ 33 "controller": {}, 34 }, 35 Models: map[string]*jujuclient.ControllerModels{ 36 "controller": { 37 Models: map[string]jujuclient.ModelDetails{ 38 "admin@local/model": {"model-uuid"}, 39 }, 40 CurrentModel: "admin@local/model", 41 }, 42 }, 43 Accounts: map[string]jujuclient.AccountDetails{ 44 "controller": { 45 User: "admin@local", 46 }, 47 }, 48 } 49 s.stub = &testing.Stub{} 50 s.mockAPI = newMockAPI(s.stub) 51 } 52 53 func (s *updateAllocationSuite) run(c *gc.C, args ...string) (*cmd.Context, error) { 54 updateAlloc := updateallocation.NewUpdateAllocateCommandForTest(s.mockAPI, s.store) 55 a := []string{"-m", "controller:model"} 56 a = append(a, args...) 57 return cmdtesting.RunCommand(c, updateAlloc, a...) 58 } 59 60 func (s *updateAllocationSuite) TestUpdateAllocation(c *gc.C) { 61 s.mockAPI.resp = "name budget set to 5" 62 ctx, err := s.run(c, "name", "5") 63 c.Assert(err, jc.ErrorIsNil) 64 c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, "name budget set to 5\n") 65 s.mockAPI.CheckCall(c, 0, "UpdateAllocation", "model-uuid", "name", "5") 66 } 67 68 func (s *updateAllocationSuite) TestUpdateAllocationAPIError(c *gc.C) { 69 s.stub.SetErrors(errors.New("something failed")) 70 _, err := s.run(c, "name", "5") 71 c.Assert(err, gc.ErrorMatches, "failed to update the allocation: something failed") 72 s.mockAPI.CheckCall(c, 0, "UpdateAllocation", "model-uuid", "name", "5") 73 } 74 75 func (s *updateAllocationSuite) TestUpdateAllocationErrors(c *gc.C) { 76 tests := []struct { 77 about string 78 args []string 79 expectedError string 80 }{ 81 { 82 about: "value needs to be a number", 83 args: []string{"name", "badvalue"}, 84 expectedError: "value needs to be a whole number", 85 }, 86 { 87 about: "value is missing", 88 args: []string{"name"}, 89 expectedError: "application and value required", 90 }, 91 { 92 about: "no args", 93 args: []string{}, 94 expectedError: "application and value required", 95 }, 96 } 97 for i, test := range tests { 98 s.mockAPI.ResetCalls() 99 c.Logf("test %d: %s", i, test.about) 100 _, err := s.run(c, test.args...) 101 c.Check(err, gc.ErrorMatches, test.expectedError) 102 s.mockAPI.CheckNoCalls(c) 103 } 104 } 105 106 func newMockAPI(s *testing.Stub) *mockapi { 107 return &mockapi{Stub: s} 108 } 109 110 type mockapi struct { 111 *testing.Stub 112 resp string 113 } 114 115 func (api *mockapi) UpdateAllocation(modelUUID, name, value string) (string, error) { 116 api.MethodCall(api, "UpdateAllocation", modelUUID, name, value) 117 return api.resp, api.NextErr() 118 }