github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/romulus/listwallets/list-wallets_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package listwallets_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  
    15  	rcmd "github.com/juju/juju/cmd/juju/romulus"
    16  	"github.com/juju/juju/cmd/juju/romulus/listwallets"
    17  	"github.com/juju/juju/cmd/modelcmd"
    18  	"github.com/juju/juju/jujuclient"
    19  	coretesting "github.com/juju/juju/testing"
    20  )
    21  
    22  var _ = gc.Suite(&listWalletsSuite{})
    23  
    24  type listWalletsSuite struct {
    25  	coretesting.FakeJujuXDGDataHomeSuite
    26  	stub    *testing.Stub
    27  	mockAPI *mockapi
    28  }
    29  
    30  func (s *listWalletsSuite) SetUpTest(c *gc.C) {
    31  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    32  	s.stub = &testing.Stub{}
    33  	s.mockAPI = &mockapi{Stub: s.stub}
    34  	s.PatchValue(listwallets.NewAPIClient, listwallets.APIClientFnc(s.mockAPI))
    35  	s.PatchValue(&rcmd.GetMeteringURLForControllerCmd, func(c *modelcmd.ControllerCommandBase) (string, error) {
    36  		return "http://example.com", nil
    37  	})
    38  }
    39  
    40  func (s *listWalletsSuite) TestUnexpectedParameters(c *gc.C) {
    41  	_, err := s.runCommand(c, "unexpected")
    42  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["unexpected"\]`)
    43  }
    44  
    45  func (s *listWalletsSuite) TestAPIError(c *gc.C) {
    46  	s.mockAPI.SetErrors(errors.New("well, this is embarrassing"))
    47  	_, err := s.runCommand(c)
    48  	c.Assert(err, gc.ErrorMatches, "failed to retrieve wallets: well, this is embarrassing")
    49  }
    50  
    51  func (s *listWalletsSuite) TestListWalletsOutput(c *gc.C) {
    52  	s.mockAPI.result = &budget.ListWalletsResponse{
    53  		Wallets: budget.WalletSummaries{
    54  			budget.WalletSummary{
    55  				Owner:       "bob",
    56  				Wallet:      "personal",
    57  				Limit:       "50",
    58  				Budgeted:    "30",
    59  				Unallocated: "20",
    60  				Available:   "45",
    61  				Consumed:    "5",
    62  				Default:     true,
    63  			},
    64  			budget.WalletSummary{
    65  				Owner:       "bob",
    66  				Wallet:      "work",
    67  				Limit:       "200",
    68  				Budgeted:    "100",
    69  				Unallocated: "100",
    70  				Available:   "150",
    71  				Consumed:    "50",
    72  			},
    73  			budget.WalletSummary{
    74  				Owner:       "bob",
    75  				Wallet:      "team",
    76  				Limit:       "50",
    77  				Budgeted:    "10",
    78  				Unallocated: "40",
    79  				Available:   "40",
    80  				Consumed:    "10",
    81  			},
    82  		},
    83  		Total: budget.WalletTotals{
    84  			Limit:       "300",
    85  			Budgeted:    "140",
    86  			Available:   "235",
    87  			Unallocated: "160",
    88  			Consumed:    "65",
    89  		},
    90  		Credit: "400",
    91  	}
    92  	// Expected command output. Make sure wallets are sorted alphabetically.
    93  	expected := "" +
    94  		"Wallet       \tMonthly\tBudgeted\tAvailable\tSpent\n" +
    95  		"personal*    \t     50\t      30\t       45\t    5\n" +
    96  		"team         \t     50\t      10\t       40\t   10\n" +
    97  		"work         \t    200\t     100\t      150\t   50\n" +
    98  		"Total        \t    300\t     140\t      235\t   65\n" +
    99  		"             \t       \t        \t         \t     \n" +
   100  		"Credit limit:\t    400\t        \t         \t     \n"
   101  
   102  	ctx, err := s.runCommand(c)
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, expected)
   105  	s.mockAPI.CheckCallNames(c, "ListWallets")
   106  }
   107  
   108  func (s *listWalletsSuite) TestListWalletsOutputNoWallets(c *gc.C) {
   109  	s.mockAPI.result = &budget.ListWalletsResponse{
   110  		Wallets: budget.WalletSummaries{},
   111  		Total: budget.WalletTotals{
   112  			Limit:       "0",
   113  			Budgeted:    "0",
   114  			Available:   "0",
   115  			Unallocated: "0",
   116  			Consumed:    "0",
   117  		},
   118  		Credit: "0",
   119  	}
   120  	expected := "" +
   121  		"Wallet       \tMonthly\tBudgeted\tAvailable\tSpent\n" +
   122  		"Total        \t      0\t       0\t        0\t    0\n" +
   123  		"             \t       \t        \t         \t     \n" +
   124  		"Credit limit:\t      0\t        \t         \t     \n"
   125  
   126  	ctx, err := s.runCommand(c)
   127  	c.Assert(err, jc.ErrorIsNil)
   128  	c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, expected)
   129  	s.mockAPI.CheckCallNames(c, "ListWallets")
   130  }
   131  
   132  func (s *listWalletsSuite) TestListWalletsNoOutput(c *gc.C) {
   133  	ctx, err := s.runCommand(c)
   134  	c.Assert(err, gc.ErrorMatches, `no wallet information available`)
   135  	c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, ``)
   136  	s.mockAPI.CheckCallNames(c, "ListWallets")
   137  }
   138  
   139  func (s *listWalletsSuite) runCommand(c *gc.C, args ...string) (*cmd.Context, error) {
   140  	cmd := listwallets.NewListWalletsCommand()
   141  	cmd.SetClientStore(newMockStore())
   142  	return cmdtesting.RunCommand(c, cmd, args...)
   143  }
   144  
   145  func newMockStore() *jujuclient.MemStore {
   146  	store := jujuclient.NewMemStore()
   147  	store.CurrentControllerName = "foo"
   148  	store.Controllers["foo"] = jujuclient.ControllerDetails{
   149  		APIEndpoints: []string{"0.1.2.3:1234"},
   150  	}
   151  	return store
   152  }
   153  
   154  type mockapi struct {
   155  	*testing.Stub
   156  	result *budget.ListWalletsResponse
   157  }
   158  
   159  func (api *mockapi) ListWallets() (*budget.ListWalletsResponse, error) {
   160  	api.AddCall("ListWallets")
   161  	if err := api.NextErr(); err != nil {
   162  		return nil, err
   163  	}
   164  	return api.result, nil
   165  }