2017-05-10 18:36:49 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 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 cmd
|
|
|
|
|
2017-06-23 20:35:45 -04:00
|
|
|
import (
|
2017-07-14 02:20:16 -04:00
|
|
|
"fmt"
|
2017-10-20 16:59:12 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-06-23 20:35:45 -04:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
minio "github.com/minio/minio-go"
|
|
|
|
)
|
2017-05-10 18:36:49 -04:00
|
|
|
|
|
|
|
func TestToGCSPageToken(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "A",
|
|
|
|
Token: "CgFB",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "AAAAAAAAAA",
|
|
|
|
Token: "CgpBQUFBQUFBQUFB",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
|
|
|
Token: "CmRBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB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FBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUE=",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
if toGCSPageToken(testCase.Name) != testCase.Token {
|
|
|
|
t.Errorf("Test %d: Expected %s, got %s", i+1, toGCSPageToken(testCase.Name), testCase.Token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-05-24 21:39:45 -04:00
|
|
|
|
2017-06-24 01:10:29 -04:00
|
|
|
// TestIsValidGCSProjectIDFormat tests isValidGCSProjectIDFormat
|
|
|
|
func TestValidGCSProjectIDFormat(t *testing.T) {
|
2017-05-24 21:39:45 -04:00
|
|
|
testCases := []struct {
|
|
|
|
ProjectID string
|
|
|
|
Valid bool
|
|
|
|
}{
|
|
|
|
{"", false},
|
|
|
|
{"a", false},
|
|
|
|
{"Abc", false},
|
|
|
|
{"1bcd", false},
|
|
|
|
// 5 chars
|
|
|
|
{"abcdb", false},
|
|
|
|
// 6 chars
|
|
|
|
{"abcdbz", true},
|
|
|
|
// 30 chars
|
|
|
|
{"project-id-1-project-id-more-1", true},
|
|
|
|
// 31 chars
|
|
|
|
{"project-id-1-project-id-more-11", false},
|
|
|
|
{"storage.googleapis.com", false},
|
|
|
|
{"http://storage.googleapis.com", false},
|
|
|
|
{"http://localhost:9000", false},
|
|
|
|
{"project-id-1", true},
|
|
|
|
{"project-id-1988832", true},
|
|
|
|
{"projectid1414", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
2017-06-24 01:10:29 -04:00
|
|
|
valid := isValidGCSProjectIDFormat(testCase.ProjectID)
|
|
|
|
if valid != testCase.Valid {
|
|
|
|
t.Errorf("Test %d: Expected %v, got %v", i+1, valid, testCase.Valid)
|
2017-05-24 21:39:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-06 02:13:53 -04:00
|
|
|
|
2017-06-09 22:48:20 -04:00
|
|
|
// Test for isGCSMarker.
|
|
|
|
func TestIsGCSMarker(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
marker string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
2017-07-19 22:33:10 -04:00
|
|
|
marker: "{minio}gcs123",
|
2017-06-09 22:48:20 -04:00
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
2017-07-19 22:33:10 -04:00
|
|
|
marker: "{mini_no}tgcs123",
|
2017-06-09 22:48:20 -04:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2017-07-19 22:33:10 -04:00
|
|
|
marker: "{minioagainnotgcs123",
|
2017-06-09 22:48:20 -04:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
marker: "obj1",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
if actual := isGCSMarker(tc.marker); actual != tc.expected {
|
|
|
|
t.Errorf("Test %d: marker is %s, expected %v but got %v",
|
|
|
|
i+1, tc.marker, tc.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-17 19:00:41 -04:00
|
|
|
|
|
|
|
// Test for gcsMultipartMetaName.
|
|
|
|
func TestGCSMultipartMetaName(t *testing.T) {
|
|
|
|
uploadID := "a"
|
2017-06-21 13:27:44 -04:00
|
|
|
expected := pathJoin(gcsMinioMultipartPathV1, uploadID, gcsMinioMultipartMeta)
|
2017-06-17 19:00:41 -04:00
|
|
|
got := gcsMultipartMetaName(uploadID)
|
|
|
|
if expected != got {
|
|
|
|
t.Errorf("expected: %s, got: %s", expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for gcsMultipartDataName.
|
|
|
|
func TestGCSMultipartDataName(t *testing.T) {
|
2017-07-14 02:20:16 -04:00
|
|
|
var (
|
|
|
|
uploadID = "a"
|
|
|
|
etag = "b"
|
|
|
|
partNumber = 1
|
|
|
|
)
|
|
|
|
expected := pathJoin(gcsMinioMultipartPathV1, uploadID, fmt.Sprintf("%05d.%s", partNumber, etag))
|
|
|
|
got := gcsMultipartDataName(uploadID, partNumber, etag)
|
2017-06-17 19:00:41 -04:00
|
|
|
if expected != got {
|
|
|
|
t.Errorf("expected: %s, got: %s", expected, got)
|
|
|
|
}
|
|
|
|
}
|
2017-06-23 20:35:45 -04:00
|
|
|
|
|
|
|
func TestFromMinioClientListBucketResultToV2Info(t *testing.T) {
|
|
|
|
|
|
|
|
listBucketResult := minio.ListBucketResult{
|
|
|
|
IsTruncated: false,
|
|
|
|
Marker: "testMarker",
|
|
|
|
NextMarker: "testMarker2",
|
|
|
|
CommonPrefixes: []minio.CommonPrefix{{Prefix: "one"}, {Prefix: "two"}},
|
|
|
|
Contents: []minio.ObjectInfo{{Key: "testobj", ContentType: ""}},
|
|
|
|
}
|
|
|
|
|
|
|
|
listBucketV2Info := ListObjectsV2Info{
|
|
|
|
Prefixes: []string{"one", "two"},
|
|
|
|
Objects: []ObjectInfo{{Name: "testobj", Bucket: "testbucket", UserDefined: map[string]string{"Content-Type": ""}}},
|
|
|
|
IsTruncated: false,
|
|
|
|
ContinuationToken: "testMarker",
|
|
|
|
NextContinuationToken: "testMarker2",
|
|
|
|
}
|
|
|
|
|
|
|
|
if got := fromMinioClientListBucketResultToV2Info("testbucket", listBucketResult); !reflect.DeepEqual(got, listBucketV2Info) {
|
|
|
|
t.Errorf("fromMinioClientListBucketResultToV2Info() = %v, want %v", got, listBucketV2Info)
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 16:59:12 -04:00
|
|
|
|
|
|
|
// Test for gcsParseProjectID
|
|
|
|
func TestGCSParseProjectID(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
contents := `
|
|
|
|
{
|
|
|
|
"type": "service_account",
|
|
|
|
"project_id": "miniotesting"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
f.WriteString(contents)
|
|
|
|
projectID, err := gcsParseProjectID(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if projectID != "miniotesting" {
|
|
|
|
t.Errorf(`Expected projectID value to be "miniotesting"`)
|
|
|
|
}
|
|
|
|
}
|