github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/cloudconfig/instancecfg" 10 "github.com/juju/juju/constraints" 11 "github.com/juju/juju/environs" 12 "github.com/juju/juju/environs/config" 13 "github.com/juju/juju/environs/simplestreams" 14 "github.com/juju/juju/environs/storage" 15 "github.com/juju/juju/instance" 16 "github.com/juju/juju/network" 17 "github.com/juju/juju/provider/common" 18 "github.com/juju/juju/tools" 19 ) 20 21 type allInstancesFunc func() ([]instance.Instance, error) 22 type startInstanceFunc func(string, constraints.Value, []string, tools.List, *instancecfg.InstanceConfig) (instance.Instance, *instance.HardwareCharacteristics, []network.InterfaceInfo, error) 23 type stopInstancesFunc func([]instance.Id) error 24 type getToolsSourcesFunc func() ([]simplestreams.DataSource, error) 25 type configFunc func() *config.Config 26 type setConfigFunc func(*config.Config) error 27 28 type mockEnviron struct { 29 storage storage.Storage 30 allInstances allInstancesFunc 31 startInstance startInstanceFunc 32 stopInstances stopInstancesFunc 33 getToolsSources getToolsSourcesFunc 34 config configFunc 35 setConfig setConfigFunc 36 environs.Environ // stub out other methods with panics 37 } 38 39 func (*mockEnviron) SupportedArchitectures() ([]string, error) { 40 return []string{"amd64", "arm64"}, nil 41 } 42 43 func (env *mockEnviron) Storage() storage.Storage { 44 return env.storage 45 } 46 47 func (env *mockEnviron) AllInstances() ([]instance.Instance, error) { 48 return env.allInstances() 49 } 50 func (env *mockEnviron) StartInstance(args environs.StartInstanceParams) (*environs.StartInstanceResult, error) { 51 inst, hw, networkInfo, err := env.startInstance( 52 args.Placement, 53 args.Constraints, 54 nil, 55 args.Tools, 56 args.InstanceConfig, 57 ) 58 if err != nil { 59 return nil, err 60 } 61 return &environs.StartInstanceResult{ 62 Instance: inst, 63 Hardware: hw, 64 NetworkInfo: networkInfo, 65 }, nil 66 } 67 68 func (env *mockEnviron) StopInstances(ids ...instance.Id) error { 69 return env.stopInstances(ids) 70 } 71 72 func (env *mockEnviron) Config() *config.Config { 73 return env.config() 74 } 75 76 func (env *mockEnviron) SetConfig(cfg *config.Config) error { 77 if env.setConfig != nil { 78 return env.setConfig(cfg) 79 } 80 return nil 81 } 82 83 func (env *mockEnviron) GetToolsSources() ([]simplestreams.DataSource, error) { 84 if env.getToolsSources != nil { 85 return env.getToolsSources() 86 } 87 datasource := storage.NewStorageSimpleStreamsDataSource("test cloud storage", env.Storage(), storage.BaseToolsPath, simplestreams.SPECIFIC_CLOUD_DATA, false) 88 return []simplestreams.DataSource{datasource}, nil 89 } 90 91 type availabilityZonesFunc func() ([]common.AvailabilityZone, error) 92 type instanceAvailabilityZoneNamesFunc func([]instance.Id) ([]string, error) 93 94 type mockZonedEnviron struct { 95 mockEnviron 96 availabilityZones availabilityZonesFunc 97 instanceAvailabilityZoneNames instanceAvailabilityZoneNamesFunc 98 } 99 100 func (env *mockZonedEnviron) AvailabilityZones() ([]common.AvailabilityZone, error) { 101 return env.availabilityZones() 102 } 103 104 func (env *mockZonedEnviron) InstanceAvailabilityZoneNames(ids []instance.Id) ([]string, error) { 105 return env.instanceAvailabilityZoneNames(ids) 106 } 107 108 type mockInstance struct { 109 id string 110 addresses []network.Address 111 addressesErr error 112 dnsName string 113 dnsNameErr error 114 instance.Instance // stub out other methods with panics 115 } 116 117 func (inst *mockInstance) Id() instance.Id { 118 return instance.Id(inst.id) 119 } 120 121 func (inst *mockInstance) Addresses() ([]network.Address, error) { 122 return inst.addresses, inst.addressesErr 123 } 124 125 type mockStorage struct { 126 storage.Storage 127 putErr error 128 removeAllErr error 129 } 130 131 func (stor *mockStorage) Put(name string, reader io.Reader, size int64) error { 132 if stor.putErr != nil { 133 return stor.putErr 134 } 135 return stor.Storage.Put(name, reader, size) 136 } 137 138 func (stor *mockStorage) RemoveAll() error { 139 if stor.removeAllErr != nil { 140 return stor.removeAllErr 141 } 142 return stor.Storage.RemoveAll() 143 } 144 145 type mockAvailabilityZone struct { 146 name string 147 available bool 148 } 149 150 func (z *mockAvailabilityZone) Name() string { 151 return z.name 152 } 153 154 func (z *mockAvailabilityZone) Available() bool { 155 return z.available 156 }