Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for TestRotator flaky test #5643

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions pkg/agent/svid/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ type rotator struct {
// Mutex used to prevent rotations when a new connection is being created
rotMtx *sync.RWMutex

// Hook that will be called when the SVID rotation finishes
rotationFinishedHook func()
hooks struct {
// Hook that will be called when the SVID rotation finishes
rotationFinishedHook func()

// Hook that is called when the rotator starts running
runRotatorSignal chan struct{}
}
tainted bool
}

Expand All @@ -83,6 +87,10 @@ func (r *rotator) Run(ctx context.Context) error {
}

func (r *rotator) runRotation(ctx context.Context) error {
if r.hooks.runRotatorSignal != nil {
r.hooks.runRotatorSignal <- struct{}{}
}

for {
err := r.rotateSVIDIfNeeded(ctx)
state, ok := r.state.Value().(State)
Expand Down Expand Up @@ -179,7 +187,7 @@ func (r *rotator) GetRotationMtx() *sync.RWMutex {
}

func (r *rotator) SetRotationFinishedHook(f func()) {
r.rotationFinishedHook = f
r.hooks.rotationFinishedHook = f
}

func (r *rotator) Reattest(ctx context.Context) error {
Expand All @@ -193,8 +201,8 @@ func (r *rotator) Reattest(ctx context.Context) error {
}

err := r.reattest(ctx)
if err == nil && r.rotationFinishedHook != nil {
r.rotationFinishedHook()
if err == nil && r.hooks.rotationFinishedHook != nil {
r.hooks.rotationFinishedHook()
}

return err
Expand All @@ -213,8 +221,8 @@ func (r *rotator) rotateSVIDIfNeeded(ctx context.Context) (err error) {
err = r.rotateSVID(ctx)
}

if err == nil && r.rotationFinishedHook != nil {
r.rotationFinishedHook()
if err == nil && r.hooks.rotationFinishedHook != nil {
r.hooks.rotationFinishedHook()
}
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/agent/svid/rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func TestRotator(t *testing.T) {
RotationStrategy: rotationutil.NewRotationStrategy(0),
})
rotator.client = mockClient
rotator.hooks.runRotatorSignal = make(chan struct{})

// Hook the rotation loop so we can determine when the rotator
// has finished a rotation evaluation (does not imply anything
Expand All @@ -179,6 +180,9 @@ func TestRotator(t *testing.T) {
errCh <- rotator.Run(ctx)
}()

// Make sure that the rotator is running
<-rotator.hooks.runRotatorSignal

// All tests should get through one rotation loop or error
select {
case <-clk.WaitForAfterCh():
Expand Down Expand Up @@ -513,7 +517,7 @@ func TestTaintedSVIDIsRotated(t *testing.T) {
})
rotator.client = mockClient
rotationFinishedCh := make(chan struct{}, 1)
rotator.rotationFinishedHook = func() {
rotator.hooks.rotationFinishedHook = func() {
close(rotationFinishedCh)
}

Expand Down
Loading