github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/misc/amazon/s3/client_test.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package s3
    18  
    19  import (
    20  	"net/http"
    21  	"os"
    22  	"reflect"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  var tc *Client
    28  
    29  func getTestClient(t *testing.T) bool {
    30  	accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
    31  	secret := os.Getenv("AWS_ACCESS_KEY_SECRET")
    32  	if accessKey == "" || secret == "" {
    33  		t.Logf("Skipping test; no AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY_SECRET set in environment")
    34  		return false
    35  	}
    36  	tc = &Client{&Auth{AccessKey: accessKey, SecretAccessKey: secret}, http.DefaultTransport}
    37  	return true
    38  }
    39  
    40  func TestBuckets(t *testing.T) {
    41  	if !getTestClient(t) {
    42  		return
    43  	}
    44  	tc.Buckets()
    45  }
    46  
    47  func TestParseBuckets(t *testing.T) {
    48  	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>"
    49  	buckets, err := parseListAllMyBuckets(strings.NewReader(res))
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	if g, w := len(buckets), 2; g != w {
    54  		t.Errorf("num parsed buckets = %d; want %d", g, w)
    55  	}
    56  	want := []*Bucket{
    57  		{Name: "bucketOne", CreationDate: "2006-06-21T07:04:31.000Z"},
    58  		{Name: "bucketTwo", CreationDate: "2006-06-21T07:04:32.000Z"},
    59  	}
    60  	dump := func(v []*Bucket) {
    61  		for i, b := range v {
    62  			t.Logf("Bucket #%d: %#v", i, b)
    63  		}
    64  	}
    65  	if !reflect.DeepEqual(buckets, want) {
    66  		t.Error("mismatch; GOT:")
    67  		dump(buckets)
    68  		t.Error("WANT:")
    69  		dump(want)
    70  	}
    71  }
    72  
    73  func TestValidBucketNames(t *testing.T) {
    74  	m := []struct {
    75  		in   string
    76  		want bool
    77  	}{
    78  		{"myawsbucket", true},
    79  		{"my.aws.bucket", true},
    80  		{"my-aws-bucket.1", true},
    81  		{"my---bucket.1", true},
    82  		{".myawsbucket", false},
    83  		{"-myawsbucket", false},
    84  		{"myawsbucket.", false},
    85  		{"myawsbucket-", false},
    86  		{"my..awsbucket", false},
    87  	}
    88  
    89  	for _, bt := range m {
    90  		got := IsValidBucket(bt.in)
    91  		if got != bt.want {
    92  			t.Errorf("func(%q) = %v; want %v", bt.in, got, bt.want)
    93  		}
    94  	}
    95  }