2014-11-29 17:42:22 -05:00
|
|
|
package gateway
|
2014-11-02 18:56:33 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-11-14 20:22:50 -05:00
|
|
|
"os"
|
2014-11-07 01:45:27 -05:00
|
|
|
"strings"
|
2014-11-29 17:42:22 -05:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2014-12-13 04:14:36 -05:00
|
|
|
"github.com/minio-io/minio/pkgs/utils"
|
2014-11-29 17:42:22 -05:00
|
|
|
. "gopkg.in/check.v1"
|
2014-11-02 18:56:33 -05:00
|
|
|
)
|
|
|
|
|
2014-11-14 20:22:50 -05:00
|
|
|
type GatewaySuite struct{}
|
2014-11-06 21:34:46 -05:00
|
|
|
|
2014-11-14 20:22:50 -05:00
|
|
|
var _ = Suite(&GatewaySuite{})
|
2014-11-06 21:34:46 -05:00
|
|
|
|
2014-11-14 20:22:50 -05:00
|
|
|
func (s *GatewaySuite) TestPrintsGateway(c *C) {
|
2014-11-07 01:45:27 -05:00
|
|
|
// set up router with in memory storage driver
|
|
|
|
router := mux.NewRouter()
|
2014-11-14 01:18:44 -05:00
|
|
|
config := GatewayConfig{
|
|
|
|
StorageDriver: InMemoryStorageDriver,
|
|
|
|
BucketDriver: SynchronizedBucketDriver,
|
|
|
|
}
|
2014-11-07 01:45:27 -05:00
|
|
|
RegisterGatewayHandlers(router, config)
|
|
|
|
server := httptest.NewServer(router)
|
2014-11-02 18:56:33 -05:00
|
|
|
defer server.Close()
|
2014-11-07 01:45:27 -05: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-02 18:56:33 -05:00
|
|
|
}
|
2014-11-06 21:34:46 -05:00
|
|
|
|
|
|
|
type TestContext struct{}
|
|
|
|
|
2014-11-14 20:22:50 -05:00
|
|
|
func (s *GatewaySuite) TestBucketCreation(c *C) {
|
2014-11-07 22:07:16 -05:00
|
|
|
config := GatewayConfig{
|
|
|
|
StorageDriver: InMemoryStorageDriver,
|
|
|
|
requestBucketChan: make(chan BucketRequest),
|
|
|
|
}
|
|
|
|
defer close(config.requestBucketChan)
|
2014-11-14 01:18:44 -05:00
|
|
|
go SynchronizedBucketDriver(config)
|
2014-11-06 21:34:46 -05:00
|
|
|
context := TestContext{}
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// get new bucket A
|
2014-11-06 21:34:46 -05:00
|
|
|
var bucketA1 Bucket
|
|
|
|
callback := make(chan Bucket)
|
2014-11-07 22:07:16 -05:00
|
|
|
config.requestBucketChan <- BucketRequest{
|
2014-11-06 21:34:46 -05:00
|
|
|
name: "bucketA",
|
|
|
|
context: context,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
bucketA1 = <-callback
|
|
|
|
c.Assert(bucketA1.GetName(context), Equals, "bucketA")
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// get bucket A again
|
2014-11-06 21:34:46 -05:00
|
|
|
var bucketA2 Bucket
|
|
|
|
callback = make(chan Bucket)
|
2014-11-07 22:07:16 -05:00
|
|
|
config.requestBucketChan <- BucketRequest{
|
2014-11-06 21:34:46 -05:00
|
|
|
name: "bucketA",
|
|
|
|
context: context,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
bucketA2 = <-callback
|
|
|
|
c.Assert(bucketA2.GetName(context), Equals, "bucketA")
|
|
|
|
c.Assert(bucketA1, DeepEquals, bucketA2)
|
|
|
|
|
2014-11-07 01:45:27 -05:00
|
|
|
// get new bucket B
|
2014-11-06 21:34:46 -05:00
|
|
|
var bucketB Bucket
|
|
|
|
callback = make(chan Bucket)
|
2014-11-07 22:07:16 -05:00
|
|
|
config.requestBucketChan <- BucketRequest{
|
2014-11-06 21:34:46 -05:00
|
|
|
name: "bucketB",
|
|
|
|
context: context,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
bucketB = <-callback
|
|
|
|
c.Assert(bucketB.GetName(context), Equals, "bucketB")
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:22:50 -05:00
|
|
|
func (s *GatewaySuite) TestInMemoryBucketOperations(c *C) {
|
2014-12-13 04:14:36 -05:00
|
|
|
simpleFileStorageRootDir, err1 := utils.MakeTempTestDir()
|
2014-12-11 19:49:35 -05:00
|
|
|
c.Assert(err1, IsNil)
|
2014-12-13 04:14:36 -05:00
|
|
|
simpleEncodedStorageRootDir, err2 := utils.MakeTempTestDir()
|
2014-12-11 19:49:35 -05:00
|
|
|
c.Assert(err2, IsNil)
|
2014-11-14 20:22:50 -05:00
|
|
|
defer os.RemoveAll(simpleFileStorageRootDir)
|
2014-12-11 19:49:35 -05:00
|
|
|
defer os.RemoveAll(simpleEncodedStorageRootDir)
|
|
|
|
|
2014-11-14 20:22:50 -05:00
|
|
|
configs := []GatewayConfig{
|
|
|
|
GatewayConfig{
|
|
|
|
StorageDriver: InMemoryStorageDriver,
|
|
|
|
requestBucketChan: make(chan BucketRequest),
|
|
|
|
},
|
|
|
|
GatewayConfig{
|
|
|
|
StorageDriver: SimpleFileStorageDriver,
|
|
|
|
requestBucketChan: make(chan BucketRequest),
|
2014-12-11 19:49:35 -05:00
|
|
|
DataDir: simpleFileStorageRootDir,
|
|
|
|
},
|
|
|
|
GatewayConfig{
|
|
|
|
StorageDriver: SimpleEncodedStorageDriver,
|
|
|
|
requestBucketChan: make(chan BucketRequest),
|
|
|
|
DataDir: simpleEncodedStorageRootDir,
|
|
|
|
K: 10,
|
|
|
|
M: 6,
|
|
|
|
BlockSize: 1024 * 1024,
|
2014-11-14 20:22:50 -05:00
|
|
|
},
|
2014-11-07 22:07:16 -05:00
|
|
|
}
|
2014-11-14 20:22:50 -05:00
|
|
|
for _, config := range configs {
|
|
|
|
defer close(config.requestBucketChan)
|
|
|
|
go SynchronizedBucketDriver(config)
|
|
|
|
context := TestContext{}
|
|
|
|
|
|
|
|
// get bucket
|
|
|
|
callback := make(chan Bucket)
|
|
|
|
config.requestBucketChan <- BucketRequest{
|
|
|
|
name: "bucket",
|
|
|
|
context: context,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
bucket := <-callback
|
|
|
|
c.Assert(bucket.GetName(context), Equals, "bucket")
|
|
|
|
|
|
|
|
// get missing value
|
|
|
|
nilResult, err := bucket.Get(context, "foo")
|
|
|
|
c.Assert(nilResult, IsNil)
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
c.Assert(err.Error(), Equals, "Object not found")
|
|
|
|
|
|
|
|
// add new value
|
|
|
|
err = bucket.Put(context, "foo", []byte("bar"))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// retrieve value
|
|
|
|
barResult, err := bucket.Get(context, "foo")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(string(barResult), Equals, "bar")
|
2014-11-06 21:34:46 -05:00
|
|
|
}
|
|
|
|
}
|