Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vm): Regression: cannot create disks larger than 99G #335

Merged
merged 1 commit into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions proxmox/types/disk_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,21 @@ func formatDiskSize(size int64) string {
return fmt.Sprintf("%d", size)
}

round := func(f float64) string {
return strconv.FormatFloat(math.Ceil(f*100)/100, 'f', -1, 64)
}

if size < 1024*1024 {
return fmt.Sprintf("%.2gK", float64(size)/1024)
return round(float64(size)/1024) + "K"
}

if size < 1024*1024*1024 {
return fmt.Sprintf("%.2gM", float64(size)/1024/1024)
return round(float64(size)/1024/1024) + "M"
}

if size < 1024*1024*1024*1024 {
return fmt.Sprintf("%.2gG", float64(size)/1024/1024/1024)
return round(float64(size)/1024/1024/1024) + "G"
}

return fmt.Sprintf("%.2gT", float64(size)/1024/1024/1024/1024)
return round(float64(size)/1024/1024/1024/1024) + "T"
}
38 changes: 36 additions & 2 deletions proxmox/types/disk_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

package types

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseDiskSize(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -60,7 +64,7 @@ func TestFormatDiskSize(t *testing.T) {
}{
{"handle 0 size", 0, "0"},
{"handle bytes", 1001, "1001"},
{"handle kilobytes", 1234, "1.2K"},
{"handle kilobytes", 1234, "1.21K"},
{"handle megabytes", 2097152, "2M"},
{"handle gigabytes", 2147483648, "2G"},
{"handle terabytes", 2199023255552, "2T"},
Expand All @@ -75,3 +79,33 @@ func TestFormatDiskSize(t *testing.T) {
})
}
}

func TestToFromGigabytes(t *testing.T) {
t.Parallel()

tests := []struct {
name string
size int
want string
}{
{"handle 0 size", 0, "0"},
{"handle 99 GB", 99, "99G"},
{"handle 100 GB", 100, "100G"},
{"handle 101 GB", 101, "101G"},
{"handle 1023 GB", 1023, "1023G"},
{"handle 1024 GB", 1024, "1T"},
{"handle 1025 GB", 1025, "1.01T"},
}
for _, test := range tests {
tt := test
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ds := DiskSizeFromGigabytes(tt.size)
gb := ds.InGigabytes()
assert.Equal(t, tt.size, gb)
if got := ds.String(); got != tt.want {
t.Errorf("DiskSize.String() = %v, want %v", got, tt.want)
}
})
}
}