github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/jujuclient/jujuclienttesting/stub.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuclienttesting
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  
     9  	"github.com/juju/juju/cloud"
    10  	"github.com/juju/juju/jujuclient"
    11  )
    12  
    13  type StubStore struct {
    14  	*testing.Stub
    15  
    16  	AllControllersFunc   func() (map[string]jujuclient.ControllerDetails, error)
    17  	ControllerByNameFunc func(name string) (*jujuclient.ControllerDetails, error)
    18  	UpdateControllerFunc func(name string, one jujuclient.ControllerDetails) error
    19  	RemoveControllerFunc func(name string) error
    20  
    21  	UpdateModelFunc     func(controller, account, model string, details jujuclient.ModelDetails) error
    22  	SetCurrentModelFunc func(controller, account, model string) error
    23  	RemoveModelFunc     func(controller, account, model string) error
    24  	AllModelsFunc       func(controller, account string) (map[string]jujuclient.ModelDetails, error)
    25  	CurrentModelFunc    func(controller, account string) (string, error)
    26  	ModelByNameFunc     func(controller, account, model string) (*jujuclient.ModelDetails, error)
    27  
    28  	UpdateAccountFunc     func(controllerName, accountName string, details jujuclient.AccountDetails) error
    29  	SetCurrentAccountFunc func(controllerName, accountName string) error
    30  	AllAccountsFunc       func(controllerName string) (map[string]jujuclient.AccountDetails, error)
    31  	CurrentAccountFunc    func(controllerName string) (string, error)
    32  	AccountByNameFunc     func(controllerName, accountName string) (*jujuclient.AccountDetails, error)
    33  	RemoveAccountFunc     func(controllerName, accountName string) error
    34  
    35  	CredentialForCloudFunc func(string) (*cloud.CloudCredential, error)
    36  	AllCredentialsFunc     func() (map[string]cloud.CloudCredential, error)
    37  	UpdateCredentialFunc   func(cloudName string, details cloud.CloudCredential) error
    38  
    39  	BootstrapConfigForControllerFunc func(controllerName string) (*jujuclient.BootstrapConfig, error)
    40  	UpdateBootstrapConfigFunc        func(controllerName string, cfg jujuclient.BootstrapConfig) error
    41  }
    42  
    43  func NewStubStore() *StubStore {
    44  	result := &StubStore{
    45  		Stub: &testing.Stub{},
    46  	}
    47  	result.AllControllersFunc = func() (map[string]jujuclient.ControllerDetails, error) {
    48  		return nil, result.Stub.NextErr()
    49  	}
    50  	result.ControllerByNameFunc = func(name string) (*jujuclient.ControllerDetails, error) {
    51  		return nil, result.Stub.NextErr()
    52  	}
    53  	result.UpdateControllerFunc = func(name string, one jujuclient.ControllerDetails) error {
    54  		return result.Stub.NextErr()
    55  	}
    56  	result.RemoveControllerFunc = func(name string) error {
    57  		return result.Stub.NextErr()
    58  	}
    59  
    60  	result.UpdateModelFunc = func(controller, account, model string, details jujuclient.ModelDetails) error {
    61  		return result.Stub.NextErr()
    62  	}
    63  	result.SetCurrentModelFunc = func(controller, account, model string) error {
    64  		return result.Stub.NextErr()
    65  	}
    66  	result.RemoveModelFunc = func(controller, account, model string) error {
    67  		return result.Stub.NextErr()
    68  	}
    69  	result.AllModelsFunc = func(controller, account string) (map[string]jujuclient.ModelDetails, error) {
    70  		return nil, result.Stub.NextErr()
    71  	}
    72  	result.CurrentModelFunc = func(controller, account string) (string, error) {
    73  		return "", result.Stub.NextErr()
    74  	}
    75  	result.ModelByNameFunc = func(controller, account, model string) (*jujuclient.ModelDetails, error) {
    76  		return nil, result.Stub.NextErr()
    77  	}
    78  
    79  	result.UpdateAccountFunc = func(controllerName, accountName string, details jujuclient.AccountDetails) error {
    80  		return result.Stub.NextErr()
    81  	}
    82  	result.SetCurrentAccountFunc = func(controllerName, accountName string) error {
    83  		return result.Stub.NextErr()
    84  	}
    85  	result.AllAccountsFunc = func(controllerName string) (map[string]jujuclient.AccountDetails, error) {
    86  		return nil, result.Stub.NextErr()
    87  	}
    88  	result.CurrentAccountFunc = func(controllerName string) (string, error) {
    89  		return "", result.Stub.NextErr()
    90  	}
    91  	result.AccountByNameFunc = func(controllerName, accountName string) (*jujuclient.AccountDetails, error) {
    92  		return nil, result.Stub.NextErr()
    93  	}
    94  	result.RemoveAccountFunc = func(controllerName, accountName string) error {
    95  		return result.Stub.NextErr()
    96  	}
    97  
    98  	result.CredentialForCloudFunc = func(string) (*cloud.CloudCredential, error) {
    99  		return nil, result.Stub.NextErr()
   100  	}
   101  	result.AllCredentialsFunc = func() (map[string]cloud.CloudCredential, error) {
   102  		return nil, result.Stub.NextErr()
   103  	}
   104  	result.UpdateCredentialFunc = func(cloudName string, details cloud.CloudCredential) error {
   105  		return result.Stub.NextErr()
   106  	}
   107  
   108  	result.BootstrapConfigForControllerFunc = func(controllerName string) (*jujuclient.BootstrapConfig, error) {
   109  		return nil, result.Stub.NextErr()
   110  	}
   111  	result.UpdateBootstrapConfigFunc = func(controllerName string, cfg jujuclient.BootstrapConfig) error {
   112  		return result.Stub.NextErr()
   113  	}
   114  	return result
   115  }
   116  
   117  // WrapClientStore wraps a ClientStore with a StubStore, where each method calls
   118  // through to the wrapped store. This can be used to override specific
   119  // methods, or just to check which calls have been made.
   120  func WrapClientStore(underlying jujuclient.ClientStore) *StubStore {
   121  	stub := NewStubStore()
   122  	stub.AllControllersFunc = underlying.AllControllers
   123  	stub.ControllerByNameFunc = underlying.ControllerByName
   124  	stub.UpdateControllerFunc = underlying.UpdateController
   125  	stub.RemoveControllerFunc = underlying.RemoveController
   126  	stub.UpdateModelFunc = underlying.UpdateModel
   127  	stub.SetCurrentModelFunc = underlying.SetCurrentModel
   128  	stub.RemoveModelFunc = underlying.RemoveModel
   129  	stub.AllModelsFunc = underlying.AllModels
   130  	stub.CurrentModelFunc = underlying.CurrentModel
   131  	stub.ModelByNameFunc = underlying.ModelByName
   132  	stub.UpdateAccountFunc = underlying.UpdateAccount
   133  	stub.SetCurrentAccountFunc = underlying.SetCurrentAccount
   134  	stub.AllAccountsFunc = underlying.AllAccounts
   135  	stub.CurrentAccountFunc = underlying.CurrentAccount
   136  	stub.AccountByNameFunc = underlying.AccountByName
   137  	stub.RemoveAccountFunc = underlying.RemoveAccount
   138  	stub.BootstrapConfigForControllerFunc = underlying.BootstrapConfigForController
   139  	stub.UpdateBootstrapConfigFunc = underlying.UpdateBootstrapConfig
   140  	return stub
   141  }
   142  
   143  // AllControllers implements ControllersGetter.AllControllers
   144  func (c *StubStore) AllControllers() (map[string]jujuclient.ControllerDetails, error) {
   145  	c.MethodCall(c, "AllControllers")
   146  	return c.AllControllersFunc()
   147  }
   148  
   149  // ControllerByName implements ControllersGetter.ControllerByName
   150  func (c *StubStore) ControllerByName(name string) (*jujuclient.ControllerDetails, error) {
   151  	c.MethodCall(c, "ControllerByName", name)
   152  	return c.ControllerByNameFunc(name)
   153  }
   154  
   155  // UpdateController implements ControllersUpdater.UpdateController
   156  func (c *StubStore) UpdateController(name string, one jujuclient.ControllerDetails) error {
   157  	c.MethodCall(c, "UpdateController", name, one)
   158  	return c.UpdateControllerFunc(name, one)
   159  }
   160  
   161  // RemoveController implements ControllersRemover.RemoveController
   162  func (c *StubStore) RemoveController(name string) error {
   163  	c.MethodCall(c, "RemoveController", name)
   164  	return c.RemoveControllerFunc(name)
   165  }
   166  
   167  // UpdateModel implements ModelUpdater.
   168  func (c *StubStore) UpdateModel(controller, account, model string, details jujuclient.ModelDetails) error {
   169  	c.MethodCall(c, "UpdateModel", controller, account, model, details)
   170  	return c.UpdateModelFunc(controller, account, model, details)
   171  }
   172  
   173  // SetCurrentModel implements ModelUpdater.
   174  func (c *StubStore) SetCurrentModel(controller, account, model string) error {
   175  	c.MethodCall(c, "SetCurrentModel", controller, account, model)
   176  	return c.SetCurrentModelFunc(controller, account, model)
   177  }
   178  
   179  // RemoveModel implements ModelRemover.
   180  func (c *StubStore) RemoveModel(controller, account, model string) error {
   181  	c.MethodCall(c, "RemoveModel", controller, account, model)
   182  	return c.RemoveModelFunc(controller, account, model)
   183  }
   184  
   185  // AllModels implements ModelGetter.
   186  func (c *StubStore) AllModels(controller, account string) (map[string]jujuclient.ModelDetails, error) {
   187  	c.MethodCall(c, "AllModels", controller, account)
   188  	return c.AllModelsFunc(controller, account)
   189  }
   190  
   191  // CurrentModel implements ModelGetter.
   192  func (c *StubStore) CurrentModel(controller, account string) (string, error) {
   193  	c.MethodCall(c, "CurrentModel", controller, account)
   194  	return c.CurrentModelFunc(controller, account)
   195  }
   196  
   197  // ModelByName implements ModelGetter.
   198  func (c *StubStore) ModelByName(controller, account, model string) (*jujuclient.ModelDetails, error) {
   199  	c.MethodCall(c, "ModelByName", controller, account, model)
   200  	return c.ModelByNameFunc(controller, account, model)
   201  }
   202  
   203  // UpdateAccount implements AccountUpdater.
   204  func (c *StubStore) UpdateAccount(controllerName, accountName string, details jujuclient.AccountDetails) error {
   205  	c.MethodCall(c, "UpdateAccount", controllerName, accountName, details)
   206  	return c.UpdateAccountFunc(controllerName, accountName, details)
   207  }
   208  
   209  // SetCurrentAccount implements AccountUpdater.
   210  func (c *StubStore) SetCurrentAccount(controllerName, accountName string) error {
   211  	c.MethodCall(c, "SetCurrentAccount", controllerName, accountName)
   212  	return c.SetCurrentAccountFunc(controllerName, accountName)
   213  }
   214  
   215  // AllAccounts implements AccountGetter.
   216  func (c *StubStore) AllAccounts(controllerName string) (map[string]jujuclient.AccountDetails, error) {
   217  	c.MethodCall(c, "AllAccounts", controllerName)
   218  	return c.AllAccountsFunc(controllerName)
   219  }
   220  
   221  // CurrentAccount implements AccountGetter.
   222  func (c *StubStore) CurrentAccount(controllerName string) (string, error) {
   223  	c.MethodCall(c, "CurrentAccount", controllerName)
   224  	return c.CurrentAccountFunc(controllerName)
   225  }
   226  
   227  // AccountByName implements AccountGetter.
   228  func (c *StubStore) AccountByName(controllerName, accountName string) (*jujuclient.AccountDetails, error) {
   229  	c.MethodCall(c, "AccountByName", controllerName, accountName)
   230  	return c.AccountByNameFunc(controllerName, accountName)
   231  }
   232  
   233  // RemoveAccount implements AccountRemover.
   234  func (c *StubStore) RemoveAccount(controllerName, accountName string) error {
   235  	c.MethodCall(c, "RemoveAccount", controllerName, accountName)
   236  	return c.RemoveAccountFunc(controllerName, accountName)
   237  }
   238  
   239  // CredentialForCloud implements CredentialsGetter.
   240  func (c *StubStore) CredentialForCloud(cloudName string) (*cloud.CloudCredential, error) {
   241  	c.MethodCall(c, "CredentialForCloud", cloudName)
   242  	return c.CredentialForCloudFunc(cloudName)
   243  }
   244  
   245  // AllCredentials implements CredentialsGetter.
   246  func (c *StubStore) AllCredentials() (map[string]cloud.CloudCredential, error) {
   247  	c.MethodCall(c, "AllCredentials")
   248  	return c.AllCredentialsFunc()
   249  }
   250  
   251  // UpdateCredential implements CredentialsUpdater.
   252  func (c *StubStore) UpdateCredential(cloudName string, details cloud.CloudCredential) error {
   253  	c.MethodCall(c, "UpdateCredential", cloudName, details)
   254  	return c.UpdateCredentialFunc(cloudName, details)
   255  }
   256  
   257  // BootstrapConfigForController implements BootstrapConfigGetter.
   258  func (c *StubStore) BootstrapConfigForController(controllerName string) (*jujuclient.BootstrapConfig, error) {
   259  	c.MethodCall(c, "BootstrapConfigForController", controllerName)
   260  	return c.BootstrapConfigForControllerFunc(controllerName)
   261  }
   262  
   263  // UpdateBootstrapConfig implements BootstrapConfigUpdater.
   264  func (c *StubStore) UpdateBootstrapConfig(controllerName string, cfg jujuclient.BootstrapConfig) error {
   265  	c.MethodCall(c, "UpdateBootstrapConfig", controllerName, cfg)
   266  	return c.UpdateBootstrapConfigFunc(controllerName, cfg)
   267  }