hyperV: Update lastUp time

LastUp now correctly reports the lastUp time for podman machine on
hyperv, for both inspect and list.

Signed-off-by: Ashley Cui <acui@redhat.com>
This commit is contained in:
Ashley Cui
2023-10-19 01:30:09 -04:00
parent 0b0f128bfb
commit d6f44d956d
2 changed files with 17 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package e2e_test
import ( import (
"fmt" "fmt"
"time"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -32,6 +33,7 @@ var _ = Describe("podman machine stop", func() {
It("Stop running machine", func() { It("Stop running machine", func() {
name := randomString() name := randomString()
i := new(initMachine) i := new(initMachine)
starttime := time.Now()
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(0)) Expect(session).To(Exit(0))
@ -46,5 +48,14 @@ var _ = Describe("podman machine stop", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(stopAgain).To(Exit((0))) Expect(stopAgain).To(Exit((0)))
Expect(stopAgain.outputToString()).To(ContainSubstring(fmt.Sprintf("Machine \"%s\" stopped successfully", name))) Expect(stopAgain.outputToString()).To(ContainSubstring(fmt.Sprintf("Machine \"%s\" stopped successfully", name)))
// Stopping a machine should update the last up time
inspect := new(inspectMachine)
inspectSession, err := mb.setName(name).setCmd(inspect.withFormat("{{.LastUp.Format \"2006-01-02T15:04:05Z07:00\"}}")).run()
Expect(err).ToNot(HaveOccurred())
Expect(inspectSession).To(Exit(0))
lastupTime, err := time.Parse(time.RFC3339, inspectSession.outputToString())
Expect(err).ToNot(HaveOccurred())
Expect(lastupTime).To(BeTemporally(">", starttime))
}) })
}) })

View File

@ -605,8 +605,13 @@ func (m *HyperVMachine) Stop(name string, opts machine.StopOptions) error {
logrus.Error(err) logrus.Error(err)
} }
return vm.Stop() if err := vm.Stop(); err != nil {
return err
}
// keep track of last up
m.LastUp = time.Now()
return m.writeConfig()
} }
func (m *HyperVMachine) jsonConfigPath() (string, error) { func (m *HyperVMachine) jsonConfigPath() (string, error) {
@ -660,7 +665,6 @@ func (m *HyperVMachine) loadFromFile() (*HyperVMachine, error) {
if cfg.Hardware.Memory > 0 { if cfg.Hardware.Memory > 0 {
mm.Memory = uint64(cfg.Hardware.Memory) mm.Memory = uint64(cfg.Hardware.Memory)
} }
mm.LastUp = cfg.Status.LastUp
return &mm, nil return &mm, nil
} }