Browse Source

Merge branch 'unlink_symlink' of CoBox/kappa-drive-mount into master

peg 3 years ago
parent
commit
7a9753a4f8
2 changed files with 272 additions and 4 deletions
  1. 240
    4
      test/index.test.js
  2. 32
    0
      test/mount_hyperdrive.js

+ 240
- 4
test/index.test.js View File

@@ -73,12 +73,40 @@ describe('mount', (context) => {
73 73
       mount(drive, storage).then((destroy) => {
74 74
         fs.mkdir(path.join(storage, 'antelopes'), (err) => {
75 75
           assert.error(err, 'no error')
76
-          fs.writeFile(path.join(storage, 'antelopes', 'tree.txt'), 'flim', (err) => {
76
+          fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
77 77
             assert.error(err, 'no error')
78
-            fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
78
+            assert.same(files, [], 'drive.readdir returns empty array for empty directory')
79
+            fs.writeFile(path.join(storage, 'antelopes', 'tree.txt'), 'flim', (err) => {
79 80
               assert.error(err, 'no error')
80
-              assert.same(files, ['tree.txt'], 'drive.readdir returns directory name')
81
-              cleanup(destroy, storage).then(next)
81
+              fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
82
+                assert.error(err, 'no error')
83
+                assert.same(files, ['tree.txt'], 'drive.readdir returns directory name')
84
+                cleanup(destroy, storage).then(next)
85
+              })
86
+            })
87
+          })
88
+        })
89
+      })
90
+    })
91
+  })
92
+
93
+  context('unlink in mounted directory', (assert, next) => {
94
+    var drive = KappaDrive(tmp())
95
+    var storage = tmp()
96
+    drive.ready(() => {
97
+      mount(drive, storage).then((destroy) => {
98
+        fs.writeFile(path.join(storage, 'tree.txt'), 'is this a tree', (err) => {
99
+          assert.error(err, 'no error')
100
+          fs.readdir(storage, (err, files) => {
101
+            assert.error(err, 'no error')
102
+            assert.same(files, ['tree.txt'], 'readdir')
103
+            fs.unlink(path.join(storage, 'tree.txt'), (err) => {
104
+              assert.error(err, 'no error')
105
+              fs.readdir(storage, (err, files) => {
106
+                assert.error(err, 'no error')
107
+                assert.same(files, [], 'drive.readdir returns nothing')
108
+                cleanup(destroy, storage).then(next)
109
+              })
82 110
             })
83 111
           })
84 112
         })
@@ -86,6 +114,46 @@ describe('mount', (context) => {
86 114
     })
87 115
   })
88 116
 
117
+  context('replicate into empty drive', (assert, next) => {
118
+    var feedsOnDisk = {
119
+      orange: tmp(),
120
+      blue: tmp()
121
+    }
122
+    var orangeDrive = KappaDrive(feedsOnDisk.orange)
123
+    var blueDrive = KappaDrive(feedsOnDisk.blue)
124
+
125
+    var storage = {}
126
+    var destroy = {}
127
+
128
+    function setup (drive, colour, cb) {
129
+      storage[colour] = tmp()
130
+      mount(drive, storage[colour]).then((destroyFn) => {
131
+        destroy[colour] = destroyFn
132
+        drive.ready(cb)
133
+      })
134
+    }
135
+
136
+    setup(orangeDrive, 'orange', function () {
137
+      setup(blueDrive, 'blue', function () {
138
+        fs.writeFile(path.join(storage.orange, 'tree.txt'), 'is this a tree', (err) => {
139
+          assert.error(err, 'no error on write')
140
+          replicate(orangeDrive, blueDrive, check)
141
+        })
142
+      })
143
+    })
144
+
145
+    function check (err) {
146
+      assert.error(err, 'no error on replicate')
147
+      fs.readdir(storage.blue, (err, files) => {
148
+        assert.error(err, 'no error on readdir')
149
+        assert.same(files, ['tree.txt'], 'fs.readdir returns file')
150
+        cleanup(destroy.orange, storage.orange).then(() => {
151
+          cleanup(destroy.blue, storage.blue).then(next)
152
+        })
153
+      })
154
+    }
155
+  })
156
+
89 157
   context("replicate - append to another peer's file - using unix commands", (assert, next) => {
90 158
     var feedsOnDisk = {
91 159
       orange: tmp(),
@@ -145,6 +213,174 @@ describe('mount', (context) => {
145 213
     }
146 214
   })
147 215
 
216
+  context("replicate - unlink another peer's file - using unix commands", (assert, next) => {
217
+    var feedsOnDisk = {
218
+      orange: tmp(),
219
+      blue: tmp()
220
+    }
221
+    var orangeDrive = KappaDrive(feedsOnDisk.orange)
222
+    var blueDrive = KappaDrive(feedsOnDisk.blue)
223
+
224
+    var storage = {}
225
+    var destroy = {}
226
+
227
+    function setup (drive, colour, cb) {
228
+      storage[colour] = tmp()
229
+      mount(drive, storage[colour]).then((destroyFn) => {
230
+        destroy[colour] = destroyFn
231
+        drive.ready(cb)
232
+      })
233
+    }
234
+
235
+    setup(orangeDrive, 'orange', function () {
236
+      setup(blueDrive, 'blue', function () {
237
+        run(`echo "what noise does a dog make?" > ${storage.orange}/hello.txt`, (err, output) => {
238
+          assert.error(err, 'no error on write')
239
+          // 'seed' the blue drive with a file (TODO this is a hack)
240
+          run(`echo "this file is not needed" > ${storage.blue}/blue.txt`, (err, output) => {
241
+            assert.error(err, 'no error on echo to file')
242
+            replicate(orangeDrive, blueDrive, check)
243
+          })
244
+        })
245
+      })
246
+    })
247
+
248
+    function check (err) {
249
+      assert.error(err, 'no error on replicate')
250
+      run(`ls ${storage.blue}`, (err, output) => {
251
+        assert.error(err, 'no error on ls')
252
+        assert.true(output[0].split('\n').indexOf('hello.txt') > -1, 'file successfully replicated')
253
+        run(`unlink ${storage.blue}/hello.txt`, (err, output) => {
254
+          assert.error(err, 'no error on rm file')
255
+          run(`cat ${storage.blue}/hello.txt`, (err, output) => {
256
+            assert.ok(err, 'error on cat out file')
257
+            assert.notOk(output, 'cat returns no output')
258
+            replicate(orangeDrive, blueDrive, (err) => {
259
+              assert.error(err, 'no error on replicate')
260
+              run(`ls ${storage.orange}`, (err, output) => {
261
+                assert.error(err, 'no error')
262
+                //TODO this line should also use .split:
263
+                assert.ok(output.indexOf('hello.txt\n') < 0, 'ls does not find the file')
264
+                cleanup(destroy.orange, storage.orange).then(() => {
265
+                  cleanup(destroy.blue, storage.blue).then(next)
266
+                })
267
+              })
268
+            })
269
+          })
270
+        })
271
+      })
272
+    }
273
+  })
274
+
275
+  context("replicate - rmdir another peer's dir - using unix commands", (assert, next) => {
276
+    var feedsOnDisk = {
277
+      orange: tmp(),
278
+      blue: tmp()
279
+    }
280
+    var orangeDrive = KappaDrive(feedsOnDisk.orange)
281
+    var blueDrive = KappaDrive(feedsOnDisk.blue)
282
+
283
+    var storage = {}
284
+    var destroy = {}
285
+
286
+    function setup (drive, colour, cb) {
287
+      storage[colour] = tmp()
288
+      mount(drive, storage[colour]).then((destroyFn) => {
289
+        destroy[colour] = destroyFn
290
+        drive.ready(cb)
291
+      })
292
+    }
293
+
294
+    setup(orangeDrive, 'orange', function () {
295
+      setup(blueDrive, 'blue', function () {
296
+        run(`mkdir ${storage.orange}/cabbage`, (err, output) => {
297
+          assert.error(err, 'no error on mkdir')
298
+          // 'seed' the blue drive with a file (TODO this is a hack)
299
+          run(`echo "this file is not needed" > ${storage.blue}/blue.txt`, (err, output) => {
300
+            assert.error(err, 'no error on echo to file')
301
+            replicate(orangeDrive, blueDrive, check)
302
+          })
303
+        })
304
+      })
305
+    })
306
+
307
+    function check (err) {
308
+      assert.error(err, 'no error on replicate')
309
+      run(`ls ${storage.blue}`, (err, output) => {
310
+        assert.error(err, 'no error on ls')
311
+        assert.true(output[0].split('\n').indexOf('cabbage') > -1, 'dir successfully replicated')
312
+        run(`rmdir ${storage.blue}/cabbage`, (err, output) => {
313
+          assert.error(err, 'no error on rmdir')
314
+          run(`ls ${storage.blue}`, (err, output) => {
315
+            assert.error(err, 'error on ls')
316
+            assert.false(output[0].split('\n').indexOf('cabbage') > -1, 'ls no longer lists dir')
317
+            replicate(orangeDrive, blueDrive, (err) => {
318
+              assert.error(err, 'no error on replicate')
319
+              run(`ls ${storage.orange}`, (err, output) => {
320
+                assert.error(err, 'no error')
321
+                assert.false(output[0].split('\n').indexOf('cabbage') > -1, 'ls no longer lists dir')
322
+                cleanup(destroy.orange, storage.orange).then(() => {
323
+                  cleanup(destroy.blue, storage.blue).then(next)
324
+                })
325
+              })
326
+            })
327
+          })
328
+        })
329
+      })
330
+    }
331
+  })
332
+
333
+  context("replicate - truncate another peer's file - using unix commands", (assert, next) => {
334
+    var feedsOnDisk = {
335
+      orange: tmp(),
336
+      blue: tmp()
337
+    }
338
+    var orangeDrive = KappaDrive(feedsOnDisk.orange)
339
+    var blueDrive = KappaDrive(feedsOnDisk.blue)
340
+
341
+    var storage = {}
342
+    var destroy = {}
343
+
344
+    function setup (drive, colour, cb) {
345
+      storage[colour] = tmp()
346
+      mount(drive, storage[colour]).then((destroyFn) => {
347
+        destroy[colour] = destroyFn
348
+        drive.ready(cb)
349
+      })
350
+    }
351
+
352
+    setup(orangeDrive, 'orange', function () {
353
+      setup(blueDrive, 'blue', function () {
354
+        run(`echo "what noise does a dog make?" > ${storage.orange}/hello.txt`, (err, output) => {
355
+          assert.error(err, 'no error on write')
356
+          // 'seed' the blue drive with a file (TODO this is a hack)
357
+          run(`echo "this file is not needed" > ${storage.blue}/blue.txt`, (err, output) => {
358
+            assert.error(err, 'no error on echo to file')
359
+            replicate(orangeDrive, blueDrive, check)
360
+          })
361
+        })
362
+      })
363
+    })
364
+
365
+    function check (err) {
366
+      assert.error(err, 'no error on replicate')
367
+      run(`ls ${storage.blue}`, (err, output) => {
368
+        assert.error(err, 'no error on ls')
369
+        assert.true(output[0].split('\n').indexOf('hello.txt') > -1, 'file successfully replicated')
370
+        fs.truncate(path.join(storage.blue, 'hello.txt'), 10, (err) => {
371
+          assert.error(err, 'no error on truncate file')
372
+          fs.readFile(path.join(storage.blue, 'hello.txt'), 'utf-8', (err, data) => {
373
+            assert.error(err, 'no error on read file')
374
+            assert.same(data, 'what noise', 'returns expected contents')
375
+            cleanup(destroy.orange, storage.orange).then(() => {
376
+              cleanup(destroy.blue, storage.blue).then(next)
377
+            })
378
+          })
379
+        })
380
+      })
381
+    }
382
+  })
383
+
148 384
   context("replicate - append to another peer's empty file - using unix commands", (assert, next) => {
149 385
     var feedsOnDisk = {
150 386
       orange: tmp(),

+ 32
- 0
test/mount_hyperdrive.js View File

@@ -0,0 +1,32 @@
1
+#!/usr/bin/env node
2
+// This script is to mount a normal hyperdrive in the same way we would
3
+// mount a kappa-drive, to make it easy to compare thier behaviour.
4
+// Ideally we want to make all filesystem operations which are possible with
5
+// hyperdrive also possible with kappa-drive
6
+const minimist = require('minimist')
7
+const ram = require('random-access-memory')
8
+const path = require('path')
9
+
10
+const mount = require('..')
11
+const hyperdrive = require('hyperdrive')
12
+
13
+const STORAGE = (p) => path.resolve(p)
14
+
15
+const argv = minimist(process.argv.slice(2))
16
+var storage
17
+
18
+if (argv.db) {
19
+  storage = argv.db ? STORAGE(argv.db) : ram
20
+} else {
21
+  storage = ram
22
+}
23
+
24
+var mountdir = argv.mount || './mnt'
25
+var key = argv.key ? Buffer.from(argv.key, 'hex') : null
26
+
27
+var drive = hyperdrive(storage, key)
28
+
29
+drive.ready(() => {
30
+  mount(drive, mountdir)
31
+  // swarm(drive)
32
+})