2013-07-21 05:36:53 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2013 The Perkeep Authors
|
2013-07-21 05:36:53 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2014-01-07 04:01:07 +00:00
|
|
|
"bufio"
|
2016-11-24 23:51:55 +00:00
|
|
|
"bytes"
|
2014-01-07 04:01:07 +00:00
|
|
|
"fmt"
|
2014-01-19 22:50:34 +00:00
|
|
|
"net/http"
|
2014-02-07 23:02:22 +00:00
|
|
|
"net/url"
|
2014-01-07 04:01:07 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-07-21 05:36:53 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-01-07 04:01:07 +00:00
|
|
|
"time"
|
2013-07-21 05:36:53 +00:00
|
|
|
|
2016-04-20 23:43:47 +00:00
|
|
|
"github.com/gorilla/websocket"
|
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
|
|
|
"perkeep.org/pkg/test"
|
2013-07-21 05:36:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test that running:
|
2023-01-01 04:40:45 +00:00
|
|
|
//
|
|
|
|
// $ pk-put permanode
|
|
|
|
//
|
2018-04-22 19:35:28 +00:00
|
|
|
// ... creates and uploads a permanode, and that we can pk-get it back.
|
2013-07-21 05:36:53 +00:00
|
|
|
func TestCamputPermanode(t *testing.T) {
|
|
|
|
w := test.GetWorld(t)
|
2014-02-07 23:02:22 +00:00
|
|
|
br := w.NewPermanode(t)
|
2013-07-21 05:36:53 +00:00
|
|
|
|
2018-04-22 19:35:28 +00:00
|
|
|
out := test.MustRunCmd(t, w.Cmd("pk-get", br.String()))
|
2013-07-21 05:36:53 +00:00
|
|
|
mustHave := []string{
|
|
|
|
`{"camliVersion": 1,`,
|
|
|
|
`"camliSigner": "`,
|
|
|
|
`"camliType": "permanode",`,
|
|
|
|
`random": "`,
|
|
|
|
`,"camliSig":"`,
|
|
|
|
}
|
|
|
|
for _, str := range mustHave {
|
|
|
|
if !strings.Contains(out, str) {
|
|
|
|
t.Errorf("Expected permanode response to contain %q; it didn't. Got: %s", str, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 04:01:07 +00:00
|
|
|
|
2014-02-07 23:02:22 +00:00
|
|
|
func TestWebsocketQuery(t *testing.T) {
|
|
|
|
w := test.GetWorld(t)
|
|
|
|
pn := w.NewPermanode(t)
|
2018-03-06 21:39:14 +00:00
|
|
|
test.MustRunCmd(t, w.Cmd("pk-put", "attr", pn.String(), "tag", "foo"))
|
2014-02-07 23:02:22 +00:00
|
|
|
|
|
|
|
check := func(err error) {
|
|
|
|
if err != nil {
|
2018-01-12 14:24:38 +00:00
|
|
|
t.Fatalf("%v", err)
|
2014-02-07 23:02:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bufSize = 1 << 20
|
|
|
|
|
2018-01-12 14:24:38 +00:00
|
|
|
dialer := websocket.Dialer{
|
|
|
|
ReadBufferSize: bufSize,
|
|
|
|
WriteBufferSize: bufSize,
|
2014-02-07 23:02:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 14:24:38 +00:00
|
|
|
searchURL := (&url.URL{Scheme: "ws", Host: w.Addr(), Path: w.SearchHandlerPath() + "ws"}).String()
|
|
|
|
wsHeaders := http.Header{
|
|
|
|
"Origin": {"http://" + w.Addr()},
|
|
|
|
}
|
|
|
|
|
|
|
|
wc, _, err := dialer.Dial(searchURL, wsHeaders)
|
2014-02-07 23:02:22 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
msg, err := wc.NextWriter(websocket.TextMessage)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
_, err = msg.Write([]byte(`{"tag": "foo", "query": { "expression": "tag:foo" }}`))
|
|
|
|
check(err)
|
|
|
|
check(msg.Close())
|
|
|
|
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
inType, inMsg, err := wc.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
errc <- err
|
|
|
|
return
|
|
|
|
}
|
2014-09-01 22:57:18 +00:00
|
|
|
if !strings.HasPrefix(string(inMsg), `{"tag":"_status"`) {
|
|
|
|
errc <- fmt.Errorf("unexpected message type=%d msg=%q, wanted status update", inType, inMsg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inType, inMsg, err = wc.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
errc <- err
|
|
|
|
return
|
|
|
|
}
|
2014-02-07 23:02:22 +00:00
|
|
|
if strings.Contains(string(inMsg), pn.String()) {
|
|
|
|
errc <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errc <- fmt.Errorf("unexpected message type=%d msg=%q", inType, inMsg)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Error("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-19 22:50:34 +00:00
|
|
|
func TestInternalHandler(t *testing.T) {
|
|
|
|
w := test.GetWorld(t)
|
|
|
|
tests := map[string]int{
|
2023-01-24 11:23:01 +00:00
|
|
|
"/": http.StatusOK,
|
|
|
|
"/test-that-root-handler-returns-404": http.StatusNotFound,
|
|
|
|
"/no-http-storage/": http.StatusUnauthorized,
|
|
|
|
"/no-http-handler/": http.StatusUnauthorized,
|
|
|
|
"/bs-and-maybe-also-index/camli": http.StatusBadRequest,
|
|
|
|
"/bs/camli/sha1-b2201302e129a4396a323cb56283cddeef11bbe8": http.StatusNotFound,
|
|
|
|
"/no-http-storage/camli/sha1-b2201302e129a4396a323cb56283cddeef11bbe8": http.StatusUnauthorized,
|
2014-01-19 22:50:34 +00:00
|
|
|
}
|
|
|
|
for suffix, want := range tests {
|
|
|
|
res, err := http.Get(w.ServerBaseURL() + suffix)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("On %s: %v", suffix, err)
|
|
|
|
}
|
|
|
|
if res.StatusCode != want {
|
|
|
|
t.Errorf("For %s: Status = %d; want %d", suffix, res.StatusCode, want)
|
|
|
|
}
|
|
|
|
res.Body.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 23:51:55 +00:00
|
|
|
func TestNoTestingLinking(t *testing.T) {
|
|
|
|
w, err := test.NewWorld()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
help, err := w.Help()
|
|
|
|
if err != nil {
|
2018-04-21 18:04:53 +00:00
|
|
|
t.Fatalf("Error running perkeepd -help: %v, %v", string(help), err)
|
2016-11-24 23:51:55 +00:00
|
|
|
}
|
|
|
|
sc := bufio.NewScanner(bytes.NewReader(help))
|
|
|
|
for sc.Scan() {
|
|
|
|
l := strings.TrimSpace(sc.Text())
|
|
|
|
if strings.HasPrefix(l, "-test.") {
|
2018-04-21 18:04:53 +00:00
|
|
|
t.Fatal("test flag detected in help output of perkeepd, because testing pkg got linked into binary")
|
2016-11-24 23:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 04:01:07 +00:00
|
|
|
func mustWriteFile(t *testing.T, path, contents string) {
|
2023-01-23 18:25:14 +00:00
|
|
|
err := os.WriteFile(path, []byte(contents), 0644)
|
2014-01-07 04:01:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 21:39:14 +00:00
|
|
|
// Run pk-put in the environment it runs in under the Android app.
|
|
|
|
// This matches how pk-put is used in UploadThread.java.
|
2014-01-07 04:01:07 +00:00
|
|
|
func TestAndroidCamputFile(t *testing.T) {
|
|
|
|
w := test.GetWorld(t)
|
|
|
|
// UploadThread.java sets:
|
|
|
|
// CAMLI_AUTH (set by w.CmdWithEnv)
|
|
|
|
// CAMLI_TRUSTED_CERT (not needed)
|
|
|
|
// CAMLI_CACHE_DIR
|
|
|
|
// CAMPUT_ANDROID_OUTPUT=1
|
2022-01-23 11:24:37 +00:00
|
|
|
cacheDir := t.TempDir()
|
2018-05-01 21:52:17 +00:00
|
|
|
env := append(os.Environ(),
|
2014-01-07 04:01:07 +00:00
|
|
|
"CAMPUT_ANDROID_OUTPUT=1",
|
2018-05-01 21:52:17 +00:00
|
|
|
"CAMLI_CACHE_DIR="+cacheDir,
|
|
|
|
)
|
2018-03-06 21:39:14 +00:00
|
|
|
cmd := w.CmdWithEnv("pk-put",
|
2014-01-07 04:01:07 +00:00
|
|
|
env,
|
|
|
|
"--server="+w.ServerBaseURL(),
|
|
|
|
"file",
|
|
|
|
"-stdinargs",
|
|
|
|
"-vivify")
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
in, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-24 18:48:28 +00:00
|
|
|
if err := w.Ping(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-07 04:01:07 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
2022-01-23 11:24:37 +00:00
|
|
|
srcDir := t.TempDir()
|
2014-01-07 04:01:07 +00:00
|
|
|
|
|
|
|
file1 := filepath.Join(srcDir, "file1.txt")
|
|
|
|
mustWriteFile(t, file1, "contents 1")
|
|
|
|
file2 := filepath.Join(srcDir, "file2.txt")
|
|
|
|
mustWriteFile(t, file2, "contents 2 longer length")
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
fmt.Fprintf(in, "%s\n", file1)
|
|
|
|
fmt.Fprintf(in, "%s\n", file2)
|
|
|
|
}()
|
|
|
|
|
|
|
|
waitc := make(chan error)
|
|
|
|
go func() {
|
|
|
|
sc := bufio.NewScanner(out)
|
|
|
|
fileUploaded := 0
|
|
|
|
for sc.Scan() {
|
|
|
|
t.Logf("Got: %q", sc.Text())
|
|
|
|
f := strings.Fields(sc.Text())
|
|
|
|
if len(f) == 0 {
|
|
|
|
t.Logf("empty text?")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if f[0] == "FILE_UPLOADED" {
|
|
|
|
fileUploaded++
|
|
|
|
if fileUploaded == 2 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
in.Close()
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
go func() {
|
|
|
|
waitc <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
2018-03-06 21:39:14 +00:00
|
|
|
t.Fatal("timeout waiting for pk-put to end")
|
2014-01-07 04:01:07 +00:00
|
|
|
case err := <-waitc:
|
|
|
|
if err != nil {
|
2018-03-06 21:39:14 +00:00
|
|
|
t.Errorf("pk-put exited uncleanly: %v", err)
|
2014-01-07 04:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|