github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/romulus/showwallet/show_wallet_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details.s 3 4 package showwallet_test 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/cmd/cmdtesting" 9 "github.com/juju/errors" 10 "github.com/juju/romulus/wireformat/budget" 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 "gopkg.in/juju/names.v2" 15 16 "github.com/juju/juju/apiserver/params" 17 rcmd "github.com/juju/juju/cmd/juju/romulus" 18 "github.com/juju/juju/cmd/juju/romulus/showwallet" 19 "github.com/juju/juju/cmd/modelcmd" 20 "github.com/juju/juju/jujuclient" 21 coretesting "github.com/juju/juju/testing" 22 ) 23 24 var _ = gc.Suite(&showWalletSuite{}) 25 26 type showWalletSuite struct { 27 coretesting.FakeJujuXDGDataHomeSuite 28 stub *testing.Stub 29 mockWalletAPI *mockWalletAPI 30 mockAPI *mockAPI 31 } 32 33 func (s *showWalletSuite) SetUpTest(c *gc.C) { 34 s.CleanupSuite.SetUpTest(c) 35 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 36 s.stub = &testing.Stub{} 37 s.mockWalletAPI = &mockWalletAPI{s.stub} 38 s.mockAPI = &mockAPI{s.stub} 39 s.PatchValue(showwallet.NewWalletAPIClient, showwallet.WalletAPIClientFnc(s.mockWalletAPI)) 40 s.PatchValue(showwallet.NewJujuclientStore, newMockClientStore) 41 s.PatchValue(&rcmd.GetMeteringURLForControllerCmd, func(c *modelcmd.ControllerCommandBase) (string, error) { 42 return "http://example.com", nil 43 }) 44 } 45 46 func (s *showWalletSuite) TestShowWalletCommand(c *gc.C) { 47 tests := []struct { 48 about string 49 args []string 50 err string 51 wallet string 52 apierr string 53 output string 54 }{{ 55 about: "missing argument", 56 err: `missing arguments`, 57 }, { 58 about: "unknown arguments", 59 args: []string{"my-special-wallet", "extra", "arguments"}, 60 err: `unrecognized args: \["extra" "arguments"\]`, 61 }, { 62 about: "api error", 63 args: []string{"personal"}, 64 apierr: "well, this is embarrassing", 65 err: "failed to retrieve the wallet: well, this is embarrassing", 66 }, { 67 about: "all ok", 68 args: []string{"personal"}, 69 wallet: "personal", 70 output: "" + 71 "Model \tSpent\tBudgeted\t By\tUsage\n" + 72 "c:m1 \t500 \t 1200\t user.joe\t42% \n" + 73 "c:m2 \t600 \t 1000\tuser.jess\t60% \n" + 74 "c:m3 \t10 \t 100\t user.bob\t10% \n" + 75 "uuid4 \t10 \t 100\t user.bob\t10% \n" + 76 " \t \t \t \n" + 77 "Total \t1120 \t 2400\t \t47% \n" + 78 "Wallet \t \t 4000\t \n" + 79 "Unallocated\t \t 1600\t \n", 80 }, 81 } 82 83 for i, test := range tests { 84 c.Logf("running test %d: %v", i, test.about) 85 s.mockAPI.ResetCalls() 86 87 errs := []error{} 88 if test.apierr != "" { 89 errs = append(errs, errors.New(test.apierr)) 90 } else { 91 errs = append(errs, nil) 92 } 93 s.mockAPI.SetErrors(errs...) 94 95 ctx, err := s.runCommand(c, test.args...) 96 if test.err == "" { 97 c.Assert(err, jc.ErrorIsNil) 98 s.stub.CheckCalls(c, []testing.StubCall{ 99 {"GetWallet", []interface{}{test.wallet}}, 100 }) 101 output := cmdtesting.Stdout(ctx) 102 c.Assert(output, gc.Equals, test.output) 103 } else { 104 c.Assert(err, gc.ErrorMatches, test.err) 105 } 106 } 107 } 108 109 func (s *showWalletSuite) runCommand(c *gc.C, args ...string) (*cmd.Context, error) { 110 cmd := showwallet.NewShowWalletCommand() 111 cmd.SetClientStore(newMockStore()) 112 return cmdtesting.RunCommand(c, cmd, args...) 113 } 114 115 func newMockStore() *jujuclient.MemStore { 116 store := jujuclient.NewMemStore() 117 store.CurrentControllerName = "foo" 118 store.Controllers["foo"] = jujuclient.ControllerDetails{ 119 APIEndpoints: []string{"0.1.2.3:1234"}, 120 } 121 return store 122 } 123 124 type mockAPI struct { 125 *testing.Stub 126 } 127 128 func (api *mockAPI) ModelInfo(tags []names.ModelTag) ([]params.ModelInfoResult, error) { 129 return nil, api.NextErr() 130 } 131 132 type mockWalletAPI struct { 133 *testing.Stub 134 } 135 136 func (api *mockWalletAPI) GetWallet(name string) (*budget.WalletWithBudgets, error) { 137 api.AddCall("GetWallet", name) 138 return &budget.WalletWithBudgets{ 139 Limit: "4000", 140 Total: budget.WalletTotals{ 141 Budgeted: "2400", 142 Unallocated: "1600", 143 Available: "1180", 144 Consumed: "1120", 145 Usage: "47%", 146 }, 147 Budgets: []budget.Budget{{ 148 Owner: "user.joe", 149 Limit: "1200", 150 Consumed: "500", 151 Usage: "42%", 152 Model: "uuid1", 153 }, { 154 Owner: "user.jess", 155 Limit: "1000", 156 Consumed: "600", 157 Usage: "60%", 158 Model: "uuid2", 159 }, { 160 Owner: "user.bob", 161 Limit: "100", 162 Consumed: "10", 163 Usage: "10%", 164 Model: "uuid3", 165 }, { 166 Owner: "user.bob", 167 Limit: "100", 168 Consumed: "10", 169 Usage: "10%", 170 Model: "uuid4", 171 }}}, api.NextErr() 172 } 173 174 type mockClientStore struct { 175 jujuclient.ClientStore 176 } 177 178 func newMockClientStore() jujuclient.ClientStore { 179 return &mockClientStore{} 180 } 181 182 func (s *mockClientStore) AllControllers() (map[string]jujuclient.ControllerDetails, error) { 183 return map[string]jujuclient.ControllerDetails{ 184 "c": {}, 185 }, nil 186 } 187 188 func (s *mockClientStore) AllModels(controllerName string) (map[string]jujuclient.ModelDetails, error) { 189 return map[string]jujuclient.ModelDetails{ 190 "m1": {ModelUUID: "uuid1"}, 191 "m2": {ModelUUID: "uuid2"}, 192 "m3": {ModelUUID: "uuid3"}, 193 }, nil 194 }