~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/blobstore.v2/gridfs_test.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 LGPLv3, see LICENCE file for details.
 
3
 
 
4
package blobstore_test
 
5
 
 
6
import (
 
7
        "crypto/md5"
 
8
        "encoding/hex"
 
9
        "io/ioutil"
 
10
        "strings"
 
11
 
 
12
        "github.com/juju/testing"
 
13
        gc "gopkg.in/check.v1"
 
14
        "gopkg.in/juju/blobstore.v2"
 
15
)
 
16
 
 
17
var _ = gc.Suite(&gridfsSuite{})
 
18
 
 
19
type gridfsSuite struct {
 
20
        testing.IsolationSuite
 
21
        testing.MgoSuite
 
22
        stor blobstore.ResourceStorage
 
23
}
 
24
 
 
25
func (s *gridfsSuite) SetUpSuite(c *gc.C) {
 
26
        s.IsolationSuite.SetUpSuite(c)
 
27
        s.MgoSuite.SetUpSuite(c)
 
28
}
 
29
 
 
30
func (s *gridfsSuite) TearDownSuite(c *gc.C) {
 
31
        s.MgoSuite.TearDownSuite(c)
 
32
        s.IsolationSuite.TearDownSuite(c)
 
33
}
 
34
 
 
35
func (s *gridfsSuite) SetUpTest(c *gc.C) {
 
36
        s.IsolationSuite.SetUpTest(c)
 
37
        s.MgoSuite.SetUpTest(c)
 
38
        s.stor = blobstore.NewGridFS("juju", "test", s.Session)
 
39
}
 
40
 
 
41
func (s *gridfsSuite) TearDownTest(c *gc.C) {
 
42
        s.MgoSuite.TearDownTest(c)
 
43
        s.IsolationSuite.TearDownTest(c)
 
44
}
 
45
 
 
46
func assertPut(c *gc.C, stor blobstore.ResourceStorage, path, data string) {
 
47
        r := strings.NewReader(data)
 
48
        checksum, err := stor.Put(path, r, int64(len(data)))
 
49
        c.Assert(err, gc.IsNil)
 
50
        md5Hash := md5.New()
 
51
        _, err = md5Hash.Write([]byte(data))
 
52
        c.Assert(err, gc.IsNil)
 
53
        c.Assert(checksum, gc.Equals, hex.EncodeToString(md5Hash.Sum(nil)))
 
54
        assertGet(c, stor, path, data)
 
55
}
 
56
 
 
57
func (s *gridfsSuite) TestPut(c *gc.C) {
 
58
        assertPut(c, s.stor, "/path/to/file", "hello world")
 
59
}
 
60
 
 
61
func (s *gridfsSuite) TestPutSameFileOverwrites(c *gc.C) {
 
62
        assertPut(c, s.stor, "/path/to/file", "hello world")
 
63
        assertPut(c, s.stor, "/path/to/file", "hello again")
 
64
}
 
65
 
 
66
func assertGet(c *gc.C, stor blobstore.ResourceStorage, path, expected string) {
 
67
        r, err := stor.Get(path)
 
68
        c.Assert(err, gc.IsNil)
 
69
        defer r.Close()
 
70
        data, err := ioutil.ReadAll(r)
 
71
        c.Assert(err, gc.IsNil)
 
72
        c.Assert(data, gc.DeepEquals, []byte(expected))
 
73
}
 
74
 
 
75
func (s *gridfsSuite) TestGetNonExistent(c *gc.C) {
 
76
        _, err := s.stor.Get("missing")
 
77
        c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "missing": not found`)
 
78
}
 
79
 
 
80
func (s *gridfsSuite) TestGet(c *gc.C) {
 
81
        data := "hello world"
 
82
        r := strings.NewReader(data)
 
83
        _, err := s.stor.Put("/path/to/file", r, int64(len(data)))
 
84
        c.Assert(err, gc.IsNil)
 
85
        assertGet(c, s.stor, "/path/to/file", data)
 
86
}
 
87
 
 
88
func (s *gridfsSuite) TestRemove(c *gc.C) {
 
89
        path := "/path/to/file"
 
90
        assertPut(c, s.stor, path, "hello world")
 
91
        err := s.stor.Remove(path)
 
92
        c.Assert(err, gc.IsNil)
 
93
        _, err = s.stor.Get(path)
 
94
        c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "/path/to/file": not found`)
 
95
}
 
96
 
 
97
func (s *gridfsSuite) TestRemoveNonExistent(c *gc.C) {
 
98
        err := s.stor.Remove("/path/to/file")
 
99
        c.Assert(err, gc.IsNil)
 
100
}
 
101
 
 
102
func (s *gridfsSuite) TestNamespaceSeparation(c *gc.C) {
 
103
        anotherStor := blobstore.NewGridFS("juju", "another", s.Session)
 
104
        path := "/path/to/file"
 
105
        assertPut(c, anotherStor, path, "hello world")
 
106
        _, err := s.stor.Get(path)
 
107
        c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "/path/to/file": not found`)
 
108
}
 
109
 
 
110
func (s *gridfsSuite) TestNamespaceSeparationRemove(c *gc.C) {
 
111
        anotherStor := blobstore.NewGridFS("juju", "another", s.Session)
 
112
        path := "/path/to/file"
 
113
        assertPut(c, s.stor, path, "hello world")
 
114
        assertPut(c, anotherStor, path, "hello again")
 
115
        err := s.stor.Remove(path)
 
116
        c.Assert(err, gc.IsNil)
 
117
        assertGet(c, anotherStor, "/path/to/file", "hello again")
 
118
}