Update to new changes in objectstorage-go

This commit is contained in:
Harshavardhana
2015-05-23 11:59:30 -07:00
parent 4782e2663f
commit d2bfe75bee
8 changed files with 185 additions and 167 deletions
+1 -1
View File
@@ -36,7 +36,7 @@
},
{
"ImportPath": "github.com/minio/objectstorage-go",
"Rev": "0b5d5f294de9f3871ae9cc23bb9753175da8f2dd"
"Rev": "883ebdb4593b397713be5f0818174f71ee6724e0"
},
{
"ImportPath": "github.com/shiena/ansicolor",
+3 -3
View File
@@ -1,6 +1,6 @@
language: go
go:
- 1.4.2
sudo: false
notifications:
slack: travisci:0lsU2EbtMOizjtgSmBZUNEQS
script:
- go vet ./...
- go test -race -v ./...
+5 -3
View File
@@ -20,6 +20,7 @@ import (
"errors"
"io"
"net/http"
"runtime"
"sort"
"strings"
"time"
@@ -156,16 +157,17 @@ func (c *Config) MustGetEndpoint() string {
// fall back to custom Endpoint, if no valid region can be found
return c.Endpoint
}
// if not endpoint or region sepcified default to us-east-1
// if not endpoint or region specified default to us-east-1
c.Region = "us-east-1"
return getEndpoint(c.Region)
}
// AddUserAgent - append to a default user agent
func (c *Config) AddUserAgent(name string, version string, comments ...string) {
c.userAgent = LibraryName + "/" + LibraryVersion + " (" + runtime.GOOS + ", " + runtime.GOARCH + ") "
// if no name and version is set we do not add new user agents
if name != "" && version != "" {
newUserAgent := name + "/" + version + " (" + strings.Join(comments, ", ") + ") "
c.userAgent = c.userAgent + " " + newUserAgent
c.userAgent = c.userAgent + " " + name + "/" + version + " (" + strings.Join(comments, ", ") + ") "
}
}
@@ -0,0 +1,131 @@
/*
* Minimal object storage library (C) 2015 Minio, 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 objectstorage
// bucketHandler is an http.Handler that verifies bucket responses and validates incoming requests
import (
"bytes"
"io"
"net/http"
"strconv"
"time"
)
type bucketHandler struct {
resource string
}
func (h bucketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
switch {
case r.URL.Path == "/":
response := []byte("<ListAllMyBucketsResult xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><Buckets><Bucket><Name>bucket</Name><CreationDate>2015-05-20T23:05:09.230Z</CreationDate></Bucket></Buckets><Owner><ID>minio</ID><DisplayName>minio</DisplayName></Owner></ListAllMyBucketsResult>")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.Write(response)
case r.URL.Path == "/bucket":
response := []byte("<ListBucketResult xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><Contents><ETag>259d04a13802ae09c7e41be50ccc6baa</ETag><Key>object</Key><LastModified>2015-05-21T18:24:21.097Z</LastModified><Size>22061</Size><Owner><ID>minio</ID><DisplayName>minio</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Delimiter></Delimiter><EncodingType></EncodingType><IsTruncated>false</IsTruncated><Marker></Marker><MaxKeys>1000</MaxKeys><Name>testbucket</Name><NextMarker></NextMarker><Prefix></Prefix></ListBucketResult>")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.Write(response)
}
case r.Method == "PUT":
switch {
case r.URL.Path == h.resource:
_, ok := r.URL.Query()["acl"]
if ok {
if r.Header.Get("x-amz-acl") != "public-read-write" {
w.WriteHeader(http.StatusNotImplemented)
}
}
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusBadRequest)
}
case r.Method == "HEAD":
switch {
case r.URL.Path == h.resource:
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusForbidden)
}
case r.Method == "DELETE":
switch {
case r.URL.Path != h.resource:
w.WriteHeader(http.StatusNotFound)
default:
h.resource = ""
w.WriteHeader(http.StatusOK)
}
}
}
// objectHandler is an http.Handler that verifies object responses and validates incoming requests
type objectHandler struct {
resource string
data []byte
}
func (h objectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "PUT":
length, err := strconv.Atoi(r.Header.Get("Content-Length"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var buffer bytes.Buffer
_, err = io.CopyN(&buffer, r.Body, int64(length))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !bytes.Equal(h.data, buffer.Bytes()) {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("ETag", "9af2f8218b150c351ad802c6f3d66abe")
w.WriteHeader(http.StatusOK)
case r.Method == "HEAD":
if r.URL.Path != h.resource {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(h.data)))
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
w.Header().Set("ETag", "9af2f8218b150c351ad802c6f3d66abe")
w.WriteHeader(http.StatusOK)
case r.Method == "POST":
case r.Method == "GET":
if r.URL.Path != h.resource {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(h.data)))
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
w.Header().Set("ETag", "9af2f8218b150c351ad802c6f3d66abe")
w.WriteHeader(http.StatusOK)
io.Copy(w, bytes.NewReader(h.data))
case r.Method == "DELETE":
if r.URL.Path != h.resource {
w.WriteHeader(http.StatusNotFound)
return
}
h.resource = ""
h.data = nil
w.WriteHeader(http.StatusOK)
}
}
+36 -104
View File
@@ -20,11 +20,9 @@ import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
func ExampleGetPartSize() {
@@ -36,52 +34,43 @@ func ExampleGetPartSize_second() {
// Output: 5368709120
}
// bucketHandler is an http.Handler that verifies bucket responses and validates incoming requests
type bucketHandler struct {
resource string
func TestMustGetEndpoint(t *testing.T) {
conf := new(Config)
conf.Region = "us-east-1"
if conf.MustGetEndpoint() != "https://s3.amazonaws.com" {
t.Fatalf("Error")
}
conf.Region = ""
conf.Endpoint = "http://localhost:9000"
if conf.MustGetEndpoint() != "http://localhost:9000" && conf.Region == "milkyway" {
t.Fatalf("Error")
}
conf.Region = ""
conf.Endpoint = "https://s3.amazonaws.com"
if conf.MustGetEndpoint() != "https://s3.amazonaws.com" && conf.Region == "us-east-1" {
t.Fatalf("Error")
}
conf.Region = "invalid"
conf.Endpoint = "http://play.minio.io:9000"
if conf.MustGetEndpoint() != "http://play.minio.io:9000" {
t.Fatalf("Error")
}
conf.Region = ""
conf.Endpoint = ""
if conf.MustGetEndpoint() != "https://s3.amazonaws.com" && conf.Region == "us-east-1" {
t.Fatalf("Error")
}
}
func (h bucketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "GET":
switch {
case r.URL.Path == "/":
response := []byte("<ListAllMyBucketsResult xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><Buckets><Bucket><Name>bucket</Name><CreationDate>2015-05-20T23:05:09.230Z</CreationDate></Bucket></Buckets><Owner><ID>minio</ID><DisplayName>minio</DisplayName></Owner></ListAllMyBucketsResult>")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.Write(response)
case r.URL.Path == "/bucket":
response := []byte("<ListBucketResult xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><Contents><ETag>259d04a13802ae09c7e41be50ccc6baa</ETag><Key>object</Key><LastModified>2015-05-21T18:24:21.097Z</LastModified><Size>22061</Size><Owner><ID>minio</ID><DisplayName>minio</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Delimiter></Delimiter><EncodingType></EncodingType><IsTruncated>false</IsTruncated><Marker></Marker><MaxKeys>1000</MaxKeys><Name>testbucket</Name><NextMarker></NextMarker><Prefix></Prefix></ListBucketResult>")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.Write(response)
}
case r.Method == "PUT":
switch {
case r.URL.Path == h.resource:
_, ok := r.URL.Query()["acl"]
if ok {
if r.Header.Get("x-amz-acl") != "public-read-write" {
w.WriteHeader(http.StatusNotImplemented)
}
}
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusBadRequest)
}
case r.Method == "HEAD":
switch {
case r.URL.Path == h.resource:
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusForbidden)
}
case r.Method == "DELETE":
switch {
case r.URL.Path != h.resource:
w.WriteHeader(http.StatusNotFound)
default:
h.resource = ""
w.WriteHeader(http.StatusOK)
}
func TestUserAgent(t *testing.T) {
conf := new(Config)
conf.AddUserAgent("minio", "1.0", "amd64")
if !strings.Contains(conf.userAgent, "minio") {
t.Fatalf("Error")
}
}
@@ -148,63 +137,6 @@ func TestBucketOperations(t *testing.T) {
}
}
// objectHandler is an http.Handler that verifies object responses and validates incoming requests
type objectHandler struct {
resource string
data []byte
}
func (h objectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == "PUT":
length, err := strconv.Atoi(r.Header.Get("Content-Length"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
var buffer bytes.Buffer
_, err = io.CopyN(&buffer, r.Body, int64(length))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !bytes.Equal(h.data, buffer.Bytes()) {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("ETag", "9af2f8218b150c351ad802c6f3d66abe")
w.WriteHeader(http.StatusOK)
case r.Method == "HEAD":
if r.URL.Path != h.resource {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(h.data)))
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
w.Header().Set("ETag", "9af2f8218b150c351ad802c6f3d66abe")
w.WriteHeader(http.StatusOK)
case r.Method == "POST":
case r.Method == "GET":
if r.URL.Path != h.resource {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(h.data)))
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
w.Header().Set("ETag", "9af2f8218b150c351ad802c6f3d66abe")
w.WriteHeader(http.StatusOK)
io.Copy(w, bytes.NewReader(h.data))
case r.Method == "DELETE":
if r.URL.Path != h.resource {
w.WriteHeader(http.StatusNotFound)
return
}
h.resource = ""
h.data = nil
w.WriteHeader(http.StatusOK)
}
}
func TestObjectOperations(t *testing.T) {
object := objectHandler(objectHandler{
resource: "/bucket/object",
@@ -31,7 +31,7 @@ import (
// urlEncodedName- encode the strings from UTF-8 byte representations to HTML hex escape sequences
func urlEncodeName(objectName string) (string, error) {
// if object matches reserved string, no need to encode them
reservedNames := regexp.MustCompile("^[a-zA-Z0-9-_.~/ ]+$")
reservedNames := regexp.MustCompile("^[a-zA-Z0-9-_.~/]+$")
if reservedNames.MatchString(objectName) {
return objectName, nil
}
@@ -42,7 +42,7 @@ func urlEncodeName(objectName string) (string, error) {
continue
}
switch s {
case '-', '_', '.', '~', '/', ' ': // §2.3 Unreserved characters (mark)
case '-', '_', '.', '~', '/': // §2.3 Unreserved characters (mark)
encodedObjectName = encodedObjectName + string(s)
continue
default:
@@ -72,8 +72,12 @@ func newRequest(op *operation, config *Config, body io.ReadSeeker) (*request, er
return nil, err
}
// if userAgent empty do not set it
if config.userAgent != "" {
config.userAgent = LibraryName + "/" + LibraryVersion + " (" + runtime.GOOS + ", " + runtime.GOARCH + ") "
}
// set UserAgent
config.userAgent = config.userAgent + LibraryName + "/" + LibraryVersion + " (" + runtime.GOOS + ", " + runtime.GOARCH + ") "
req.Header.Set("User-Agent", config.userAgent)
// set Accept header for response encoding style, if available
@@ -206,6 +210,8 @@ func (r *request) getSignedHeaders() string {
func (r *request) getCanonicalRequest(hashedPayload string) string {
r.req.URL.RawQuery = strings.Replace(r.req.URL.Query().Encode(), "+", "%20", -1)
encodedPath, _ := urlEncodeName(r.req.URL.Path)
// convert any space strings back to "+"
encodedPath = strings.Replace(encodedPath, "+", "%20", -1)
canonicalRequest := strings.Join([]string{
r.req.Method,
encodedPath,
@@ -1,53 +0,0 @@
/*
* Minimal object storage library (C) 2015 Minio, 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 objectstorage_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
var robotsTxtHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Last-Modified", "sometime")
fmt.Fprintf(w, "User-agent: go\nDisallow: /something/")
})
func TestNewRequest(t *testing.T) {
}
func TestResponseToError(t *testing.T) {
}
func TestCommonFunctions(t *testing.T) {
}
func TestLowLevelBucketOperations(t *testing.T) {
ts := httptest.NewServer(robotsTxtHandler)
defer ts.Close()
}
func TestLowLevelObjectOperations(t *testing.T) {
ts := httptest.NewServer(robotsTxtHandler)
defer ts.Close()
}
func TestLowLevelMultiPartOperations(t *testing.T) {
ts := httptest.NewServer(robotsTxtHandler)
defer ts.Close()
}