2014-11-03 00:56:33 +01:00
|
|
|
package minio
|
|
|
|
|
|
|
|
import (
|
2014-11-07 07:45:27 +01:00
|
|
|
"github.com/gorilla/mux"
|
2014-11-07 03:34:46 +01:00
|
|
|
. "gopkg.in/check.v1"
|
2014-11-03 00:56:33 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-11-07 07:45:27 +01:00
|
|
|
"strings"
|
2014-11-03 00:56:33 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-11-07 03:34:46 +01:00
|
|
|
type MySuite struct{}
|
|
|
|
|
|
|
|
var _ = Suite(&MySuite{})
|
|
|
|
|
|
|
|
func Test(t *testing.T) { TestingT(t) }
|
|
|
|
|
|
|
|
func (s *MySuite) TestPrintsGateway(c *C) {
|
2014-11-07 07:45:27 +01:00
|
|
|
// set up router with in memory storage driver
|
|
|
|
router := mux.NewRouter()
|
2014-11-14 07:18:44 +01:00
|
|
|
config := GatewayConfig{
|
|
|
|
StorageDriver: InMemoryStorageDriver,
|
|
|
|
BucketDriver: SynchronizedBucketDriver,
|
|
|
|
}
|
2014-11-07 07:45:27 +01:00
|
|
|
RegisterGatewayHandlers(router, config)
|
|
|
|
server := httptest.NewServer(router)
|
2014-11-03 00:56:33 +01:00
|
|
|
defer server.Close()
|
2014-11-07 07:45:27 +01:00
|
|
|
|
|
|
|
// GET request, empty
|
|
|
|
getReq1, _ := http.NewRequest("GET", server.URL+"/one/two/three", nil)
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(getReq1)
|
|
|
|
c.Assert(resp.StatusCode, Equals, 404)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// assert object not found response
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
c.Assert(string(body), Equals, "Object not found\n")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// add new object
|
|
|
|
putReq, _ := http.NewRequest("PUT", server.URL+"/one/two/three", strings.NewReader("hello"))
|
|
|
|
resp, err = client.Do(putReq)
|
|
|
|
c.Assert(resp.StatusCode, Equals, 200)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// verify object exists
|
|
|
|
getReq2, _ := http.NewRequest("GET", server.URL+"/one/two/three", strings.NewReader("hello"))
|
|
|
|
resp, err = client.Do(getReq2)
|
|
|
|
c.Assert(resp.StatusCode, Equals, 200)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// verify object's contents
|
|
|
|
body2, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
c.Assert(string(body2), Equals, "hello")
|
|
|
|
c.Assert(err, IsNil)
|
2014-11-03 00:56:33 +01:00
|
|
|
}
|
2014-11-07 03:34:46 +01:00
|
|
|
|
|
|
|
type TestContext struct{}
|
|
|
|
|
|
|
|
func (s *MySuite) TestBucketCreation(c *C) {
|
2014-11-08 04:07:16 +01:00
|
|
|
config := GatewayConfig{
|
|
|
|
StorageDriver: InMemoryStorageDriver,
|
|
|
|
requestBucketChan: make(chan BucketRequest),
|
|
|
|
}
|
|
|
|
defer close(config.requestBucketChan)
|
2014-11-14 07:18:44 +01:00
|
|
|
go SynchronizedBucketDriver(config)
|
2014-11-07 03:34:46 +01:00
|
|
|
context := TestContext{}
|
|
|
|
|
2014-11-07 07:45:27 +01:00
|
|
|
// get new bucket A
|
2014-11-07 03:34:46 +01:00
|
|
|
var bucketA1 Bucket
|
|
|
|
callback := make(chan Bucket)
|
2014-11-08 04:07:16 +01:00
|
|
|
config.requestBucketChan <- BucketRequest{
|
2014-11-07 03:34:46 +01:00
|
|
|
name: "bucketA",
|
|
|
|
context: context,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
bucketA1 = <-callback
|
|
|
|
c.Assert(bucketA1.GetName(context), Equals, "bucketA")
|
|
|
|
|
2014-11-07 07:45:27 +01:00
|
|
|
// get bucket A again
|
2014-11-07 03:34:46 +01:00
|
|
|
var bucketA2 Bucket
|
|
|
|
callback = make(chan Bucket)
|
2014-11-08 04:07:16 +01:00
|
|
|
config.requestBucketChan <- BucketRequest{
|
2014-11-07 03:34:46 +01:00
|
|
|
name: "bucketA",
|
|
|
|
context: context,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
bucketA2 = <-callback
|
|
|
|
c.Assert(bucketA2.GetName(context), Equals, "bucketA")
|
|
|
|
c.Assert(bucketA1, DeepEquals, bucketA2)
|
|
|
|
|
2014-11-07 07:45:27 +01:00
|
|
|
// get new bucket B
|
2014-11-07 03:34:46 +01:00
|
|
|
var bucketB Bucket
|
|
|
|
callback = make(chan Bucket)
|
2014-11-08 04:07:16 +01:00
|
|
|
config.requestBucketChan <- BucketRequest{
|
2014-11-07 03:34:46 +01:00
|
|
|
name: "bucketB",
|
|
|
|
context: context,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
bucketB = <-callback
|
|
|
|
c.Assert(bucketB.GetName(context), Equals, "bucketB")
|
|
|
|
}
|
|
|
|
|
2014-11-07 07:45:27 +01:00
|
|
|
func (s *MySuite) TestInMemoryBucketOperations(c *C) {
|
|
|
|
// Test in memory bucket operations
|
2014-11-08 04:07:16 +01:00
|
|
|
config := GatewayConfig{
|
|
|
|
StorageDriver: InMemoryStorageDriver,
|
|
|
|
requestBucketChan: make(chan BucketRequest),
|
|
|
|
}
|
|
|
|
defer close(config.requestBucketChan)
|
2014-11-14 07:18:44 +01:00
|
|
|
go SynchronizedBucketDriver(config)
|
2014-11-07 03:34:46 +01:00
|
|
|
context := TestContext{}
|
|
|
|
|
2014-11-07 07:45:27 +01:00
|
|
|
// get bucket
|
2014-11-07 03:34:46 +01:00
|
|
|
callback := make(chan Bucket)
|
2014-11-08 04:07:16 +01:00
|
|
|
config.requestBucketChan <- BucketRequest{
|
2014-11-07 03:34:46 +01:00
|
|
|
name: "bucket",
|
|
|
|
context: context,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
bucket := <-callback
|
|
|
|
c.Assert(bucket.GetName(context), Equals, "bucket")
|
|
|
|
|
2014-11-07 07:45:27 +01:00
|
|
|
// get missing value
|
2014-11-07 03:34:46 +01:00
|
|
|
nilResult, err := bucket.Get(context, "foo")
|
|
|
|
c.Assert(nilResult, IsNil)
|
2014-11-08 08:32:01 +01:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
c.Assert(err.Error(), Equals, "Object not found")
|
2014-11-07 03:34:46 +01:00
|
|
|
|
2014-11-07 07:45:27 +01:00
|
|
|
// add new value
|
2014-11-07 03:34:46 +01:00
|
|
|
err = bucket.Put(context, "foo", []byte("bar"))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2014-11-07 07:45:27 +01:00
|
|
|
// retrieve value
|
2014-11-07 03:34:46 +01:00
|
|
|
barResult, err := bucket.Get(context, "foo")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(string(barResult), Equals, "bar")
|
|
|
|
}
|