~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/testing/storage.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        storage.Storage
 
18
        Map map[string][]byte
 
19
}
 
20
 
 
21
var _ storage.Storage = (*MapStorage)(nil)
 
22
 
 
23
func (s *MapStorage) Get(path string) (r io.ReadCloser, length int64, err error) {
 
24
        data, ok := s.Map[path]
 
25
        if !ok {
 
26
                return nil, -1, errors.NotFoundf("%s", path)
 
27
        }
 
28
        return ioutil.NopCloser(bytes.NewReader(data)), int64(len(data)), nil
 
29
}
 
30
 
 
31
func (s *MapStorage) Put(path string, r io.Reader, length int64) error {
 
32
        if s.Map == nil {
 
33
                s.Map = make(map[string][]byte)
 
34
        }
 
35
        buf := make([]byte, int(length))
 
36
        _, err := io.ReadFull(r, buf)
 
37
        if err != nil {
 
38
                return err
 
39
        }
 
40
        s.Map[path] = buf
 
41
        return nil
 
42
}
 
43
 
 
44
func (s *MapStorage) Remove(path string) error {
 
45
        if _, ok := s.Map[path]; !ok {
 
46
                return errors.NotFoundf("%s", path)
 
47
        }
 
48
        delete(s.Map, path)
 
49
        return nil
 
50
}