@@ -18,9 +18,15 @@ package e2e
1818
1919import (
2020 "context"
21+ "fmt"
2122 "os"
2223 "time"
2324
25+ "github.com/cuisongliu/automq-operator/internal/controller"
26+ v2 "k8s.io/api/apps/v1"
27+ "k8s.io/apimachinery/pkg/labels"
28+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
29+
2430 infrav1beta1 "github.com/cuisongliu/automq-operator/api/v1beta1"
2531 . "github.com/onsi/ginkgo/v2"
2632 . "github.com/onsi/gomega"
@@ -33,7 +39,7 @@ import (
3339var _ = Describe ("automq_controller" , func () {
3440 Context ("automq_controller cr tests" , func () {
3541 ctx := context .Background ()
36- namespaceName := "automq-operator "
42+ namespaceName := "automq-cr "
3743 namespace := & v1.Namespace {
3844 ObjectMeta : metav1.ObjectMeta {
3945 Name : namespaceName ,
@@ -44,22 +50,23 @@ var _ = Describe("automq_controller", func() {
4450 automq .Name = "automq-s1"
4551 automq .Namespace = namespaceName
4652 automq .Spec .ClusterID = "rZdE0DjZSrqy96PXrMUZVw"
47-
48- BeforeEach (func () {
53+ It ("create cr namespace" , func () {
4954 By ("Creating the Namespace to perform the tests" )
5055 err := k8sClient .Create (ctx , namespace )
5156 Expect (err ).To (Not (HaveOccurred ()))
52- By ("Setting the NAMESPACE_NAME ENV VAR which stores the Operand image" )
53- err = os .Setenv ("NAMESPACE_NAME" , namespaceName )
54- Expect (err ).To (Not (HaveOccurred ()))
5557 })
56- It ("Update Endpoint" , func () {
58+ It ("create cr" , func () {
59+ By ("get minio ip and port" )
60+ minioService := & v1.Service {}
61+ err := k8sClient .Get (ctx , client.ObjectKey {Namespace : "minio" , Name : "minio" }, minioService )
62+ Expect (err ).To (Not (HaveOccurred ()))
63+ ip := minioService .Spec .ClusterIP
5764 By ("creating the custom resource for the automq" )
58- err : = k8sClient .Get (ctx , client .ObjectKeyFromObject (automq ), automq )
65+ err = k8sClient .Get (ctx , client .ObjectKeyFromObject (automq ), automq )
5966 if err != nil && errors .IsNotFound (err ) {
6067 // Let's mock our custom resource at the same way that we would
6168 // apply on the cluster the manifest under config/samples
62- automq .Spec .S3 .Endpoint = "http://minio.minio.svc.cluster.local :9000"
69+ automq .Spec .S3 .Endpoint = fmt . Sprintf ( "http://%s :9000" , ip )
6370 automq .Spec .S3 .Bucket = "ko3"
6471 automq .Spec .S3 .AccessKeyID = "admin"
6572 automq .Spec .S3 .SecretAccessKey = "minio123"
@@ -72,7 +79,120 @@ var _ = Describe("automq_controller", func() {
7279 Expect (err ).To (Not (HaveOccurred ()))
7380 }
7481 })
75- AfterEach (func () {
82+ It ("should successfully reconcile the resource" , func () {
83+ By ("Reconciling the created resource" )
84+ controllerReconciler := & controller.AutoMQReconciler {
85+ Client : k8sClient ,
86+ Scheme : k8sClient .Scheme (),
87+ Finalizer : "apps.cuisongliu.com/automq.finalizer" ,
88+ MountTZ : true ,
89+ }
90+ _ , err := controllerReconciler .Reconcile (ctx , reconcile.Request {
91+ NamespacedName : client .ObjectKeyFromObject (automq ),
92+ })
93+ Expect (err ).NotTo (HaveOccurred ())
94+ })
95+ It ("get automq deployment" , func () {
96+ ctx := context .Background ()
97+ Eventually (func () error {
98+ deployment := & v2.DeploymentList {}
99+ labelSelector := labels .Set (map [string ]string {"app.kubernetes.io/owner-by" : "automq" , "app.kubernetes.io/instance" : automq .Name }).AsSelector ()
100+ err := k8sClient .List (ctx , deployment , & client.ListOptions {Namespace : automq .Namespace , LabelSelector : labelSelector })
101+ if err != nil {
102+ return err
103+ }
104+ if len (deployment .Items ) != 4 {
105+ return fmt .Errorf ("expected 4 deploy, found %d" , len (deployment .Items ))
106+ }
107+ for i , deploy := range deployment .Items {
108+ if deploy .Status .ReadyReplicas != 1 {
109+ return fmt .Errorf ("expected deploy %d ready replicas to be 1, got '%d'" , i , deploy .Status .ReadyReplicas )
110+ }
111+ }
112+ return nil
113+ }, "60s" , "1s" ).Should (Succeed ())
114+ })
115+ It ("check controller status" , func () {
116+ ctx := context .Background ()
117+ Eventually (func () error {
118+ podList := & v1.PodList {}
119+ labelSelector := labels .Set (map [string ]string {"app.kubernetes.io/owner-by" : "automq" , "app.kubernetes.io/instance" : automq .Name , "app.kubernetes.io/role" : "controller" }).AsSelector ()
120+ err := k8sClient .List (ctx , podList , & client.ListOptions {Namespace : automq .Namespace , LabelSelector : labelSelector })
121+ if err != nil {
122+ return err
123+ }
124+ if len (podList .Items ) != 1 {
125+ return fmt .Errorf ("expected 3 pod, found %d" , len (podList .Items ))
126+ }
127+ for i , pod := range podList .Items {
128+ if pod .Status .Phase != v1 .PodRunning {
129+ return fmt .Errorf ("expected pod %d phase to be 'Running', got '%s'" , i , pod .Status .Phase )
130+ }
131+ }
132+ return nil
133+ }, "60s" , "1s" ).Should (Succeed ())
134+ })
135+
136+ It ("check broker status" , func () {
137+ ctx := context .Background ()
138+ Eventually (func () error {
139+ podList := & v1.PodList {}
140+ labelSelector := labels .Set (map [string ]string {"app.kubernetes.io/owner-by" : "automq" , "app.kubernetes.io/instance" : automq .Name , "app.kubernetes.io/role" : "broker" }).AsSelector ()
141+ err := k8sClient .List (ctx , podList , & client.ListOptions {Namespace : automq .Namespace , LabelSelector : labelSelector })
142+ if err != nil {
143+ return err
144+ }
145+ if len (podList .Items ) != 3 {
146+ return fmt .Errorf ("expected 1 pod, found %d" , len (podList .Items ))
147+ }
148+ for i , pod := range podList .Items {
149+ if pod .Status .Phase != v1 .PodRunning {
150+ return fmt .Errorf ("expected pod %d phase to be 'Running', got '%s'" , i , pod .Status .Phase )
151+ }
152+ }
153+ return nil
154+ }, "60s" , "1s" ).Should (Succeed ())
155+ })
156+ It ("check automq status" , func () {
157+ ctx := context .Background ()
158+ Eventually (func () error {
159+ err := k8sClient .Get (ctx , client .ObjectKeyFromObject (automq ), automq )
160+ if err != nil {
161+ return err
162+ }
163+ if automq .Status .Phase != infrav1beta1 .AutoMQReady {
164+ return fmt .Errorf ("expected automq phase to be 'Ready', got '%s'" , automq .Status .Phase )
165+ }
166+ if automq .Status .ControllerReplicas != automq .Spec .Controller .Replicas {
167+ return fmt .Errorf ("expected automq controller replicas to be %d, got '%d'" , automq .Spec .Controller .Replicas , automq .Status .ControllerReplicas )
168+ }
169+ if automq .Status .BrokerReplicas != automq .Spec .Broker .Replicas {
170+ return fmt .Errorf ("expected automq broker replicas to be %d, got '%d'" , automq .Spec .Broker .Replicas , automq .Status .BrokerReplicas )
171+ }
172+ showReadyPods := automq .Spec .Controller .Replicas + automq .Spec .Broker .Replicas
173+ if automq .Status .ReadyPods != showReadyPods {
174+ return fmt .Errorf ("expected automq ready pods to be %d, got '%d'" , showReadyPods , automq .Status .ReadyPods )
175+ }
176+ if len (automq .Status .ControllerAddresses ) != int (automq .Spec .Controller .Replicas ) {
177+ return fmt .Errorf ("expected automq controller addresses to have %d elements, got '%d'" , automq .Spec .Controller .Replicas , len (automq .Status .ControllerAddresses ))
178+ }
179+ if automq .Status .BootstrapInternalAddress == "" {
180+ return fmt .Errorf ("expected automq bootstrap internal address to be set" )
181+ }
182+ bootstrapService := fmt .Sprintf ("%s.%s.svc:%d" , "automq-" + "broker-bootstrap" , automq .Namespace , 9092 )
183+ if automq .Status .BootstrapInternalAddress != bootstrapService {
184+ return fmt .Errorf ("expected automq bootstrap internal address to be '%s', got '%s'" , bootstrapService , automq .Status .BootstrapInternalAddress )
185+ }
186+ for i , address := range automq .Status .ControllerAddresses {
187+ controllerService := fmt .Sprintf ("%d@%s.%s.svc:%d" , i , "automq-controller-" + fmt .Sprintf ("%d" , i ), automq .Namespace , 9093 )
188+ if address != controllerService {
189+ return fmt .Errorf ("expected automq controller address %d to be '%s', got '%s'" , i , controllerService , address )
190+ }
191+ }
192+ return nil
193+ }, "60s" , "1s" ).Should (Succeed ())
194+ })
195+ It ("clean automq" , func () {
76196 By ("removing the custom resource for the automq" )
77197 found := & infrav1beta1.AutoMQ {}
78198 err := k8sClient .Get (ctx , client .ObjectKeyFromObject (automq ), found )
0 commit comments