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