github.com/go-kivik/kivik/v4@v4.3.2/x/fsdb/compact_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package fs 14 15 import ( 16 "context" 17 "errors" 18 "net/http" 19 "os" 20 "path" 21 "path/filepath" 22 "runtime" 23 "testing" 24 25 "gitlab.com/flimzy/testy" 26 27 internal "github.com/go-kivik/kivik/v4/int/errors" 28 "github.com/go-kivik/kivik/v4/x/fsdb/filesystem" 29 ) 30 31 const isGopherJS117 = runtime.GOARCH == "js" 32 33 func TestCompact(t *testing.T) { 34 if isGopherJS117 { 35 t.Skip("Tests broken for GopherJS 1.17") 36 } 37 type tt struct { 38 fs filesystem.Filesystem 39 path string 40 dbname string 41 status int 42 err string 43 } 44 tests := testy.NewTable() 45 tests.Add("directory does not exist", tt{ 46 path: "testdata", 47 dbname: "notfound", 48 status: http.StatusNotFound, 49 err: "^open testdata/notfound: [Nn]o such file or directory$", 50 }) 51 tests.Add("empty directory", func(t *testing.T) interface{} { 52 tmpdir := tempDir(t) 53 tests.Cleanup(func() error { 54 return os.RemoveAll(tmpdir) 55 }) 56 if err := os.Mkdir(filepath.Join(tmpdir, "foo"), 0o666); err != nil { 57 t.Fatal(err) 58 } 59 60 return tt{ 61 path: tmpdir, 62 dbname: "foo", 63 } 64 }) 65 tests.Add("permission denied", tt{ 66 fs: &filesystem.MockFS{ 67 OpenFunc: func(_ string) (filesystem.File, error) { 68 return nil, statusError{status: http.StatusForbidden, error: errors.New("permission denied")} 69 }, 70 }, 71 path: "somepath", 72 dbname: "doesntmatter", 73 status: http.StatusForbidden, 74 err: "permission denied$", 75 }) 76 tests.Add("no attachments", func(t *testing.T) interface{} { 77 tmpdir := testy.CopyTempDir(t, "testdata/compact_noatt", 1) 78 tests.Cleanup(func() error { 79 return os.RemoveAll(tmpdir) 80 }) 81 82 return tt{ 83 path: tmpdir, 84 dbname: "compact_noatt", 85 } 86 }) 87 tests.Add("non-winning revs only, no attachments", func(t *testing.T) interface{} { 88 tmpdir := testy.CopyTempDir(t, "testdata/compact_nowinner_noatt", 1) 89 tests.Cleanup(func() error { 90 return os.RemoveAll(tmpdir) 91 }) 92 93 return tt{ 94 path: tmpdir, 95 dbname: "compact_nowinner_noatt", 96 } 97 }) 98 tests.Add("clean up old revs", func(t *testing.T) interface{} { 99 tmpdir := testy.CopyTempDir(t, "testdata/compact_oldrevs", 1) 100 tests.Cleanup(func() error { 101 return os.RemoveAll(tmpdir) 102 }) 103 104 return tt{ 105 path: tmpdir, 106 dbname: "compact_oldrevs", 107 } 108 }) 109 tests.Add("clean up old revs with atts", func(t *testing.T) interface{} { 110 tmpdir := testy.CopyTempDir(t, "testdata/compact_oldrevsatt", 1) 111 tests.Cleanup(func() error { 112 return os.RemoveAll(tmpdir) 113 }) 114 115 return tt{ 116 path: tmpdir, 117 dbname: "compact_oldrevsatt", 118 } 119 }) 120 121 tests.Run(t, func(t *testing.T, tt tt) { 122 fs := tt.fs 123 if fs == nil { 124 fs = filesystem.Default() 125 } 126 db := &db{ 127 client: &client{root: tt.path}, 128 dbPath: path.Join(tt.path, tt.dbname), 129 dbName: tt.dbname, 130 } 131 err := db.compact(context.Background(), fs) 132 if d := internal.StatusErrorDiffRE(tt.err, tt.status, err); d != "" { 133 t.Error(d) 134 } 135 if err != nil { 136 return 137 } 138 if d := testy.DiffAsJSON(testy.Snapshot(t), testy.JSONDir{ 139 Path: tt.path, 140 NoMD5Sum: true, 141 FileContent: true, 142 }); d != nil { 143 t.Error(d) 144 } 145 }) 146 }