Skip to content

Commit

Permalink
Adding leader verification before calling delete expired image
Browse files Browse the repository at this point in the history
  • Loading branch information
tsorya committed Sep 7, 2020
1 parent b3ab248 commit b0a8b33
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/imgexpirer/imgexpirer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func (m *Manager) ExpirationTask() {
}

func (m *Manager) DeletedImageCallback(ctx context.Context, log logrus.FieldLogger, objectName string) {
if !m.leaderElector.IsLeader() {
return
}
matches := uuidRegex.FindStringSubmatch(objectName)
if len(matches) != 2 {
log.Errorf("Cannot find cluster ID in object name: %s", objectName)
Expand Down
17 changes: 16 additions & 1 deletion internal/imgexpirer/imgexpirer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package imgexpirer
import (
"context"
"fmt"
"github.com/openshift/assisted-service/pkg/leader"
"io/ioutil"
"testing"
"time"
Expand All @@ -28,24 +29,38 @@ var _ = Describe("imgexpirer", func() {
ctx = context.Background()
ctrl *gomock.Controller
mockEvents *events.MockHandler
leaderMock *leader.MockElectorInterface
log = logrus.New()
)

leaderSuccess := func() {
leaderMock.EXPECT().IsLeader().Return(true).Times(1)
}

BeforeEach(func() {
log.SetOutput(ioutil.Discard)
ctrl = gomock.NewController(GinkgoT())
mockEvents = events.NewMockHandler(ctrl)
deleteTime, _ := time.ParseDuration("60m")
imgExp = NewManager(nil, mockEvents, deleteTime, nil)
leaderMock = leader.NewMockElectorInterface(ctrl)
imgExp = NewManager(nil, mockEvents, deleteTime, leaderMock)
})
It("callback_valid_objname", func() {
clusterId := "53116787-3eb0-4211-93ac-611d5cedaa30"
leaderSuccess()
mockEvents.EXPECT().AddEvent(gomock.Any(), strfmt.UUID(clusterId), nil, models.EventSeverityInfo, gomock.Any(), gomock.Any())
imgExp.DeletedImageCallback(ctx, log, fmt.Sprintf("discovery-image-%s.iso", clusterId))
})
It("callback_invalid_objname", func() {
clusterId := "53116787-3eb0-4211-93ac-611d5cedaa30"
leaderSuccess()
imgExp.DeletedImageCallback(ctx, log, fmt.Sprintf("discovery-image-%s", clusterId))
})
It("callback_not_leader", func() {
clusterId := "53116787-3eb0-4211-93ac-611d5cedaa30"
leaderMock.EXPECT().IsLeader().Return(false).Times(1)
imgExp.DeletedImageCallback(ctx, log, fmt.Sprintf("discovery-image-%s.iso", clusterId))
})

AfterEach(func() {
ctrl.Finish()
Expand Down

0 comments on commit b0a8b33

Please sign in to comment.