github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/state/testing/storage.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "bytes" 8 "io" 9 "io/ioutil" 10 11 "github.com/juju/errors" 12 13 "github.com/juju/juju/state/storage" 14 ) 15 16 type MapStorage struct { 17 Map map[string][]byte 18 } 19 20 var _ storage.Storage = (*MapStorage)(nil) 21 22 func (s *MapStorage) Get(path string) (r io.ReadCloser, length int64, err error) { 23 data, ok := s.Map[path] 24 if !ok { 25 return nil, -1, errors.NotFoundf("%s", path) 26 } 27 return ioutil.NopCloser(bytes.NewReader(data)), int64(len(data)), nil 28 } 29 30 func (s *MapStorage) Put(path string, r io.Reader, length int64) error { 31 if s.Map == nil { 32 s.Map = make(map[string][]byte) 33 } 34 buf := make([]byte, int(length)) 35 _, err := io.ReadFull(r, buf) 36 if err != nil { 37 return err 38 } 39 s.Map[path] = buf 40 return nil 41 } 42 43 func (s *MapStorage) Remove(path string) error { 44 if _, ok := s.Map[path]; !ok { 45 return errors.NotFoundf("%s", path) 46 } 47 delete(s.Map, path) 48 return nil 49 }