cloud.google.com/go/storage@v1.40.0/copy_test.go (about)

     1  // Copyright 2018 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package storage
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  func TestCopyMissingFields(t *testing.T) {
    24  	// Verify that copying checks for missing fields.a
    25  	t.Parallel()
    26  	var tests = []struct {
    27  		srcBucket, srcName, destBucket, destName string
    28  		errMsg                                   string
    29  	}{
    30  		{
    31  			"mybucket", "", "mybucket", "destname",
    32  			"name is empty",
    33  		},
    34  		{
    35  			"mybucket", "srcname", "mybucket", "",
    36  			"name is empty",
    37  		},
    38  		{
    39  			"", "srcfile", "mybucket", "destname",
    40  			"name is empty",
    41  		},
    42  		{
    43  			"mybucket", "srcfile", "", "destname",
    44  			"name is empty",
    45  		},
    46  	}
    47  	ctx := context.Background()
    48  	client := mockClient(t, &mockTransport{})
    49  	for i, test := range tests {
    50  		src := client.Bucket(test.srcBucket).Object(test.srcName)
    51  		dst := client.Bucket(test.destBucket).Object(test.destName)
    52  		_, err := dst.CopierFrom(src).Run(ctx)
    53  		if !strings.Contains(err.Error(), test.errMsg) {
    54  			t.Errorf("CopyTo test #%v:\ngot err  %q\nwant err %q", i, err, test.errMsg)
    55  		}
    56  	}
    57  }
    58  
    59  func TestCopyBothEncryptionKeys(t *testing.T) {
    60  	// Test that using both a customer-supplied key and a KMS key is an error.
    61  	ctx := context.Background()
    62  	client := mockClient(t, &mockTransport{})
    63  	dest := client.Bucket("b").Object("d").Key(testEncryptionKey)
    64  	c := dest.CopierFrom(client.Bucket("b").Object("s"))
    65  	c.DestinationKMSKeyName = "key"
    66  	if _, err := c.Run(ctx); err == nil {
    67  		t.Error("got nil, want error")
    68  	} else if !strings.Contains(err.Error(), "KMS") {
    69  		t.Errorf(`got %q, want it to contain "KMS"`, err)
    70  	}
    71  }