github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/resources/testing/resource.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"io"
     8  	"strings"
     9  	"time"
    10  
    11  	charmresource "github.com/juju/charm/v12/resource"
    12  	"github.com/juju/testing"
    13  	jc "github.com/juju/testing/checkers"
    14  	"github.com/juju/testing/filetesting"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/core/resources"
    18  )
    19  
    20  type newCharmResourceFunc func(c *gc.C, name, content string) charmresource.Resource
    21  
    22  // NewResource produces full resource info for the given name and
    23  // content. The origin is set set to "upload". A reader is also returned
    24  // which contains the content.
    25  func NewResource(c *gc.C, stub *testing.Stub, name, applicationID, content string) resources.Opened {
    26  	username := "a-user"
    27  	return resources.Opened{
    28  		Resource:   newResource(c, name, applicationID, username, content, NewCharmResource),
    29  		ReadCloser: newStubReadCloser(stub, content),
    30  	}
    31  }
    32  
    33  // NewDockerResource produces full resource info for the given name and
    34  // content. The origin is set set to "upload" (via resource created by  NewCharmDockerResource).
    35  // A reader is also returned which contains the content.
    36  func NewDockerResource(c *gc.C, stub *testing.Stub, name, applicationID, content string) resources.Opened {
    37  	username := "a-user"
    38  	return resources.Opened{
    39  		Resource:   newResource(c, name, applicationID, username, content, NewCharmDockerResource),
    40  		ReadCloser: newStubReadCloser(stub, content),
    41  	}
    42  }
    43  
    44  // NewCharmResource produces basic resource info for the given name
    45  // and content. The origin is set set to "upload".
    46  func NewCharmResource(c *gc.C, name, content string) charmresource.Resource {
    47  	fp, err := charmresource.GenerateFingerprint(strings.NewReader(content))
    48  	c.Assert(err, jc.ErrorIsNil)
    49  	res := charmresource.Resource{
    50  		Meta: charmresource.Meta{
    51  			Name:        name,
    52  			Type:        charmresource.TypeFile,
    53  			Path:        name + ".tgz",
    54  			Description: name + " description",
    55  		},
    56  		Origin:      charmresource.OriginUpload,
    57  		Revision:    0,
    58  		Fingerprint: fp,
    59  		Size:        int64(len(content)),
    60  	}
    61  	err = res.Validate()
    62  	c.Assert(err, jc.ErrorIsNil)
    63  
    64  	return res
    65  }
    66  
    67  // NewCharmDockerResource produces basic docker resource info for the given name
    68  // and content. The origin is set set to "upload".
    69  func NewCharmDockerResource(c *gc.C, name, content string) charmresource.Resource {
    70  	res := charmresource.Resource{
    71  		Meta: charmresource.Meta{
    72  			Name:        name,
    73  			Type:        charmresource.TypeContainerImage,
    74  			Description: name + " description",
    75  		},
    76  		Origin:      charmresource.OriginUpload,
    77  		Revision:    0,
    78  		Fingerprint: charmresource.Fingerprint{},
    79  		Size:        0,
    80  	}
    81  	err := res.Validate()
    82  	c.Assert(err, jc.ErrorIsNil)
    83  
    84  	return res
    85  }
    86  
    87  // NewPlaceholderResource returns resource info for a resource that
    88  // has not been uploaded or pulled from the charm store yet. The origin
    89  // is set to "upload".
    90  func NewPlaceholderResource(c *gc.C, name, applicationID string) resources.Resource {
    91  	res := newResource(c, name, applicationID, "", "", NewCharmResource)
    92  	res.Fingerprint = charmresource.Fingerprint{}
    93  	return res
    94  }
    95  
    96  func newResource(c *gc.C, name, applicationID, username, content string, charmResourceFunc newCharmResourceFunc) resources.Resource {
    97  	var timestamp time.Time
    98  	if username != "" {
    99  		// TODO(perrito666) 2016-05-02 lp:1558657
   100  		timestamp = time.Now().UTC()
   101  	}
   102  	res := resources.Resource{
   103  		Resource:      charmResourceFunc(c, name, content),
   104  		ID:            applicationID + "/" + name,
   105  		PendingID:     "",
   106  		ApplicationID: applicationID,
   107  		Username:      username,
   108  		Timestamp:     timestamp,
   109  	}
   110  	err := res.Validate()
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	return res
   113  }
   114  
   115  type stubReadCloser struct {
   116  	io.Reader
   117  	io.Closer
   118  }
   119  
   120  func newStubReadCloser(stub *testing.Stub, content string) io.ReadCloser {
   121  	return &stubReadCloser{
   122  		Reader: filetesting.NewStubReader(stub, content),
   123  		Closer: &filetesting.StubCloser{
   124  			Stub: stub,
   125  		},
   126  	}
   127  }