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