From 357cd710127472ef6b69d4c8f8bc1a2a35fe9f53 Mon Sep 17 00:00:00 2001 From: Allen Zhong Date: Mon, 28 Dec 2020 16:04:44 +0800 Subject: [PATCH] respository: update root before snapshot (#1001) * respository: update root before snapshot * repository: fix unit test Co-authored-by: SIGSEGV --- pkg/repository/v1_repository.go | 13 ++++++------- pkg/repository/v1_repository_test.go | 6 +++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/pkg/repository/v1_repository.go b/pkg/repository/v1_repository.go index 0a91f2d49f..dc8e78ecbf 100644 --- a/pkg/repository/v1_repository.go +++ b/pkg/repository/v1_repository.go @@ -207,14 +207,13 @@ func (r *V1Repository) ensureManifests() error { verbose.Log("Ensure manifests finished in %s", time.Since(start)) }(time.Now()) - // Update snapshot. - snapshot, err := r.updateLocalSnapshot() - if err != nil { + // Update root before anything else. + if err := r.updateLocalRoot(); err != nil { return errors.Trace(err) } - // Update root. - err = r.updateLocalRoot() + // Update snapshot. + snapshot, err := r.updateLocalSnapshot() if err != nil { return errors.Trace(err) } @@ -353,7 +352,7 @@ func (r *V1Repository) updateLocalRoot() error { url := FnameWithVersion(v1manifest.ManifestURLRoot, oldRoot.Version+1) nextManifest, err := r.fetchManifestWithKeyStore(url, &newRoot, maxRootSize, &keyStore) if err != nil { - // Break if we have read the newest version. + // Break if we have read the latest version. if errors.Cause(err) == ErrNotFound { break } @@ -825,7 +824,7 @@ func (r *V1Repository) BinaryPath(installPath string, componentID string, versio specVersion = component.Nightly } - // We need yanked version because we may install that version before it was yanked + // We need yanked version because we may have installed that version before it was yanked versionItem, ok := component.VersionListWithYanked(r.PlatformString())[specVersion] if !ok { return "", errors.Errorf("no version: %s", version) diff --git a/pkg/repository/v1_repository_test.go b/pkg/repository/v1_repository_test.go index 27faec8dfb..b0bdd6f221 100644 --- a/pkg/repository/v1_repository_test.go +++ b/pkg/repository/v1_repository_test.go @@ -443,7 +443,7 @@ func TestEnsureManifests(t *testing.T) { assert.NotContains(t, local.Saved, v1manifest.ManifestFilenameRoot) // Happy update - root2, priv2 := rootManifest(t) + root2, priv2 := rootManifest(t) // generate new root key root, _ := repo.loadRoot() root2.Version = root.Version + 1 mirror.Resources["/43.root.json"] = serialize(t, root2, priv, priv2) @@ -451,11 +451,11 @@ func TestEnsureManifests(t *testing.T) { rootMeta := snapshot.Meta[v1manifest.ManifestURLRoot] rootMeta.Version = root2.Version snapshot.Meta[v1manifest.ManifestURLRoot] = rootMeta - snapStr = serialize(t, snapshot, priv) + snapStr = serialize(t, snapshot, priv2) // sign snapshot with new key ts.Meta[v1manifest.ManifestURLSnapshot].Hashes[v1manifest.SHA256] = hash(snapStr) ts.Version++ mirror.Resources[v1manifest.ManifestURLSnapshot] = snapStr - mirror.Resources[v1manifest.ManifestURLTimestamp] = serialize(t, ts, priv) + mirror.Resources[v1manifest.ManifestURLTimestamp] = serialize(t, ts, priv2) local.Saved = []string{} err = repo.ensureManifests()