Skip to content

Commit

Permalink
leaselock test
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <frank.jogeleit@lovoo.com>
  • Loading branch information
Frank Jogeleit committed Apr 25, 2024
1 parent 38c665a commit 0f2f1c9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
34 changes: 34 additions & 0 deletions pkg/leaderelection/cleant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package leaderelection_test

import (
"context"
"testing"
"time"

"github.com/kyverno/policy-reporter/pkg/leaderelection"
"k8s.io/client-go/kubernetes/typed/coordination/v1/fake"
)

func TestClient(t *testing.T) {
client := leaderelection.New(&fake.FakeCoordinationV1{}, "policy-reporter", "namespace", "pod-123", time.Second, time.Second, time.Second, false)

if client == nil {
t.Fatal("failed to create leaderelection client")
}

client.RegisterOnNew(func(currentID, lockID string) {})
client.RegisterOnStart(func(c context.Context) {})
client.RegisterOnStop(func() {})

lock := client.CreateLock()

if lock.LeaseMeta.Name != "policy-reporter" {
t.Error("unexpected lease name")
}
if lock.LeaseMeta.Namespace != "namespace" {
t.Error("unexpected lease namespace")
}
if lock.LockConfig.Identity != "pod-123" {
t.Error("unexpected lease identity")
}
}
4 changes: 2 additions & 2 deletions pkg/leaderelection/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *Client) RegisterOnNew(callback func(currentID string, lockID string)) *

func (c *Client) Run(ctx context.Context) error {
k8sleaderelection.RunOrDie(ctx, k8sleaderelection.LeaderElectionConfig{
Lock: c.createLock(),
Lock: c.CreateLock(),
ReleaseOnCancel: c.releaseOnCancel,
LeaseDuration: c.leaseDuration,
RenewDeadline: c.renewDeadline,
Expand All @@ -63,7 +63,7 @@ func (c *Client) Run(ctx context.Context) error {
return errors.New("leaderelection stopped")
}

func (c *Client) createLock() *resourcelock.LeaseLock {
func (c *Client) CreateLock() *resourcelock.LeaseLock {
return &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{
Name: c.lockName,
Expand Down

0 comments on commit 0f2f1c9

Please sign in to comment.