Skip to content

Commit 69d020f

Browse files
committed
cmd/snap: improve snap size reporting
The code used a helper which rounds down the quantities for formatting. The rounding error isn't super significant while in the MBs range, but above 1GB, there's an observable difference between 1.9GB and 1GB. Switch to a different helper which tries to preserve the informational value of the formatted quantity. Fixes: LP#2142655 Signed-off-by: Maciej Borzecki <maciej.borzecki@canonical.com>
1 parent 4017487 commit 69d020f

File tree

2 files changed

+138
-30
lines changed

2 files changed

+138
-30
lines changed

cmd/snap/cmd_info.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/snapcore/snapd/snap/snapfile"
4444
"github.com/snapcore/snapd/snap/squashfs"
4545
"github.com/snapcore/snapd/strutil"
46+
"github.com/snapcore/snapd/strutil/quantity"
4647
)
4748

4849
type infoCmd struct {
@@ -584,8 +585,9 @@ func (chInfos *channelInfos) add(indent, name, version string, revision snap.Rev
584585
name: name,
585586
version: version,
586587
revision: fmt.Sprintf("(%s)", revision),
587-
size: strutil.SizeToStr(size),
588-
notes: notes.String(),
588+
// -1 for auto-width of 5 chars
589+
size: quantity.FormatAmount(uint64(size), -1) + "B",
590+
notes: notes.String(),
589591
}
590592
if !released.IsZero() {
591593
chInfo.released = released.Format(chInfos.releasedfmt)

cmd/snap/cmd_info_test.go

Lines changed: 134 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ description: |
888888
snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
889889
tracking: beta
890890
refresh-date: 2006-01-02T22:04:07Z
891-
installed: 2.10 (1) 1kB disabled,blocked
891+
installed: 2.10 (1) 1024B disabled,blocked
892892
`[1:])
893893
c.Check(s.Stderr(), check.Equals, "")
894894
}
@@ -946,7 +946,7 @@ description: |
946946
snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
947947
tracking: beta
948948
refresh-date: 2006-01-02T22:04:07Z
949-
installed: 2.10 (100) 1kB disabled
949+
installed: 2.10 (100) 1024B disabled
950950
`)
951951
c.Check(s.Stderr(), check.Equals, "")
952952
}
@@ -984,11 +984,11 @@ snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
984984
tracking: beta
985985
refresh-date: 2006-01-02T22:04:07Z
986986
channels:
987-
1/stable: 2.10 2018-12-18T15:16:56Z (1) 65kB -
988-
1/candidate: ^
989-
1/beta: ^
990-
1/edge: ^
991-
installed: 2.10 (100) 1kB disabled
987+
1/stable: 2.10 2018-12-18T15:16:56Z (1) 65.5kB -
988+
1/candidate: ^
989+
1/beta: ^
990+
1/edge: ^
991+
installed: 2.10 (100) 1024B disabled
992992
`)
993993
c.Check(s.Stderr(), check.Equals, "")
994994
c.Check(n, check.Equals, 2)
@@ -1011,11 +1011,11 @@ snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
10111011
tracking: beta
10121012
refresh-date: %s
10131013
channels:
1014-
1/stable: 2.10 2018-12-18 (1) 65kB -
1015-
1/candidate: ^
1016-
1/beta: ^
1017-
1/edge: ^
1018-
installed: 2.10 (100) 1kB disabled
1014+
1/stable: 2.10 2018-12-18 (1) 65.5kB -
1015+
1/candidate: ^
1016+
1/beta: ^
1017+
1/edge: ^
1018+
installed: 2.10 (100) 1024B disabled
10191019
`, refreshDate))
10201020
c.Check(s.Stderr(), check.Equals, "")
10211021
c.Check(n, check.Equals, 4)
@@ -1037,16 +1037,122 @@ snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
10371037
tracking: beta
10381038
refresh-date: %s
10391039
channels:
1040-
1/stable: 2.10 2018-12-18 (1) 65kB -
1041-
1/candidate: ↑
1042-
1/beta: ↑
1043-
1/edge: ↑
1044-
installed: 2.10 (100) 1kB disabled
1040+
1/stable: 2.10 2018-12-18 (1) 65.5kB -
1041+
1/candidate: ↑
1042+
1/beta: ↑
1043+
1/edge: ↑
1044+
installed: 2.10 (100) 1024B disabled
10451045
`, refreshDate))
10461046
c.Check(s.Stderr(), check.Equals, "")
10471047
c.Check(n, check.Equals, 6)
10481048
}
10491049

1050+
const mockInfoJSONWithChannelsBigSize = `
1051+
{
1052+
"type": "sync",
1053+
"status-code": 200,
1054+
"status": "OK",
1055+
"result": [
1056+
{
1057+
"channel": "stable",
1058+
"confinement": "strict",
1059+
"description": "GNU hello prints a friendly greeting. This is part of the snapcraft tour at https://snapcraft.io/",
1060+
"developer": "canonical",
1061+
"publisher": {
1062+
"id": "canonical",
1063+
"username": "canonical",
1064+
"display-name": "Canonical",
1065+
"validation": "verified"
1066+
},
1067+
"download-size": 65536,
1068+
"icon": "",
1069+
"id": "mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6",
1070+
"name": "hello",
1071+
"private": false,
1072+
"resource": "/v2/snaps/hello",
1073+
"revision": "1",
1074+
"status": "available",
1075+
"summary": "The GNU Hello snap",
1076+
"store-url": "https://snapcraft.io/hello",
1077+
"type": "app",
1078+
"version": "2.10",
1079+
"license": "MIT",
1080+
"channels": {
1081+
"1/stable": {
1082+
"revision": "1",
1083+
"version": "2.10",
1084+
"channel": "1/stable",
1085+
"size": 1652695040,
1086+
"released-at": "2018-12-18T15:16:56.723501Z"
1087+
},
1088+
"1/beta": {
1089+
"revision": "3",
1090+
"version": "2.10",
1091+
"channel": "1/beta",
1092+
"size": 1468006,
1093+
"released-at": "2018-12-18T15:12:56.723501Z"
1094+
},
1095+
"1/edge": {
1096+
"revision": "2",
1097+
"version": "2.10",
1098+
"channel": "1/edge",
1099+
"size": 795869184,
1100+
"released-at": "2018-12-18T15:10:56.723501Z"
1101+
}
1102+
},
1103+
"tracks": ["1"]
1104+
}
1105+
],
1106+
"sources": [
1107+
"store"
1108+
],
1109+
"suggested-currency": "GBP"
1110+
}
1111+
`
1112+
1113+
func (s *infoSuite) TestInfoHumanSizes(c *check.C) {
1114+
n := 0
1115+
s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
1116+
switch n {
1117+
case 0:
1118+
c.Check(r.Method, check.Equals, "GET")
1119+
c.Check(r.URL.Path, check.Equals, "/v2/find")
1120+
fmt.Fprint(w, mockInfoJSONWithChannelsBigSize)
1121+
case 1:
1122+
c.Check(r.Method, check.Equals, "GET")
1123+
c.Check(r.URL.Path, check.Equals, "/v2/snaps/hello")
1124+
fmt.Fprint(w, mockInfoJSONNoLicense)
1125+
default:
1126+
c.Fatalf("expected to get 2 requests, now on %d (%v)", n+1, r)
1127+
}
1128+
1129+
n++
1130+
})
1131+
rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"info", "--abs-time", "hello"})
1132+
c.Assert(err, check.IsNil)
1133+
c.Assert(rest, check.DeepEquals, []string{})
1134+
c.Check(s.Stdout(), check.Equals, `name: hello
1135+
summary: The GNU Hello snap
1136+
publisher: Canonical**
1137+
store-url: https://snapcraft.io/hello
1138+
license: unset
1139+
description: |
1140+
GNU hello prints a friendly greeting. This is part of the snapcraft tour at
1141+
https://snapcraft.io/
1142+
snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
1143+
tracking: beta
1144+
refresh-date: 2006-01-02T22:04:07Z
1145+
channels:
1146+
1/stable: 2.10 2018-12-18T15:16:56Z (1) 1.65GB -
1147+
1/candidate: ^
1148+
1/beta: 2.10 2018-12-18T15:12:56Z (3) 1.47MB -
1149+
1/edge: 2.10 2018-12-18T15:10:56Z (2) 796MB -
1150+
installed: 2.10 (100) 1024B disabled
1151+
`)
1152+
c.Check(s.Stderr(), check.Equals, "")
1153+
c.Check(n, check.Equals, 2)
1154+
}
1155+
10501156
func (s *infoSuite) TestInfoHumanTimes(c *check.C) {
10511157
// checks that tiemutil.Human is called when no --abs-time is given
10521158
restore := snap.MockTimeutilHuman(func(time.Time) string { return "TOTALLY NOT A ROBOT" })
@@ -1082,7 +1188,7 @@ description: |
10821188
snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
10831189
tracking: beta
10841190
refresh-date: TOTALLY NOT A ROBOT
1085-
installed: 2.10 (100) 1kB disabled
1191+
installed: 2.10 (100) 1024B disabled
10861192
`)
10871193
c.Check(s.Stderr(), check.Equals, "")
10881194
}
@@ -1309,11 +1415,11 @@ snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
13091415
tracking: beta
13101416
refresh-date: %s
13111417
channels:
1312-
1/stable: 2.10 2018-12-18 (1) 65kB -
1313-
1/candidate: ^
1314-
1/beta: ^
1315-
1/edge: ^
1316-
installed: 2.10 (100) 1kB disabled
1418+
1/stable: 2.10 2018-12-18 (1) 65.5kB -
1419+
1/candidate: ^
1420+
1/beta: ^
1421+
1/edge: ^
1422+
installed: 2.10 (100) 1024B disabled
13171423
`, refreshDate))
13181424
c.Check(s.Stderr(), check.Equals, "")
13191425
}
@@ -1393,11 +1499,11 @@ snap-id: mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6
13931499
tracking: beta
13941500
refresh-date: %s
13951501
channels:
1396-
1/stable: 2.10 2018-12-18 (1) 65kB -
1397-
1/candidate: ^
1398-
1/beta: ^
1399-
1/edge: ^
1400-
installed: 2.10 (100) 1kB disabled
1502+
1/stable: 2.10 2018-12-18 (1) 65.5kB -
1503+
1/candidate: ^
1504+
1/beta: ^
1505+
1/edge: ^
1506+
installed: 2.10 (100) 1024B disabled
14011507
`, refreshDate))
14021508
c.Check(s.Stderr(), check.Equals, "")
14031509
}

0 commit comments

Comments
 (0)