github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/util/gsutil_test.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     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 util
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  const (
    28  	file      = "source/file"
    29  	gcsFile   = "gs://bucket/file"
    30  	folder    = "source/"
    31  	gcsFolder = "gs://bucket/folder/"
    32  )
    33  
    34  func TestCopy(t *testing.T) {
    35  	tests := []struct {
    36  		description string
    37  		src         string
    38  		dst         string
    39  		commands    Command
    40  		recursive   bool
    41  		shouldErr   bool
    42  	}{
    43  		{
    44  			description: "copy single file",
    45  			src:         file,
    46  			dst:         gcsFile,
    47  			commands:    testutil.CmdRunOut(fmt.Sprintf("gsutil cp %s %s", file, gcsFile), "logs"),
    48  		},
    49  		{
    50  			description: "copy recursively",
    51  			src:         folder,
    52  			dst:         gcsFolder,
    53  			commands:    testutil.CmdRunOut(fmt.Sprintf("gsutil cp -r %s %s", folder, gcsFolder), "logs"),
    54  			recursive:   true,
    55  		},
    56  		{
    57  			description: "copy failed",
    58  			src:         file,
    59  			dst:         gcsFile,
    60  			commands:    testutil.CmdRunOutErr(fmt.Sprintf("gsutil cp %s %s", file, gcsFile), "logs", fmt.Errorf("file not found")),
    61  			shouldErr:   true,
    62  		},
    63  	}
    64  	for _, test := range tests {
    65  		testutil.Run(t, test.description, func(t *testutil.T) {
    66  			t.Override(&DefaultExecCommand, test.commands)
    67  
    68  			gcs := Gsutil{}
    69  			err := gcs.Copy(context.Background(), test.src, test.dst, test.recursive)
    70  
    71  			t.CheckError(test.shouldErr, err)
    72  		})
    73  	}
    74  }