Timeout must be created before, not during consumption

Fix: https://github.com/kairos-io/immucore/issues/12
This commit is contained in:
mudler 2023-02-04 15:24:39 +01:00
parent 45e2622cd1
commit 70cce41004
2 changed files with 13 additions and 1 deletions

View File

@ -92,6 +92,7 @@ func (s *State) RunStageOp(stage string) func(context.Context) error {
func (s *State) MountOP(what, where, t string, options []string, timeout time.Duration) func(context.Context) error {
s.Logger.Debug().Str("what", what).Str("where", where).Str("type", t)
return func(c context.Context) error {
cc := time.After(timeout)
for {
select {
default:
@ -119,7 +120,7 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du
return nil
case <-c.Done():
return fmt.Errorf("context canceled")
case <-time.After(timeout):
case <-cc:
return fmt.Errorf("timeout exhausted")
}
}

View File

@ -1,6 +1,9 @@
package mount_test
import (
"context"
"time"
"github.com/kairos-io/immucore/pkg/mount"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
@ -97,5 +100,13 @@ var _ = Describe("mounting immutable setup", func() {
Expect(dag[7][0].Name).To(Equal("mount-bind"))
Expect(dag[8][0].Name).To(Equal("write-fstab"))
})
It("Mountop timeouts", func() {
s := &mount.State{}
f := s.MountOP("", "", "", []string{}, 1*time.Second)
err := f(context.Background())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("exhausted"))
})
})
})