forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocertcache_test.go
94 lines (72 loc) · 2.4 KB
/
autocertcache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
jc "github.com/juju/testing/checkers"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/net/context"
gc "gopkg.in/check.v1"
statetesting "github.com/juju/juju/state/testing"
)
type autocertCacheSuite struct {
statetesting.StateSuite
}
var _ = gc.Suite(&autocertCacheSuite{})
func (s *autocertCacheSuite) TestCachePutGet(c *gc.C) {
ctx := context.Background()
cache := s.State.AutocertCache()
err := cache.Put(ctx, "a", []byte("aval"))
c.Assert(err, jc.ErrorIsNil)
err = cache.Put(ctx, "b", []byte("bval"))
c.Assert(err, jc.ErrorIsNil)
// Check that we can get the existing entries.
data, err := cache.Get(ctx, "a")
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(data), gc.Equals, "aval")
data, err = cache.Get(ctx, "b")
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(data), gc.Equals, "bval")
}
func (s *autocertCacheSuite) TestGetNonexistentEntry(c *gc.C) {
ctx := context.Background()
cache := s.State.AutocertCache()
// Getting a non-existent entry must return ErrCacheMiss.
data, err := cache.Get(ctx, "c")
c.Assert(err, gc.Equals, autocert.ErrCacheMiss)
c.Assert(data, gc.IsNil)
}
func (s *autocertCacheSuite) TestDelete(c *gc.C) {
ctx := context.Background()
cache := s.State.AutocertCache()
err := cache.Put(ctx, "a", []byte("aval"))
c.Assert(err, jc.ErrorIsNil)
err = cache.Put(ctx, "b", []byte("bval"))
c.Assert(err, jc.ErrorIsNil)
// Check that we can delete an entry.
err = cache.Delete(ctx, "b")
c.Assert(err, jc.ErrorIsNil)
data, err := cache.Get(ctx, "b")
c.Assert(err, gc.Equals, autocert.ErrCacheMiss)
c.Assert(data, gc.IsNil)
// Check that the non-deleted entry is still there.
data, err = cache.Get(ctx, "a")
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(data), gc.Equals, "aval")
}
func (s *autocertCacheSuite) TestDeleteNonexistentEntry(c *gc.C) {
ctx := context.Background()
cache := s.State.AutocertCache()
err := cache.Delete(ctx, "a")
c.Assert(err, jc.ErrorIsNil)
}
func (s *autocertCacheSuite) TestPutExistingEntry(c *gc.C) {
ctx := context.Background()
cache := s.State.AutocertCache()
err := cache.Put(ctx, "a", []byte("aval"))
c.Assert(err, jc.ErrorIsNil)
err = cache.Put(ctx, "a", []byte("aval2"))
c.Assert(err, jc.ErrorIsNil)
data, err := cache.Get(ctx, "a")
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(data), gc.Equals, "aval2")
}