Skip to content

Commit 406e9db

Browse files
committed
Merge pull request #343 from zacharyalexstern/fix_to_bytes
Added correct converstions for PB and EB.
2 parents 4bff0d2 + 9295d0d commit 406e9db

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/puppet/parser/functions/to_bytes.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module Puppet::Parser::Functions
22
newfunction(:to_bytes, :type => :rvalue, :doc => <<-EOS
33
Converts the argument into bytes, for example 4 kB becomes 4096.
44
Takes a single string value as an argument.
5+
These conversions reflect a layperson's understanding of
6+
1 MB = 1024 KB, when in fact 1 MB = 1000 KB, and 1 MiB = 1024 KiB.
57
EOS
68
) do |arguments|
79

@@ -21,7 +23,8 @@ module Puppet::Parser::Functions
2123
when 'M' then return (value*(1<<20)).to_i
2224
when 'G' then return (value*(1<<30)).to_i
2325
when 'T' then return (value*(1<<40)).to_i
24-
when 'E' then return (value*(1<<50)).to_i
26+
when 'P' then return (value*(1<<50)).to_i
27+
when 'E' then return (value*(1<<60)).to_i
2528
else raise Puppet::ParseError, "to_bytes(): Unknown prefix #{prefix}"
2629
end
2730
end

spec/functions/to_bytes_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@
1818
expect(result).to(eq(4096))
1919
end
2020

21+
it "should convert MB to B" do
22+
result = scope.function_to_bytes(["4 MB"])
23+
expect(result).to(eq(4194304))
24+
end
25+
26+
it "should convert GB to B" do
27+
result = scope.function_to_bytes(["4 GB"])
28+
expect(result).to(eq(4294967296))
29+
end
30+
31+
it "should convert TB to B" do
32+
result = scope.function_to_bytes(["4 TB"])
33+
expect(result).to(eq(4398046511104))
34+
end
35+
36+
it "should convert PB to B" do
37+
result = scope.function_to_bytes(["4 PB"])
38+
expect(result).to(eq(4503599627370496))
39+
end
40+
41+
it "should convert PB to B" do
42+
result = scope.function_to_bytes(["4 EB"])
43+
expect(result).to(eq(4611686018427387904))
44+
end
45+
2146
it "should work without B in unit" do
2247
result = scope.function_to_bytes(["4 k"])
2348
expect(result).to(eq(4096))

0 commit comments

Comments
 (0)