gopkg.in/juju/charm.v6-unstable@v6.0.0-20171026192109-50d0c219b496/resource/resource_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the LGPLv3, see LICENCE file for details. 3 4 package resource_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "gopkg.in/juju/charm.v6-unstable/resource" 12 ) 13 14 var fingerprint = []byte("123456789012345678901234567890123456789012345678") 15 16 var _ = gc.Suite(&ResourceSuite{}) 17 18 type ResourceSuite struct{} 19 20 func (s *ResourceSuite) TestValidateFull(c *gc.C) { 21 fp, err := resource.NewFingerprint(fingerprint) 22 c.Assert(err, jc.ErrorIsNil) 23 res := resource.Resource{ 24 Meta: resource.Meta{ 25 Name: "my-resource", 26 Type: resource.TypeFile, 27 Path: "filename.tgz", 28 Description: "One line that is useful when operators need to push it.", 29 }, 30 Origin: resource.OriginStore, 31 Revision: 1, 32 Fingerprint: fp, 33 Size: 1, 34 } 35 err = res.Validate() 36 37 c.Check(err, jc.ErrorIsNil) 38 } 39 40 func (s *ResourceSuite) TestValidateZeroValue(c *gc.C) { 41 var res resource.Resource 42 err := res.Validate() 43 44 c.Check(err, jc.Satisfies, errors.IsNotValid) 45 } 46 47 func (s *ResourceSuite) TestValidateBadMetadata(c *gc.C) { 48 var meta resource.Meta 49 c.Assert(meta.Validate(), gc.NotNil) 50 51 fp, err := resource.NewFingerprint(fingerprint) 52 c.Assert(err, jc.ErrorIsNil) 53 res := resource.Resource{ 54 Meta: meta, 55 Origin: resource.OriginStore, 56 Revision: 1, 57 Fingerprint: fp, 58 } 59 err = res.Validate() 60 61 c.Check(err, jc.Satisfies, errors.IsNotValid) 62 c.Check(err, gc.ErrorMatches, `bad metadata: .*`) 63 } 64 65 func (s *ResourceSuite) TestValidateBadOrigin(c *gc.C) { 66 var origin resource.Origin 67 c.Assert(origin.Validate(), gc.NotNil) 68 fp, err := resource.NewFingerprint(fingerprint) 69 c.Assert(err, jc.ErrorIsNil) 70 res := resource.Resource{ 71 Meta: resource.Meta{ 72 Name: "my-resource", 73 Type: resource.TypeFile, 74 Path: "filename.tgz", 75 Description: "One line that is useful when operators need to push it.", 76 }, 77 Origin: origin, 78 Revision: 1, 79 Fingerprint: fp, 80 } 81 err = res.Validate() 82 83 c.Check(err, jc.Satisfies, errors.IsNotValid) 84 c.Check(err, gc.ErrorMatches, `bad origin: .*`) 85 } 86 87 func (s *ResourceSuite) TestValidateUploadNegativeRevision(c *gc.C) { 88 fp, err := resource.NewFingerprint(fingerprint) 89 c.Assert(err, jc.ErrorIsNil) 90 res := resource.Resource{ 91 Meta: resource.Meta{ 92 Name: "my-resource", 93 Type: resource.TypeFile, 94 Path: "filename.tgz", 95 Description: "One line that is useful when operators need to push it.", 96 }, 97 Origin: resource.OriginUpload, 98 Revision: -1, 99 Fingerprint: fp, 100 Size: 10, 101 } 102 err = res.Validate() 103 104 c.Check(err, jc.ErrorIsNil) 105 } 106 107 func (s *ResourceSuite) TestValidateStoreNegativeRevisionNoFile(c *gc.C) { 108 res := resource.Resource{ 109 Meta: resource.Meta{ 110 Name: "my-resource", 111 Type: resource.TypeFile, 112 Path: "filename.tgz", 113 Description: "One line that is useful when operators need to push it.", 114 }, 115 Origin: resource.OriginStore, 116 Revision: -1, 117 } 118 err := res.Validate() 119 120 c.Check(err, jc.ErrorIsNil) 121 } 122 123 func (s *ResourceSuite) TestValidateBadRevision(c *gc.C) { 124 fp, err := resource.NewFingerprint(fingerprint) 125 c.Assert(err, jc.ErrorIsNil) 126 res := resource.Resource{ 127 Meta: resource.Meta{ 128 Name: "my-resource", 129 Type: resource.TypeFile, 130 Path: "filename.tgz", 131 Description: "One line that is useful when operators need to push it.", 132 }, 133 Origin: resource.OriginStore, 134 Revision: -1, 135 Fingerprint: fp, 136 } 137 err = res.Validate() 138 139 c.Check(err, jc.Satisfies, errors.IsNotValid) 140 c.Check(err, gc.ErrorMatches, `bad revision: must be non-negative, got -1`) 141 } 142 143 func (s *ResourceSuite) TestValidateZeroValueFingerprint(c *gc.C) { 144 var fp resource.Fingerprint 145 c.Assert(fp.Validate(), gc.NotNil) 146 147 res := resource.Resource{ 148 Meta: resource.Meta{ 149 Name: "my-resource", 150 Type: resource.TypeFile, 151 Path: "filename.tgz", 152 Description: "One line that is useful when operators need to push it.", 153 }, 154 Origin: resource.OriginStore, 155 Revision: 1, 156 Fingerprint: fp, 157 } 158 err := res.Validate() 159 160 c.Check(err, jc.ErrorIsNil) 161 } 162 163 func (s *ResourceSuite) TestValidateMissingFingerprint(c *gc.C) { 164 var fp resource.Fingerprint 165 c.Assert(fp.Validate(), gc.NotNil) 166 167 res := resource.Resource{ 168 Meta: resource.Meta{ 169 Name: "my-resource", 170 Type: resource.TypeFile, 171 Path: "filename.tgz", 172 Description: "One line that is useful when operators need to push it.", 173 }, 174 Origin: resource.OriginStore, 175 Revision: 1, 176 Fingerprint: fp, 177 Size: 10, 178 } 179 err := res.Validate() 180 181 c.Check(err, jc.Satisfies, errors.IsNotValid) 182 c.Check(err, gc.ErrorMatches, `bad file info: missing fingerprint`) 183 } 184 185 func (s *ResourceSuite) TestValidateBadSize(c *gc.C) { 186 fp, err := resource.NewFingerprint(fingerprint) 187 c.Assert(err, jc.ErrorIsNil) 188 res := resource.Resource{ 189 Meta: resource.Meta{ 190 Name: "my-resource", 191 Type: resource.TypeFile, 192 Path: "filename.tgz", 193 Description: "One line that is useful when operators need to push it.", 194 }, 195 Origin: resource.OriginStore, 196 Revision: 1, 197 Fingerprint: fp, 198 Size: -1, 199 } 200 err = res.Validate() 201 202 c.Check(err, jc.Satisfies, errors.IsNotValid) 203 c.Check(err, gc.ErrorMatches, `bad file info: negative size`) 204 }