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 (
|
2012-03-20 01:44:50 +00:00
|
|
|
. "camlistore.org/pkg/test/asserts"
|
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
|
|
|
"camlistore.org/pkg/blobref"
|
|
|
|
"camlistore.org/pkg/blobserver"
|
2011-02-24 02:25:00 +00:00
|
|
|
"crypto/sha1"
|
2011-02-23 02:48:48 +00:00
|
|
|
"fmt"
|
2011-02-24 02:25:00 +00:00
|
|
|
"io"
|
2011-02-23 02:48:48 +00:00
|
|
|
"os"
|
2011-02-24 02:25:00 +00:00
|
|
|
"strings"
|
2011-02-23 02:48:48 +00:00
|
|
|
"sync"
|
2011-02-24 02:25:00 +00:00
|
|
|
"testing"
|
2011-02-25 17:36:58 +00:00
|
|
|
"time"
|
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-02-24 02:25:00 +00:00
|
|
|
type testBlob struct {
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBlob) BlobRef() *blobref.BlobRef {
|
|
|
|
h := sha1.New()
|
|
|
|
h.Write([]byte(tb.val))
|
|
|
|
return blobref.FromHash("sha1", h)
|
|
|
|
}
|
|
|
|
|
2011-02-25 17:36:58 +00:00
|
|
|
func (tb *testBlob) BlobRefSlice() []*blobref.BlobRef {
|
|
|
|
return []*blobref.BlobRef{tb.BlobRef()}
|
|
|
|
}
|
|
|
|
|
2011-02-24 02:25:00 +00:00
|
|
|
func (tb *testBlob) Size() int64 {
|
|
|
|
return int64(len(tb.val))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBlob) Reader() io.Reader {
|
|
|
|
return strings.NewReader(tb.val)
|
|
|
|
}
|
|
|
|
|
2011-05-10 23:13:37 +00:00
|
|
|
func (tb *testBlob) AssertMatches(t *testing.T, sb blobref.SizedBlobRef) {
|
2011-02-25 17:36:58 +00:00
|
|
|
if sb.Size != tb.Size() {
|
|
|
|
t.Fatalf("Got size %d; expected %d", sb.Size, tb.Size())
|
|
|
|
}
|
|
|
|
if sb.BlobRef.String() != tb.BlobRef().String() {
|
|
|
|
t.Fatalf("Got blob %q; expected %q", sb.BlobRef.String(), tb.BlobRef())
|
|
|
|
}
|
|
|
|
}
|
2011-02-24 02:25:00 +00:00
|
|
|
|
2011-02-25 17:36:58 +00:00
|
|
|
func (tb *testBlob) ExpectUploadBlob(t *testing.T, ds blobserver.BlobReceiver) {
|
2011-05-09 16:11:18 +00:00
|
|
|
sb, err := ds.ReceiveBlob(tb.BlobRef(), tb.Reader())
|
2011-02-24 02:25:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReceiveBlob error: %v", err)
|
|
|
|
}
|
2011-02-25 17:36:58 +00:00
|
|
|
tb.AssertMatches(t, sb)
|
|
|
|
}
|
|
|
|
|
2011-06-01 01:58:58 +00:00
|
|
|
func TestUploadDup(t *testing.T) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
|
|
|
ds.CreateQueue("some-queue")
|
|
|
|
tb := &testBlob{"Foo"}
|
|
|
|
tb.ExpectUploadBlob(t, ds)
|
|
|
|
tb.ExpectUploadBlob(t, ds)
|
|
|
|
}
|
|
|
|
|
2011-02-25 17:36:58 +00:00
|
|
|
func TestReceiveStat(t *testing.T) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
|
|
|
|
|
|
|
tb := &testBlob{"Foo"}
|
|
|
|
tb.ExpectUploadBlob(t, ds)
|
2011-02-24 02:25:00 +00:00
|
|
|
|
2011-05-10 23:13:37 +00:00
|
|
|
ch := make(chan blobref.SizedBlobRef, 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() {
|
2011-09-29 02:37:28 +00:00
|
|
|
errch <- ds.StatBlobs(ch, tb.BlobRefSlice(), 0)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatWait(t *testing.T) {
|
2011-02-23 02:48:48 +00:00
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
2011-02-25 17:36:58 +00:00
|
|
|
tb := &testBlob{"Foo"}
|
|
|
|
|
2011-02-26 17:26:23 +00:00
|
|
|
// Do a stat before the blob exists, but wait 2 seconds for it to arrive.
|
2012-03-20 01:44:50 +00:00
|
|
|
wait := 2 * time.Second
|
2011-05-10 23:13:37 +00:00
|
|
|
ch := make(chan blobref.SizedBlobRef, 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-25 17:36:58 +00:00
|
|
|
go func() {
|
2012-03-20 01:44:50 +00:00
|
|
|
errch <- ds.StatBlobs(ch, tb.BlobRefSlice(), wait)
|
2011-02-25 17:36:58 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Sum and verify the stat results, writing the total number of returned matches
|
|
|
|
// to statCountCh (expected: 1)
|
|
|
|
statCountCh := make(chan int)
|
|
|
|
go func() {
|
|
|
|
got := 0
|
|
|
|
for sb := range ch {
|
|
|
|
got++
|
|
|
|
tb.AssertMatches(t, sb)
|
|
|
|
}
|
|
|
|
statCountCh <- got
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Now upload the blob, now that everything else is in-flight.
|
|
|
|
// Sleep a bit to make sure the ds.Stat above has had a chance to fail and sleep.
|
2011-02-26 21:31:23 +00:00
|
|
|
time.Sleep(1e9 / 5) // 200ms in nanos
|
2011-02-25 17:36:58 +00:00
|
|
|
tb.ExpectUploadBlob(t, ds)
|
|
|
|
|
2011-02-26 21:31:23 +00:00
|
|
|
AssertInt(t, 1, <-statCountCh, "number stat results")
|
2011-02-23 02:48:48 +00:00
|
|
|
}
|
2011-02-26 21:31:23 +00:00
|
|
|
|
|
|
|
func TestMultiStat(t *testing.T) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
|
|
|
|
|
|
|
blobfoo := &testBlob{"foo"}
|
|
|
|
blobbar := &testBlob{"bar!"}
|
|
|
|
blobfoo.ExpectUploadBlob(t, ds)
|
|
|
|
blobbar.ExpectUploadBlob(t, ds)
|
|
|
|
|
|
|
|
need := make(map[string]bool)
|
|
|
|
need[blobfoo.BlobRef().String()] = true
|
|
|
|
need[blobbar.BlobRef().String()] = true
|
|
|
|
|
2011-05-10 23:13:37 +00:00
|
|
|
ch := make(chan blobref.SizedBlobRef, 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() {
|
2011-09-29 02:37:28 +00:00
|
|
|
errch <- ds.StatBlobs(ch,
|
2011-02-26 21:31:23 +00:00
|
|
|
[]*blobref.BlobRef{blobfoo.BlobRef(), blobbar.BlobRef()},
|
|
|
|
0)
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
got := 0
|
|
|
|
for sb := range ch {
|
|
|
|
got++
|
|
|
|
br := sb.BlobRef
|
|
|
|
brstr := br.String()
|
2011-06-01 01:58:58 +00:00
|
|
|
Expect(t, need[brstr], "need stat of blobref "+brstr)
|
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
|
|
|
delete(need, brstr)
|
2011-02-26 21:31:23 +00:00
|
|
|
}
|
|
|
|
ExpectInt(t, 2, got, "number stat results")
|
|
|
|
ExpectNil(t, <-errch, "result from stat")
|
|
|
|
ExpectInt(t, 0, len(need), "all stat results needed returned")
|
2011-03-13 02:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingGetReturnsNoEnt(t *testing.T) {
|
|
|
|
ds := NewStorage(t)
|
|
|
|
defer cleanUp(ds)
|
|
|
|
foo := &testBlob{"foo"}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|