Fix wrong units size return

Signed-off-by: Shion Tanaka <shtanaka@redhat.com>
This commit is contained in:
Shion Tanaka 2024-03-03 01:25:42 +09:00
parent 460fc4d65f
commit 92b67a69ae
4 changed files with 45 additions and 8 deletions

View File

@ -164,8 +164,8 @@ func toMachineFormat(vms []*machine.ListResponse, defaultCon string) []*entities
response.Stream = streamName(vm.Stream)
response.VMType = vm.VMType
response.CPUs = vm.CPUs
response.Memory = strUint(vm.Memory)
response.DiskSize = strUint(vm.DiskSize)
response.Memory = strUint(uint64(vm.Memory.ToBytes()))
response.DiskSize = strUint(uint64(vm.DiskSize.ToBytes()))
response.Port = vm.Port
response.RemoteUsername = vm.RemoteUsername
response.IdentityPath = vm.IdentityPath
@ -202,8 +202,8 @@ func toHumanFormat(vms []*machine.ListResponse, defaultCon string) []*entities.L
response.Created = units.HumanDuration(time.Since(vm.CreatedAt)) + " ago"
response.VMType = vm.VMType
response.CPUs = vm.CPUs
response.Memory = units.BytesSize(float64(vm.Memory))
response.DiskSize = units.BytesSize(float64(vm.DiskSize))
response.Memory = units.BytesSize(float64(vm.Memory.ToBytes()))
response.DiskSize = units.BytesSize(float64(vm.DiskSize.ToBytes()))
humanResponses = append(humanResponses, response)
}

View File

@ -13,6 +13,7 @@ import (
"strings"
"time"
"github.com/containers/common/pkg/strongunits"
"github.com/containers/podman/v5/pkg/machine/compression"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
@ -57,8 +58,8 @@ type ListResponse struct {
Stream string
VMType string
CPUs uint64
Memory uint64
DiskSize uint64
Memory strongunits.MiB
DiskSize strongunits.GiB
Port int
RemoteUsername string
IdentityPath string

View File

@ -1,6 +1,7 @@
package e2e_test
import (
"strconv"
"strings"
"time"
@ -158,6 +159,40 @@ var _ = Describe("podman machine list", func() {
listNames3 := listSession3.outputToStringSlice()
Expect(listNames3).To(HaveLen(2))
})
It("list machine in machine-readable byte format", func() {
i := new(initMachine)
session, err := mb.setCmd(i.withImage(mb.imagePath)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0))
list := new(listMachine)
list = list.withFormat(("json"))
listSession, err := mb.setCmd(list).run()
Expect(err).NotTo(HaveOccurred())
var listResponse []*entities.ListReporter
err = jsoniter.Unmarshal(listSession.Bytes(), &listResponse)
Expect(err).NotTo(HaveOccurred())
for _, reporter := range listResponse {
memory, err := strconv.Atoi(reporter.Memory)
Expect(err).NotTo(HaveOccurred())
Expect(memory).To(BeNumerically(">", 2000000000)) // 2GiB
diskSize, err := strconv.Atoi(reporter.DiskSize)
Expect(err).NotTo(HaveOccurred())
Expect(diskSize).To(BeNumerically(">", 11000000000)) // 11GiB
}
})
It("list machine in human-readable format", func() {
i := new(initMachine)
session, err := mb.setCmd(i.withImage(mb.imagePath)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0))
list := new(listMachine)
listSession, err := mb.setCmd(list.withFormat("{{.Memory}} {{.DiskSize}}")).run()
Expect(err).NotTo(HaveOccurred())
Expect(listSession).To(Exit(0))
Expect(listSession.outputToString()).To(Equal("2GiB 11GiB"))
})
})
func stripAsterisk(sl []string) {

View File

@ -10,6 +10,7 @@ import (
"strings"
"time"
"github.com/containers/common/pkg/strongunits"
"github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/connection"
machineDefine "github.com/containers/podman/v5/pkg/machine/define"
@ -51,8 +52,8 @@ func List(vmstubbers []vmconfigs.VMProvider, _ machine.ListOptions) ([]*machine.
//Stream: "", // No longer applicable
VMType: s.VMType().String(),
CPUs: mc.Resources.CPUs,
Memory: mc.Resources.Memory,
DiskSize: mc.Resources.DiskSize,
Memory: strongunits.MiB(mc.Resources.Memory),
DiskSize: strongunits.GiB(mc.Resources.DiskSize),
Port: mc.SSH.Port,
RemoteUsername: mc.SSH.RemoteUsername,
IdentityPath: mc.SSH.IdentityPath,