Fix parsing of non-unit memory numbers and add tests

Signed-off-by: Cameron Diver <cameron@resin.io>
This commit is contained in:
Cameron Diver 2018-04-04 14:03:24 +01:00
parent a6dbfb57a1
commit 1c27ebc354
No known key found for this signature in database
GPG Key ID: E76D7ACBEE436E12
2 changed files with 7 additions and 2 deletions

View File

@ -14,7 +14,7 @@ validRestartPolicies = [ 'no', 'always', 'on-failure', 'unless-stopped' ]
PORTS_REGEX = /^(?:(?:([a-fA-F\d.:]+):)?([\d]*)(?:-([\d]+))?:)?([\d]+)(?:-([\d]+))?(?:\/(udp|tcp))?$/
parseMemoryNumber = (numAsString, defaultVal) ->
m = numAsString?.toString().match(/^([0-9]+)([bkmg])?b?$/i)
m = numAsString?.toString().match(/^([0-9]+)([bkmg]?)b?$/i)
if !m? and defaultVal?
return parseMemoryNumber(defaultVal)
num = m[1]

View File

@ -4,7 +4,6 @@ ComposeService = require '../../src/compose/service'
describe 'compose/service.cofee', ->
describe 'parseMemoryNumber()', ->
makeComposeServiceWithLimit = (memLimit) ->
console.log('MAKING SERVICE WITH LIMIT', memLimit)
new ComposeService(
appId: 123456
serviceId: 123456
@ -15,6 +14,12 @@ describe 'compose/service.cofee', ->
it 'should correctly parse memory number strings without a unit', ->
expect(makeComposeServiceWithLimit('64').memLimit).to.equal(64)
it 'should correctly apply the default value', ->
expect(makeComposeServiceWithLimit(undefined).memLimit).to.equal(0)
it 'should correctly support parsing numbers as memory limits', ->
expect(makeComposeServiceWithLimit(64).memLimit).to.equal(64)
it 'should correctly parse memory number strings that use a byte unit', ->
expect(makeComposeServiceWithLimit('64b').memLimit).to.equal(64)
expect(makeComposeServiceWithLimit('64B').memLimit).to.equal(64)