github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/clock" 10 "github.com/juju/cmd" 11 12 "github.com/juju/juju/api" 13 "github.com/juju/juju/api/base" 14 "github.com/juju/juju/cmd/modelcmd" 15 "github.com/juju/juju/environs" 16 "github.com/juju/juju/environs/context" 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 providerRegistry environs.ProviderRegistry, 50 ) (cmd.Command, *AddModelCommand) { 51 c := &addModelCommand{ 52 apiRoot: apiRoot, 53 newAddModelAPI: func(caller base.APICallCloser) AddModelAPI { 54 return api 55 }, 56 newCloudAPI: func(base.APICallCloser) CloudAPI { 57 return cloudAPI 58 }, 59 providerRegistry: providerRegistry, 60 } 61 c.SetClientStore(store) 62 return modelcmd.WrapController(c), &AddModelCommand{c} 63 } 64 65 // NewListModelsCommandForTest returns a ListModelsCommand with the API 66 // and userCreds provided as specified. 67 func NewListModelsCommandForTest(modelAPI ModelManagerAPI, sysAPI ModelsSysAPI, store jujuclient.ClientStore) cmd.Command { 68 c := &modelsCommand{ 69 modelAPI: modelAPI, 70 sysAPI: sysAPI, 71 } 72 c.SetClientStore(store) 73 return modelcmd.WrapController(c) 74 } 75 76 // NewRegisterCommandForTest returns a RegisterCommand with the function used 77 // to open the API connection mocked out. 78 func NewRegisterCommandForTest(apiOpen api.OpenFunc, listModels func(jujuclient.ClientStore, string, string) ([]base.UserModel, error), store jujuclient.ClientStore) modelcmd.Command { 79 return modelcmd.WrapBase(®isterCommand{ 80 apiOpen: apiOpen, 81 listModelsFunc: listModels, 82 store: store, 83 }) 84 } 85 86 // NewEnableDestroyControllerCommandForTest returns a enableDestroyController with the 87 // function used to open the API connection mocked out. 88 func NewEnableDestroyControllerCommandForTest(api removeBlocksAPI, store jujuclient.ClientStore) cmd.Command { 89 c := &enableDestroyController{ 90 api: api, 91 } 92 c.SetClientStore(store) 93 return modelcmd.WrapController(c) 94 } 95 96 // NewDestroyCommandForTest returns a DestroyCommand with the controller and 97 // client endpoints mocked out. 98 func NewDestroyCommandForTest( 99 api destroyControllerAPI, 100 clientapi destroyClientAPI, 101 storageAPI storageAPI, 102 store jujuclient.ClientStore, 103 apierr error, 104 controllerCredentialAPIFunc newCredentialAPIFunc, 105 environsDestroy func(string, environs.ControllerDestroyer, context.ProviderCallContext, jujuclient.ControllerStore) error, 106 107 ) cmd.Command { 108 cmd := &destroyCommand{ 109 destroyCommandBase: destroyCommandBase{ 110 api: api, 111 clientapi: clientapi, 112 apierr: apierr, 113 controllerCredentialAPIFunc: controllerCredentialAPIFunc, 114 environsDestroy: environsDestroy, 115 }, 116 storageAPI: storageAPI, 117 } 118 cmd.SetClientStore(store) 119 return modelcmd.WrapController( 120 cmd, 121 modelcmd.WrapControllerSkipControllerFlags, 122 modelcmd.WrapControllerSkipDefaultController, 123 ) 124 } 125 126 // NewKillCommandForTest returns a killCommand with the controller and client 127 // endpoints mocked out. 128 func NewKillCommandForTest( 129 api destroyControllerAPI, 130 clientapi destroyClientAPI, 131 store jujuclient.ClientStore, 132 apierr error, 133 clock clock.Clock, 134 apiOpen api.OpenFunc, 135 controllerCredentialAPIFunc newCredentialAPIFunc, 136 environsDestroy func(string, environs.ControllerDestroyer, context.ProviderCallContext, jujuclient.ControllerStore) error, 137 ) cmd.Command { 138 kill := &killCommand{ 139 destroyCommandBase: destroyCommandBase{ 140 api: api, 141 clientapi: clientapi, 142 apierr: apierr, 143 controllerCredentialAPIFunc: controllerCredentialAPIFunc, 144 environsDestroy: environsDestroy, 145 }, 146 clock: clock, 147 } 148 kill.SetClientStore(store) 149 wrapped := wrapKillCommand(kill) 150 if apiOpen != nil { 151 wrapped.SetAPIOpen(apiOpen) 152 } 153 return wrapped 154 } 155 156 // KillTimeout returns the internal timeout duration of the kill command. 157 func KillTimeout(command cmd.Command) time.Duration { 158 return modelcmd.InnerCommand(command).(*killCommand).timeout 159 } 160 161 // KillWaitForModels calls the WaitForModels method of the kill command. 162 func KillWaitForModels(command cmd.Command, ctx *cmd.Context, api destroyControllerAPI, uuid string) error { 163 return modelcmd.InnerCommand(command).(*killCommand).WaitForModels(ctx, api, uuid) 164 } 165 166 // NewConfigCommandForTest returns a ConfigCommand with 167 // the api provided as specified. 168 func NewConfigCommandForTest(api controllerAPI, store jujuclient.ClientStore) cmd.Command { 169 c := &configCommand{api: api} 170 c.SetClientStore(store) 171 return modelcmd.WrapController(c) 172 } 173 174 type CtrData ctrData 175 type ModelData modelData 176 177 func FmtCtrStatus(data CtrData) string { 178 return fmtCtrStatus(ctrData(data)) 179 } 180 181 func FmtModelStatus(data ModelData) string { 182 return fmtModelStatus(modelData(data)) 183 } 184 185 func NewData(api destroyControllerAPI, ctrUUID string) (ctrData, []modelData, error) { 186 return newData(api, ctrUUID) 187 } 188 189 var ( 190 NoModelsMessage = noModelsMessage 191 )