Skip to content

Commit

Permalink
add leaderelection tests
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 28, 2024
1 parent 6410d00 commit 57788fc
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 28 deletions.
3 changes: 2 additions & 1 deletion pkg/api/v1/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package v1_test
import (
"testing"

"github.com/stretchr/testify/assert"

v1 "github.com/kyverno/policy-reporter/pkg/api/v1"
"github.com/kyverno/policy-reporter/pkg/database"
"github.com/stretchr/testify/assert"
)

func TestMapping(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/api/v2/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package v2_test
import (
"testing"

"github.com/stretchr/testify/assert"

v2 "github.com/kyverno/policy-reporter/pkg/api/v2"
"github.com/kyverno/policy-reporter/pkg/config"
"github.com/kyverno/policy-reporter/pkg/database"
"github.com/stretchr/testify/assert"
)

func TestV2Views(t *testing.T) {
Expand Down Expand Up @@ -330,7 +331,6 @@ func TestV2Views(t *testing.T) {
Valid: true,
Channels: []*config.Target[config.GCSOptions]{
{

Name: "Target 2",
MinimumPriority: "warning",
Config: &config.GCSOptions{
Expand All @@ -340,7 +340,6 @@ func TestV2Views(t *testing.T) {
Valid: true,
},
{

Name: "Target 2",
MinimumPriority: "warning",
Config: &config.GCSOptions{
Expand All @@ -354,5 +353,4 @@ func TestV2Views(t *testing.T) {

assert.Equal(t, 2, len(targets))
})

}
1 change: 1 addition & 0 deletions pkg/cache/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

goredis "github.com/go-redis/redis/v8"

"github.com/kyverno/policy-reporter/pkg/cache"
"github.com/kyverno/policy-reporter/pkg/fixtures"
)
Expand Down
18 changes: 17 additions & 1 deletion pkg/config/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/client-go/rest"

"github.com/kyverno/policy-reporter/pkg/config"
Expand Down Expand Up @@ -218,6 +219,9 @@ var testConfig = &config.Config{
},
},
Targets: targets,
Logging: config.Logging{
Development: true,
},
}

func Test_ResolveTargets(t *testing.T) {
Expand Down Expand Up @@ -326,7 +330,11 @@ func Test_ResolvePolicyStore(t *testing.T) {
}

func Test_ResolveAPIServer(t *testing.T) {
resolver := config.NewResolver(&config.Config{}, &rest.Config{})
resolver := config.NewResolver(&config.Config{
API: config.API{
BasicAuth: config.BasicAuth{Username: "user", Password: "password"},
},
}, &rest.Config{})

server, _ := resolver.Server(context.Background(), nil)
if server == nil {
Expand Down Expand Up @@ -576,6 +584,14 @@ func Test_ResolveLogger(t *testing.T) {
}
}

func Test_Logger(t *testing.T) {
resolver := config.NewResolver(&config.Config{}, &rest.Config{})

logger, _ := resolver.Logger()

assert.NotNil(t, logger)
}

func Test_ResolveEnableLeaderElection(t *testing.T) {
t.Run("general disabled", func(t *testing.T) {
resolver := config.NewResolver(&config.Config{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package v1alpha2_test
import (
"testing"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
)

func TestClusterPolicyReport(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/crd/api/policyreport/v1alpha2/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/json"
"testing"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
corev1 "k8s.io/api/core/v1"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
)

func TestCommon(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/crd/api/policyreport/v1alpha2/policyreport_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package v1alpha2_test
import (
"testing"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
)

func TestPolicyReport(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/helper/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"net/http/httptest"
"testing"

"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/stretchr/testify/assert"

"github.com/kyverno/policy-reporter/pkg/helper"
)

func TestSendJSONResponse(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubernetes/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"errors"
"testing"

"github.com/kyverno/policy-reporter/pkg/kubernetes"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"

"github.com/kyverno/policy-reporter/pkg/kubernetes"
)

func newFakeClient() v1.NamespaceInterface {
Expand Down
12 changes: 8 additions & 4 deletions pkg/leaderelection/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ func (c *Client) RegisterOnNew(callback func(currentID string, lockID string)) *
}

func (c *Client) Run(ctx context.Context) error {
k8sleaderelection.RunOrDie(ctx, k8sleaderelection.LeaderElectionConfig{
k8sleaderelection.RunOrDie(ctx, c.CreateConfig())

return errors.New("leaderelection stopped")
}

func (c *Client) CreateConfig() k8sleaderelection.LeaderElectionConfig {
return k8sleaderelection.LeaderElectionConfig{
Lock: c.CreateLock(),
ReleaseOnCancel: c.releaseOnCancel,
LeaseDuration: c.leaseDuration,
Expand All @@ -58,9 +64,7 @@ func (c *Client) Run(ctx context.Context) error {
c.onNewLeader(identity, c.identity)
},
},
})

return errors.New("leaderelection stopped")
}
}

func (c *Client) CreateLock() *resourcelock.LeaseLock {
Expand Down
36 changes: 24 additions & 12 deletions pkg/leaderelection/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,42 @@ import (
"testing"
"time"

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

"github.com/kyverno/policy-reporter/pkg/leaderelection"
)

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

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

client.RegisterOnNew(func(currentID, lockID string) {})
var isLeader bool
client.RegisterOnNew(func(currentID, lockID string) {
isLeader = currentID == lockID
})

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")
}
assert.Equal(t, "policy-reporter", lock.LeaseMeta.Name)
assert.Equal(t, "namespace", lock.LeaseMeta.Namespace)
assert.Equal(t, "pod-123", lock.LockConfig.Identity)

assert.False(t, isLeader)

config := client.CreateConfig()

config.Callbacks.OnNewLeader("pod-123")

assert.True(t, isLeader)
assert.Equal(t, time.Second, config.LeaseDuration)
assert.Equal(t, time.Second, config.RenewDeadline)
assert.Equal(t, time.Second, config.RetryPeriod)
assert.True(t, config.ReleaseOnCancel)
}
3 changes: 2 additions & 1 deletion pkg/target/provider/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package aws_test
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/kyverno/policy-reporter/pkg/target/provider/aws"
"github.com/stretchr/testify/assert"
)

func TestS3Client(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/target/securityhub/securityhub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
hub "github.com/aws/aws-sdk-go-v2/service/securityhub"
"github.com/aws/aws-sdk-go-v2/service/securityhub/types"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
"github.com/kyverno/policy-reporter/pkg/fixtures"
"github.com/kyverno/policy-reporter/pkg/target/securityhub"
Expand Down

0 comments on commit 57788fc

Please sign in to comment.