github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/test/integration/camlistore_test.go (about)

     1  /*
     2  Copyright 2013 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package integration
    18  
    19  import (
    20  	"bufio"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"os"
    25  	"path/filepath"
    26  	"strings"
    27  	"testing"
    28  	"time"
    29  
    30  	"camlistore.org/pkg/blob"
    31  	"camlistore.org/pkg/test"
    32  )
    33  
    34  // Test that running:
    35  //   $ camput permanode
    36  // ... creates and uploads a permanode, and that we can camget it back.
    37  func TestCamputPermanode(t *testing.T) {
    38  	w := test.GetWorld(t)
    39  	out := test.MustRunCmd(t, w.Cmd("camput", "permanode"))
    40  	br, ok := blob.Parse(strings.TrimSpace(out))
    41  	if !ok {
    42  		t.Fatalf("Expected permanode in stdout; got %q", out)
    43  	}
    44  
    45  	out = test.MustRunCmd(t, w.Cmd("camget", br.String()))
    46  	mustHave := []string{
    47  		`{"camliVersion": 1,`,
    48  		`"camliSigner": "`,
    49  		`"camliType": "permanode",`,
    50  		`random": "`,
    51  		`,"camliSig":"`,
    52  	}
    53  	for _, str := range mustHave {
    54  		if !strings.Contains(out, str) {
    55  			t.Errorf("Expected permanode response to contain %q; it didn't. Got: %s", str, out)
    56  		}
    57  	}
    58  }
    59  
    60  func TestInternalHandler(t *testing.T) {
    61  	w := test.GetWorld(t)
    62  	tests := map[string]int{
    63  		"/no-http-storage/":                                                    401,
    64  		"/no-http-handler/":                                                    401,
    65  		"/good-status/":                                                        200,
    66  		"/bs-and-maybe-also-index/camli":                                       400,
    67  		"/bs/camli/sha1-b2201302e129a4396a323cb56283cddeef11bbe8":              404,
    68  		"/no-http-storage/camli/sha1-b2201302e129a4396a323cb56283cddeef11bbe8": 401,
    69  	}
    70  	for suffix, want := range tests {
    71  		res, err := http.Get(w.ServerBaseURL() + suffix)
    72  		if err != nil {
    73  			t.Fatalf("On %s: %v", suffix, err)
    74  		}
    75  		if res.StatusCode != want {
    76  			t.Errorf("For %s: Status = %d; want %d", suffix, res.StatusCode, want)
    77  		}
    78  		res.Body.Close()
    79  	}
    80  }
    81  
    82  func mustTempDir(t *testing.T) (name string, cleanup func()) {
    83  	dir, err := ioutil.TempDir("", "")
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	return dir, func() { os.RemoveAll(dir) }
    88  }
    89  
    90  func mustWriteFile(t *testing.T, path, contents string) {
    91  	err := ioutil.WriteFile(path, []byte(contents), 0644)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  }
    96  
    97  // Run camput in the environment it runs in under the Android app.
    98  // This matches how camput is used in UploadThread.java.
    99  func TestAndroidCamputFile(t *testing.T) {
   100  	w := test.GetWorld(t)
   101  	// UploadThread.java sets:
   102  	//   CAMLI_AUTH (set by w.CmdWithEnv)
   103  	//   CAMLI_TRUSTED_CERT (not needed)
   104  	//   CAMLI_CACHE_DIR
   105  	//   CAMPUT_ANDROID_OUTPUT=1
   106  	cacheDir, clean := mustTempDir(t)
   107  	defer clean()
   108  	env := []string{
   109  		"CAMPUT_ANDROID_OUTPUT=1",
   110  		"CAMLI_CACHE_DIR=" + cacheDir,
   111  	}
   112  	cmd := w.CmdWithEnv("camput",
   113  		env,
   114  		"--server="+w.ServerBaseURL(),
   115  		"file",
   116  		"-stdinargs",
   117  		"-vivify")
   118  	cmd.Stderr = os.Stderr
   119  	in, err := cmd.StdinPipe()
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	out, err := cmd.StdoutPipe()
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	if err := cmd.Start(); err != nil {
   128  		t.Fatal(err)
   129  	}
   130  	defer cmd.Process.Kill()
   131  
   132  	srcDir, clean := mustTempDir(t)
   133  	defer clean()
   134  
   135  	file1 := filepath.Join(srcDir, "file1.txt")
   136  	mustWriteFile(t, file1, "contents 1")
   137  	file2 := filepath.Join(srcDir, "file2.txt")
   138  	mustWriteFile(t, file2, "contents 2 longer length")
   139  
   140  	go func() {
   141  		fmt.Fprintf(in, "%s\n", file1)
   142  		fmt.Fprintf(in, "%s\n", file2)
   143  	}()
   144  
   145  	waitc := make(chan error)
   146  	go func() {
   147  		sc := bufio.NewScanner(out)
   148  		fileUploaded := 0
   149  		for sc.Scan() {
   150  			t.Logf("Got: %q", sc.Text())
   151  			f := strings.Fields(sc.Text())
   152  			if len(f) == 0 {
   153  				t.Logf("empty text?")
   154  				continue
   155  			}
   156  			if f[0] == "FILE_UPLOADED" {
   157  				fileUploaded++
   158  				if fileUploaded == 2 {
   159  					break
   160  				}
   161  			}
   162  		}
   163  		in.Close()
   164  		if err := sc.Err(); err != nil {
   165  			t.Error(err)
   166  		}
   167  	}()
   168  
   169  	defer cmd.Process.Kill()
   170  	go func() {
   171  		waitc <- cmd.Wait()
   172  	}()
   173  	select {
   174  	case <-time.After(5 * time.Second):
   175  		t.Fatal("timeout waiting for camput to end")
   176  	case err := <-waitc:
   177  		if err != nil {
   178  			t.Errorf("camput exited uncleanly: %v", err)
   179  		}
   180  	}
   181  }