github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/romulus/listbudgets/list-budgets_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package listbudgets_test 5 6 import ( 7 "github.com/juju/cmd/cmdtesting" 8 "github.com/juju/errors" 9 "github.com/juju/romulus/wireformat/budget" 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/listbudgets" 15 coretesting "github.com/juju/juju/testing" 16 ) 17 18 var _ = gc.Suite(&listBudgetsSuite{}) 19 20 type listBudgetsSuite struct { 21 coretesting.FakeJujuXDGDataHomeSuite 22 stub *testing.Stub 23 mockAPI *mockapi 24 } 25 26 func (s *listBudgetsSuite) SetUpTest(c *gc.C) { 27 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 28 s.stub = &testing.Stub{} 29 s.mockAPI = &mockapi{Stub: s.stub} 30 s.PatchValue(listbudgets.NewAPIClient, listbudgets.APIClientFnc(s.mockAPI)) 31 } 32 33 func (s *listBudgetsSuite) TestUnexpectedParameters(c *gc.C) { 34 listBudgets := listbudgets.NewListBudgetsCommand() 35 _, err := cmdtesting.RunCommand(c, listBudgets, "unexpected") 36 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["unexpected"\]`) 37 } 38 39 func (s *listBudgetsSuite) TestAPIError(c *gc.C) { 40 s.mockAPI.SetErrors(errors.New("well, this is embarrassing")) 41 listBudgets := listbudgets.NewListBudgetsCommand() 42 _, err := cmdtesting.RunCommand(c, listBudgets) 43 c.Assert(err, gc.ErrorMatches, "failed to retrieve budgets: well, this is embarrassing") 44 } 45 46 func (s *listBudgetsSuite) TestListBudgetsOutput(c *gc.C) { 47 s.mockAPI.result = &budget.ListBudgetsResponse{ 48 Budgets: budget.BudgetSummaries{ 49 budget.BudgetSummary{ 50 Owner: "bob", 51 Budget: "personal", 52 Limit: "50", 53 Allocated: "30", 54 Unallocated: "20", 55 Available: "45", 56 Consumed: "5", 57 }, 58 budget.BudgetSummary{ 59 Owner: "bob", 60 Budget: "work", 61 Limit: "200", 62 Allocated: "100", 63 Unallocated: "100", 64 Available: "150", 65 Consumed: "50", 66 }, 67 budget.BudgetSummary{ 68 Owner: "bob", 69 Budget: "team", 70 Limit: "50", 71 Allocated: "10", 72 Unallocated: "40", 73 Available: "40", 74 Consumed: "10", 75 }, 76 }, 77 Total: budget.BudgetTotals{ 78 Limit: "300", 79 Allocated: "140", 80 Available: "235", 81 Unallocated: "160", 82 Consumed: "65", 83 }, 84 Credit: "400", 85 } 86 // Expected command output. Make sure budgets are sorted alphabetically. 87 expected := "" + 88 "BUDGET \tMONTHLY\tALLOCATED\tAVAILABLE\tSPENT\n" + 89 "personal \t 50\t 30\t 45\t 5\n" + 90 "team \t 50\t 10\t 40\t 10\n" + 91 "work \t 200\t 100\t 150\t 50\n" + 92 "TOTAL \t 300\t 140\t 235\t 65\n" + 93 " \t \t \t \t \n" + 94 "Credit limit:\t 400\t \t \t \n" 95 96 listBudgets := listbudgets.NewListBudgetsCommand() 97 98 ctx, err := cmdtesting.RunCommand(c, listBudgets) 99 c.Assert(err, jc.ErrorIsNil) 100 c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, expected) 101 s.mockAPI.CheckCallNames(c, "ListBudgets") 102 } 103 104 func (s *listBudgetsSuite) TestListBudgetsOutputNoBudgets(c *gc.C) { 105 s.mockAPI.result = &budget.ListBudgetsResponse{ 106 Budgets: budget.BudgetSummaries{}, 107 Total: budget.BudgetTotals{ 108 Limit: "0", 109 Allocated: "0", 110 Available: "0", 111 Unallocated: "0", 112 Consumed: "0", 113 }, 114 Credit: "0", 115 } 116 expected := "" + 117 "BUDGET \tMONTHLY\tALLOCATED\tAVAILABLE\tSPENT\n" + 118 "TOTAL \t 0\t 0\t 0\t 0\n" + 119 " \t \t \t \t \n" + 120 "Credit limit:\t 0\t \t \t \n" 121 122 listBudgets := listbudgets.NewListBudgetsCommand() 123 124 ctx, err := cmdtesting.RunCommand(c, listBudgets) 125 c.Assert(err, jc.ErrorIsNil) 126 c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, expected) 127 s.mockAPI.CheckCallNames(c, "ListBudgets") 128 } 129 130 func (s *listBudgetsSuite) TestListBudgetsNoOutput(c *gc.C) { 131 listBudgets := listbudgets.NewListBudgetsCommand() 132 133 ctx, err := cmdtesting.RunCommand(c, listBudgets) 134 c.Assert(err, gc.ErrorMatches, `no budget information available`) 135 c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, ``) 136 s.mockAPI.CheckCallNames(c, "ListBudgets") 137 } 138 139 type mockapi struct { 140 *testing.Stub 141 result *budget.ListBudgetsResponse 142 } 143 144 func (api *mockapi) ListBudgets() (*budget.ListBudgetsResponse, error) { 145 api.AddCall("ListBudgets") 146 if err := api.NextErr(); err != nil { 147 return nil, err 148 } 149 return api.result, nil 150 }