-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from castai/lost_delete_delta_fix
keep delete delta in cache
- Loading branch information
Showing
4 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
internal/services/controller/controller_exclude_race_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
//go:build !race | ||
// +build !race | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"castai-agent/internal/castai" | ||
mock_castai "castai-agent/internal/castai/mock" | ||
"castai-agent/internal/config" | ||
mock_types "castai-agent/internal/services/providers/types/mock" | ||
mock_version "castai-agent/internal/services/version/mock" | ||
"github.com/golang/mock/gomock" | ||
"github.com/google/uuid" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestController_ShouldKeepDeltaAfterDelete(t *testing.T) { | ||
mockctrl := gomock.NewController(t) | ||
castaiclient := mock_castai.NewMockClient(mockctrl) | ||
version := mock_version.NewMockInterface(mockctrl) | ||
provider := mock_types.NewMockProvider(mockctrl) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: v1.NamespaceDefault, Name: "pod1"}} | ||
podData, err := encode(pod) | ||
require.NoError(t, err) | ||
|
||
clientset := fake.NewSimpleClientset() | ||
f := informers.NewSharedInformerFactory(clientset, 0) | ||
|
||
version.EXPECT().MinorInt().Return(19) | ||
version.EXPECT().Full().Return("1.19+") | ||
|
||
clusterID := uuid.New() | ||
|
||
var invocations int64 | ||
|
||
// initial full snapshot | ||
castaiclient.EXPECT(). | ||
SendDelta(gomock.Any(), clusterID.String(), gomock.Any()). | ||
DoAndReturn(func(_ context.Context, clusterID string, d *castai.Delta) error { | ||
defer atomic.AddInt64(&invocations, 1) | ||
|
||
require.Equal(t, clusterID, d.ClusterID) | ||
require.Equal(t, "1.19+", d.ClusterVersion) | ||
require.True(t, d.FullSnapshot) | ||
require.Len(t, d.Items, 0) | ||
|
||
clientset.CoreV1().Pods("default").Create(ctx, pod, metav1.CreateOptions{}) | ||
|
||
return nil | ||
}) | ||
|
||
// first delta add pod - fail and trigger pod delete | ||
castaiclient.EXPECT(). | ||
SendDelta(gomock.Any(), clusterID.String(), gomock.Any()). | ||
DoAndReturn(func(_ context.Context, clusterID string, d *castai.Delta) error { | ||
defer atomic.AddInt64(&invocations, 1) | ||
|
||
require.Equal(t, clusterID, d.ClusterID) | ||
require.Equal(t, "1.19+", d.ClusterVersion) | ||
require.False(t, d.FullSnapshot) | ||
require.Len(t, d.Items, 1) | ||
|
||
var actualValues []string | ||
for _, item := range d.Items { | ||
actualValues = append(actualValues, fmt.Sprintf("%s-%s-%s", item.Event, item.Kind, item.Data)) | ||
} | ||
|
||
require.Contains(t, actualValues, fmt.Sprintf("%s-%s-%s", castai.EventAdd, "Pod", podData)) | ||
|
||
clientset.CoreV1().Pods("default").Delete(ctx, pod.Name, metav1.DeleteOptions{}) | ||
|
||
return fmt.Errorf("testError") | ||
}) | ||
|
||
// second attempt to send data when pod delete is received | ||
castaiclient.EXPECT(). | ||
SendDelta(gomock.Any(), clusterID.String(), gomock.Any()). | ||
DoAndReturn(func(_ context.Context, clusterID string, d *castai.Delta) error { | ||
defer atomic.AddInt64(&invocations, 1) | ||
|
||
require.Equal(t, clusterID, d.ClusterID) | ||
require.Equal(t, "1.19+", d.ClusterVersion) | ||
require.False(t, d.FullSnapshot) | ||
require.Len(t, d.Items, 1) | ||
|
||
var actualValues []string | ||
for _, item := range d.Items { | ||
actualValues = append(actualValues, fmt.Sprintf("%s-%s-%s", item.Event, item.Kind, item.Data)) | ||
} | ||
|
||
require.Contains(t, actualValues, fmt.Sprintf("%s-%s-%s", castai.EventDelete, "Pod", podData)) | ||
|
||
return nil | ||
}) | ||
|
||
agentVersion := &config.AgentVersion{Version: "1.2.3"} | ||
castaiclient.EXPECT().ExchangeAgentTelemetry(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes(). | ||
Return(&castai.AgentTelemetryResponse{}, nil). | ||
Do(func(ctx context.Context, clusterID string, req *castai.AgentTelemetryRequest) { | ||
require.Equalf(t, "1.2.3", req.AgentVersion, "got request: %+v", req) | ||
}) | ||
|
||
log := logrus.New() | ||
log.SetLevel(logrus.DebugLevel) | ||
ctrl := New( | ||
log, | ||
f, | ||
castaiclient, | ||
provider, | ||
clusterID.String(), | ||
2*time.Second, | ||
2*time.Second, | ||
10*time.Millisecond, | ||
version, | ||
agentVersion, | ||
) | ||
|
||
f.Start(ctx.Done()) | ||
|
||
go ctrl.Run(ctx) | ||
|
||
wait.Until(func() { | ||
if atomic.LoadInt64(&invocations) >= 3 { | ||
cancel() | ||
} | ||
}, 10*time.Millisecond, ctx.Done()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters