mirror of https://github.com/perkeep/perkeep.git
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
This commit is contained in:
parent
d7e5128bbe
commit
dabca278be
|
@ -17,6 +17,9 @@ limitations under the License.
|
||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -61,6 +64,7 @@ func TestCamgetSymlink(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
out := test.MustRunCmd(t, w.Cmd("camput", "file", srcDir))
|
out := test.MustRunCmd(t, w.Cmd("camput", "file", srcDir))
|
||||||
|
// TODO(mpl): rm call and delete pkg.
|
||||||
asserts.ExpectBool(t, true, out != "", "camput")
|
asserts.ExpectBool(t, true, out != "", "camput")
|
||||||
br := strings.Split(out, "\n")[0]
|
br := strings.Split(out, "\n")[0]
|
||||||
dstDir, err := ioutil.TempDir("", "camget-test-")
|
dstDir, err := ioutil.TempDir("", "camget-test-")
|
||||||
|
@ -156,3 +160,56 @@ func TestCamgetSocket(t *testing.T) {
|
||||||
t.Fatalf("Retrieved file %s: Not a socket", name)
|
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"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue