From 7133ecfcda1ae82d1ebd8c6fbcab1216b3f75c23 Mon Sep 17 00:00:00 2001
From: Dmitriy Ryajov <dryajov@gmail.com>
Date: Thu, 18 Jan 2018 21:38:05 -0600
Subject: [PATCH] Revert "feat: use new ipfsd-ctl (#186)"

This reverts commit 4d4ef7f043db4f8a3f0f847b1af05453341b6228.
---
 src/block.js         | 10 ++++----
 src/config.js        | 10 ++++----
 src/dag.js           | 10 ++++----
 src/dht.js           | 37 ++++++++++-------------------
 src/files.js         | 10 ++++----
 src/key.js           | 12 ++++------
 src/miscellaneous.js | 12 +++++-----
 src/object.js        | 14 +++++------
 src/pin.js           | 10 ++++----
 src/pubsub.js        | 39 +++++++++++-------------------
 src/swarm.js         | 56 +++++++++++++++-----------------------------
 11 files changed, 83 insertions(+), 137 deletions(-)

diff --git a/src/block.js b/src/block.js
index 54695a43..d0cf8af6 100644
--- a/src/block.js
+++ b/src/block.js
@@ -20,25 +20,23 @@ function expectKey (block, expected, callback) {
 module.exports = (common) => {
   describe('.block', () => {
     let ipfs
-    let ipfsd
 
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        df.spawn({ type, exec }, (err, node) => {
+        factory.spawnNode((err, node) => {
           expect(err).to.not.exist()
-          ipfsd = node
-          ipfs = node.api
+          ipfs = node
           done()
         })
       })
     })
 
-    after((done) => ipfsd.stop(done))
+    after((done) => common.teardown(done))
 
     describe('.put', () => {
       it('a buffer, using defaults', (done) => {
diff --git a/src/config.js b/src/config.js
index 7fc852c6..1cff56c8 100644
--- a/src/config.js
+++ b/src/config.js
@@ -12,25 +12,23 @@ module.exports = (common) => {
   describe('.config', function () {
     this.timeout(30 * 1000)
     let ipfs
-    let ipfsd
 
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        df.spawn({ type, exec }, (err, node) => {
+        factory.spawnNode((err, node) => {
           expect(err).to.not.exist()
-          ipfsd = node
-          ipfs = node.api
+          ipfs = node
           done()
         })
       })
     })
 
-    after((done) => ipfsd.stop(done))
+    after((done) => common.teardown(done))
 
     describe('.get', () => {
       it('retrieve the whole config', (done) => {
diff --git a/src/dag.js b/src/dag.js
index 1304a85e..a8a70cac 100644
--- a/src/dag.js
+++ b/src/dag.js
@@ -17,25 +17,23 @@ const CID = require('cids')
 module.exports = (common) => {
   describe('.dag', () => {
     let ipfs
-    let ipfsd
 
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        df.spawn({ type, exec }, (err, node) => {
+        factory.spawnNode((err, node) => {
           expect(err).to.not.exist()
-          ipfs = node.api
-          ipfsd = node
+          ipfs = node
           done()
         })
       })
     })
 
-    after((done) => ipfsd.stop(done))
+    after((done) => common.teardown(done))
 
     let pbNode
     let cborNode
diff --git a/src/dht.js b/src/dht.js
index ad3047d3..06af6e92 100644
--- a/src/dht.js
+++ b/src/dht.js
@@ -10,24 +10,14 @@ const series = require('async/series')
 const parallel = require('async/parallel')
 const CID = require('cids')
 
-function spawnWithId (df, type, exec, callback) {
-  if (typeof type === 'function') {
-    callback = type
-    type = undefined
-  }
-
-  if (typeof exec === 'function') {
-    callback = exec
-    exec = undefined
-  }
-
+function spawnWithId (factory, callback) {
   waterfall([
-    (cb) => df.spawn({ type, exec }, cb),
-    (node, cb) => node.api.id((err, peerId) => {
+    (cb) => factory.spawnNode(cb),
+    (node, cb) => node.id((err, peerId) => {
       if (err) {
         return cb(err)
       }
-      node.api.peerId = peerId
+      node.peerId = peerId
       cb(null, node)
     })
   ], callback)
@@ -41,26 +31,23 @@ module.exports = (common) => {
     let nodeB
     let nodeC
 
-    let ipfsdNodes
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
         series([
-          (cb) => spawnWithId(df, type, cb),
-          (cb) => spawnWithId(df, type, cb),
-          (cb) => spawnWithId(df, type, cb)
+          (cb) => spawnWithId(factory, cb),
+          (cb) => spawnWithId(factory, cb),
+          (cb) => spawnWithId(factory, cb)
         ], (err, nodes) => {
           expect(err).to.not.exist()
 
-          ipfsdNodes = nodes
-
-          nodeA = nodes[0].api
-          nodeB = nodes[1].api
-          nodeC = nodes[2].api
+          nodeA = nodes[0]
+          nodeB = nodes[1]
+          nodeC = nodes[2]
 
           parallel([
             (cb) => nodeA.swarm.connect(nodeB.peerId.addresses[0], cb),
@@ -71,7 +58,7 @@ module.exports = (common) => {
       })
     })
 
-    after((done) => parallel(ipfsdNodes.map((node) => (cb) => node.stop(cb)), done))
+    after((done) => common.teardown(done))
 
     describe('.get and .put', () => {
       it('errors when getting a non-existent key from the DHT', (done) => {
diff --git a/src/files.js b/src/files.js
index f981bb67..41725156 100644
--- a/src/files.js
+++ b/src/files.js
@@ -24,7 +24,6 @@ module.exports = (common) => {
     this.timeout(40 * 1000)
 
     let ipfs
-    let ipfsd
 
     function fixture (path) {
       return loadFixture(__dirname, path, 'interface-ipfs-core')
@@ -56,18 +55,17 @@ module.exports = (common) => {
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        df.spawn({ type, exec }, (err, node) => {
+        factory.spawnNode((err, node) => {
           expect(err).to.not.exist()
-          ipfs = node.api
-          ipfsd = node
+          ipfs = node
           done()
         })
       })
     })
 
-    after((done) => ipfsd.stop(done))
+    after((done) => common.teardown(done))
 
     describe('.add', () => {
       it('a Buffer', (done) => {
diff --git a/src/key.js b/src/key.js
index 43750893..4e60f72e 100644
--- a/src/key.js
+++ b/src/key.js
@@ -12,11 +12,10 @@ const hat = require('hat')
 module.exports = (common) => {
   describe('.key', () => {
     const keyTypes = [
-      { type: 'rsa', size: 2048 }
+      {type: 'rsa', size: 2048}
     ]
     const keys = []
     let ipfs
-    let ipfsd
     let withGo
 
     before(function (done) {
@@ -24,12 +23,11 @@ module.exports = (common) => {
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        df.spawn({ type, exec }, (err, node) => {
+        factory.spawnNode((err, node) => {
           expect(err).to.not.exist()
-          ipfsd = node
-          ipfs = node.api
+          ipfs = node
           ipfs.id((err, id) => {
             expect(err).to.not.exist()
             withGo = id.agentVersion.startsWith('go-ipfs')
@@ -39,7 +37,7 @@ module.exports = (common) => {
       })
     })
 
-    after((done) => ipfsd.stop(done))
+    after((done) => common.teardown(done))
 
     describe('.gen', () => {
       keyTypes.forEach((kt) => {
diff --git a/src/miscellaneous.js b/src/miscellaneous.js
index 56f5a189..7a7d60c9 100644
--- a/src/miscellaneous.js
+++ b/src/miscellaneous.js
@@ -11,25 +11,25 @@ chai.use(dirtyChai)
 module.exports = (common) => {
   describe('.miscellaneous', () => {
     let ipfs
-    let ipfsd
 
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        df.spawn({ type, exec }, (err, node) => {
+        factory.spawnNode((err, node) => {
           expect(err).to.not.exist()
-          ipfs = node.api
-          ipfsd = node
+          ipfs = node
           done()
         })
       })
     })
 
-    after((done) => ipfsd.stop(done))
+    after((done) => {
+      common.teardown(done)
+    })
 
     it('.id', (done) => {
       ipfs.id((err, res) => {
diff --git a/src/object.js b/src/object.js
index e33736c8..0ebf3df4 100644
--- a/src/object.js
+++ b/src/object.js
@@ -17,25 +17,25 @@ module.exports = (common) => {
     this.timeout(80 * 1000)
 
     let ipfs
-    let ipfsd
 
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        df.spawn({ type, exec }, (err, node) => {
+        factory.spawnNode((err, node) => {
           expect(err).to.not.exist()
-          ipfs = node.api
-          ipfsd = node
+          ipfs = node
           done()
         })
       })
     })
 
-    after((done) => ipfsd.stop(done))
+    after((done) => {
+      common.teardown(done)
+    })
 
     describe('callback API', () => {
       describe('.new', () => {
@@ -843,7 +843,7 @@ module.exports = (common) => {
         return ipfs.object.put(testObj, (err, node) => {
           expect(err).to.not.exist()
 
-          return ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', { enc: 'base58' })
+          return ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', {enc: 'base58'})
             .then((stats) => {
               const expected = {
                 Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ',
diff --git a/src/pin.js b/src/pin.js
index f0cc5cfd..6c76050e 100644
--- a/src/pin.js
+++ b/src/pin.js
@@ -17,19 +17,17 @@ module.exports = (common) => {
     this.timeout(50 * 1000)
 
     let ipfs
-    let ipfsd
 
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(60 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        df.spawn({ type, exec }, (err, node) => {
+        factory.spawnNode((err, node) => {
           expect(err).to.not.exist()
-          ipfs = node.api
-          ipfsd = node
+          ipfs = node
           populate()
         })
       })
@@ -45,7 +43,7 @@ module.exports = (common) => {
       }
     })
 
-    after((done) => ipfsd.stop(done))
+    after((done) => common.teardown(done))
 
     describe('callback API', () => {
       // 1st, because ipfs.files.add pins automatically
diff --git a/src/pubsub.js b/src/pubsub.js
index d324501a..d9e40f96 100644
--- a/src/pubsub.js
+++ b/src/pubsub.js
@@ -32,24 +32,14 @@ function waitForPeers (ipfs, topic, peersToWait, callback) {
   }, 500)
 }
 
-function spawnWithId (df, type, exec, callback) {
-  if (typeof type === 'function') {
-    callback = type
-    type = undefined
-  }
-
-  if (typeof exec === 'function') {
-    callback = exec
-    exec = undefined
-  }
-
+function spawnWithId (factory, callback) {
   waterfall([
-    (cb) => df.spawn({ type, exec, args: ['--enable-pubsub-experiment'] }, cb),
-    (node, cb) => node.api.id((err, res) => {
+    (cb) => factory.spawnNode(cb),
+    (node, cb) => node.id((err, res) => {
       if (err) {
         return cb(err)
       }
-      node.api.peerId = res
+      node.peerId = res
       cb(null, node)
     })
   ], callback)
@@ -78,37 +68,36 @@ module.exports = (common) => {
     let ipfs2
     let ipfs3
 
-    let ipfsdNodes
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(100 * 1000)
 
-      common.setup((err, df) => {
+      common.setup((err, factory) => {
         if (err) {
           return done(err)
         }
 
         series([
-          (cb) => spawnWithId(df, cb),
-          (cb) => spawnWithId(df, cb),
-          (cb) => spawnWithId(df, cb)
+          (cb) => spawnWithId(factory, cb),
+          (cb) => spawnWithId(factory, cb),
+          (cb) => spawnWithId(factory, cb)
         ], (err, nodes) => {
           if (err) {
             return done(err)
           }
 
-          ipfsdNodes = nodes
-
-          ipfs1 = nodes[0].api
-          ipfs2 = nodes[1].api
-          ipfs3 = nodes[2].api
+          ipfs1 = nodes[0]
+          ipfs2 = nodes[1]
+          ipfs3 = nodes[2]
           done()
         })
       })
     })
 
-    after((done) => parallel(ipfsdNodes.map((node) => (cb) => node.stop(cb)), done))
+    after((done) => {
+      common.teardown(done)
+    })
 
     describe('single node', () => {
       describe('.publish', () => {
diff --git a/src/swarm.js b/src/swarm.js
index 8ee89314..9c43869a 100644
--- a/src/swarm.js
+++ b/src/swarm.js
@@ -8,7 +8,6 @@ const dirtyChai = require('dirty-chai')
 const expect = chai.expect
 chai.use(dirtyChai)
 const series = require('async/series')
-const parallel = require('async/parallel')
 const multiaddr = require('multiaddr')
 const os = require('os')
 const path = require('path')
@@ -20,35 +19,32 @@ module.exports = (common) => {
 
     let ipfsA
     let ipfsB
-    let dfInstance
+    let factoryInstance
 
-    let nodes = []
     before(function (done) {
       // CI takes longer to instantiate the daemon, so we need to increase the
       // timeout for the before step
       this.timeout(100 * 1000)
 
-      common.setup((err, df, type, exec) => {
+      common.setup((err, factory) => {
         expect(err).to.not.exist()
-        dfInstance = df
+        factoryInstance = factory
         series([
-          (cb) => df.spawn({ type, exec }, (err, node) => {
+          (cb) => factory.spawnNode((err, node) => {
             expect(err).to.not.exist()
-            ipfsA = node.api
-            nodes.push(node)
+            ipfsA = node
             cb()
           }),
-          (cb) => df.spawn((err, node) => {
+          (cb) => factory.spawnNode((err, node) => {
             expect(err).to.not.exist()
-            ipfsB = node.api
-            nodes.push(node)
+            ipfsB = node
             cb()
           })
         ], done)
       })
     })
 
-    after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done))
+    after((done) => common.teardown(done))
 
     let ipfsBId
 
@@ -94,7 +90,7 @@ module.exports = (common) => {
         })
 
         it('verbose', (done) => {
-          ipfsA.swarm.peers({ verbose: true }, (err, peers) => {
+          ipfsA.swarm.peers({verbose: true}, (err, peers) => {
             expect(err).to.not.exist()
             expect(peers).to.have.length.above(0)
 
@@ -136,8 +132,6 @@ module.exports = (common) => {
           }
 
           it('Connecting two peers with one address each', (done) => {
-            let nodes = []
-
             let nodeA
             let nodeB
             let nodeBAddress
@@ -145,18 +139,16 @@ module.exports = (common) => {
             const config = getConfig(addresses)
             series([
               (cb) => {
-                dfInstance.spawn({ repoPath: getRepoPath(), config }, (err, node) => {
+                factoryInstance.spawnNode(getRepoPath(), config, (err, node) => {
                   expect(err).to.not.exist()
-                  nodes.push(node)
-                  nodeA = node.api
+                  nodeA = node
                   cb()
                 })
               },
               (cb) => {
-                dfInstance.spawn({ repoPath: getRepoPath(), config }, (err, node) => {
+                factoryInstance.spawnNode(getRepoPath(), config, (err, node) => {
                   expect(err).to.not.exist()
-                  nodes.push(node)
-                  nodeB = node.api
+                  nodeB = node
                   cb()
                 })
               },
@@ -183,15 +175,10 @@ module.exports = (common) => {
                   cb()
                 })
               }
-            ], (err) => {
-              expect(err).to.not.exist()
-              parallel(nodes.map((node) => (cb) => node.stop(cb)), done)
-            })
+            ], done)
           })
 
           it('Connecting two peers with two addresses each', (done) => {
-            let nodes = []
-
             let nodeA
             let nodeB
             let nodeBAddress
@@ -207,18 +194,16 @@ module.exports = (common) => {
             ])
             series([
               (cb) => {
-                dfInstance.spawn({ repoPath: getRepoPath(), config: configA }, (err, node) => {
+                factoryInstance.spawnNode(getRepoPath(), configA, (err, node) => {
                   expect(err).to.not.exist()
-                  nodes.push(node)
-                  nodeA = node.api
+                  nodeA = node
                   cb()
                 })
               },
               (cb) => {
-                dfInstance.spawn({ repoPath: getRepoPath(), config: configB }, (err, node) => {
+                factoryInstance.spawnNode(getRepoPath(), configB, (err, node) => {
                   expect(err).to.not.exist()
-                  nodes.push(node)
-                  nodeB = node.api
+                  nodeB = node
                   cb()
                 })
               },
@@ -245,10 +230,7 @@ module.exports = (common) => {
                   cb()
                 })
               }
-            ], (err) => {
-              expect(err).to.not.exist()
-              parallel(nodes.map((node) => (cb) => node.stop(cb)), done)
-            })
+            ], done)
           })
         })
       })