Skip to content

Commit

Permalink
hyperV: Update lastUp time
Browse files Browse the repository at this point in the history
LastUp now correctly reports the lastUp time for podman machine on
hyperv, for both inspect and list.

Signed-off-by: Ashley Cui <[email protected]>
  • Loading branch information
ashley-cui committed Oct 20, 2023
1 parent 0b0f128 commit d6f44d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 11 additions & 0 deletions pkg/machine/e2e/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e_test

import (
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -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))
Expand All @@ -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))
})
})
8 changes: 6 additions & 2 deletions pkg/machine/hyperv/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d6f44d9

Please sign in to comment.