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