github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/state/autocertcache_test.go (about)

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