github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/provider/common/mock_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/juju/juju/constraints"
    10  	"github.com/juju/juju/environs"
    11  	"github.com/juju/juju/environs/cloudinit"
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/environs/network"
    14  	"github.com/juju/juju/environs/simplestreams"
    15  	"github.com/juju/juju/environs/storage"
    16  	"github.com/juju/juju/instance"
    17  	"github.com/juju/juju/tools"
    18  )
    19  
    20  type allInstancesFunc func() ([]instance.Instance, error)
    21  type startInstanceFunc func(string, constraints.Value, []string, tools.List, *cloudinit.MachineConfig) (instance.Instance, *instance.HardwareCharacteristics, []network.Info, error)
    22  type stopInstancesFunc func([]instance.Id) error
    23  type getToolsSourcesFunc func() ([]simplestreams.DataSource, error)
    24  type configFunc func() *config.Config
    25  type setConfigFunc func(*config.Config) error
    26  
    27  type mockEnviron struct {
    28  	storage          storage.Storage
    29  	allInstances     allInstancesFunc
    30  	startInstance    startInstanceFunc
    31  	stopInstances    stopInstancesFunc
    32  	getToolsSources  getToolsSourcesFunc
    33  	config           configFunc
    34  	setConfig        setConfigFunc
    35  	environs.Environ // stub out other methods with panics
    36  }
    37  
    38  func (*mockEnviron) Name() string {
    39  	return "mock environment"
    40  }
    41  
    42  func (*mockEnviron) SupportedArchitectures() ([]string, error) {
    43  	return []string{"amd64", "arm64"}, nil
    44  }
    45  
    46  func (env *mockEnviron) Storage() storage.Storage {
    47  	return env.storage
    48  }
    49  
    50  func (env *mockEnviron) AllInstances() ([]instance.Instance, error) {
    51  	return env.allInstances()
    52  }
    53  func (env *mockEnviron) StartInstance(args environs.StartInstanceParams) (instance.Instance, *instance.HardwareCharacteristics, []network.Info, error) {
    54  	return env.startInstance(
    55  		args.Placement,
    56  		args.Constraints,
    57  		args.MachineConfig.Networks,
    58  		args.Tools,
    59  		args.MachineConfig)
    60  }
    61  
    62  func (env *mockEnviron) StopInstances(ids ...instance.Id) error {
    63  	return env.stopInstances(ids)
    64  }
    65  
    66  func (env *mockEnviron) Config() *config.Config {
    67  	return env.config()
    68  }
    69  
    70  func (env *mockEnviron) SetConfig(cfg *config.Config) error {
    71  	if env.setConfig != nil {
    72  		return env.setConfig(cfg)
    73  	}
    74  	return nil
    75  }
    76  
    77  func (env *mockEnviron) GetToolsSources() ([]simplestreams.DataSource, error) {
    78  	if env.getToolsSources != nil {
    79  		return env.getToolsSources()
    80  	}
    81  	datasource := storage.NewStorageSimpleStreamsDataSource("test cloud storage", env.Storage(), storage.BaseToolsPath)
    82  	return []simplestreams.DataSource{datasource}, nil
    83  }
    84  
    85  func (env *mockEnviron) GetImageSources() ([]simplestreams.DataSource, error) {
    86  	datasource := storage.NewStorageSimpleStreamsDataSource("test cloud storage", env.Storage(), storage.BaseImagesPath)
    87  	return []simplestreams.DataSource{datasource}, nil
    88  }
    89  
    90  type mockInstance struct {
    91  	id                string
    92  	addresses         []instance.Address
    93  	addressesErr      error
    94  	dnsName           string
    95  	dnsNameErr        error
    96  	instance.Instance // stub out other methods with panics
    97  }
    98  
    99  func (inst *mockInstance) Id() instance.Id {
   100  	return instance.Id(inst.id)
   101  }
   102  
   103  func (inst *mockInstance) Addresses() ([]instance.Address, error) {
   104  	return inst.addresses, inst.addressesErr
   105  }
   106  
   107  type mockStorage struct {
   108  	storage.Storage
   109  	putErr       error
   110  	removeAllErr error
   111  }
   112  
   113  func (stor *mockStorage) Put(name string, reader io.Reader, size int64) error {
   114  	if stor.putErr != nil {
   115  		return stor.putErr
   116  	}
   117  	return stor.Storage.Put(name, reader, size)
   118  }
   119  
   120  func (stor *mockStorage) RemoveAll() error {
   121  	if stor.removeAllErr != nil {
   122  		return stor.removeAllErr
   123  	}
   124  	return stor.Storage.RemoveAll()
   125  }