-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap_test.go
More file actions
185 lines (177 loc) · 4.55 KB
/
bootstrap_test.go
File metadata and controls
185 lines (177 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package boltdb
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"syscall"
"testing"
"github.com/go-ap/errors"
"github.com/google/go-cmp/cmp"
bolt "go.etcd.io/bbolt"
)
func createForbiddenDir(t *testing.T) string {
forbiddenPath := filepath.Join(t.TempDir(), "forbidden")
err := os.MkdirAll(forbiddenPath, 0o000)
if err != nil {
t.Fatalf("unable to create forbidden test path %s: %s", forbiddenPath, err)
}
return forbiddenPath
}
func TestBootstrap(t *testing.T) {
forbiddenPath := createForbiddenDir(t)
tests := []struct {
name string
arg Config
wantErr error
}{
{
name: "empty",
arg: Config{},
wantErr: os.ErrNotExist,
},
{
name: "temp",
arg: Config{Path: filepath.Join(t.TempDir())},
},
{
name: "deeper than forbidden",
arg: Config{Path: filepath.Join(forbiddenPath, "should-fail")},
wantErr: &fs.PathError{Op: "stat", Path: filepath.Join(forbiddenPath, "should-fail"), Err: syscall.EACCES},
},
{
name: "forbidden",
arg: Config{Path: forbiddenPath},
wantErr: &fs.PathError{Op: "open", Path: filepath.Join(forbiddenPath, dbFile), Err: syscall.EACCES},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Bootstrap(tt.arg); !cmp.Equal(err, tt.wantErr, EquateWeakErrors) {
t.Errorf("Bootstrap() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr != nil {
return
}
ff := fields{
path: tt.arg.Path,
root: []byte(rootBucket),
}
r := mockRepo(t, ff, withOpenRoot)
defer r.Close()
err := r.d.View(func(tx *bolt.Tx) error {
path := tt.arg.Path
bucket := ff.root
root := tx.Bucket(bucket)
if false {
// NOTICE(marius): these have been disabled in the bootstrap, because they're dynamically created
activities := root.Bucket([]byte(bucketActivities))
if activities == nil {
t.Errorf("Could not find bucket %s/%s at boltdb path %s", bucket, bucketActivities, path)
return nil
}
actors := root.Bucket([]byte(bucketActors))
if actors == nil {
t.Errorf("Could not find bucket %s/%s at boltdb path %s", bucket, bucketActors, path)
return nil
}
objects := root.Bucket([]byte(bucketObjects))
if objects == nil {
t.Errorf("Could not find bucket %s/%s at boltdb path %s", bucket, bucketObjects, path)
return nil
}
}
return nil
})
if err != nil {
t.Errorf("Opening boltdb repo for viewing failed: %s", err)
}
})
}
}
func TestClean(t *testing.T) {
forbiddenPath := createForbiddenDir(t)
tests := []struct {
name string
arg Config
wantErr error
}{
{
name: "empty",
arg: Config{},
wantErr: nil,
},
{
name: "temp - exists, but empty",
arg: Config{Path: t.TempDir()},
wantErr: nil,
},
{
name: "temp - does not exists",
arg: Config{Path: filepath.Join(t.TempDir(), "test")},
wantErr: nil,
},
{
name: "invalid path " + os.DevNull,
arg: Config{Path: os.DevNull},
wantErr: errors.Errorf("path exists, and is not a folder %s", os.DevNull),
},
{
name: "deeper than forbidden",
arg: Config{Path: filepath.Join(forbiddenPath, "should-fail")},
wantErr: &fs.PathError{Op: "stat", Path: filepath.Join(forbiddenPath, "should-fail"), Err: syscall.EACCES},
},
{
name: "forbidden",
arg: Config{Path: forbiddenPath},
wantErr: &fs.PathError{Op: "open", Path: forbiddenPath, Err: syscall.EACCES},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Clean(tt.arg); !cmp.Equal(err, tt.wantErr, EquateWeakErrors) {
t.Errorf("Clean() error = %s", cmp.Diff(tt.wantErr, err, EquateWeakErrors))
}
})
}
}
func Test_bootstrap(t *testing.T) {
noRoot, err := bolt.Open(filepath.Join(t.TempDir(), "/no-root.bdb"), 0x600, &bolt.Options{ReadOnly: false})
if err != nil {
t.Errorf("failed opening boltdb: %s", err)
}
type args struct {
db *bolt.DB
root []byte
}
tests := []struct {
name string
args args
wantErr error
}{
{
name: "empty",
args: args{},
wantErr: errNotOpen,
},
{
name: "no root bucket",
args: args{
db: noRoot,
root: nil,
},
wantErr: errors.Annotatef(fmt.Errorf("bucket name required"), "could not create root bucket"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := bootstrap(tt.args.db, tt.args.root); !cmp.Equal(err, tt.wantErr, EquateWeakErrors) {
t.Errorf("bootstrap() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.args.db != nil {
_ = tt.args.db.Close()
}
})
}
}