github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/resource/workers/charmstorepoller_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package workers_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/charm.v6-unstable"
    14  	charmresource "gopkg.in/juju/charm.v6-unstable/resource"
    15  	"gopkg.in/juju/names.v2"
    16  
    17  	"github.com/juju/juju/charmstore"
    18  	"github.com/juju/juju/resource/resourcetesting"
    19  	"github.com/juju/juju/resource/workers"
    20  )
    21  
    22  type LatestCharmHandlerSuite struct {
    23  	testing.IsolationSuite
    24  
    25  	stub  *testing.Stub
    26  	store *stubDataStore
    27  }
    28  
    29  var _ = gc.Suite(&LatestCharmHandlerSuite{})
    30  
    31  func (s *LatestCharmHandlerSuite) SetUpTest(c *gc.C) {
    32  	s.IsolationSuite.SetUpTest(c)
    33  
    34  	s.stub = &testing.Stub{}
    35  	s.store = &stubDataStore{Stub: s.stub}
    36  }
    37  
    38  func (s *LatestCharmHandlerSuite) TestSuccess(c *gc.C) {
    39  	applicationID := names.NewApplicationTag("a-application")
    40  	info := charmstore.CharmInfo{
    41  		OriginalURL:    &charm.URL{},
    42  		Timestamp:      time.Now().UTC(),
    43  		LatestRevision: 2,
    44  		LatestResources: []charmresource.Resource{
    45  			resourcetesting.NewCharmResource(c, "spam", "<some data>"),
    46  		},
    47  	}
    48  	handler := workers.NewLatestCharmHandler(s.store)
    49  
    50  	err := handler.HandleLatest(applicationID, info)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  
    53  	s.stub.CheckCallNames(c, "SetCharmStoreResources")
    54  	s.stub.CheckCall(c, 0, "SetCharmStoreResources", "a-application", info.LatestResources, info.Timestamp)
    55  }
    56  
    57  type stubDataStore struct {
    58  	*testing.Stub
    59  }
    60  
    61  func (s *stubDataStore) SetCharmStoreResources(applicationID string, info []charmresource.Resource, lastPolled time.Time) error {
    62  	s.AddCall("SetCharmStoreResources", applicationID, info, lastPolled)
    63  	if err := s.NextErr(); err != nil {
    64  		return errors.Trace(err)
    65  	}
    66  
    67  	return nil
    68  }