2017-08-29 22:01:01 +03:00
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gps
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2017-10-08 23:11:00 +03:00
|
|
|
"path"
|
2017-08-29 22:01:01 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-11-03 16:05:41 +03:00
|
|
|
"github.com/golang/dep/gps/pkgtree"
|
2017-08-29 22:01:01 +03:00
|
|
|
"github.com/golang/dep/internal/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoltCacheTimeout(t *testing.T) {
|
|
|
|
const root = "example.com/test"
|
|
|
|
cpath, err := ioutil.TempDir("", "singlesourcecache")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create temp cache dir: %s", err)
|
|
|
|
}
|
|
|
|
pi := ProjectIdentifier{ProjectRoot: root}
|
2017-09-11 22:25:31 +03:00
|
|
|
logger := log.New(test.Writer{TB: t}, "", 0)
|
2017-08-29 22:01:01 +03:00
|
|
|
|
|
|
|
start := time.Now()
|
2017-09-01 00:53:13 +03:00
|
|
|
bc, err := newBoltCache(cpath, start.Unix(), logger)
|
2017-08-29 22:01:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-01 00:53:13 +03:00
|
|
|
defer bc.close()
|
|
|
|
c := bc.newSingleSourceCache(pi)
|
2017-08-29 22:01:01 +03:00
|
|
|
|
|
|
|
rev := Revision("test")
|
|
|
|
ai := ProjectAnalyzerInfo{Name: "name", Version: 42}
|
|
|
|
|
2017-08-31 16:21:52 +03:00
|
|
|
manifest := &simpleRootManifest{
|
|
|
|
c: ProjectConstraints{
|
2017-08-29 22:01:01 +03:00
|
|
|
ProjectRoot("foo"): ProjectProperties{
|
|
|
|
Constraint: Any(),
|
|
|
|
},
|
|
|
|
ProjectRoot("bar"): ProjectProperties{
|
|
|
|
Source: "whatever",
|
|
|
|
Constraint: testSemverConstraint(t, "> 1.3"),
|
|
|
|
},
|
|
|
|
},
|
2017-08-31 16:21:52 +03:00
|
|
|
ovr: ProjectConstraints{
|
2017-08-29 22:01:01 +03:00
|
|
|
ProjectRoot("b"): ProjectProperties{
|
|
|
|
Constraint: testSemverConstraint(t, "2.0.0"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-08-31 16:21:52 +03:00
|
|
|
lock := &safeLock{
|
|
|
|
p: []LockedProject{
|
2018-07-04 03:31:50 +03:00
|
|
|
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("foo"), []string{"gps"}),
|
|
|
|
NewLockedProject(mkPI("github.com/sdboyer/gps2"), NewVersion("v0.10.0").Pair("bar"), nil),
|
|
|
|
NewLockedProject(mkPI("github.com/sdboyer/gps3"), NewVersion("v0.10.0").Pair("baz"), []string{"gps", "flugle"}),
|
|
|
|
NewLockedProject(mkPI("foo"), NewVersion("nada").Pair("zero"), []string{"foo"}),
|
|
|
|
NewLockedProject(mkPI("github.com/sdboyer/gps4"), NewVersion("v0.10.0").Pair("qux"), []string{"flugle", "gps"}),
|
2017-08-29 22:01:01 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ptree := pkgtree.PackageTree{
|
|
|
|
ImportRoot: root,
|
|
|
|
Packages: map[string]pkgtree.PackageOrErr{
|
2017-10-08 23:11:00 +03:00
|
|
|
root: {
|
2017-08-29 22:01:01 +03:00
|
|
|
P: pkgtree.Package{
|
2017-10-08 23:11:00 +03:00
|
|
|
ImportPath: root,
|
|
|
|
CommentPath: "comment",
|
|
|
|
Name: "test",
|
|
|
|
Imports: []string{
|
|
|
|
"sort",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
path.Join(root, "simple"): {
|
|
|
|
P: pkgtree.Package{
|
|
|
|
ImportPath: path.Join(root, "simple"),
|
2017-08-29 22:01:01 +03:00
|
|
|
CommentPath: "comment",
|
|
|
|
Name: "simple",
|
|
|
|
Imports: []string{
|
2017-11-03 16:05:41 +03:00
|
|
|
"github.com/golang/dep/gps",
|
2017-08-29 22:01:01 +03:00
|
|
|
"sort",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-08 23:11:00 +03:00
|
|
|
path.Join(root, "m1p"): {
|
2017-08-29 22:01:01 +03:00
|
|
|
P: pkgtree.Package{
|
2017-10-08 23:11:00 +03:00
|
|
|
ImportPath: path.Join(root, "m1p"),
|
2017-08-29 22:01:01 +03:00
|
|
|
CommentPath: "",
|
|
|
|
Name: "m1p",
|
|
|
|
Imports: []string{
|
2017-11-03 16:05:41 +03:00
|
|
|
"github.com/golang/dep/gps",
|
2017-08-29 22:01:01 +03:00
|
|
|
"os",
|
|
|
|
"sort",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pvs := []PairedVersion{
|
|
|
|
NewBranch("originalbranch").Pair("rev1"),
|
|
|
|
NewVersion("originalver").Pair("rev2"),
|
|
|
|
}
|
|
|
|
|
2017-08-31 17:31:22 +03:00
|
|
|
// Write values timestamped > `start`.
|
2017-08-29 22:01:01 +03:00
|
|
|
{
|
|
|
|
c.setManifestAndLock(rev, ai, manifest, lock)
|
|
|
|
c.setPackageTree(rev, ptree)
|
|
|
|
c.setVersionMap(pvs)
|
|
|
|
}
|
2017-08-31 17:31:22 +03:00
|
|
|
// Read back values timestamped > `start`.
|
2017-08-29 22:01:01 +03:00
|
|
|
{
|
|
|
|
gotM, gotL, ok := c.getManifestAndLock(rev, ai)
|
|
|
|
if !ok {
|
|
|
|
t.Error("no manifest and lock found for revision")
|
|
|
|
}
|
|
|
|
compareManifests(t, manifest, gotM)
|
2018-06-28 08:32:17 +03:00
|
|
|
// TODO(sdboyer) use DiffLocks after refactoring to avoid import cycles
|
|
|
|
if !locksAreEq(lock, gotL) {
|
|
|
|
t.Errorf("locks are different:\n\t(GOT): %s\n\t(WNT): %s", lock, gotL)
|
2017-08-29 22:01:01 +03:00
|
|
|
}
|
|
|
|
|
2017-10-08 23:11:00 +03:00
|
|
|
got, ok := c.getPackageTree(rev, root)
|
2017-08-29 22:01:01 +03:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("no package tree found:\n\t(WNT): %#v", ptree)
|
|
|
|
}
|
|
|
|
comparePackageTree(t, ptree, got)
|
|
|
|
|
2017-09-23 18:18:32 +03:00
|
|
|
gotV, ok := c.getAllVersions()
|
|
|
|
if !ok || len(gotV) != len(pvs) {
|
2017-08-29 22:01:01 +03:00
|
|
|
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs)
|
|
|
|
} else {
|
|
|
|
SortPairedForDowngrade(gotV)
|
|
|
|
for i := range pvs {
|
|
|
|
if !pvs[i].identical(gotV[i]) {
|
|
|
|
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 00:53:13 +03:00
|
|
|
if err := bc.close(); err != nil {
|
2017-08-29 22:01:01 +03:00
|
|
|
t.Fatal("failed to close cache:", err)
|
|
|
|
}
|
|
|
|
|
2017-08-31 17:31:22 +03:00
|
|
|
// Read with a later epoch. Expect no *timestamped* values, since all were < `after`.
|
2017-08-29 22:01:01 +03:00
|
|
|
{
|
2017-09-06 21:32:08 +03:00
|
|
|
after := start.Add(1000 * time.Hour)
|
2017-09-01 00:53:13 +03:00
|
|
|
bc, err = newBoltCache(cpath, after.Unix(), logger)
|
2017-08-29 22:01:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-01 00:53:13 +03:00
|
|
|
c = bc.newSingleSourceCache(pi)
|
2017-08-29 22:01:01 +03:00
|
|
|
|
2017-08-31 17:31:22 +03:00
|
|
|
gotM, gotL, ok := c.getManifestAndLock(rev, ai)
|
|
|
|
if !ok {
|
|
|
|
t.Error("no manifest and lock found for revision")
|
|
|
|
}
|
|
|
|
compareManifests(t, manifest, gotM)
|
2018-06-28 08:32:17 +03:00
|
|
|
// TODO(sdboyer) use DiffLocks after refactoring to avoid import cycles
|
|
|
|
if !locksAreEq(lock, gotL) {
|
|
|
|
t.Errorf("locks are different:\n\t(GOT): %s\n\t(WNT): %s", lock, gotL)
|
2017-08-29 22:01:01 +03:00
|
|
|
}
|
|
|
|
|
2017-10-08 23:11:00 +03:00
|
|
|
gotPtree, ok := c.getPackageTree(rev, root)
|
2017-08-31 17:31:22 +03:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("no package tree found:\n\t(WNT): %#v", ptree)
|
2017-08-29 22:01:01 +03:00
|
|
|
}
|
2017-08-31 17:31:22 +03:00
|
|
|
comparePackageTree(t, ptree, gotPtree)
|
2017-08-29 22:01:01 +03:00
|
|
|
|
2017-09-23 18:18:32 +03:00
|
|
|
pvs, ok := c.getAllVersions()
|
|
|
|
if ok || len(pvs) > 0 {
|
2017-08-29 22:01:01 +03:00
|
|
|
t.Errorf("expected no cached versions, but got:\n\t%#v", pvs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 00:53:13 +03:00
|
|
|
if err := bc.close(); err != nil {
|
2017-08-29 22:01:01 +03:00
|
|
|
t.Fatal("failed to close cache:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-connect with the original epoch.
|
2017-09-01 00:53:13 +03:00
|
|
|
bc, err = newBoltCache(cpath, start.Unix(), logger)
|
2017-08-29 22:01:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-01 00:53:13 +03:00
|
|
|
c = bc.newSingleSourceCache(pi)
|
2017-08-31 17:31:22 +03:00
|
|
|
// Read values timestamped > `start`.
|
2017-08-29 22:01:01 +03:00
|
|
|
{
|
|
|
|
gotM, gotL, ok := c.getManifestAndLock(rev, ai)
|
|
|
|
if !ok {
|
|
|
|
t.Error("no manifest and lock found for revision")
|
|
|
|
}
|
|
|
|
compareManifests(t, manifest, gotM)
|
2018-06-28 08:32:17 +03:00
|
|
|
// TODO(sdboyer) use DiffLocks after refactoring to avoid import cycles
|
|
|
|
if !locksAreEq(lock, gotL) {
|
|
|
|
t.Errorf("locks are different:\n\t(GOT): %s\n\t(WNT): %s", lock, gotL)
|
2017-08-29 22:01:01 +03:00
|
|
|
}
|
|
|
|
|
2017-10-08 23:11:00 +03:00
|
|
|
got, ok := c.getPackageTree(rev, root)
|
2017-08-29 22:01:01 +03:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("no package tree found:\n\t(WNT): %#v", ptree)
|
|
|
|
}
|
|
|
|
comparePackageTree(t, ptree, got)
|
|
|
|
|
2017-09-23 18:18:32 +03:00
|
|
|
gotV, ok := c.getAllVersions()
|
|
|
|
if !ok || len(gotV) != len(pvs) {
|
2017-08-29 22:01:01 +03:00
|
|
|
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs)
|
|
|
|
} else {
|
|
|
|
SortPairedForDowngrade(gotV)
|
|
|
|
for i := range pvs {
|
|
|
|
if !pvs[i].identical(gotV[i]) {
|
|
|
|
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New values.
|
2017-08-31 16:21:52 +03:00
|
|
|
newManifest := &simpleRootManifest{
|
|
|
|
c: ProjectConstraints{
|
2017-08-29 22:01:01 +03:00
|
|
|
ProjectRoot("foo"): ProjectProperties{
|
|
|
|
Constraint: NewBranch("master"),
|
|
|
|
},
|
|
|
|
ProjectRoot("bar"): ProjectProperties{
|
|
|
|
Source: "whatever",
|
|
|
|
Constraint: testSemverConstraint(t, "> 1.5"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-08-31 16:21:52 +03:00
|
|
|
newLock := &safeLock{
|
|
|
|
p: []LockedProject{
|
2018-07-04 03:31:50 +03:00
|
|
|
NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v1").Pair("rev1"), []string{"gps"}),
|
2017-08-29 22:01:01 +03:00
|
|
|
},
|
2018-07-04 03:31:50 +03:00
|
|
|
i: []string{"foo", "bar"},
|
2017-08-29 22:01:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
newPtree := pkgtree.PackageTree{
|
|
|
|
ImportRoot: root,
|
|
|
|
Packages: map[string]pkgtree.PackageOrErr{
|
2017-10-08 23:11:00 +03:00
|
|
|
path.Join(root, "simple"): {
|
2017-08-29 22:01:01 +03:00
|
|
|
P: pkgtree.Package{
|
2017-10-08 23:11:00 +03:00
|
|
|
ImportPath: path.Join(root, "simple"),
|
2017-08-29 22:01:01 +03:00
|
|
|
CommentPath: "newcomment",
|
|
|
|
Name: "simple",
|
|
|
|
Imports: []string{
|
2017-11-03 16:05:41 +03:00
|
|
|
"github.com/golang/dep/gps42",
|
2017-08-29 22:01:01 +03:00
|
|
|
"test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-08 23:11:00 +03:00
|
|
|
path.Join(root, "m1p"): {
|
2017-08-29 22:01:01 +03:00
|
|
|
P: pkgtree.Package{
|
2017-10-08 23:11:00 +03:00
|
|
|
ImportPath: path.Join(root, "m1p"),
|
2017-08-29 22:01:01 +03:00
|
|
|
CommentPath: "",
|
|
|
|
Name: "m1p",
|
|
|
|
Imports: []string{
|
|
|
|
"os",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
newPVS := []PairedVersion{
|
|
|
|
NewBranch("newbranch").Pair("revA"),
|
|
|
|
NewVersion("newver").Pair("revB"),
|
|
|
|
}
|
2017-08-31 17:31:22 +03:00
|
|
|
// Overwrite with new values, and with timestamps > `after`.
|
2017-08-29 22:01:01 +03:00
|
|
|
{
|
|
|
|
c.setManifestAndLock(rev, ai, newManifest, newLock)
|
|
|
|
c.setPackageTree(rev, newPtree)
|
|
|
|
c.setVersionMap(newPVS)
|
|
|
|
}
|
|
|
|
// Read new values.
|
|
|
|
{
|
|
|
|
gotM, gotL, ok := c.getManifestAndLock(rev, ai)
|
|
|
|
if !ok {
|
|
|
|
t.Error("no manifest and lock found for revision")
|
|
|
|
}
|
|
|
|
compareManifests(t, newManifest, gotM)
|
2018-06-28 08:32:17 +03:00
|
|
|
// TODO(sdboyer) use DiffLocks after refactoring to avoid import cycles
|
2018-07-04 03:31:50 +03:00
|
|
|
if !locksAreEq(newLock, gotL) {
|
|
|
|
t.Errorf("locks are different:\n\t(GOT): %s\n\t(WNT): %s", newLock, gotL)
|
2017-08-29 22:01:01 +03:00
|
|
|
}
|
|
|
|
|
2017-10-08 23:11:00 +03:00
|
|
|
got, ok := c.getPackageTree(rev, root)
|
2017-08-29 22:01:01 +03:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("no package tree found:\n\t(WNT): %#v", newPtree)
|
|
|
|
}
|
|
|
|
comparePackageTree(t, newPtree, got)
|
|
|
|
|
2017-09-23 18:18:32 +03:00
|
|
|
gotV, ok := c.getAllVersions()
|
|
|
|
if !ok || len(gotV) != len(newPVS) {
|
2017-08-29 22:01:01 +03:00
|
|
|
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, newPVS)
|
|
|
|
} else {
|
|
|
|
SortPairedForDowngrade(gotV)
|
|
|
|
for i := range newPVS {
|
|
|
|
if !newPVS[i].identical(gotV[i]) {
|
|
|
|
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, newPVS)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|