Skip to content

Commit

Permalink
Fix for TestRotator flaky test
Browse files Browse the repository at this point in the history
Signed-off-by: Agustín Martínez Fayó <[email protected]>
  • Loading branch information
amartinezfayo committed Nov 8, 2024
1 parent a8857ba commit 91bd30b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
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 shure 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

0 comments on commit 91bd30b

Please sign in to comment.