Merge pull request #8567 from rgl/fix-handle-libvirt-qemu-disk-size-string

correctly handle the diskSize property as a qemu size string
This commit is contained in:
Megan Marsh 2020-01-07 09:29:26 -08:00 committed by GitHub
commit 8ebd3a9acb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 120 additions and 1 deletions

View File

@ -3,11 +3,61 @@ package vagrant
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/hashicorp/packer/packer"
)
// Lowercase a ascii letter.
func lower(c byte) byte {
return c | ('a' - 'A')
}
// Convert a string that represents a qemu disk image size to megabytes.
//
// Valid units (case-insensitive):
//
// B (byte) 1B
// K (kilobyte) 1024B
// M (megabyte) 1024K
// G (gigabyte) 1024M
// T (terabyte) 1024G
// P (petabyte) 1024T
// E (exabyte) 1024P
//
// The default is M.
func sizeInMegabytes(size string) uint64 {
unit := size[len(size)-1]
if unit >= '0' && unit <= '9' {
unit = 'm'
} else {
size = size[:len(size)-1]
}
value, _ := strconv.ParseUint(size, 10, 64)
switch lower(unit) {
case 'b':
return value / 1024 / 1024
case 'k':
return value / 1024
case 'm':
return value
case 'g':
return value * 1024
case 't':
return value * 1024 * 1024
case 'p':
return value * 1024 * 1024 * 1024
case 'e':
return value * 1024 * 1024 * 1024 * 1024
default:
panic(fmt.Sprintf("Unknown size unit %c", unit))
}
}
type LibVirtProvider struct{}
func (p *LibVirtProvider) KeepInputArtifact() bool {
@ -28,7 +78,7 @@ func (p *LibVirtProvider) Process(ui packer.Ui, artifact packer.Artifact, dir st
}
format := artifact.State("diskType").(string)
origSize := artifact.State("diskSize").(uint64)
origSize := sizeInMegabytes(artifact.State("diskSize").(string))
size := origSize / 1024 // In MB, want GB
if origSize%1024 > 0 {
// Make sure we don't make the size smaller

View File

@ -0,0 +1,69 @@
package vagrant
import (
"fmt"
"testing"
)
func assertSizeInMegabytes(t *testing.T, size string, expected uint64) {
actual := sizeInMegabytes(size)
if actual != expected {
t.Fatalf("the size `%s` was converted to `%d` but expected `%d`", size, actual, expected)
}
}
func Test_sizeInMegabytes_WithInvalidUnitMustPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("expected a panic but got none")
}
}()
sizeInMegabytes("1234x")
}
func Test_sizeInMegabytes_WithoutUnitMustDefaultToMegabytes(t *testing.T) {
assertSizeInMegabytes(t, "1234", 1234)
}
func Test_sizeInMegabytes_WithBytesUnit(t *testing.T) {
assertSizeInMegabytes(t, fmt.Sprintf("%db", 1234*1024*1024), 1234)
assertSizeInMegabytes(t, fmt.Sprintf("%dB", 1234*1024*1024), 1234)
assertSizeInMegabytes(t, "1B", 0)
}
func Test_sizeInMegabytes_WithKiloBytesUnit(t *testing.T) {
assertSizeInMegabytes(t, fmt.Sprintf("%dk", 1234*1024), 1234)
assertSizeInMegabytes(t, fmt.Sprintf("%dK", 1234*1024), 1234)
assertSizeInMegabytes(t, "1K", 0)
}
func Test_sizeInMegabytes_WithMegabytesUnit(t *testing.T) {
assertSizeInMegabytes(t, "1234m", 1234)
assertSizeInMegabytes(t, "1234M", 1234)
assertSizeInMegabytes(t, "1M", 1)
}
func Test_sizeInMegabytes_WithGigabytesUnit(t *testing.T) {
assertSizeInMegabytes(t, "1234g", 1234*1024)
assertSizeInMegabytes(t, "1234G", 1234*1024)
assertSizeInMegabytes(t, "1G", 1*1024)
}
func Test_sizeInMegabytes_WithTerabytesUnit(t *testing.T) {
assertSizeInMegabytes(t, "1234t", 1234*1024*1024)
assertSizeInMegabytes(t, "1234T", 1234*1024*1024)
assertSizeInMegabytes(t, "1T", 1*1024*1024)
}
func Test_sizeInMegabytes_WithPetabytesUnit(t *testing.T) {
assertSizeInMegabytes(t, "1234p", 1234*1024*1024*1024)
assertSizeInMegabytes(t, "1234P", 1234*1024*1024*1024)
assertSizeInMegabytes(t, "1P", 1*1024*1024*1024)
}
func Test_sizeInMegabytes_WithExabytesUnit(t *testing.T) {
assertSizeInMegabytes(t, "1234e", 1234*1024*1024*1024*1024)
assertSizeInMegabytes(t, "1234E", 1234*1024*1024*1024*1024)
assertSizeInMegabytes(t, "1E", 1*1024*1024*1024*1024)
}