@@ -41,7 +41,13 @@ var _ = Describe("LightrunJavaAgent controller", func() {
41
41
42
42
var patchedDepl appsv1.Deployment
43
43
deplRequest := types.NamespacedName {
44
- Name : "app-deployment" ,
44
+ Name : deployment ,
45
+ Namespace : namespace ,
46
+ }
47
+
48
+ var patchedDepl2 appsv1.Deployment
49
+ deplRequest2 := types.NamespacedName {
50
+ Name : deployment + "-2" ,
45
51
Namespace : namespace ,
46
52
}
47
53
@@ -63,6 +69,12 @@ var _ = Describe("LightrunJavaAgent controller", func() {
63
69
Namespace : namespace ,
64
70
}
65
71
72
+ var lrAgent3 agentsv1beta.LightrunJavaAgent
73
+ lrAgentRequest3 := types.NamespacedName {
74
+ Name : "duplicate" ,
75
+ Namespace : namespace ,
76
+ }
77
+
66
78
ctx := context .Background ()
67
79
Context ("When setting up the test environment" , func () {
68
80
It ("Should create LightrunJavaAgent custom resource" , func () {
@@ -134,7 +146,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
134
146
depl := appsv1.Deployment {
135
147
TypeMeta : metav1.TypeMeta {APIVersion : appsv1 .SchemeGroupVersion .String (), Kind : "Deployment" },
136
148
ObjectMeta : metav1.ObjectMeta {
137
- Name : "app- deployment" ,
149
+ Name : deployment ,
138
150
Namespace : namespace ,
139
151
},
140
152
Spec : appsv1.DeploymentSpec {
@@ -421,7 +433,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
421
433
depl := appsv1.Deployment {
422
434
TypeMeta : metav1.TypeMeta {APIVersion : appsv1 .SchemeGroupVersion .String (), Kind : "Deployment" },
423
435
ObjectMeta : metav1.ObjectMeta {
424
- Name : "app- deployment-2" ,
436
+ Name : deployment + " -2" ,
425
437
Namespace : namespace ,
426
438
},
427
439
Spec : appsv1.DeploymentSpec {
@@ -472,7 +484,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
472
484
It ("Should delete deployment" , func () {
473
485
depl := appsv1.Deployment {
474
486
ObjectMeta : metav1.ObjectMeta {
475
- Name : "app- deployment-2" ,
487
+ Name : deployment + " -2" ,
476
488
Namespace : namespace ,
477
489
},
478
490
}
@@ -489,4 +501,105 @@ var _ = Describe("LightrunJavaAgent controller", func() {
489
501
})
490
502
491
503
})
504
+
505
+ Context ("When creating CR with deployment already patched by another CR" , func () {
506
+ It ("Should create Deployment" , func () {
507
+ By ("Creating deployment" )
508
+ depl := appsv1.Deployment {
509
+ TypeMeta : metav1.TypeMeta {APIVersion : appsv1 .SchemeGroupVersion .String (), Kind : "Deployment" },
510
+ ObjectMeta : metav1.ObjectMeta {
511
+ Name : deployment + "-2" ,
512
+ Namespace : namespace ,
513
+ },
514
+ Spec : appsv1.DeploymentSpec {
515
+ Selector : & metav1.LabelSelector {
516
+ MatchLabels : map [string ]string {"app" : "app" },
517
+ },
518
+ Template : corev1.PodTemplateSpec {
519
+ ObjectMeta : metav1.ObjectMeta {
520
+ Labels : map [string ]string {"app" : "app" },
521
+ },
522
+ Spec : corev1.PodSpec {
523
+ Containers : []corev1.Container {
524
+ {
525
+ Name : "app" ,
526
+ Image : "busybox" ,
527
+ },
528
+ {
529
+ Name : "app2" ,
530
+ Image : "busybox" ,
531
+ Env : []corev1.EnvVar {
532
+ {
533
+ Name : javaEnv ,
534
+ Value : "-Djava.net.preferIPv4Stack=true" ,
535
+ },
536
+ },
537
+ },
538
+ {
539
+ Name : "no-patch" ,
540
+ Image : "busybox" ,
541
+ },
542
+ },
543
+ },
544
+ },
545
+ },
546
+ }
547
+ Expect (k8sClient .Create (ctx , & depl )).Should (Succeed ())
548
+ })
549
+
550
+ It ("prepare new CR with patched deployment" , func () {
551
+ By ("Creating new CR" )
552
+ lrAgent3 := agentsv1beta.LightrunJavaAgent {
553
+ ObjectMeta : metav1.ObjectMeta {
554
+ Name : "duplicate" ,
555
+ Namespace : namespace ,
556
+ },
557
+ Spec : agentsv1beta.LightrunJavaAgentSpec {
558
+ DeploymentName : deployment + "-2" ,
559
+ SecretName : secret ,
560
+ ServerHostname : server ,
561
+ AgentName : agentName ,
562
+ AgentTags : agentTags ,
563
+ AgentConfig : agentConfig ,
564
+ AgentEnvVarName : javaEnv ,
565
+ ContainerSelector : containerSelector ,
566
+ InitContainer : agentsv1beta.InitContainer {
567
+ Image : initContainerImage ,
568
+ SharedVolumeName : initVolumeName ,
569
+ SharedVolumeMountPath : "/lightrun" ,
570
+ },
571
+ },
572
+ }
573
+ Expect (k8sClient .Create (ctx , & lrAgent3 )).Should (Succeed ())
574
+ })
575
+
576
+ It ("Should have failed status of CR" , func () {
577
+ Eventually (func () bool {
578
+ if err := k8sClient .Get (ctx , lrAgentRequest3 , & lrAgent3 ); err != nil {
579
+ return false
580
+ }
581
+ return lrAgent3 .Status .DeploymentStatus == "ReconcileFailed"
582
+ }).Should (BeTrue ())
583
+ })
584
+
585
+ It ("Should not add finalizer to the duplicate CR" , func () {
586
+ Eventually (func () bool {
587
+ if err := k8sClient .Get (ctx , lrAgentRequest3 , & lrAgent3 ); err != nil {
588
+ return false
589
+ }
590
+ return len (lrAgent3 .ObjectMeta .Finalizers ) == 0
591
+ }).Should (BeTrue ())
592
+ })
593
+
594
+ It ("Should keep deployment annotation of the original CR" , func () {
595
+ Eventually (func () bool {
596
+ if err := k8sClient .Get (ctx , deplRequest2 , & patchedDepl2 ); err != nil {
597
+ return false
598
+ }
599
+ return patchedDepl2 .Annotations ["lightrun.com/lightrunjavaagent" ] == lrAgent2 .Name
600
+ }).Should (BeTrue ())
601
+ })
602
+
603
+ })
604
+
492
605
})
0 commit comments