Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
more tests + testability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenpeeters committed Mar 31, 2017
1 parent 3ac9cec commit bd2a5e0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 32 deletions.
33 changes: 4 additions & 29 deletions src/coffee/storage.coffee
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
exec = (require 'child_process').exec
fs = require 'fs-extra'
path = require 'path'
request = require 'request'

lib = require './storage/lib.coffee'

module.exports = (agent, mqtt, config) ->
remoteFs = (cmd, payload, cb) ->
request
url: "#{config.remotefsUrl}/fs/#{cmd}"
method: 'POST'
json: payload
, (err, res, body) ->
console.error err if err
cb err, body

publishDataStoreUsage = (dir) -> ->
exec "df -B1 #{dir} | tail -1 | awk '{ print $2 }{ print $3}{ print $5}'", (err, stdout, stderr) ->
if err
console.error err
callback null, stderr
totalSize = stdout.split("\n")[0]
usedSize = stdout.split("\n")[1]
percentage = stdout.split("\n")[2]
mqtt.publish '/agent/storage/size',
name: dir
total: totalSize
used: usedSize
percentage: percentage

setInterval publishDataStoreUsage(config.dataDir), 5000
setInterval lib.publishDataStoreUsage(mqtt, config.dataDir), 5000

publishStorageBuckets = (err, buckets) ->
mqtt.publish '/agent/storage/buckets', buckets unless err
Expand All @@ -50,7 +25,7 @@ module.exports = (agent, mqtt, config) ->
targetpath = path.join '/', config.domain, name
lockFile = path.join basePath, ".#{name}.delete.lock"
fs.writeFile lockFile, "Deleting #{targetpath}...", ->
remoteFs 'rm', {dir: targetpath}, ->
lib.remoteFs config.remotefsUrl, 'rm', {dir: targetpath}, ->
fs.unlink lockFile, callback

agent.on '/storage/create', (params, {name, source}, callback) ->
Expand All @@ -59,7 +34,7 @@ module.exports = (agent, mqtt, config) ->
targetpath = path.join '/', config.domain, name
lockFile = path.join basePath, ".#{name}.copy.lock"
fs.writeFile lockFile, "Copying #{srcpath} to #{targetpath}...", ->
remoteFs 'cp', {source: srcpath, destination: targetpath}, ->
lib.remoteFs config.remotefsUrl, 'cp', {source: srcpath, destination: targetpath}, ->
fs.unlink lockFile, callback
else
targetpath = path.join basePath, name
Expand All @@ -71,6 +46,6 @@ module.exports = (agent, mqtt, config) ->
lockFile = path.join basePath, ".#{name}.size.lock"
console.log "Retrieving size #{targetpath}"
fs.writeFile lockFile, "Retrieving size #{targetpath} ...", ->
remoteFs 'du', {dir: targetpath}, (err, response) ->
lib.remoteFs config.remotefsUrl, 'du', {dir: targetpath}, (err, response) ->
fs.unlink lockFile, ->
callback null, { name: name, size: response.size}
25 changes: 25 additions & 0 deletions src/coffee/storage/lib.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
exec = (require 'child_process').exec
path = require 'path'
request = require 'request'

module.exports =
listStorageBuckets: (fs, dir, cb) ->
Expand All @@ -20,3 +22,26 @@ module.exports =
cb? null, files
catch ex
cb? ex, null

remoteFs: (remoteFsUrl, cmd, payload, cb) ->
request
url: "#{remoteFsUrl}/fs/#{cmd}"
method: 'POST'
json: payload
, (err, res, body) ->
console.error err if err
cb err, body

publishDataStoreUsage: (mqtt, dir) -> ->
exec "df -B1 #{dir} | tail -1 | awk '{ print $2 }{ print $3}{ print $5}'", (err, stdout, stderr) ->
if err
console.error err
callback null, stderr
totalSize = stdout.split("\n")[0]
usedSize = stdout.split("\n")[1]
percentage = stdout.split("\n")[2]
mqtt.publish '/agent/storage/size',
name: dir
total: totalSize
used: usedSize
percentage: percentage
30 changes: 27 additions & 3 deletions tests/coffee/storage/lib.test.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
assert = require 'assert'
td = require 'testdouble'
lib = require '../../../src/coffee/storage/lib.coffee'
assert = require 'assert'
td = require 'testdouble'
request = td.replace 'request'
exec = td.replace('child_process').exec
lib = require '../../../src/coffee/storage/lib.coffee'

describe 'Storage/Lib', ->
describe 'listStorageBuckets', ->
Expand Down Expand Up @@ -58,3 +60,25 @@ describe 'Storage/Lib', ->
created: 'sometime'
isLocked: true
]

describe 'remoteFs', ->
it 'should call the remoteFs endpoint with the desired command and return its response to the caller', ->
cb = td.function()
td.when(request({
url: 'remoteFsUrl/fs/some-cmd'
method: 'POST'
json: 'payload'}
)).thenCallback null, null, 'mydata'
lib.remoteFs 'remoteFsUrl', 'some-cmd', 'payload', cb
td.verify cb null, 'mydata'

describe 'publishDataStoreUsage', ->
it 'should', ->
mqtt = td.object ['publish']
td.when(exec "df -B1 mydir | tail -1 | awk '{ print $2 }{ print $3}{ print $5}'" ).thenCallback null, "1\n2\n3"
lib.publishDataStoreUsage(mqtt, 'mydir')()
td.verify mqtt.publish '/agent/storage/size',
name: 'mydir'
total: "1"
used: "2"
percentage: "3"

0 comments on commit bd2a5e0

Please sign in to comment.