mirror of https://github.com/perkeep/perkeep.git
Add integration test for Android camput environment.
Fixes camlistore.org/issue/323 Change-Id: Iefa5b073aa6d6b00899e7e17afb3bcf2f18ad6b3
This commit is contained in:
parent
dde76aec90
commit
b84c2457ae
|
@ -17,8 +17,14 @@ limitations under the License.
|
|||
package integration
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"camlistore.org/pkg/blob"
|
||||
"camlistore.org/pkg/test"
|
||||
|
@ -49,3 +55,104 @@ func TestCamputPermanode(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mustTempDir(t *testing.T) (name string, cleanup func()) {
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return dir, func() { os.RemoveAll(dir) }
|
||||
}
|
||||
|
||||
func mustWriteFile(t *testing.T, path, contents string) {
|
||||
err := ioutil.WriteFile(path, []byte(contents), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Run camput in the environment it runs in under the Android app.
|
||||
// This matches how camput is used in UploadThread.java.
|
||||
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
|
||||
cacheDir, clean := mustTempDir(t)
|
||||
defer clean()
|
||||
env := []string{
|
||||
"CAMPUT_ANDROID_OUTPUT=1",
|
||||
"CAMLI_CACHE_DIR=" + cacheDir,
|
||||
}
|
||||
cmd := w.CmdWithEnv("camput",
|
||||
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)
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cmd.Process.Kill()
|
||||
|
||||
srcDir, clean := mustTempDir(t)
|
||||
defer clean()
|
||||
|
||||
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):
|
||||
t.Fatal("timeout waiting for camput to end")
|
||||
case err := <-waitc:
|
||||
if err != nil {
|
||||
t.Errorf("camput exited uncleanly: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ import (
|
|||
//
|
||||
// It's used to run the actual Camlistore binaries (camlistored,
|
||||
// camput, camget, camtool, etc) together in large tests, including
|
||||
// building them, finding them, and wiring the up in an isolated way.
|
||||
// building them, finding them, and wiring them up in an isolated way.
|
||||
type World struct {
|
||||
camRoot string // typically $GOPATH[0]/src/camlistore.org
|
||||
tempDir string
|
||||
|
@ -160,6 +160,10 @@ func (w *World) Stop() {
|
|||
}
|
||||
|
||||
func (w *World) Cmd(binary string, args ...string) *exec.Cmd {
|
||||
return w.CmdWithEnv(binary, os.Environ(), args...)
|
||||
}
|
||||
|
||||
func (w *World) CmdWithEnv(binary string, env []string, args ...string) *exec.Cmd {
|
||||
cmd := exec.Command(filepath.Join(w.camRoot, "bin", binary), args...)
|
||||
switch binary {
|
||||
case "camget", "camput", "camtool", "cammount":
|
||||
|
@ -172,7 +176,7 @@ func (w *World) Cmd(binary string, args ...string) *exec.Cmd {
|
|||
"CAMLI_KEYID=26F5ABDA",
|
||||
"CAMLI_DEV_KEYBLOBS=" + filepath.Join(clientConfigDir, "keyblobs"),
|
||||
"CAMLI_AUTH=userpass:testuser:passTestWorld",
|
||||
}, os.Environ()...)
|
||||
}, env...)
|
||||
default:
|
||||
panic("Unknown binary " + binary)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue