github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/resources_staged_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  	"bytes"
     8  	"crypto/sha512"
     9  	"fmt"
    10  
    11  	charmresource "github.com/juju/charm/v12/resource"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/state"
    16  	"github.com/juju/juju/testing/factory"
    17  )
    18  
    19  var _ = gc.Suite(&StagedResourceSuite{})
    20  
    21  type StagedResourceSuite struct {
    22  	ConnSuite
    23  }
    24  
    25  func (s *StagedResourceSuite) assertActivate(c *gc.C, inc state.IncrementCharmModifiedVersionType) {
    26  	ch := s.ConnSuite.AddTestingCharm(c, "starsay")
    27  	app := s.Factory.MakeApplication(c, &factory.ApplicationParams{
    28  		Name:  "starsay",
    29  		Charm: ch,
    30  	})
    31  
    32  	res := s.State.Resources()
    33  	spam := newResourceFromCharm(ch, "store-resource")
    34  
    35  	data := "spamspamspam"
    36  	spam.Size = int64(len(data))
    37  	sha384hash := sha512.New384()
    38  	sha384hash.Write([]byte(data))
    39  	fp := fmt.Sprintf("%x", sha384hash.Sum(nil))
    40  	var err error
    41  	spam.Fingerprint, err = charmresource.ParseFingerprint(fp)
    42  	c.Assert(err, jc.ErrorIsNil)
    43  
    44  	_, err = res.SetResource("starsay", spam.Username, spam.Resource, bytes.NewBufferString(data), inc)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  
    47  	staged := state.StagedResourceForTest(c, s.State, spam)
    48  	err = staged.Activate(inc)
    49  	c.Assert(err, jc.ErrorIsNil)
    50  
    51  	_, err = res.GetResource("starsay", "store-resource")
    52  	c.Assert(err, jc.ErrorIsNil)
    53  
    54  	err = app.Refresh()
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	if inc {
    57  		c.Assert(app.CharmModifiedVersion(), gc.Equals, 2)
    58  	} else {
    59  		c.Assert(app.CharmModifiedVersion(), gc.Equals, 0)
    60  	}
    61  }
    62  
    63  func (s *StagedResourceSuite) TestActivateIncrement(c *gc.C) {
    64  	s.assertActivate(c, state.IncrementCharmModifiedVersion)
    65  }
    66  
    67  func (s *StagedResourceSuite) TestActivateNoIncrement(c *gc.C) {
    68  	s.assertActivate(c, state.DoNotIncrementCharmModifiedVersion)
    69  }