2011-02-23 02:48:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
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 localdisk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-10-25 15:31:16 +00:00
|
|
|
"io/ioutil"
|
2011-02-23 02:48:48 +00:00
|
|
|
"os"
|
2013-10-25 15:31:16 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2013-08-23 14:20:21 +00:00
|
|
|
"strconv"
|
2011-02-23 02:48:48 +00:00
|
|
|
"sync"
|
2011-02-24 02:25:00 +00:00
|
|
|
"testing"
|
2013-07-07 19:15:19 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-11-30 21:58:16 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/blobserver/storagetest"
|
2013-07-07 19:15:19 +00:00
|
|
|
"camlistore.org/pkg/test"
|
2013-08-04 02:54:30 +00:00
|
|
|
. "camlistore.org/pkg/test/asserts"
|
2011-02-23 02:48:48 +00:00
|
|
|
)
|
|
|
|
|
2011-05-09 16:11:18 +00:00
|
|
|
func cleanUp(ds *DiskStorage) {
|
2011-02-23 02:48:48 +00:00
|
|
|
os.RemoveAll(ds.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
epochLock sync.Mutex
|
|
|
|
rootEpoch = 0
|
|
|
|
)
|
|
|
|
|
2011-05-09 16:11:18 +00:00
|
|
|
func NewStorage(t *testing.T) *DiskStorage {
|
2011-02-23 02:48:48 +00:00
|
|
|
epochLock.Lock()
|
|
|
|
rootEpoch++
|
|
|
|
path := fmt.Sprintf("%s/camli-testroot-%d-%d", os.TempDir(), os.Getpid(), rootEpoch)
|
|
|
|
epochLock.Unlock()
|
|
|
|
if err := os.Mkdir(path, 0755); err != nil {
|
|
|
|
t.Fatalf("Failed to create temp directory %q: %v", path, err)
|
|
|
|
}
|
|
|
|
ds, err := New(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to run New: %v", err)
|
|
|
|
}
|
2011-05-09 16:11:18 +00:00
|
|
|
return ds
|
2011-02-23 02:48:48 +00:00
|
|
|
}
|
|
|
|
|
2011-06-01 01:58:58 +00:00
|
|
|
func TestUploadDup(t *testing.T) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
2013-07-07 19:15:19 +00:00
|
|
|
tb := &test.Blob{"Foo"}
|
|
|
|
tb.MustUpload(t, ds)
|
|
|
|
tb.MustUpload(t, ds)
|
2011-06-01 01:58:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 17:36:58 +00:00
|
|
|
func TestReceiveStat(t *testing.T) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
|
|
|
|
2013-07-07 19:15:19 +00:00
|
|
|
tb := &test.Blob{"Foo"}
|
|
|
|
tb.MustUpload(t, ds)
|
2011-02-24 02:25:00 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
ch := make(chan blob.SizedRef, 0)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
errch := make(chan error, 1)
|
2011-02-24 02:25:00 +00:00
|
|
|
go func() {
|
2013-08-21 20:57:28 +00:00
|
|
|
errch <- ds.StatBlobs(ch, tb.BlobRefSlice())
|
2011-02-24 20:52:36 +00:00
|
|
|
close(ch)
|
2011-02-24 02:25:00 +00:00
|
|
|
}()
|
|
|
|
got := 0
|
2011-02-25 17:36:58 +00:00
|
|
|
for sb := range ch {
|
2011-02-24 02:25:00 +00:00
|
|
|
got++
|
2011-02-25 17:36:58 +00:00
|
|
|
tb.AssertMatches(t, sb)
|
|
|
|
break
|
2011-02-24 02:25:00 +00:00
|
|
|
}
|
2011-02-26 21:31:23 +00:00
|
|
|
AssertInt(t, 1, got, "number stat results")
|
|
|
|
AssertNil(t, <-errch, "result from stat")
|
2011-02-24 02:25:00 +00:00
|
|
|
}
|
|
|
|
|
2011-02-26 21:31:23 +00:00
|
|
|
func TestMultiStat(t *testing.T) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
|
|
|
|
2013-07-07 19:15:19 +00:00
|
|
|
blobfoo := &test.Blob{"foo"}
|
|
|
|
blobbar := &test.Blob{"bar!"}
|
|
|
|
blobfoo.MustUpload(t, ds)
|
|
|
|
blobbar.MustUpload(t, ds)
|
2011-02-26 21:31:23 +00:00
|
|
|
|
2013-10-11 03:05:14 +00:00
|
|
|
need := make(map[blob.Ref]bool)
|
|
|
|
need[blobfoo.BlobRef()] = true
|
|
|
|
need[blobbar.BlobRef()] = true
|
2011-02-26 21:31:23 +00:00
|
|
|
|
2013-08-23 14:20:21 +00:00
|
|
|
blobs := []blob.Ref{blobfoo.BlobRef(), blobbar.BlobRef()}
|
|
|
|
|
|
|
|
// In addition to the two "foo" and "bar" blobs, add
|
|
|
|
// maxParallelStats other dummy blobs, to exercise the stat
|
|
|
|
// rate-limiting (which had a deadlock once after a cleanup)
|
|
|
|
for i := 0; i < maxParallelStats; i++ {
|
|
|
|
blobs = append(blobs, blob.SHA1FromString(strconv.Itoa(i)))
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
ch := make(chan blob.SizedRef, 0)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
errch := make(chan error, 1)
|
2011-02-26 21:31:23 +00:00
|
|
|
go func() {
|
2013-08-23 14:20:21 +00:00
|
|
|
errch <- ds.StatBlobs(ch, blobs)
|
2011-02-26 21:31:23 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
got := 0
|
|
|
|
for sb := range ch {
|
|
|
|
got++
|
2013-10-11 03:05:14 +00:00
|
|
|
if !need[sb.Ref] {
|
|
|
|
t.Errorf("didn't need %s", sb.Ref)
|
|
|
|
}
|
|
|
|
delete(need, sb.Ref)
|
|
|
|
}
|
|
|
|
if want := 2; got != want {
|
|
|
|
t.Errorf("number stats = %d; want %d", got, want)
|
|
|
|
}
|
|
|
|
if err := <-errch; err != nil {
|
|
|
|
t.Errorf("StatBlobs: %v", err)
|
|
|
|
}
|
|
|
|
if len(need) != 0 {
|
|
|
|
t.Errorf("Not all stat results returned; still need %d", len(need))
|
2011-02-26 21:31:23 +00:00
|
|
|
}
|
2011-03-13 02:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingGetReturnsNoEnt(t *testing.T) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
2013-07-07 19:15:19 +00:00
|
|
|
foo := &test.Blob{"foo"}
|
2011-03-13 02:28:05 +00:00
|
|
|
|
|
|
|
blob, _, err := ds.Fetch(foo.BlobRef())
|
2012-03-20 01:44:50 +00:00
|
|
|
if err != os.ErrNotExist {
|
|
|
|
t.Errorf("expected ErrNotExist; got %v", err)
|
2011-03-13 02:28:05 +00:00
|
|
|
}
|
|
|
|
if blob != nil {
|
|
|
|
t.Errorf("expected nil blob; got a value")
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:31:16 +00:00
|
|
|
|
|
|
|
func rename(old, new string) error {
|
|
|
|
if err := os.Rename(old, new); err != nil {
|
|
|
|
if renameErr := mapRenameError(err, old, new); renameErr != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type file struct {
|
|
|
|
name string
|
|
|
|
contents string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRename(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.Skip("Skipping test if not on windows")
|
|
|
|
}
|
|
|
|
files := []file{
|
|
|
|
file{name: filepath.Join(os.TempDir(), "foo"), contents: "foo"},
|
|
|
|
file{name: filepath.Join(os.TempDir(), "bar"), contents: "barr"},
|
|
|
|
file{name: filepath.Join(os.TempDir(), "baz"), contents: "foo"},
|
|
|
|
}
|
|
|
|
for _, v := range files {
|
|
|
|
if err := ioutil.WriteFile(v.name, []byte(v.contents), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwriting "bar" with "foo" should not be allowed
|
|
|
|
if err := rename(files[0].name, files[1].name); err == nil {
|
|
|
|
t.Fatalf("Renaming %v into %v should not succeed", files[0].name, files[1].name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// but overwriting "baz" with "foo" is ok because they have the same
|
|
|
|
// contents
|
|
|
|
if err := rename(files[0].name, files[2].name); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2013-11-30 21:58:16 +00:00
|
|
|
|
|
|
|
func TestLocaldisk(t *testing.T) {
|
|
|
|
storagetest.Test(t, func(t *testing.T) (blobserver.Storage, func()) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
return ds, func() { cleanUp(ds) }
|
|
|
|
})
|
|
|
|
}
|