github.com/GoogleCloudPlatform/testgrid@v0.0.174/util/gcs/local_gcs_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes 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 gcs
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"path"
    23  	"testing"
    24  )
    25  
    26  func TestCleanFilepath(t *testing.T) {
    27  	cases := []struct {
    28  		path string
    29  		want string
    30  	}{
    31  		{
    32  			path: "",
    33  			want: "",
    34  		},
    35  		{
    36  			path: "afile",
    37  			want: "afile",
    38  		},
    39  		{
    40  			path: "path/to/something",
    41  			want: "path/to/something",
    42  		},
    43  		{
    44  			path: "gs://path/to/something",
    45  			want: "gs://path/to/something",
    46  		},
    47  		{
    48  			path: "file:/path/to/something",
    49  			want: "/path/to/something",
    50  		},
    51  		{
    52  			path: "file://path/to/something",
    53  			want: "/path/to/something",
    54  		},
    55  		{
    56  			path: "file:///path/to/something",
    57  			want: "/path/to/something",
    58  		},
    59  		{
    60  			path: "file:/base/valid-in gcs!",
    61  			want: `/base/valid-in gcs!`,
    62  		},
    63  	}
    64  
    65  	for _, tc := range cases {
    66  		t.Run(tc.path, func(t *testing.T) {
    67  			var p Path
    68  			err := p.Set(tc.path)
    69  			if err != nil {
    70  				t.Fatalf("bad path: %v", err)
    71  			}
    72  			if got := cleanFilepath(p); got != tc.want {
    73  				t.Errorf("cleanFilepath(%v) got %q, want %q", p, got, tc.want)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestUpload_UploadsToLocalStorage(t *testing.T) {
    80  	tests := []struct {
    81  		name        string
    82  		destination string
    83  		ExpectErr   bool
    84  		ExpectStat  bool
    85  	}{
    86  		{
    87  			name:       "won't upload nothing",
    88  			ExpectErr:  true,
    89  			ExpectStat: true,
    90  		},
    91  		{
    92  			name:        "uploads files",
    93  			destination: "foo",
    94  			ExpectStat:  true,
    95  		},
    96  		{
    97  			name:        "uploads nested files",
    98  			destination: "foo/bar",
    99  			ExpectStat:  true,
   100  		},
   101  	}
   102  
   103  	for _, tc := range tests {
   104  		t.Run(tc.name, func(t *testing.T) {
   105  			ctx := context.Background()
   106  			client := NewLocalClient()
   107  			base := t.TempDir()
   108  
   109  			location, err := NewPath(path.Join(base, tc.destination))
   110  			if err != nil || location == nil {
   111  				t.Fatalf("Fatal error with location %v; %v", location, err)
   112  			}
   113  
   114  			_, err = client.Upload(ctx, *location, []byte("some-content"), DefaultACL, NoCache)
   115  			if tc.ExpectErr && err == nil {
   116  				t.Error("Expected error, but got none")
   117  			}
   118  			if !tc.ExpectErr && err != nil {
   119  				t.Errorf("Unexpected error: %v", err)
   120  			}
   121  
   122  			if tc.ExpectStat {
   123  				_, err = os.Stat(path.Join(base, tc.destination))
   124  				if err != nil {
   125  					t.Errorf("Stat failed: %v", err)
   126  				}
   127  			}
   128  		})
   129  	}
   130  }