You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.9 KiB
153 lines
3.9 KiB
10 years ago
|
package gateway
|
||
10 years ago
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
10 years ago
|
"os"
|
||
10 years ago
|
"strings"
|
||
10 years ago
|
|
||
|
"github.com/gorilla/mux"
|
||
|
"github.com/minio-io/minio/pkgs/miniotest"
|
||
|
. "gopkg.in/check.v1"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
type GatewaySuite struct{}
|
||
10 years ago
|
|
||
10 years ago
|
var _ = Suite(&GatewaySuite{})
|
||
10 years ago
|
|
||
10 years ago
|
func (s *GatewaySuite) TestPrintsGateway(c *C) {
|
||
10 years ago
|
// set up router with in memory storage driver
|
||
|
router := mux.NewRouter()
|
||
10 years ago
|
config := GatewayConfig{
|
||
|
StorageDriver: InMemoryStorageDriver,
|
||
|
BucketDriver: SynchronizedBucketDriver,
|
||
|
}
|
||
10 years ago
|
RegisterGatewayHandlers(router, config)
|
||
|
server := httptest.NewServer(router)
|
||
10 years ago
|
defer server.Close()
|
||
10 years ago
|
|
||
|
// 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)
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
type TestContext struct{}
|
||
|
|
||
10 years ago
|
func (s *GatewaySuite) TestBucketCreation(c *C) {
|
||
10 years ago
|
config := GatewayConfig{
|
||
|
StorageDriver: InMemoryStorageDriver,
|
||
|
requestBucketChan: make(chan BucketRequest),
|
||
|
}
|
||
|
defer close(config.requestBucketChan)
|
||
10 years ago
|
go SynchronizedBucketDriver(config)
|
||
10 years ago
|
context := TestContext{}
|
||
|
|
||
10 years ago
|
// get new bucket A
|
||
10 years ago
|
var bucketA1 Bucket
|
||
|
callback := make(chan Bucket)
|
||
10 years ago
|
config.requestBucketChan <- BucketRequest{
|
||
10 years ago
|
name: "bucketA",
|
||
|
context: context,
|
||
|
callback: callback,
|
||
|
}
|
||
|
bucketA1 = <-callback
|
||
|
c.Assert(bucketA1.GetName(context), Equals, "bucketA")
|
||
|
|
||
10 years ago
|
// get bucket A again
|
||
10 years ago
|
var bucketA2 Bucket
|
||
|
callback = make(chan Bucket)
|
||
10 years ago
|
config.requestBucketChan <- BucketRequest{
|
||
10 years ago
|
name: "bucketA",
|
||
|
context: context,
|
||
|
callback: callback,
|
||
|
}
|
||
|
bucketA2 = <-callback
|
||
|
c.Assert(bucketA2.GetName(context), Equals, "bucketA")
|
||
|
c.Assert(bucketA1, DeepEquals, bucketA2)
|
||
|
|
||
10 years ago
|
// get new bucket B
|
||
10 years ago
|
var bucketB Bucket
|
||
|
callback = make(chan Bucket)
|
||
10 years ago
|
config.requestBucketChan <- BucketRequest{
|
||
10 years ago
|
name: "bucketB",
|
||
|
context: context,
|
||
|
callback: callback,
|
||
|
}
|
||
|
bucketB = <-callback
|
||
|
c.Assert(bucketB.GetName(context), Equals, "bucketB")
|
||
|
}
|
||
|
|
||
10 years ago
|
func (s *GatewaySuite) TestInMemoryBucketOperations(c *C) {
|
||
10 years ago
|
simpleFileStorageRootDir, err := miniotest.MakeTempTestDir()
|
||
10 years ago
|
c.Assert(err, IsNil)
|
||
|
defer os.RemoveAll(simpleFileStorageRootDir)
|
||
|
configs := []GatewayConfig{
|
||
|
GatewayConfig{
|
||
|
StorageDriver: InMemoryStorageDriver,
|
||
|
requestBucketChan: make(chan BucketRequest),
|
||
|
},
|
||
|
GatewayConfig{
|
||
|
StorageDriver: SimpleFileStorageDriver,
|
||
|
requestBucketChan: make(chan BucketRequest),
|
||
|
dataDir: simpleFileStorageRootDir,
|
||
|
},
|
||
10 years ago
|
}
|
||
10 years ago
|
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")
|
||
10 years ago
|
|
||
|
}
|
||
|
}
|