2011-07-06 18:59:50 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2014 The Perkeep Authors
|
2011-07-06 18:59:50 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
package main
|
2011-07-06 18:59:50 +00:00
|
|
|
|
|
|
|
import (
|
2017-11-26 09:05:38 +00:00
|
|
|
"context"
|
2014-06-14 20:14:34 +00:00
|
|
|
"io"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2011-07-06 23:33:31 +00:00
|
|
|
"strings"
|
2011-07-06 18:59:50 +00:00
|
|
|
"testing"
|
2014-03-17 23:27:37 +00:00
|
|
|
"time"
|
2011-07-06 18:59:50 +00:00
|
|
|
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/httputil"
|
2018-05-01 19:52:02 +00:00
|
|
|
"perkeep.org/internal/testhooks"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
camliClient "perkeep.org/pkg/client"
|
|
|
|
"perkeep.org/pkg/index"
|
|
|
|
"perkeep.org/pkg/index/indextest"
|
|
|
|
"perkeep.org/pkg/search"
|
2011-07-06 18:59:50 +00:00
|
|
|
)
|
|
|
|
|
2018-05-01 19:52:02 +00:00
|
|
|
func init() {
|
|
|
|
testhooks.SetUseSHA1(true)
|
|
|
|
}
|
|
|
|
|
2011-07-06 23:33:31 +00:00
|
|
|
type publishURLTest struct {
|
2011-07-07 01:38:27 +00:00
|
|
|
path string // input
|
|
|
|
subject, subres string // expected
|
2011-07-06 23:33:31 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 23:27:37 +00:00
|
|
|
var publishURLTests []publishURLTest
|
|
|
|
|
|
|
|
func setupContent(rootName string) *indextest.IndexDeps {
|
|
|
|
idx := index.NewMemoryIndex()
|
|
|
|
idxd := indextest.NewIndexDeps(idx)
|
|
|
|
|
|
|
|
picNode := idxd.NewPlannedPermanode("picpn-1234") // sha1-f5e90fcc50a79caa8b22a4aa63ba92e436cab9ec
|
|
|
|
galRef := idxd.NewPlannedPermanode("gal-1234") // sha1-2bdf2053922c3dfa70b01a4827168fce1c1df691
|
|
|
|
rootRef := idxd.NewPlannedPermanode("root-abcd") // sha1-dbb3e5f28c7e01536d43ce194f3dd7b921b8460d
|
|
|
|
camp0 := idxd.NewPlannedPermanode("picpn-9876543210") // sha1-2d473e07ca760231dd82edeef4019d5b7d0ccb42
|
|
|
|
camp1 := idxd.NewPlannedPermanode("picpn-9876543211") // sha1-961b700536d5151fc1f3920955cc92767572a064
|
|
|
|
camp0f, _ := idxd.UploadFile("picfile-f00ff00f00a5.jpg", "picfile-f00ff00f00a5", time.Time{}) // sha1-01dbcb193fc789033fb2d08ed22abe7105b48640
|
|
|
|
camp1f, _ := idxd.UploadFile("picfile-f00ff00f00b6.jpg", "picfile-f00ff00f00b6", time.Time{}) // sha1-1213ec17a42cc51bdeb95ff91ac1b5fc5157740f
|
|
|
|
|
|
|
|
idxd.SetAttribute(rootRef, "camliRoot", rootName)
|
|
|
|
idxd.SetAttribute(rootRef, "camliPath:singlepic", picNode.String())
|
|
|
|
idxd.SetAttribute(picNode, "title", "picnode without a pic?")
|
|
|
|
idxd.SetAttribute(rootRef, "camliPath:camping", galRef.String())
|
|
|
|
idxd.AddAttribute(galRef, "camliMember", camp0.String())
|
|
|
|
idxd.AddAttribute(galRef, "camliMember", camp1.String())
|
|
|
|
idxd.SetAttribute(camp0, "camliContent", camp0f.String())
|
|
|
|
idxd.SetAttribute(camp1, "camliContent", camp1f.String())
|
|
|
|
|
|
|
|
publishURLTests = []publishURLTest{
|
|
|
|
// URL to a single picture permanode (returning its HTML wrapper page)
|
|
|
|
{
|
|
|
|
path: "/pics/singlepic",
|
|
|
|
subject: picNode.String(),
|
|
|
|
},
|
|
|
|
|
|
|
|
// URL to a gallery permanode (returning its HTML wrapper page)
|
|
|
|
{
|
|
|
|
path: "/pics/camping",
|
|
|
|
subject: galRef.String(),
|
|
|
|
},
|
|
|
|
|
|
|
|
// URL to a picture permanode within a gallery (following one hop, returning HTML)
|
|
|
|
{
|
|
|
|
path: "/pics/camping/-/h2d473e07ca",
|
|
|
|
subject: camp0.String(),
|
|
|
|
},
|
|
|
|
|
|
|
|
// URL to a gallery -> picture permanode -> its file
|
|
|
|
// (following two hops, returning HTML)
|
|
|
|
{
|
|
|
|
path: "/pics/camping/-/h2d473e07ca/h01dbcb193f",
|
|
|
|
subject: camp0f.String(),
|
|
|
|
},
|
|
|
|
|
|
|
|
// URL to a gallery -> picture permanode -> its file
|
|
|
|
// (following two hops, returning the file download)
|
|
|
|
{
|
|
|
|
path: "/pics/camping/-/h2d473e07ca/h01dbcb193f/=f/marshmallow.jpg",
|
|
|
|
subject: camp0f.String(),
|
|
|
|
subres: "/=f/marshmallow.jpg",
|
|
|
|
},
|
|
|
|
|
|
|
|
// URL to a gallery -> picture permanode -> its file
|
|
|
|
// (following two hops, returning the file, scaled as an image)
|
|
|
|
{
|
|
|
|
path: "/pics/camping/-/h961b700536/h1213ec17a4/=i/marshmallow.jpg?mw=200&mh=200",
|
|
|
|
subject: camp1f.String(),
|
|
|
|
subres: "/=i/marshmallow.jpg",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Path to a static file in the root.
|
|
|
|
// TODO: ditch these and use content-addressable javascript + css, having
|
|
|
|
// the server digest them on start, or rather part of fileembed. This is
|
|
|
|
// a short-term hack to unblock Lindsey.
|
|
|
|
{
|
|
|
|
path: "/pics/=s/pics.js",
|
|
|
|
subject: "",
|
|
|
|
subres: "/=s/pics.js",
|
|
|
|
},
|
2013-12-17 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 23:27:37 +00:00
|
|
|
return idxd
|
2013-12-17 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
type fakeClient struct {
|
|
|
|
*camliClient.Client // for blob.Fetcher
|
|
|
|
sh *search.Handler
|
|
|
|
}
|
|
|
|
|
2018-05-01 19:52:02 +00:00
|
|
|
func (fc *fakeClient) Query(ctx context.Context, req *search.SearchQuery) (*search.SearchResult, error) {
|
|
|
|
return fc.sh.Query(ctx, req)
|
2016-06-28 00:12:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 19:52:02 +00:00
|
|
|
func (fc *fakeClient) Search(ctx context.Context, req *search.SearchQuery) (*search.SearchResult, error) {
|
|
|
|
return fc.sh.Query(ctx, req)
|
2014-06-14 20:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 00:12:47 +00:00
|
|
|
func (fc *fakeClient) Describe(ctx context.Context, req *search.DescribeRequest) (*search.DescribeResponse, error) {
|
|
|
|
return fc.sh.Describe(ctx, req)
|
2014-06-14 20:14:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 19:52:02 +00:00
|
|
|
func (fc *fakeClient) GetJSON(ctx context.Context, url string, data interface{}) error {
|
2014-06-14 20:14:34 +00:00
|
|
|
// no need to implement
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-01 19:52:02 +00:00
|
|
|
func (fc *fakeClient) Post(ctx context.Context, url string, bodyType string, body io.Reader) error {
|
2014-06-14 20:14:34 +00:00
|
|
|
// no need to implement
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-28 00:12:47 +00:00
|
|
|
// TODO(mpl): fix test. it was actually already broken on master. will fix before landing.
|
|
|
|
|
2013-12-17 22:05:53 +00:00
|
|
|
func TestPublishURLs(t *testing.T) {
|
2011-07-06 23:33:31 +00:00
|
|
|
rootName := "foo"
|
2014-03-17 23:27:37 +00:00
|
|
|
idxd := setupContent(rootName)
|
2018-05-01 19:52:02 +00:00
|
|
|
ownerRef := indextest.PubKey
|
|
|
|
owner := index.NewOwner(indextest.KeyID, ownerRef.BlobRef())
|
|
|
|
sh := search.NewHandler(idxd.Index, owner)
|
2014-03-17 23:27:37 +00:00
|
|
|
corpus, err := idxd.Index.KeepInMemory()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error slurping index to memory: %v", err)
|
|
|
|
}
|
|
|
|
sh.SetCorpus(corpus)
|
2018-05-01 19:52:02 +00:00
|
|
|
cl, err := camliClient.New(camliClient.OptionServer("http://whatever.fake"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-14 20:14:34 +00:00
|
|
|
fcl := &fakeClient{cl, sh}
|
|
|
|
ph := &publishHandler{
|
|
|
|
rootName: rootName,
|
|
|
|
cl: fcl,
|
|
|
|
}
|
|
|
|
if err := ph.initRootNode(); err != nil {
|
|
|
|
t.Fatalf("initRootNode: %v", err)
|
2014-03-17 23:27:37 +00:00
|
|
|
}
|
2011-07-06 23:33:31 +00:00
|
|
|
|
|
|
|
for ti, tt := range publishURLTests {
|
2011-07-07 01:38:27 +00:00
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
if !strings.HasPrefix(tt.path, "/pics/") {
|
|
|
|
panic("expected /pics/ prefix on " + tt.path)
|
2011-07-06 23:33:31 +00:00
|
|
|
}
|
2011-07-07 01:38:27 +00:00
|
|
|
req, _ := http.NewRequest("GET", "http://foo.com"+tt.path, nil)
|
|
|
|
|
|
|
|
pfxh := &httputil.PrefixHandler{
|
2016-06-28 00:12:47 +00:00
|
|
|
Prefix: "/",
|
2011-07-07 01:38:27 +00:00
|
|
|
Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
|
2016-06-28 00:12:47 +00:00
|
|
|
// Because the app handler strips the prefix before passing it on to the app
|
|
|
|
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/pics/")
|
2014-06-14 20:14:34 +00:00
|
|
|
pr, err := ph.NewRequest(rw, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("test #%d, NewRequest: %v", ti, err)
|
|
|
|
}
|
2011-07-07 01:38:27 +00:00
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
err = pr.findSubject()
|
2011-07-07 01:38:27 +00:00
|
|
|
if tt.subject != "" {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("test #%d, findSubject: %v", ti, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if pr.subject.String() != tt.subject {
|
|
|
|
t.Errorf("test #%d, got subject %q, want %q", ti, pr.subject, tt.subject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pr.subres != tt.subres {
|
|
|
|
t.Errorf("test #%d, got subres %q, want %q", ti, pr.subres, tt.subres)
|
|
|
|
}
|
|
|
|
}),
|
2011-07-06 23:33:31 +00:00
|
|
|
}
|
2011-07-07 01:38:27 +00:00
|
|
|
pfxh.ServeHTTP(rw, req)
|
2011-07-06 18:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-17 22:05:53 +00:00
|
|
|
|
|
|
|
func TestPublishMembers(t *testing.T) {
|
|
|
|
rootName := "foo"
|
2014-03-17 23:27:37 +00:00
|
|
|
idxd := setupContent(rootName)
|
2013-12-17 22:05:53 +00:00
|
|
|
|
2018-05-01 19:52:02 +00:00
|
|
|
ownerRef := indextest.PubKey
|
|
|
|
owner := index.NewOwner(indextest.KeyID, ownerRef.BlobRef())
|
|
|
|
sh := search.NewHandler(idxd.Index, owner)
|
2014-03-17 23:27:37 +00:00
|
|
|
corpus, err := idxd.Index.KeepInMemory()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error slurping index to memory: %v", err)
|
|
|
|
}
|
|
|
|
sh.SetCorpus(corpus)
|
2018-05-01 19:52:02 +00:00
|
|
|
cl, err := camliClient.New(camliClient.OptionServer("http://whatever.fake"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-14 20:14:34 +00:00
|
|
|
fcl := &fakeClient{cl, sh}
|
|
|
|
ph := &publishHandler{
|
|
|
|
rootName: rootName,
|
|
|
|
cl: fcl,
|
2013-12-17 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest("GET", "http://foo.com/pics", nil)
|
|
|
|
|
|
|
|
pfxh := &httputil.PrefixHandler{
|
|
|
|
Prefix: "/pics/",
|
|
|
|
Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
|
2014-06-14 20:14:34 +00:00
|
|
|
pr, err := ph.NewRequest(rw, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewRequest: %v", err)
|
|
|
|
}
|
2013-12-17 22:05:53 +00:00
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
res, err := pr.ph.deepDescribe(pr.subject)
|
2013-12-17 22:05:53 +00:00
|
|
|
if err != nil {
|
2014-06-14 20:14:34 +00:00
|
|
|
t.Fatalf("deepDescribe: %v", err)
|
2013-12-17 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
members, err := pr.subjectMembers(res.Meta)
|
2013-12-17 22:05:53 +00:00
|
|
|
if len(members.Members) != 2 {
|
|
|
|
t.Errorf("Expected two members in publish root (one camlipath, one camlimember), got %d", len(members.Members))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
pfxh.ServeHTTP(rw, req)
|
|
|
|
}
|