/
test_repo.go
339 lines (293 loc) · 12.1 KB
/
test_repo.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package test
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"path/filepath"
"runtime"
crypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/qri-io/dataset"
"github.com/qri-io/dataset/dstest"
"github.com/qri-io/qfs"
"github.com/qri-io/qfs/muxfs"
"github.com/qri-io/qri/base/dsfs"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/dsref"
"github.com/qri-io/qri/event"
"github.com/qri-io/qri/logbook"
"github.com/qri-io/qri/profile"
"github.com/qri-io/qri/repo"
reporef "github.com/qri-io/qri/repo/ref"
)
// base64-encoded Test Private Key, decoded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bEG6f5C4MUkJ38z1t1OjA==`)
privKey crypto.PrivKey
profileID = "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt"
testPeerProfile = &profile.Profile{
Peername: "peer",
ID: profile.IDB58MustDecode(profileID),
}
)
func init() {
data, err := base64.StdEncoding.DecodeString(string(testPk))
if err != nil {
panic(err)
}
testPk = data
privKey, err = crypto.UnmarshalPrivateKey(testPk)
if err != nil {
panic(fmt.Errorf("error unmarshaling private key: %s", err.Error()))
}
testPeerProfile.PrivKey = privKey
}
// TestdataPath returns the absolute path to a file in the testdata diretory
func TestdataPath(path string) string {
// Get the testdata directory relative to this source file.
_, currfile, _, _ := runtime.Caller(0)
testdataPath := filepath.Join(filepath.Dir(currfile), "testdata")
return filepath.Join(testdataPath, path)
}
// ProfileConfig returns the test profile as a config.Profile
func ProfileConfig() *config.ProfilePod {
return &config.ProfilePod{
Peername: "peer",
ID: profileID,
PrivKey: string(_testPk),
Type: "peer",
}
}
// NewEmptyTestRepo initializes a test repo with no contents
func NewEmptyTestRepo(bus event.Bus) (mr *repo.MemRepo, err error) {
ctx := context.TODO()
pro := &profile.Profile{
Peername: "peer",
ID: profile.IDB58MustDecode(profileID),
PrivKey: privKey,
}
return repo.NewMemRepoWithProfile(ctx, pro, newTestFS(ctx), bus)
}
func newTestFS(ctx context.Context) *muxfs.Mux {
fs, err := muxfs.New(ctx, []qfs.Config{
{Type: "mem"},
{Type: "local"},
{Type: "http"},
})
if err != nil {
panic(err)
}
return fs
}
// NewTestRepo generates a repository usable for testing purposes
func NewTestRepo() (mr *repo.MemRepo, err error) {
datasets := []string{"movies", "cities", "counter", "craigslist", "sitemap"}
mr, err = NewEmptyTestRepo(event.NilBus)
if err != nil {
return
}
for _, dsDirName := range datasets {
tc, err := dstest.NewTestCaseFromDir(TestdataPath(dsDirName))
if err != nil {
return nil, err
}
if _, err := createDataset(mr, tc); err != nil {
return nil, fmt.Errorf("%s error creating dataset: %s", dsDirName, err.Error())
}
}
return
}
// NewTestRepoWithHistory generates a repository with a dataset that has a history, usable for testing purposes
func NewTestRepoWithHistory() (mr *repo.MemRepo, refs []reporef.DatasetRef, err error) {
datasets := []string{"movies", "cities", "counter", "craigslist", "sitemap"}
mr, err = NewEmptyTestRepo(event.NilBus)
if err != nil {
return
}
prevPath := ""
for _, dsDirName := range datasets {
tc, err := dstest.NewTestCaseFromDir(TestdataPath(dsDirName))
if err != nil {
return nil, nil, err
}
tc.Input.Name = "logtest"
tc.Input.PreviousPath = prevPath
ref, err := createDataset(mr, tc)
if err != nil {
return nil, nil, fmt.Errorf("%s error creating dataset: %s", dsDirName, err.Error())
}
prevPath = ref.Path
refs = append(refs, ref)
}
// return refs with the first ref as the head of the log
for i := len(refs)/2 - 1; i >= 0; i-- {
opp := len(refs) - 1 - i
refs[i], refs[opp] = refs[opp], refs[i]
}
return
}
// NewTestRepoFromProfileID constructs a repo from a profileID, usable for tests
func NewTestRepoFromProfileID(id profile.ID, peerNum int, dataIndex int) (repo.Repo, error) {
ctx := context.TODO()
datasets := []string{"movies", "cities", "counter", "craigslist", "sitemap"}
pk, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
if err != nil {
return nil, err
}
r, err := repo.NewMemRepoWithProfile(ctx, &profile.Profile{
ID: id,
Peername: fmt.Sprintf("test-repo-%d", peerNum),
PrivKey: pk,
}, newTestFS(ctx), event.NilBus)
if err != nil {
return r, err
}
if dataIndex == -1 || dataIndex >= len(datasets) {
return r, nil
}
tc, err := dstest.NewTestCaseFromDir(TestdataPath(datasets[dataIndex]))
if err != nil {
return r, err
}
if _, err := createDataset(r, tc); err != nil {
return nil, fmt.Errorf("error creating dataset: %s", err.Error())
}
return r, nil
}
// it's tempting to use base.CreateDataset here, but we can't b/c import cycle :/
// this version of createDataset doesn't run transforms or prepare viz. Test cases
// should be designed to avoid requiring Tranforms be run or Viz be prepped
func createDataset(r repo.Repo, tc dstest.TestCase) (ref reporef.DatasetRef, err error) {
var (
ctx = context.Background()
ds = tc.Input
pro = r.Profiles().Owner()
// NOTE - struct fields need to be instantiated to make assign set to
// new pointer values
userSet = &dataset.Dataset{
Commit: &dataset.Commit{},
Meta: &dataset.Meta{},
Structure: &dataset.Structure{},
Transform: &dataset.Transform{},
Viz: &dataset.Viz{},
}
path string
resBody qfs.File
dsName = ds.Name
)
userSet.Assign(ds)
if ds.Commit != nil {
// NOTE: add author ProfileID here to keep the dataset package agnostic to
// all identity stuff except keypair crypto
ds.Commit.Author = &dataset.User{ID: pro.ID.String()}
}
sw := dsfs.SaveSwitches{Pin: true, ShouldRender: true}
fs := r.Filesystem()
if path, err = dsfs.CreateDataset(ctx, fs, fs.DefaultWriteFS(), r.Bus(), ds, nil, pro.PrivKey, sw); err != nil {
return
}
if ds.PreviousPath != "" && ds.PreviousPath != "/" {
prev := reporef.DatasetRef{
ProfileID: pro.ID,
Peername: pro.Peername,
Name: dsName,
Path: ds.PreviousPath,
}
// should be ok to skip this error. we may not have the previous
// reference locally
_ = r.DeleteRef(prev)
}
ref = reporef.DatasetRef{
ProfileID: pro.ID,
Peername: pro.Peername,
Name: dsName,
Path: path,
}
if err = r.PutRef(ref); err != nil {
return
}
// TODO (b5): confirm these assignments happen in dsfs.CreateDataset with tests
ds.ProfileID = pro.ID.String()
ds.Peername = pro.Peername
ds.Path = path
// TODO(dustmop): When we switch to initIDs, use the standard resolver instead of logbook.
// Whether there is a previous version is equivalent to whether we have an initID coming
// into this function.
initID, err := r.Logbook().RefToInitID(dsref.Ref{Username: ds.Peername, Name: dsName})
if err == logbook.ErrNotFound {
// If dataset does not exist yet, initialize with the given name
initID, err = r.Logbook().WriteDatasetInit(ctx, dsName)
if err != nil {
return ref, err
}
}
if err = r.Logbook().WriteVersionSave(ctx, initID, ds, nil); err != nil && err != logbook.ErrNoLogbook {
return
}
ds, err = dsfs.LoadDataset(ctx, r.Filesystem(), ref.Path)
if err != nil {
return ref, err
}
ds.Name = ref.Name
ds.Peername = ref.Peername
ref.Dataset = ds
// need to open here b/c we might be doing a dry-run, which would mean we have
// references to files in a store that won't exist after this function call
// TODO (b5): this should be replaced with a call to OpenDataset with a qfs that
// knows about the store
if resBody, err = r.Filesystem().Get(ctx, ref.Dataset.BodyPath); err != nil {
return ref, err
}
ref.Dataset.SetBodyFile(resBody)
return ref, nil
}
// NewMemRepoFromDir reads a director of testCases and calls createDataset
// on each case with the given privatekey, yeilding a repo where the peer with
// this pk has created each dataset in question
func NewMemRepoFromDir(path string) (repo.Repo, crypto.PrivKey, error) {
ctx := context.TODO()
// TODO (b5) - use a function & a contstant to get this path
cfgPath := filepath.Join(path, "config.yaml")
cfg, err := config.ReadFromFile(cfgPath)
if err != nil {
return nil, nil, err
}
pro, err := profile.NewProfile(cfg.Profile)
if err != nil {
return nil, nil, err
}
mr, err := repo.NewMemRepoWithProfile(ctx, pro, newTestFS(ctx), event.NilBus)
if err != nil {
return mr, pro.PrivKey, err
}
tc, err := dstest.LoadTestCases(path)
if err != nil {
return mr, pro.PrivKey, err
}
for _, c := range tc {
if _, err := createDataset(mr, c); err != nil {
return mr, pro.PrivKey, err
}
}
return mr, pro.PrivKey, nil
}
// BadBodyFile is a bunch of bad CSV data
var BadBodyFile = qfs.NewMemfileBytes("bad_csv_file.csv", []byte(`
asdlkfasd,,
fm as
f;lajsmf
a
's;f a'
sdlfj asdf`))
// BadDataFormatFile has weird line lengths
var BadDataFormatFile = qfs.NewMemfileBytes("abc.csv", []byte(`
"colA","colB","colC","colD"
1,2,3,4
1,2,3`))
// BadStructureFile has double-named columns
var BadStructureFile = qfs.NewMemfileBytes("badStructure.csv", []byte(`
colA, colB, colB, colC
1,2,3,4
1,2,3,4`))