github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/resource/formatter_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package resource_test 5 6 import ( 7 "fmt" 8 "strings" 9 "time" 10 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 charmresource "gopkg.in/juju/charm.v6/resource" 15 "gopkg.in/juju/names.v2" 16 17 resourcecmd "github.com/juju/juju/cmd/juju/resource" 18 "github.com/juju/juju/resource" 19 ) 20 21 var _ = gc.Suite(&CharmFormatterSuite{}) 22 23 type CharmFormatterSuite struct { 24 testing.IsolationSuite 25 } 26 27 func (s *CharmFormatterSuite) TestFormatCharmResource(c *gc.C) { 28 res := charmRes(c, "spam", ".tgz", "X", "spamspamspam") 29 res.Revision = 5 30 31 formatted := resourcecmd.FormatCharmResource(res) 32 33 c.Check(formatted, jc.DeepEquals, resourcecmd.FormattedCharmResource{ 34 Name: "spam", 35 Type: "file", 36 Path: "spam.tgz", 37 Description: "X", 38 Revision: 5, 39 Fingerprint: res.Fingerprint.String(), 40 Size: int64(len("spamspamspam")), 41 Origin: "store", 42 }) 43 } 44 45 var _ = gc.Suite(&SvcFormatterSuite{}) 46 47 type SvcFormatterSuite struct { 48 testing.IsolationSuite 49 } 50 51 func (s *SvcFormatterSuite) TestFormatSvcResource(c *gc.C) { 52 fp, err := charmresource.GenerateFingerprint(strings.NewReader("something")) 53 c.Assert(err, jc.ErrorIsNil) 54 r := resource.Resource{ 55 Resource: charmresource.Resource{ 56 Meta: charmresource.Meta{ 57 Name: "website", 58 Description: "your website data", 59 Type: charmresource.TypeFile, 60 Path: "foobar", 61 }, 62 Revision: 5, 63 Origin: charmresource.OriginStore, 64 Fingerprint: fp, 65 Size: 10, 66 }, 67 Username: "Bill User", 68 Timestamp: time.Now().Add(-1 * time.Hour * 24 * 365), 69 ID: "a-application/website", 70 ApplicationID: "a-application", 71 } 72 73 f := resourcecmd.FormatAppResource(r) 74 c.Assert(f, gc.Equals, resourcecmd.FormattedAppResource{ 75 ID: "a-application/website", 76 ApplicationID: "a-application", 77 Name: r.Name, 78 Type: "file", 79 Path: r.Path, 80 Used: true, 81 Revision: fmt.Sprintf("%v", r.Revision), 82 Origin: "store", 83 Fingerprint: fp.String(), 84 Size: 10, 85 Description: r.Description, 86 Timestamp: r.Timestamp, 87 Username: r.Username, 88 CombinedRevision: "5", 89 UsedYesNo: "yes", 90 CombinedOrigin: "charmstore", 91 }) 92 93 } 94 95 func (s *SvcFormatterSuite) TestNotUsed(c *gc.C) { 96 r := resource.Resource{ 97 Timestamp: time.Time{}, 98 } 99 f := resourcecmd.FormatAppResource(r) 100 c.Assert(f.Used, jc.IsFalse) 101 } 102 103 func (s *SvcFormatterSuite) TestUsed(c *gc.C) { 104 r := resource.Resource{ 105 Timestamp: time.Now(), 106 } 107 f := resourcecmd.FormatAppResource(r) 108 c.Assert(f.Used, jc.IsTrue) 109 } 110 111 func (s *SvcFormatterSuite) TestOriginUploadDeployed(c *gc.C) { 112 // represents what we get when we first deploy an application 113 r := resource.Resource{ 114 Resource: charmresource.Resource{ 115 Origin: charmresource.OriginUpload, 116 }, 117 Username: "bill", 118 Timestamp: time.Now(), 119 } 120 f := resourcecmd.FormatAppResource(r) 121 c.Assert(f.CombinedOrigin, gc.Equals, "bill") 122 } 123 124 func (s *SvcFormatterSuite) TestInitialOriginUpload(c *gc.C) { 125 r := resource.Resource{ 126 Resource: charmresource.Resource{ 127 Origin: charmresource.OriginUpload, 128 }, 129 } 130 f := resourcecmd.FormatAppResource(r) 131 c.Assert(f.CombinedOrigin, gc.Equals, "upload") 132 } 133 134 var _ = gc.Suite(&DetailFormatterSuite{}) 135 136 type DetailFormatterSuite struct { 137 testing.IsolationSuite 138 } 139 140 func (s *DetailFormatterSuite) TestFormatDetail(c *gc.C) { 141 fp, err := charmresource.GenerateFingerprint(strings.NewReader("something")) 142 c.Assert(err, jc.ErrorIsNil) 143 144 svc := resource.Resource{ 145 Resource: charmresource.Resource{ 146 Meta: charmresource.Meta{ 147 Name: "website", 148 Description: "your website data", 149 Type: charmresource.TypeFile, 150 Path: "foobar", 151 }, 152 Revision: 5, 153 Origin: charmresource.OriginStore, 154 Fingerprint: fp, 155 Size: 10, 156 }, 157 Username: "Bill User", 158 Timestamp: time.Now().Add(-1 * time.Hour * 24 * 365), 159 ID: "a-application/website", 160 ApplicationID: "a-application", 161 } 162 163 fp2, err := charmresource.GenerateFingerprint(strings.NewReader("other")) 164 c.Assert(err, jc.ErrorIsNil) 165 166 unit := resource.Resource{ 167 Resource: charmresource.Resource{ 168 Meta: charmresource.Meta{ 169 Name: "website", 170 Description: "your website data", 171 Type: charmresource.TypeFile, 172 Path: "foobar", 173 }, 174 Revision: 7, 175 Origin: charmresource.OriginStore, 176 Fingerprint: fp2, 177 Size: 15, 178 }, 179 Username: "Bill User", 180 Timestamp: time.Now(), 181 ID: "a-application/website", 182 ApplicationID: "a-application", 183 } 184 tag := names.NewUnitTag("a-application/55") 185 186 d, err := resourcecmd.FormatDetailResource(tag, svc, unit, 8) 187 c.Assert(err, jc.ErrorIsNil) 188 c.Assert(d, gc.Equals, 189 resourcecmd.FormattedDetailResource{ 190 UnitNumber: 55, 191 UnitID: "a-application/55", 192 Expected: resourcecmd.FormatAppResource(svc), 193 Progress: 8, 194 RevProgress: "5 (fetching: 80%)", 195 Unit: resourcecmd.FormatAppResource(unit), 196 }, 197 ) 198 } 199 200 func (s *DetailFormatterSuite) TestFormatDetailEmpty(c *gc.C) { 201 fp, err := charmresource.GenerateFingerprint(strings.NewReader("something")) 202 c.Assert(err, jc.ErrorIsNil) 203 204 svc := resource.Resource{ 205 Resource: charmresource.Resource{ 206 Meta: charmresource.Meta{ 207 Name: "website", 208 Description: "your website data", 209 Type: charmresource.TypeFile, 210 Path: "foobar", 211 }, 212 Revision: 5, 213 Origin: charmresource.OriginStore, 214 Fingerprint: fp, 215 Size: 10, 216 }, 217 Username: "Bill User", 218 Timestamp: time.Now().Add(-1 * time.Hour * 24 * 365), 219 ID: "a-application/website", 220 ApplicationID: "a-application", 221 } 222 223 unit := resource.Resource{} 224 tag := names.NewUnitTag("a-application/55") 225 226 d, err := resourcecmd.FormatDetailResource(tag, svc, unit, 0) 227 c.Assert(err, jc.ErrorIsNil) 228 c.Assert(d, gc.Equals, 229 resourcecmd.FormattedDetailResource{ 230 UnitNumber: 55, 231 UnitID: "a-application/55", 232 Expected: resourcecmd.FormatAppResource(svc), 233 Progress: 0, 234 RevProgress: "5 (fetching: 0%)", 235 Unit: resourcecmd.FormatAppResource(unit), 236 }, 237 ) 238 }