Skip to content

Commit

Permalink
chore: Move from mocks to stubs (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakesh-garimella authored Aug 6, 2024
1 parent 7afedd0 commit 795ba92
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 545 deletions.
8 changes: 2 additions & 6 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,8 @@ linters-settings:
alias: metricsserver
- pkg: sigs.k8s.io/controller-runtime/pkg/predicate
alias: ctrlpredicate
- pkg: "github.com/kyma-project/telemetry-manager/internal/reconciler/logpipeline/mocks"
alias: logMocks
- pkg: "github.com/kyma-project/telemetry-manager/internal/reconciler/metricpipeline/mocks"
alias: metricMocks
- pkg: "github.com/kyma-project/telemetry-manager/internal/reconciler/tracepipeline/mocks"
alias: traceMocks
- pkg: "github.com/kyma-project/telemetry-manager/internal/reconciler/commonstatus/stubs"
alias: commonStatusStubs
errcheck:
check-type-assertions: true # Reports type assertions: `a := b.(SomeStruct)`.
check-blank: true # Report assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`.
Expand Down
7 changes: 0 additions & 7 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@ dir: "{{.InterfaceDir}}/mocks/"
mockname: "{{.InterfaceName | firstUpper}}"
filename: "{{.InterfaceName | snakecase}}.go"
packages:
github.com/kyma-project/telemetry-manager/internal/reconciler/logparser:
interfaces:
DaemonSetProber:
github.com/kyma-project/telemetry-manager/internal/reconciler/logpipeline:
interfaces:
DaemonSetAnnotator:
DaemonSetProber:
FlowHealthProber:
IstioStatusChecker:
OverridesHandler:
ErrorToMessageConverter:
github.com/kyma-project/telemetry-manager/internal/reconciler/tracepipeline:
interfaces:
DeploymentProber:
FlowHealthProber:
GatewayApplierDeleter:
GatewayConfigBuilder:
Expand All @@ -31,8 +26,6 @@ packages:
interfaces:
AgentApplierDeleter:
AgentConfigBuilder:
DaemonSetProber:
DeploymentProber:
FlowHealthProber:
GatewayApplierDeleter:
GatewayConfigBuilder:
Expand Down
16 changes: 5 additions & 11 deletions internal/reconciler/commonstatus/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"k8s.io/apimachinery/pkg/types"

"github.com/kyma-project/telemetry-manager/internal/conditions"
logMocks "github.com/kyma-project/telemetry-manager/internal/reconciler/logpipeline/mocks"
metricMocks "github.com/kyma-project/telemetry-manager/internal/reconciler/metricpipeline/mocks"
traceMocks "github.com/kyma-project/telemetry-manager/internal/reconciler/tracepipeline/mocks"
commonStatusStubs "github.com/kyma-project/telemetry-manager/internal/reconciler/commonstatus/stubs"
"github.com/kyma-project/telemetry-manager/internal/workloadstatus"
)

Expand Down Expand Up @@ -65,8 +63,7 @@ func TestTracesGetHealthCondition(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gatewayProberStub := &traceMocks.DeploymentProber{}
gatewayProberStub.On("IsReady", context.TODO(), types.NamespacedName{}).Return(tt.proberErr)
gatewayProberStub := commonStatusStubs.NewDeploymentSetProber(tt.proberErr)
actualCondition := GetGatewayHealthyCondition(context.TODO(), gatewayProberStub, types.NamespacedName{}, &conditions.ErrorToMessageConverter{}, SignalTypeTraces)
require.True(t, validateCondition(t, tt.expectedCondition, actualCondition))
})
Expand Down Expand Up @@ -153,10 +150,8 @@ func TestMetricsGetHealthCondition(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

agentProberStub := &metricMocks.DaemonSetProber{}
agentProberStub.On("IsReady", context.TODO(), types.NamespacedName{}).Return(tt.proberAgentErr)
gatewayProberStub := &metricMocks.DeploymentProber{}
gatewayProberStub.On("IsReady", context.TODO(), types.NamespacedName{}).Return(tt.preberGatewayErr)
agentProberStub := commonStatusStubs.NewDaemonSetProber(tt.proberAgentErr)
gatewayProberStub := commonStatusStubs.NewDeploymentSetProber(tt.preberGatewayErr)

actualAgentCondition := GetAgentHealthyCondition(context.TODO(), agentProberStub, types.NamespacedName{}, &conditions.ErrorToMessageConverter{}, SignalTypeMetrics)
actualGatewayCondition := GetGatewayHealthyCondition(context.TODO(), gatewayProberStub, types.NamespacedName{}, &conditions.ErrorToMessageConverter{}, SignalTypeMetrics)
Expand Down Expand Up @@ -216,8 +211,7 @@ func TestLogsGetHealthCondition(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agentProberStub := &logMocks.DaemonSetProber{}
agentProberStub.On("IsReady", context.TODO(), types.NamespacedName{}).Return(tt.proberErr)
agentProberStub := commonStatusStubs.NewDaemonSetProber(tt.proberErr)
actualCondition := GetAgentHealthyCondition(context.TODO(), agentProberStub, types.NamespacedName{}, &conditions.ErrorToMessageConverter{}, SignalTypeLogs)
require.True(t, validateCondition(t, tt.expectedCondition, actualCondition))
})
Expand Down
21 changes: 21 additions & 0 deletions internal/reconciler/commonstatus/stubs/daemon_set_prober.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package stubs

import (
"context"

"k8s.io/apimachinery/pkg/types"
)

type DaemonSetProber struct {
err error
}

func NewDaemonSetProber(err error) *DaemonSetProber {
return &DaemonSetProber{
err: err,
}
}

func (d *DaemonSetProber) IsReady(ctx context.Context, name types.NamespacedName) error {
return d.err
}
21 changes: 21 additions & 0 deletions internal/reconciler/commonstatus/stubs/deployment_set_prober.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package stubs

import (
"context"

"k8s.io/apimachinery/pkg/types"
)

type DeploymentSetProber struct {
err error
}

func NewDeploymentSetProber(err error) *DeploymentSetProber {
return &DeploymentSetProber{
err: err,
}
}

func (d *DeploymentSetProber) IsReady(ctx context.Context, name types.NamespacedName) error {
return d.err
}
48 changes: 0 additions & 48 deletions internal/reconciler/logparser/mocks/daemon_set_prober.go

This file was deleted.

9 changes: 3 additions & 6 deletions internal/reconciler/logparser/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -15,7 +14,7 @@ import (

telemetryv1alpha1 "github.com/kyma-project/telemetry-manager/apis/telemetry/v1alpha1"
"github.com/kyma-project/telemetry-manager/internal/conditions"
"github.com/kyma-project/telemetry-manager/internal/reconciler/logparser/mocks"
commonStatusStubs "github.com/kyma-project/telemetry-manager/internal/reconciler/commonstatus/stubs"
"github.com/kyma-project/telemetry-manager/internal/workloadstatus"
)

Expand All @@ -34,8 +33,7 @@ func TestUpdateStatus(t *testing.T) {
}
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(parser).WithStatusSubresource(parser).Build()

proberStub := &mocks.DaemonSetProber{}
proberStub.On("IsReady", mock.Anything, mock.Anything).Return(&workloadstatus.PodIsPendingError{ContainerName: "foo", Message: "OOMKilled"})
proberStub := commonStatusStubs.NewDaemonSetProber(&workloadstatus.PodIsPendingError{ContainerName: "foo", Message: "OOMKilled"})

errToMsgConverter := &conditions.ErrorToMessageConverter{}

Expand Down Expand Up @@ -74,8 +72,7 @@ func TestUpdateStatus(t *testing.T) {
}
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(parser).WithStatusSubresource(parser).Build()

proberStub := &mocks.DaemonSetProber{}
proberStub.On("IsReady", mock.Anything, mock.Anything).Return(nil)
proberStub := commonStatusStubs.NewDaemonSetProber(nil)

errToMsgConverter := &conditions.ErrorToMessageConverter{}

Expand Down
48 changes: 0 additions & 48 deletions internal/reconciler/logpipeline/mocks/daemon_set_prober.go

This file was deleted.

Loading

0 comments on commit 795ba92

Please sign in to comment.