Skip to content

Commit

Permalink
Test passes with correct values
Browse files Browse the repository at this point in the history
  • Loading branch information
matoval committed Dec 9, 2024
1 parent 0e1affb commit e8e5520
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions pkg/workceptor/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package workceptor_test

import (
"context"
"fmt"
"io"
"net/http"
"os"
"reflect"
"strings"
"sync"
"testing"
"time"
Expand All @@ -20,9 +24,10 @@ import (
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
fakerest "k8s.io/client-go/rest/fake"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/client-go/kubernetes/scheme"
)

func startNetceptorNodeWithWorkceptor() (*workceptor.KubeUnit, error) {
Expand Down Expand Up @@ -463,25 +468,46 @@ func TestKubeLoggingWithReconnect(t *testing.T) {
mockBaseWorkUnitForWorkUnit.EXPECT().GetStatusWithoutExtraData().Return(&status).AnyTimes()
mockBaseWorkUnitForWorkUnit.EXPECT().GetStatusCopy().Return(status).AnyTimes()
mockBaseWorkUnitForWorkUnit.EXPECT().GetContext().Return(ctx).AnyTimes()
pod := corev1.Pod{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "Test Name"}, Spec: corev1.PodSpec{}, Status: corev1.PodStatus{}}
pod := corev1.Pod{TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{Name: "Test_Name"}, Spec: corev1.PodSpec{}, Status: corev1.PodStatus{}}
mockKubeAPI.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&pod, nil).AnyTimes()
mockBaseWorkUnitForWorkUnit.EXPECT().UpdateFullStatus(gomock.Any()).AnyTimes()
field := hasTerm{}
mockKubeAPI.EXPECT().OneTermEqualSelector(gomock.Any(), gomock.Any()).Return(&field).AnyTimes()
ev := watch.Event{Object: &pod}
mockKubeAPI.EXPECT().UntilWithSync(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&ev, nil).AnyTimes()
mockKubeAPI.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&pod, nil).AnyTimes()
client := fake.NewSimpleClientset()
req := client.CoreV1().Pods("default").GetLogs("pod-1", &corev1.PodLogOptions{})
mockKubeAPI.EXPECT().GetLogs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(req)
req := fakerest.RESTClient{
Client: fakerest.CreateHTTPClient(func(request *http.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("2024-12-09T00:31:18.823849250Z HI\n kube error")),
}
return resp, nil

Check failure on line 485 in pkg/workceptor/kubernetes_test.go

View workflow job for this annotation

GitHub Actions / lint-receptor

return with no blank line before (nlreturn)
}),
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
GroupVersion: pod.GroupVersionKind().GroupVersion(),
VersionedAPIPath: fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/log", pod.Namespace, pod.Name),
}
mockKubeAPI.EXPECT().GetLogs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(req.Request()).AnyTimes()
mockBaseWorkUnitForWorkUnit.EXPECT().GetWorkceptor().Return(w).AnyTimes()
logger := logger.NewReceptorLogger("")
mockNetceptor.EXPECT().GetLogger().Return(logger).AnyTimes()
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.expectedCalls()
kw.CreatePod(nil)
kw.KubeLoggingWithReconnect(&sync.WaitGroup{}, &workceptor.STDoutWriter{}, &stdinErr, &stdoutErr)
wg := &sync.WaitGroup{}
wg.Add(1)
mockfilesystemer := mock_workceptor.NewMockFileSystemer(ctrl)
mockfilesystemer.EXPECT().OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).Return(&os.File{}, nil)
stdout, _ := workceptor.NewStdoutWriter(mockfilesystemer, "")
mockFileWC := mock_workceptor.NewMockFileWriteCloser(ctrl)
stdout.SetWriter(mockFileWC)
mockFileWC.EXPECT().Write(gomock.AnyOf([]byte("HI\n"), []byte(" kube error\n"))).Return(0, nil).Times(2)
kw.KubeLoggingWithReconnect(wg, stdout, &stdinErr, &stdoutErr)
})
}
}

0 comments on commit e8e5520

Please sign in to comment.