Fix an error where we get a JSON syntax error on server 404 or 50X.

We were testing to see if the cached metadata was nil, but we actually
set it to an empty data.Signed object, but didn't always set it to nil
if we failed to get local metadata.

Signed-off-by: Ying Li <ying.li@docker.com>
This commit is contained in:
Ying Li 2016-01-20 14:40:01 -08:00
parent 36684a3290
commit dde9531b4a
2 changed files with 11 additions and 11 deletions

View File

@ -461,7 +461,6 @@ func TestUpdateRemoteRoot50XCannotUseLocalCache(t *testing.T) {
// but is missing other data, then we propagate the ErrMetaNotFound. Skipping // but is missing other data, then we propagate the ErrMetaNotFound. Skipping
// force check, because that only matters for root. // force check, because that only matters for root.
func TestUpdateNonRootRemoteMissingMetadataNoLocalCache(t *testing.T) { func TestUpdateNonRootRemoteMissingMetadataNoLocalCache(t *testing.T) {
// TODO: fix json syntax error
for _, role := range append(data.BaseRoles, "targets/a", "targets/a/b") { for _, role := range append(data.BaseRoles, "targets/a", "targets/a/b") {
if role == data.CanonicalRootRole { if role == data.CanonicalRootRole {
continue continue
@ -531,7 +530,6 @@ func TestUpdateNonRootRemoteMissingMetadataCannotUseLocalCache(t *testing.T) {
// If there is no local cache, we just update. If the server 50X's when getting // If there is no local cache, we just update. If the server 50X's when getting
// metadata, we propagate ErrServerUnavailable. // metadata, we propagate ErrServerUnavailable.
func TestUpdateNonRootRemote50XNoLocalCache(t *testing.T) { func TestUpdateNonRootRemote50XNoLocalCache(t *testing.T) {
// TODO: fix json syntax error
for _, role := range append(data.BaseRoles, "targets/a", "targets/a/b") { for _, role := range append(data.BaseRoles, "targets/a", "targets/a/b") {
if role == data.CanonicalRootRole { if role == data.CanonicalRootRole {
continue continue

View File

@ -247,19 +247,21 @@ func (c *Client) downloadTimestamp() error {
// We may not have a cached timestamp if this is the first time // We may not have a cached timestamp if this is the first time
// we're interacting with the repo. This will result in the // we're interacting with the repo. This will result in the
// version being 0 // version being 0
var download bool var (
old := &data.Signed{} saveToCache bool
version := 0 old *data.Signed
version = 0
)
cachedTS, err := c.cache.GetMeta(role, maxSize) cachedTS, err := c.cache.GetMeta(role, maxSize)
if err == nil { if err == nil {
err := json.Unmarshal(cachedTS, old) cached := &data.Signed{}
err := json.Unmarshal(cachedTS, cached)
if err == nil { if err == nil {
ts, err := data.TimestampFromSigned(old) ts, err := data.TimestampFromSigned(cached)
if err == nil { if err == nil {
version = ts.Signed.Version version = ts.Signed.Version
} }
} else { old = cached
old = nil
} }
} }
// unlike root, targets and snapshot, always try and download timestamps // unlike root, targets and snapshot, always try and download timestamps
@ -278,14 +280,14 @@ func (c *Client) downloadTimestamp() error {
logrus.Warn("Error while downloading remote metadata, using cached timestamp - this might not be the latest version available remotely") logrus.Warn("Error while downloading remote metadata, using cached timestamp - this might not be the latest version available remotely")
s = old s = old
} else { } else {
download = true saveToCache = true
} }
err = signed.Verify(s, role, version, c.keysDB) err = signed.Verify(s, role, version, c.keysDB)
if err != nil { if err != nil {
return err return err
} }
logrus.Debug("successfully verified timestamp") logrus.Debug("successfully verified timestamp")
if download { if saveToCache {
c.cache.SetMeta(role, raw) c.cache.SetMeta(role, raw)
} }
ts, err := data.TimestampFromSigned(s) ts, err := data.TimestampFromSigned(s)