@@ -25,8 +25,8 @@ var _ = Describe("LightrunJavaAgent controller", func() {
2525 timeout = time .Second * 10
2626 duration = time .Second * 10
2727 interval = time .Millisecond * 250
28- namespace = "default "
29- initContainerImage = "lightruncom/lightrun-init-agent:0.1 "
28+ wrongNamespace = "wrong-namespace "
29+ initContainerImage = "lightruncom/lightrun-init-agent:latest "
3030 agentPlatform = "linux"
3131 initVolumeName = "lightrun-agent-init"
3232 javaEnv = "JAVA_TOOL_OPTIONS"
@@ -44,47 +44,77 @@ var _ = Describe("LightrunJavaAgent controller", func() {
4444 var patchedDepl appsv1.Deployment
4545 deplRequest := types.NamespacedName {
4646 Name : deployment ,
47- Namespace : namespace ,
47+ Namespace : testNamespace ,
4848 }
4949
5050 var patchedDepl2 appsv1.Deployment
5151 deplRequest2 := types.NamespacedName {
5252 Name : deployment + "-2" ,
53- Namespace : namespace ,
53+ Namespace : testNamespace ,
54+ }
55+
56+ var patchedDepl3 appsv1.Deployment
57+ deplRequest3 := types.NamespacedName {
58+ Name : deployment + "-3" ,
59+ Namespace : wrongNamespace ,
5460 }
5561
5662 var cm corev1.ConfigMap
5763 cmRequest := types.NamespacedName {
5864 Name : cmNamePrefix + lragent1Name ,
59- Namespace : namespace ,
65+ Namespace : testNamespace ,
6066 }
6167
6268 var lrAgent agentsv1beta.LightrunJavaAgent
6369 lrAgentRequest := types.NamespacedName {
6470 Name : lragent1Name ,
65- Namespace : namespace ,
71+ Namespace : testNamespace ,
6672 }
6773
6874 var lrAgent2 agentsv1beta.LightrunJavaAgent
6975 lrAgentRequest2 := types.NamespacedName {
7076 Name : "lragent2" ,
71- Namespace : namespace ,
77+ Namespace : testNamespace ,
7278 }
7379
7480 var lrAgent3 agentsv1beta.LightrunJavaAgent
7581 lrAgentRequest3 := types.NamespacedName {
7682 Name : "duplicate" ,
77- Namespace : namespace ,
83+ Namespace : testNamespace ,
84+ }
85+
86+ var lrAgent4 agentsv1beta.LightrunJavaAgent
87+ lrAgentRequest4 := types.NamespacedName {
88+ Name : "wrong-namespace" ,
89+ Namespace : wrongNamespace ,
7890 }
7991
8092 ctx := context .Background ()
8193 Context ("When setting up the test environment" , func () {
94+ It ("Should create a test Namespace" , func () {
95+ By ("Creating a Namespace" )
96+ ns := corev1.Namespace {
97+ ObjectMeta : metav1.ObjectMeta {
98+ Name : testNamespace ,
99+ },
100+ }
101+ Expect (k8sClient .Create (ctx , & ns )).Should (Succeed ())
102+ })
103+ It ("Should create a wrong Namespace" , func () {
104+ By ("Creating a Namespace" )
105+ ns := corev1.Namespace {
106+ ObjectMeta : metav1.ObjectMeta {
107+ Name : wrongNamespace ,
108+ },
109+ }
110+ Expect (k8sClient .Create (ctx , & ns )).Should (Succeed ())
111+ })
82112 It ("Should create LightrunJavaAgent custom resource" , func () {
83113 By ("Creating a first LightrunJavaAgent resource" )
84114 lrAgent := agentsv1beta.LightrunJavaAgent {
85115 ObjectMeta : metav1.ObjectMeta {
86116 Name : lragent1Name ,
87- Namespace : namespace ,
117+ Namespace : testNamespace ,
88118 },
89119 Spec : agentsv1beta.LightrunJavaAgentSpec {
90120 DeploymentName : deployment ,
@@ -109,7 +139,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
109139 lrAgent2 := agentsv1beta.LightrunJavaAgent {
110140 ObjectMeta : metav1.ObjectMeta {
111141 Name : "lragent2" ,
112- Namespace : namespace ,
142+ Namespace : testNamespace ,
113143 },
114144 Spec : agentsv1beta.LightrunJavaAgentSpec {
115145 DeploymentName : deployment + "-2" ,
@@ -133,7 +163,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
133163 secret := corev1.Secret {
134164 ObjectMeta : metav1.ObjectMeta {
135165 Name : secret ,
136- Namespace : namespace ,
166+ Namespace : testNamespace ,
137167 },
138168 StringData : secretData ,
139169 }
@@ -150,7 +180,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
150180 TypeMeta : metav1.TypeMeta {APIVersion : appsv1 .SchemeGroupVersion .String (), Kind : "Deployment" },
151181 ObjectMeta : metav1.ObjectMeta {
152182 Name : deployment ,
153- Namespace : namespace ,
183+ Namespace : testNamespace ,
154184 },
155185 Spec : appsv1.DeploymentSpec {
156186 Selector : & metav1.LabelSelector {
@@ -331,7 +361,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
331361 lrAgent := agentsv1beta.LightrunJavaAgent {
332362 ObjectMeta : metav1.ObjectMeta {
333363 Name : "lragent" ,
334- Namespace : namespace ,
364+ Namespace : testNamespace ,
335365 },
336366 }
337367 Expect (k8sClient .Delete (ctx , & lrAgent )).Should (Succeed ())
@@ -435,7 +465,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
435465 TypeMeta : metav1.TypeMeta {APIVersion : appsv1 .SchemeGroupVersion .String (), Kind : "Deployment" },
436466 ObjectMeta : metav1.ObjectMeta {
437467 Name : deployment + "-2" ,
438- Namespace : namespace ,
468+ Namespace : testNamespace ,
439469 },
440470 Spec : appsv1.DeploymentSpec {
441471 Selector : & metav1.LabelSelector {
@@ -510,7 +540,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
510540 depl := appsv1.Deployment {
511541 ObjectMeta : metav1.ObjectMeta {
512542 Name : deployment + "-2" ,
513- Namespace : namespace ,
543+ Namespace : testNamespace ,
514544 },
515545 }
516546 Expect (k8sClient .Delete (ctx , & depl )).Should (Succeed ())
@@ -534,7 +564,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
534564 TypeMeta : metav1.TypeMeta {APIVersion : appsv1 .SchemeGroupVersion .String (), Kind : "Deployment" },
535565 ObjectMeta : metav1.ObjectMeta {
536566 Name : deployment + "-2" ,
537- Namespace : namespace ,
567+ Namespace : testNamespace ,
538568 },
539569 Spec : appsv1.DeploymentSpec {
540570 Selector : & metav1.LabelSelector {
@@ -586,7 +616,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
586616 lrAgent3 := agentsv1beta.LightrunJavaAgent {
587617 ObjectMeta : metav1.ObjectMeta {
588618 Name : "duplicate" ,
589- Namespace : namespace ,
619+ Namespace : testNamespace ,
590620 },
591621 Spec : agentsv1beta.LightrunJavaAgentSpec {
592622 DeploymentName : deployment + "-2" ,
@@ -615,7 +645,6 @@ var _ = Describe("LightrunJavaAgent controller", func() {
615645 return lrAgent3 .Status .DeploymentStatus == "ReconcileFailed"
616646 }).Should (BeTrue ())
617647 })
618-
619648 It ("Should not add finalizer to the duplicate CR" , func () {
620649 Eventually (func () bool {
621650 if err := k8sClient .Get (ctx , lrAgentRequest3 , & lrAgent3 ); err != nil {
@@ -635,5 +664,93 @@ var _ = Describe("LightrunJavaAgent controller", func() {
635664 })
636665
637666 })
638-
667+ Context ("When trying to patch deployment in the wrong namespace " , func () {
668+ It ("Should create Deployment" , func () {
669+ By ("Creating deployment" )
670+ depl := appsv1.Deployment {
671+ TypeMeta : metav1.TypeMeta {APIVersion : appsv1 .SchemeGroupVersion .String (), Kind : "Deployment" },
672+ ObjectMeta : metav1.ObjectMeta {
673+ Name : deployment + "-3" ,
674+ Namespace : wrongNamespace ,
675+ },
676+ Spec : appsv1.DeploymentSpec {
677+ Selector : & metav1.LabelSelector {
678+ MatchLabels : map [string ]string {"app" : "app" },
679+ },
680+ Template : corev1.PodTemplateSpec {
681+ ObjectMeta : metav1.ObjectMeta {
682+ Labels : map [string ]string {"app" : "app" },
683+ },
684+ Spec : corev1.PodSpec {
685+ Containers : []corev1.Container {
686+ {
687+ Name : "app" ,
688+ Image : "busybox" ,
689+ },
690+ {
691+ Name : "app2" ,
692+ Image : "busybox" ,
693+ Env : []corev1.EnvVar {
694+ {
695+ Name : javaEnv ,
696+ Value : "-Djava.net.preferIPv4Stack=true" ,
697+ },
698+ },
699+ },
700+ {
701+ Name : "no-patch" ,
702+ Image : "busybox" ,
703+ },
704+ },
705+ },
706+ },
707+ },
708+ }
709+ Expect (k8sClient .Create (ctx , & depl )).Should (Succeed ())
710+ })
711+ It ("Should create CR in the wrong namespace" , func () {
712+ By ("Creating new CR" )
713+ lrAgent4 := agentsv1beta.LightrunJavaAgent {
714+ ObjectMeta : metav1.ObjectMeta {
715+ Name : "wrong-namespace" ,
716+ Namespace : wrongNamespace ,
717+ },
718+ Spec : agentsv1beta.LightrunJavaAgentSpec {
719+ DeploymentName : deployment + "-3" ,
720+ SecretName : secret ,
721+ ServerHostname : server ,
722+ AgentName : agentName ,
723+ AgentTags : agentTags ,
724+ AgentConfig : agentConfig ,
725+ AgentEnvVarName : javaEnv ,
726+ ContainerSelector : containerSelector ,
727+ InitContainer : agentsv1beta.InitContainer {
728+ Image : initContainerImage ,
729+ SharedVolumeName : initVolumeName ,
730+ SharedVolumeMountPath : "/lightrun" ,
731+ },
732+ },
733+ }
734+ Expect (k8sClient .Create (ctx , & lrAgent4 )).Should (Succeed ())
735+ })
736+ It ("Should not change the CR status" , func () {
737+ Consistently (func () bool {
738+ if err := k8sClient .Get (ctx , lrAgentRequest4 , & lrAgent4 ); err != nil {
739+ return false
740+ }
741+ return lrAgent4 .Status .DeploymentStatus == "" && lrAgent4 .Status .Conditions == nil
742+ }).Should (BeTrue ())
743+ })
744+ It ("Should not patch the deployment" , func () {
745+ Consistently (func () bool {
746+ if err := k8sClient .Get (ctx , deplRequest3 , & patchedDepl3 ); err != nil {
747+ return false
748+ }
749+ if _ , ok := patchedDepl3 .Annotations ["lightrun.com/lightrunjavaagent" ]; ! ok && len (patchedDepl3 .Finalizers ) == 0 {
750+ return true
751+ }
752+ return false
753+ }).Should (BeTrue ())
754+ })
755+ })
639756})
0 commit comments