github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/blobs/service_test.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package blobs 12 13 import ( 14 "context" 15 "os" 16 "path/filepath" 17 "testing" 18 19 "github.com/cockroachdb/cockroach/pkg/blobs/blobspb" 20 "github.com/cockroachdb/cockroach/pkg/testutils" 21 ) 22 23 func TestBlobServiceList(t *testing.T) { 24 tmpDir, cleanupFn := testutils.TempDir(t) 25 defer cleanupFn() 26 27 fileContent := []byte("a") 28 files := []string{"/file/dir/a.csv", "/file/dir/b.csv", "/file/dir/c.csv"} 29 for _, file := range files { 30 writeTestFile(t, filepath.Join(tmpDir, file), fileContent) 31 } 32 33 service, err := NewBlobService(tmpDir) 34 if err != nil { 35 t.Fatal(err) 36 } 37 ctx := context.Background() 38 39 t.Run("list-correct-files", func(t *testing.T) { 40 resp, err := service.List(ctx, &blobspb.GlobRequest{ 41 Pattern: "file/dir/*.csv", 42 }) 43 if err != nil { 44 t.Fatal(err) 45 } 46 resultList := resp.Files 47 if len(resultList) != len(files) { 48 t.Fatal("result list does not have the correct number of files") 49 } 50 for i, f := range resultList { 51 if f != files[i] { 52 t.Fatalf("result list is incorrect %s", resultList) 53 } 54 } 55 }) 56 t.Run("not-in-external-io-dir", func(t *testing.T) { 57 _, err := service.List(ctx, &blobspb.GlobRequest{ 58 Pattern: "file/../../*.csv", 59 }) 60 if err == nil { 61 t.Fatal("expected error but was not caught") 62 } 63 if !testutils.IsError(err, "outside of external-io-dir is not allowed") { 64 t.Fatal("incorrect error message: " + err.Error()) 65 } 66 }) 67 } 68 69 func TestBlobServiceDelete(t *testing.T) { 70 tmpDir, cleanupFn := testutils.TempDir(t) 71 defer cleanupFn() 72 73 fileContent := []byte("file_content") 74 filename := "path/to/file/content.txt" 75 writeTestFile(t, filepath.Join(tmpDir, filename), fileContent) 76 77 service, err := NewBlobService(tmpDir) 78 if err != nil { 79 t.Fatal(err) 80 } 81 ctx := context.Background() 82 83 t.Run("delete-correct-file", func(t *testing.T) { 84 _, err := service.Delete(ctx, &blobspb.DeleteRequest{ 85 Filename: filename, 86 }) 87 if err != nil { 88 t.Fatal(err) 89 } 90 if _, err := os.Stat(filepath.Join(tmpDir, filename)); !os.IsNotExist(err) { 91 t.Fatalf("expected not exists err, got: %s", err) 92 } 93 }) 94 t.Run("file-not-exist", func(t *testing.T) { 95 _, err := service.Delete(ctx, &blobspb.DeleteRequest{ 96 Filename: "file/does/not/exist", 97 }) 98 if err == nil { 99 t.Fatal("expected error but was not caught") 100 } 101 if !testutils.IsError(err, "no such file") { 102 t.Fatal("incorrect error message: " + err.Error()) 103 } 104 }) 105 t.Run("not-in-external-io-dir", func(t *testing.T) { 106 _, err := service.Delete(ctx, &blobspb.DeleteRequest{ 107 Filename: "file/../../content.txt", 108 }) 109 if err == nil { 110 t.Fatal("expected error but was not caught") 111 } 112 if !testutils.IsError(err, "outside of external-io-dir is not allowed") { 113 t.Fatal("incorrect error message: " + err.Error()) 114 } 115 }) 116 } 117 118 func TestBlobServiceStat(t *testing.T) { 119 tmpDir, cleanupFn := testutils.TempDir(t) 120 defer cleanupFn() 121 122 fileContent := []byte("file_content") 123 filename := "path/to/file/content.txt" 124 writeTestFile(t, filepath.Join(tmpDir, filename), fileContent) 125 126 service, err := NewBlobService(tmpDir) 127 if err != nil { 128 t.Fatal(err) 129 } 130 ctx := context.Background() 131 132 t.Run("get-correct-file-size", func(t *testing.T) { 133 resp, err := service.Stat(ctx, &blobspb.StatRequest{ 134 Filename: filename, 135 }) 136 if err != nil { 137 t.Fatal(err) 138 } 139 if resp.Filesize != int64(len(fileContent)) { 140 t.Fatalf("expected filesize: %d, got %d", len(fileContent), resp.Filesize) 141 } 142 }) 143 t.Run("file-not-exist", func(t *testing.T) { 144 _, err := service.Stat(ctx, &blobspb.StatRequest{ 145 Filename: "file/does/not/exist", 146 }) 147 if err == nil { 148 t.Fatal("expected error but was not caught") 149 } 150 if !testutils.IsError(err, "no such file") { 151 t.Fatal("incorrect error message: " + err.Error()) 152 } 153 }) 154 t.Run("not-in-external-io-dir", func(t *testing.T) { 155 _, err := service.Stat(ctx, &blobspb.StatRequest{ 156 Filename: "file/../../content.txt", 157 }) 158 if err == nil { 159 t.Fatal("expected error but was not caught") 160 } 161 if !testutils.IsError(err, "outside of external-io-dir is not allowed") { 162 t.Fatal("incorrect error message: " + err.Error()) 163 } 164 }) 165 t.Run("stat-directory", func(t *testing.T) { 166 _, err := service.Stat(ctx, &blobspb.StatRequest{ 167 Filename: filepath.Dir(filename), 168 }) 169 if err == nil { 170 t.Fatalf("expected error but was not caught") 171 } 172 if !testutils.IsError(err, "expected a file") { 173 t.Fatal("incorrect error message: " + err.Error()) 174 } 175 }) 176 }