 exec   = require('child_process').execSync
 
 describe 'gobgp-node', ->
-  global_rib = ->
-    JSON.parse(exec('gobgp -j global rib'))
+  global_rib = (prefix) ->
+    table = JSON.parse(exec('gobgp -j global rib'))
+    if prefix
+      table[prefix]
+    else
+      table
 
-  flowspec_rib = ->
-    JSON.parse(exec('gobgp -j global rib -a ipv4-flowspec'))
+  flowspec_rib = (prefix) ->
+    table = JSON.parse(exec('gobgp -j global rib -a ipv4-flowspec'))
+    if prefix
+      table[prefix]
+    else
+      table
 
   communities = (prefix) ->
-    table = global_rib()
-    paths = table.filter (i) -> i['prefix']==prefix
+    paths = global_rib(prefix)
     return if paths.empty?
 
-    comms = paths[0]['paths'][0]['attrs'].filter (i) -> i['type']==8
+    comms = paths[0]['attrs'].filter (i) -> i['type']==8
     return comms[0]['communities']
 
   PREFIX = '10.0.0.0/24'
   FLOWSPEC_PREFIX = "match source #{PREFIX} then rate-limit 10000"
+  FLOWSPEC_JSON_PREFIX = "[source:#{PREFIX}]"
 
   beforeEach ->
     exec "gobgp global rib del #{PREFIX}"


      gobgp.modPath family: 'ipv4-unicast', PREFIX

      table = global_rib()
      expect(table.length).to.equal 1
      expect(table[0]['prefix']).to.equal PREFIX

    it 'withdraws a route', ->
      exec "gobgp global rib add #{PREFIX}"


      gobgp.addPath family: 'ipv4-flowspec', FLOWSPEC_PREFIX

      prefixes = flowspec_rib(FLOWSPEC_JSON_PREFIX)
      expect(prefixes.length).to.equal 1

    it 'shows the RIB', ->  # TODO: This does actually nothing. Use chai-as-promised
      exec "gobgp global rib -a ipv4-flowspec add #{FLOWSPEC_PREFIX}"
