Files
mc/pkg/client/s3/client_test.go
T
Harshavardhana 8bc587b8cf Now clients only take URLs - fixes #425
- Interface change, takes only minimal arguments
 - Passing URLs all the way down
 - Bucket and Object awareness only at per client level
2015-04-19 19:39:02 -07:00

134 lines
3.7 KiB
Go

// Original license //
// ---------------- //
/*
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.
*/
// All other modifications and improvements //
// ---------------------------------------- //
/*
* Mini Copy, (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 s3
import (
"encoding/xml"
"errors"
"io"
"reflect"
"strings"
"testing"
"time"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/minio/pkg/iodine"
)
func listAllMyBuckets(r io.Reader) ([]*client.Item, error) {
type bucket struct {
Name string
CreationDate time.Time
}
type allMyBuckets struct {
Buckets struct {
Bucket []*bucket
}
}
var buckets allMyBuckets
if err := xml.NewDecoder(r).Decode(&buckets); err != nil {
return nil, iodine.New(client.UnexpectedError{Err: errors.New("Malformed response received from server")},
map[string]string{"XMLError": err.Error()})
}
var items []*client.Item
for _, b := range buckets.Buckets.Bucket {
item := new(client.Item)
item.Name = b.Name
item.Time = b.CreationDate
items = append(items, item)
}
return items, nil
}
func TestParseBuckets(t *testing.T) {
res := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListAllMyBucketsResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Owner><ID>ownerIDField</ID><DisplayName>bobDisplayName</DisplayName></Owner><Buckets><Bucket><Name>bucketOne</Name><CreationDate>2006-06-21T07:04:31.000Z</CreationDate></Bucket><Bucket><Name>bucketTwo</Name><CreationDate>2006-06-21T07:04:32.000Z</CreationDate></Bucket></Buckets></ListAllMyBucketsResult>"
buckets, err := listAllMyBuckets(strings.NewReader(res))
if err != nil {
t.Fatal(err)
}
if g, w := len(buckets), 2; g != w {
t.Errorf("num parsed buckets = %d; want %d", g, w)
}
t1, err := time.Parse(time.RFC3339, "2006-06-21T07:04:31.000Z")
t2, err := time.Parse(time.RFC3339, "2006-06-21T07:04:32.000Z")
want := []*client.Item{
{Name: "bucketOne", Time: t1},
{Name: "bucketTwo", Time: t2},
}
dump := func(v []*client.Item) {
for i, b := range v {
t.Logf("Bucket #%d: %#v", i, b)
}
}
if !reflect.DeepEqual(buckets, want) {
t.Error("mismatch; GOT:")
dump(buckets)
t.Error("WANT:")
dump(want)
}
}
func TestValidBucketNames(t *testing.T) {
m := []struct {
in string
want bool
}{
{"myawsbucket", true},
{"myaws-bucket", true},
{"my-aws-bucket", true},
{"my.aws.bucket", false},
{"my-aws-bucket.1", false},
{"my---bucket.1", false},
{".myawsbucket", false},
{"-myawsbucket", false},
{"myawsbucket.", false},
{"myawsbucket-", false},
{"my..awsbucket", false},
}
for _, bt := range m {
got := client.IsValidBucketName(bt.in)
if got != bt.want {
t.Errorf("func(%q) = %v; want %v", bt.in, got, bt.want)
}
}
}