launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/environs/testing/storage.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "fmt" 8 "io" 9 10 gc "launchpad.net/gocheck" 11 12 "launchpad.net/juju-core/environs" 13 "launchpad.net/juju-core/environs/filestorage" 14 "launchpad.net/juju-core/environs/httpstorage" 15 "launchpad.net/juju-core/environs/storage" 16 "launchpad.net/juju-core/state" 17 ) 18 19 // CreateLocalTestStorage returns the listener, which needs to be closed, and 20 // the storage that is backed by a directory created in the running test's temp 21 // directory. 22 func CreateLocalTestStorage(c *gc.C) (closer io.Closer, stor storage.Storage, dataDir string) { 23 dataDir = c.MkDir() 24 underlying, err := filestorage.NewFileStorageWriter(dataDir, filestorage.UseDefaultTmpDir) 25 c.Assert(err, gc.IsNil) 26 listener, err := httpstorage.Serve("localhost:0", underlying) 27 c.Assert(err, gc.IsNil) 28 stor = httpstorage.Client(listener.Addr().String()) 29 closer = listener 30 return 31 } 32 33 // GetEnvironStorage creates an Environ from the config in state and 34 // returns its storage interface. 35 func GetEnvironStorage(st *state.State) (storage.Storage, error) { 36 envConfig, err := st.EnvironConfig() 37 if err != nil { 38 return nil, fmt.Errorf("cannot get environment config: %v", err) 39 } 40 env, err := environs.New(envConfig) 41 if err != nil { 42 return nil, fmt.Errorf("cannot access environment: %v", err) 43 } 44 return env.Storage(), nil 45 }