Skip to content

Commit

Permalink
test(checks): el check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Jan 30, 2025
1 parent 4dcf7f3 commit 4b9ed74
Show file tree
Hide file tree
Showing 3 changed files with 351 additions and 0 deletions.
117 changes: 117 additions & 0 deletions pkg/checks/el_block_height_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package checks

import (
"context"
"testing"

"github.com/ethpandaops/panda-pulse/pkg/grafana"
"github.com/ethpandaops/panda-pulse/pkg/grafana/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestELBlockHeightCheck_Run(t *testing.T) {
failingResponse := &grafana.QueryResponse{
Results: grafana.QueryResults{
PandaPulse: grafana.QueryPandaPulse{
Frames: []grafana.QueryFrame{
{
Schema: grafana.QuerySchema{
Fields: []grafana.QueryField{
{
Labels: map[string]string{
"instance": "node1",
"ingress_user": "user1",
},
},
},
},
Data: grafana.QueryData{
Values: []interface{}{1.0},
},
},
},
},
},
}

tests := []struct {
name string
config Config
mockResponse *grafana.QueryResponse
mockError error
expectedStatus Status
expectError bool
}{
{
name: "all nodes advancing",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockResponse: &grafana.QueryResponse{},
expectedStatus: StatusOK,
},
{
name: "nodes not advancing",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockResponse: failingResponse,
expectedStatus: StatusFail,
},
{
name: "grafana error",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockError: assert.AnError,
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockClient := mock.NewMockGrafanaClient(ctrl)
mockClient.EXPECT().Query(gomock.Any(), gomock.Any()).Return(tt.mockResponse, tt.mockError)

check := NewELBlockHeightCheck(mockClient)
result, err := check.Run(context.Background(), tt.config)

if tt.expectError {
require.Error(t, err)
return
}

require.NoError(t, err)
assert.Equal(t, tt.expectedStatus, result.Status)
assert.NotEmpty(t, result.Description)
assert.NotNil(t, result.Details)
assert.Contains(t, result.Details, "query")
})
}
}

func TestELBlockHeightCheck_Name(t *testing.T) {
check := NewELBlockHeightCheck(nil)
assert.Equal(t, "Block height not advancing", check.Name())
}

func TestELBlockHeightCheck_Category(t *testing.T) {
check := NewELBlockHeightCheck(nil)
assert.Equal(t, CategorySync, check.Category())
}

func TestELBlockHeightCheck_ClientType(t *testing.T) {
check := NewELBlockHeightCheck(nil)
assert.Equal(t, ClientTypeEL, check.ClientType())
}
117 changes: 117 additions & 0 deletions pkg/checks/el_peer_count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package checks

import (
"context"
"testing"

"github.com/ethpandaops/panda-pulse/pkg/grafana"
"github.com/ethpandaops/panda-pulse/pkg/grafana/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestELPeerCountCheck_Run(t *testing.T) {
failingResponse := &grafana.QueryResponse{
Results: grafana.QueryResults{
PandaPulse: grafana.QueryPandaPulse{
Frames: []grafana.QueryFrame{
{
Schema: grafana.QuerySchema{
Fields: []grafana.QueryField{
{
Labels: map[string]string{
"instance": "node1",
"network": "mainnet",
},
},
},
},
Data: grafana.QueryData{
Values: []interface{}{1.0},
},
},
},
},
},
}

tests := []struct {
name string
config Config
mockResponse *grafana.QueryResponse
mockError error
expectedStatus Status
expectError bool
}{
{
name: "all nodes have sufficient peers",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockResponse: &grafana.QueryResponse{},
expectedStatus: StatusOK,
},
{
name: "nodes with low peer count",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockResponse: failingResponse,
expectedStatus: StatusFail,
},
{
name: "grafana error",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockError: assert.AnError,
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockClient := mock.NewMockGrafanaClient(ctrl)
mockClient.EXPECT().Query(gomock.Any(), gomock.Any()).Return(tt.mockResponse, tt.mockError)

check := NewELPeerCountCheck(mockClient)
result, err := check.Run(context.Background(), tt.config)

if tt.expectError {
require.Error(t, err)
return
}

require.NoError(t, err)
assert.Equal(t, tt.expectedStatus, result.Status)
assert.NotEmpty(t, result.Description)
assert.NotNil(t, result.Details)
assert.Contains(t, result.Details, "query")
})
}
}

func TestELPeerCountCheck_Name(t *testing.T) {
check := NewELPeerCountCheck(nil)
assert.Equal(t, "Low peer count", check.Name())
}

func TestELPeerCountCheck_Category(t *testing.T) {
check := NewELPeerCountCheck(nil)
assert.Equal(t, CategorySync, check.Category())
}

func TestELPeerCountCheck_ClientType(t *testing.T) {
check := NewELPeerCountCheck(nil)
assert.Equal(t, ClientTypeEL, check.ClientType())
}
117 changes: 117 additions & 0 deletions pkg/checks/el_sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package checks

import (
"context"
"testing"

"github.com/ethpandaops/panda-pulse/pkg/grafana"
"github.com/ethpandaops/panda-pulse/pkg/grafana/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestELSyncCheck_Run(t *testing.T) {
failingResponse := &grafana.QueryResponse{
Results: grafana.QueryResults{
PandaPulse: grafana.QueryPandaPulse{
Frames: []grafana.QueryFrame{
{
Schema: grafana.QuerySchema{
Fields: []grafana.QueryField{
{
Labels: map[string]string{
"instance": "node1",
"network": "mainnet",
},
},
},
},
Data: grafana.QueryData{
Values: []interface{}{1.0},
},
},
},
},
},
}

tests := []struct {
name string
config Config
mockResponse *grafana.QueryResponse
mockError error
expectedStatus Status
expectError bool
}{
{
name: "all nodes synced",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockResponse: &grafana.QueryResponse{},
expectedStatus: StatusOK,
},
{
name: "nodes not syncing",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockResponse: failingResponse,
expectedStatus: StatusFail,
},
{
name: "grafana error",
config: Config{
Network: "mainnet",
ConsensusNode: "lighthouse",
ExecutionNode: "geth",
},
mockError: assert.AnError,
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockClient := mock.NewMockGrafanaClient(ctrl)
mockClient.EXPECT().Query(gomock.Any(), gomock.Any()).Return(tt.mockResponse, tt.mockError)

check := NewELSyncCheck(mockClient)
result, err := check.Run(context.Background(), tt.config)

if tt.expectError {
require.Error(t, err)
return
}

require.NoError(t, err)
assert.Equal(t, tt.expectedStatus, result.Status)
assert.NotEmpty(t, result.Description)
assert.NotNil(t, result.Details)
assert.Contains(t, result.Details, "query")
})
}
}

func TestELSyncCheck_Name(t *testing.T) {
check := NewELSyncCheck(nil)
assert.Equal(t, "Node failing to sync", check.Name())
}

func TestELSyncCheck_Category(t *testing.T) {
check := NewELSyncCheck(nil)
assert.Equal(t, CategorySync, check.Category())
}

func TestELSyncCheck_ClientType(t *testing.T) {
check := NewELSyncCheck(nil)
assert.Equal(t, ClientTypeEL, check.ClientType())
}

0 comments on commit 4b9ed74

Please sign in to comment.