From d6f44d956dae8542fe75d9da54de0443f9e2f673 Mon Sep 17 00:00:00 2001 From: Ashley Cui Date: Thu, 19 Oct 2023 01:30:09 -0400 Subject: [PATCH] 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 --- pkg/machine/e2e/stop_test.go | 11 +++++++++++ pkg/machine/hyperv/machine.go | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/machine/e2e/stop_test.go b/pkg/machine/e2e/stop_test.go index 28d17e4772..2bac773567 100644 --- a/pkg/machine/e2e/stop_test.go +++ b/pkg/machine/e2e/stop_test.go @@ -2,6 +2,7 @@ package e2e_test import ( "fmt" + "time" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -32,6 +33,7 @@ var _ = Describe("podman machine stop", func() { It("Stop running machine", func() { name := randomString() i := new(initMachine) + starttime := time.Now() session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run() Expect(err).ToNot(HaveOccurred()) Expect(session).To(Exit(0)) @@ -46,5 +48,14 @@ var _ = Describe("podman machine stop", func() { Expect(err).ToNot(HaveOccurred()) Expect(stopAgain).To(Exit((0))) 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)) }) }) diff --git a/pkg/machine/hyperv/machine.go b/pkg/machine/hyperv/machine.go index dd1e73ff6f..4914378022 100644 --- a/pkg/machine/hyperv/machine.go +++ b/pkg/machine/hyperv/machine.go @@ -605,8 +605,13 @@ func (m *HyperVMachine) Stop(name string, opts machine.StopOptions) error { 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) { @@ -660,7 +665,6 @@ func (m *HyperVMachine) loadFromFile() (*HyperVMachine, error) { if cfg.Hardware.Memory > 0 { mm.Memory = uint64(cfg.Hardware.Memory) } - mm.LastUp = cfg.Status.LastUp return &mm, nil }