From dabca278beb3513a60e6005722ecdaa07d8a42a4 Mon Sep 17 00:00:00 2001 From: mpl Date: Mon, 10 Nov 2014 22:33:00 +0100 Subject: [PATCH] test/integration: test for camget file -contents Also tests that we don't bother fetching if we already have the file. http://camlistore.org/issue/526 Change-Id: I852b63051daecf82e2376ca0683abffb73f47393 --- pkg/test/integration/camget_test.go | 57 +++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/pkg/test/integration/camget_test.go b/pkg/test/integration/camget_test.go index f9fa41e49..fd2357f44 100644 --- a/pkg/test/integration/camget_test.go +++ b/pkg/test/integration/camget_test.go @@ -17,6 +17,9 @@ limitations under the License. package integration import ( + "bytes" + "errors" + "fmt" "io/ioutil" "os" "path/filepath" @@ -61,6 +64,7 @@ func TestCamgetSymlink(t *testing.T) { } out := test.MustRunCmd(t, w.Cmd("camput", "file", srcDir)) + // TODO(mpl): rm call and delete pkg. asserts.ExpectBool(t, true, out != "", "camput") br := strings.Split(out, "\n")[0] dstDir, err := ioutil.TempDir("", "camget-test-") @@ -156,3 +160,56 @@ func TestCamgetSocket(t *testing.T) { t.Fatalf("Retrieved file %s: Not a socket", name) } } + +// Test that: +// 1) `camget -contents' can restore a regular file correctly. +// 2) if the file already exists, and has the same size as the one held by the server, +// stop early and do not even fetch it from the server. +func TestCamgetFile(t *testing.T) { + dirName, err := ioutil.TempDir("", "camli-TestCamgetFile") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dirName) + f, err := os.Create(filepath.Join(dirName, "test.txt")) + if err != nil { + t.Fatal(err) + } + filename := f.Name() + contents := "not empty anymore" + if _, err := f.Write([]byte(contents)); err != nil { + t.Fatal(err) + } + if err := f.Close(); err != nil { + t.Fatal(err) + } + outDir := filepath.Join(dirName, "fetched") + if err := os.Mkdir(outDir, 0700); err != nil { + t.Fatal(err) + } + + w := test.GetWorld(t) + out := test.MustRunCmd(t, w.Cmd("camput", "file", filename)) + + br := strings.Split(out, "\n")[0] + _ = test.MustRunCmd(t, w.Cmd("camget", "-o", outDir, "-contents", br)) + + fetchedName := filepath.Join(outDir, "test.txt") + b, err := ioutil.ReadFile(fetchedName) + if err != nil { + t.Fatal(err) + } + if string(b) != contents { + t.Fatalf("fetched file different from original file, got contents %q, wanted %q", b, contents) + } + + var stderr bytes.Buffer + c := w.Cmd("camget", "-o", outDir, "-contents", "-verbose", br) + c.Stderr = &stderr + if err := c.Run(); err != nil { + t.Fatalf("running second camget: %v", err) + } + if !strings.Contains(stderr.String(), fmt.Sprintf("Skipping %s; already exists.", fetchedName)) { + t.Fatal(errors.New("Was expecting info message about local file already existing")) + } +}