github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/controller/export_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/utils/clock"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/api"
    15  	"github.com/juju/juju/api/base"
    16  	"github.com/juju/juju/cmd/modelcmd"
    17  	"github.com/juju/juju/jujuclient"
    18  )
    19  
    20  // NewListControllersCommandForTest returns a listControllersCommand with the clientstore provided
    21  // as specified.
    22  func NewListControllersCommandForTest(testStore jujuclient.ClientStore, api func(string) ControllerAccessAPI) *listControllersCommand {
    23  	return &listControllersCommand{
    24  		store: testStore,
    25  		api:   api,
    26  	}
    27  }
    28  
    29  // NewShowControllerCommandForTest returns a showControllerCommand with the clientstore provided
    30  // as specified.
    31  func NewShowControllerCommandForTest(testStore jujuclient.ClientStore, api func(string) ControllerAccessAPI) *showControllerCommand {
    32  	return &showControllerCommand{
    33  		store: testStore,
    34  		api:   api,
    35  	}
    36  }
    37  
    38  type AddModelCommand struct {
    39  	*addModelCommand
    40  }
    41  
    42  // NewAddModelCommandForTest returns a AddModelCommand with
    43  // the api provided as specified.
    44  func NewAddModelCommandForTest(
    45  	apiRoot api.Connection,
    46  	api AddModelAPI,
    47  	cloudAPI CloudAPI,
    48  	store jujuclient.ClientStore,
    49  ) (cmd.Command, *AddModelCommand) {
    50  	c := &addModelCommand{
    51  		apiRoot: apiRoot,
    52  		newAddModelAPI: func(caller base.APICallCloser) AddModelAPI {
    53  			return api
    54  		},
    55  		newCloudAPI: func(base.APICallCloser) CloudAPI {
    56  			return cloudAPI
    57  		},
    58  	}
    59  	c.SetClientStore(store)
    60  	return modelcmd.WrapController(c), &AddModelCommand{c}
    61  }
    62  
    63  // NewListModelsCommandForTest returns a ListModelsCommand with the API
    64  // and userCreds provided as specified.
    65  func NewListModelsCommandForTest(modelAPI ModelManagerAPI, sysAPI ModelsSysAPI, store jujuclient.ClientStore) cmd.Command {
    66  	c := &modelsCommand{
    67  		modelAPI: modelAPI,
    68  		sysAPI:   sysAPI,
    69  	}
    70  	c.SetClientStore(store)
    71  	return modelcmd.WrapController(c)
    72  }
    73  
    74  // NewRegisterCommandForTest returns a RegisterCommand with the function used
    75  // to open the API connection mocked out.
    76  func NewRegisterCommandForTest(apiOpen api.OpenFunc, listModels func(jujuclient.ClientStore, string, string) ([]base.UserModel, error), store jujuclient.ClientStore) *registerCommand {
    77  	return &registerCommand{apiOpen: apiOpen, listModelsFunc: listModels, store: store}
    78  }
    79  
    80  // NewEnableDestroyControllerCommandForTest returns a enableDestroyController with the
    81  // function used to open the API connection mocked out.
    82  func NewEnableDestroyControllerCommandForTest(api removeBlocksAPI, store jujuclient.ClientStore) cmd.Command {
    83  	c := &enableDestroyController{
    84  		api: api,
    85  	}
    86  	c.SetClientStore(store)
    87  	return modelcmd.WrapController(c)
    88  }
    89  
    90  // NewDestroyCommandForTest returns a DestroyCommand with the controller and
    91  // client endpoints mocked out.
    92  func NewDestroyCommandForTest(
    93  	api destroyControllerAPI,
    94  	clientapi destroyClientAPI,
    95  	store jujuclient.ClientStore,
    96  	apierr error,
    97  ) cmd.Command {
    98  	cmd := &destroyCommand{
    99  		destroyCommandBase: destroyCommandBase{
   100  			api:       api,
   101  			clientapi: clientapi,
   102  			apierr:    apierr,
   103  		},
   104  	}
   105  	cmd.SetClientStore(store)
   106  	return modelcmd.WrapController(
   107  		cmd,
   108  		modelcmd.WrapControllerSkipControllerFlags,
   109  		modelcmd.WrapControllerSkipDefaultController,
   110  	)
   111  }
   112  
   113  // NewKillCommandForTest returns a killCommand with the controller and client
   114  // endpoints mocked out.
   115  func NewKillCommandForTest(
   116  	api destroyControllerAPI,
   117  	clientapi destroyClientAPI,
   118  	store jujuclient.ClientStore,
   119  	apierr error,
   120  	clock clock.Clock,
   121  	apiOpen modelcmd.APIOpener,
   122  ) (cmd.Command, *killCommand) {
   123  	kill := &killCommand{
   124  		destroyCommandBase: destroyCommandBase{
   125  			api:       api,
   126  			clientapi: clientapi,
   127  			apierr:    apierr,
   128  		},
   129  		clock: clock,
   130  	}
   131  	kill.SetClientStore(store)
   132  	return wrapKillCommand(kill, apiOpen, clock), kill
   133  }
   134  
   135  // KillTimeout returns the internal timeout duration of the kill command.
   136  func KillTimeout(c *gc.C, command cmd.Command) time.Duration {
   137  	kill, ok := command.(*killCommand)
   138  	c.Assert(ok, jc.IsTrue)
   139  	return kill.timeout
   140  }
   141  
   142  // KillWaitForModels calls the WaitForModels method of the kill command.
   143  func KillWaitForModels(command cmd.Command, ctx *cmd.Context, api destroyControllerAPI, uuid string) error {
   144  	kill := command.(*killCommand)
   145  	return kill.WaitForModels(ctx, api, uuid)
   146  }
   147  
   148  // NewGetConfigCommandCommandForTest returns a GetConfigCommandCommand with
   149  // the api provided as specified.
   150  func NewGetConfigCommandForTest(api controllerAPI, store jujuclient.ClientStore) cmd.Command {
   151  	c := &getConfigCommand{api: api}
   152  	c.SetClientStore(store)
   153  	return modelcmd.WrapController(c)
   154  }
   155  
   156  type CtrData ctrData
   157  type ModelData modelData
   158  
   159  func FmtCtrStatus(data CtrData) string {
   160  	return fmtCtrStatus(ctrData(data))
   161  }
   162  
   163  func FmtModelStatus(data ModelData) string {
   164  	return fmtModelStatus(modelData(data))
   165  }
   166  
   167  func NewData(api destroyControllerAPI, ctrUUID string) (ctrData, []modelData, error) {
   168  	return newData(api, ctrUUID)
   169  }