@@ -20,6 +20,7 @@ import (
20
20
"k8s.io/apimachinery/pkg/selection"
21
21
"k8s.io/apimachinery/pkg/watch"
22
22
"k8s.io/client-go/kubernetes"
23
+ "k8s.io/client-go/kubernetes/fake"
23
24
"k8s.io/client-go/rest"
24
25
"k8s.io/client-go/tools/remotecommand"
25
26
)
@@ -423,3 +424,64 @@ func Test_IsCompatibleK8S(t *testing.T) {
423
424
})
424
425
}
425
426
}
427
+
428
+ func TestKubeLoggingWithReconnect (t * testing.T ) {
429
+ var stdinErr error
430
+ var stdoutErr error
431
+ ctx := context .Background ()
432
+ ctrl := gomock .NewController (t )
433
+ mockBaseWorkUnitForWorkUnit := mock_workceptor .NewMockBaseWorkUnitForWorkUnit (ctrl )
434
+ mockBaseWorkUnit := mock_workceptor .NewMockBaseWorkUnitForWorkUnit (ctrl )
435
+ mockNetceptor := mock_workceptor .NewMockNetceptorForWorkceptor (ctrl )
436
+ mockNetceptor .EXPECT ().NodeID ().Return ("NodeID" )
437
+ mockKubeAPI := mock_workceptor .NewMockKubeAPIer (ctrl )
438
+
439
+ kw := & workceptor.KubeUnit {
440
+ BaseWorkUnitForWorkUnit : mockBaseWorkUnitForWorkUnit ,
441
+ }
442
+
443
+ w , err := workceptor .New (ctx , mockNetceptor , "/tmp" )
444
+ if err != nil {
445
+ t .Errorf ("Error while creating Workceptor: %v" , err )
446
+ }
447
+
448
+ mockBaseWorkUnit .EXPECT ().Init (w , "" , "" , workceptor.FileSystem {}, nil )
449
+ kubeConfig := workceptor.KubeWorkerCfg {AuthMethod : "incluster" }
450
+ kubeConfig .NewkubeWorker (mockBaseWorkUnit , w , "" , "" , mockKubeAPI )
451
+
452
+ tests := []struct {
453
+ name string
454
+ expectedCalls func ()
455
+ }{
456
+ {
457
+ name : "Kube error should be read" ,
458
+ expectedCalls : func () {
459
+ lock := & sync.RWMutex {}
460
+ mockBaseWorkUnitForWorkUnit .EXPECT ().GetStatusLock ().Return (lock ).AnyTimes ()
461
+ kubeExtraData := workceptor.KubeExtraData {}
462
+ status := workceptor.StatusFileData {ExtraData : & kubeExtraData }
463
+ mockBaseWorkUnitForWorkUnit .EXPECT ().GetStatusWithoutExtraData ().Return (& status ).AnyTimes ()
464
+ mockBaseWorkUnitForWorkUnit .EXPECT ().GetStatusCopy ().Return (status ).AnyTimes ()
465
+ mockBaseWorkUnitForWorkUnit .EXPECT ().GetContext ().Return (ctx ).AnyTimes ()
466
+ pod := corev1.Pod {TypeMeta : metav1.TypeMeta {}, ObjectMeta : metav1.ObjectMeta {Name : "Test Name" }, Spec : corev1.PodSpec {}, Status : corev1.PodStatus {}}
467
+ mockKubeAPI .EXPECT ().Create (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (& pod , nil ).AnyTimes ()
468
+ mockBaseWorkUnitForWorkUnit .EXPECT ().UpdateFullStatus (gomock .Any ()).AnyTimes ()
469
+ field := hasTerm {}
470
+ mockKubeAPI .EXPECT ().OneTermEqualSelector (gomock .Any (), gomock .Any ()).Return (& field ).AnyTimes ()
471
+ ev := watch.Event {Object : & pod }
472
+ mockKubeAPI .EXPECT ().UntilWithSync (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (& ev , nil ).AnyTimes ()
473
+ mockKubeAPI .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (& pod , nil ).AnyTimes ()
474
+ client := fake .NewSimpleClientset ()
475
+ req := client .CoreV1 ().Pods ("default" ).GetLogs ("pod-1" , & corev1.PodLogOptions {})
476
+ mockKubeAPI .EXPECT ().GetLogs (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (req )
477
+ },
478
+ },
479
+ }
480
+ for _ , tt := range tests {
481
+ t .Run (tt .name , func (t * testing.T ) {
482
+ tt .expectedCalls ()
483
+ kw .CreatePod (nil )
484
+ kw .KubeLoggingWithReconnect (& sync.WaitGroup {}, & workceptor.STDoutWriter {}, & stdinErr , & stdoutErr )
485
+ })
486
+ }
487
+ }
0 commit comments