Skip to content

Commit b5ff3da

Browse files
author
Arturo Pie
committed
fixes and adds tests for the dehumanize function
1 parent bd5fbab commit b5ff3da

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

packer/linux/conf/bin/bk-check-disk-space.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@ set -euo pipefail
77
# or 500M, or a percentage like 5%
88
# min inodes must be a number, default to 250,000
99

10-
# Converts human-readable units like 1.43K and 120.3M to bytes
11-
dehumanize() {
12-
awk '/[0-9][bB]?$/ {printf "%u\n", $1*1024}
13-
/[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)}
14-
/[gG][bB]?$/ {printf "%u\n", $1*(1024*1024)}
15-
/[mM][bB]?$/ {printf "%u\n", $1*(1024)}
16-
/[kK][bB]?$/ {printf "%u\n", $1*1}' <<< "$1"
17-
}
10+
. "$(dirname "$0")"/dehumanize.sh
1811

1912
min_available=${1:-5G}
2013
docker_dir="/var/lib/docker/"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
set -o pipefail
3+
4+
. "$(dirname "$0")"/dehumanize.sh
5+
6+
test_without_unit(){
7+
assertEquals 45 $(dehumanize 45)
8+
}
9+
10+
test_bytes(){
11+
assertEquals 45 $(dehumanize 45b)
12+
assertEquals 45 $(dehumanize 45B)
13+
}
14+
15+
test_kilobytes(){
16+
assertEquals 46080 $(dehumanize 45kb)
17+
assertEquals 46080 $(dehumanize 45KB)
18+
}
19+
20+
test_megabytes(){
21+
assertEquals 47185920 $(dehumanize 45mb)
22+
assertEquals 47185920 $(dehumanize 45MB)
23+
}
24+
25+
test_gigabytes(){
26+
assertEquals 48318382080 $(dehumanize 45gb)
27+
assertEquals 48318382080 $(dehumanize 45GB)
28+
}
29+
30+
test_terabytes(){
31+
assertEquals 49478023249920 $(dehumanize 45tb)
32+
assertEquals 49478023249920 $(dehumanize 45TB)
33+
}
34+
35+
test_using_decimals(){
36+
assertEquals 1610612736 $(dehumanize 1.5gb)
37+
}
38+
39+
. shunit2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
# Converts human-readable units like 1.43K and 120.3M to bytes
4+
dehumanize() {
5+
awk '/[0-9][bB]?$/ {printf "%u\n", $1*1}
6+
/[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024*1024)}
7+
/[gG][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)}
8+
/[mM][bB]?$/ {printf "%u\n", $1*(1024*1024)}
9+
/[kK][bB]?$/ {printf "%u\n", $1*1024}' <<< "$1"
10+
}

0 commit comments

Comments
 (0)