|
1 | 1 | package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + enterpriseApi "github.com/splunk/splunk-operator/api/v4" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/types" |
| 13 | + "k8s.io/client-go/kubernetes/scheme" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 15 | +) |
| 16 | + |
| 17 | +var _ = Describe("AppRuntime Controller", func() { |
| 18 | + BeforeEach(func() { |
| 19 | + Expect(enterpriseApi.AddToScheme(scheme.Scheme)).To(Succeed()) |
| 20 | + }) |
| 21 | + |
| 22 | + It("creates a headless service that exposes the appruntime port", func() { |
| 23 | + ctx := context.Background() |
| 24 | + ar := &enterpriseApi.AppRuntime{ |
| 25 | + ObjectMeta: metav1.ObjectMeta{ |
| 26 | + Name: "stack1-standalone-appruntime", |
| 27 | + Namespace: "test", |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + reconciler := AppRuntimeReconciler{ |
| 32 | + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(ar).Build(), |
| 33 | + Scheme: scheme.Scheme, |
| 34 | + } |
| 35 | + |
| 36 | + svc, err := reconciler.createHeadlessService(ctx, ar, types.NamespacedName{ |
| 37 | + Name: getHeadlessName(ar.Name), |
| 38 | + Namespace: ar.Namespace, |
| 39 | + }) |
| 40 | + Expect(err).NotTo(HaveOccurred()) |
| 41 | + Expect(svc.Spec.ClusterIP).To(Equal(corev1.ClusterIPNone)) |
| 42 | + Expect(svc.Spec.Ports).To(HaveLen(1)) |
| 43 | + Expect(svc.Spec.Ports[0].Name).To(Equal("appruntime")) |
| 44 | + Expect(svc.Spec.Ports[0].Port).To(Equal(int32(9000))) |
| 45 | + Expect(svc.Spec.Ports[0].TargetPort.IntValue()).To(Equal(9000)) |
| 46 | + }) |
| 47 | + |
| 48 | + It("creates appruntime pods with container port 9000", func() { |
| 49 | + ctx := context.Background() |
| 50 | + ar := &enterpriseApi.AppRuntime{ |
| 51 | + ObjectMeta: metav1.ObjectMeta{ |
| 52 | + Name: "stack1-standalone-appruntime", |
| 53 | + Namespace: "test", |
| 54 | + }, |
| 55 | + Spec: enterpriseApi.AppRuntimeSpec{ |
| 56 | + Image: "supervisor:0.0.1", |
| 57 | + Replicas: 1, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + reconciler := AppRuntimeReconciler{ |
| 62 | + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(ar).Build(), |
| 63 | + Scheme: scheme.Scheme, |
| 64 | + } |
| 65 | + |
| 66 | + err := reconciler.createPod(ctx, ar, types.NamespacedName{ |
| 67 | + Name: getPodName(ar.Name, 0), |
| 68 | + Namespace: ar.Namespace, |
| 69 | + }, "splunk-stack1-standalone", 0) |
| 70 | + Expect(err).NotTo(HaveOccurred()) |
| 71 | + |
| 72 | + pod := &corev1.Pod{} |
| 73 | + err = reconciler.Get(ctx, types.NamespacedName{ |
| 74 | + Name: getPodName(ar.Name, 0), |
| 75 | + Namespace: ar.Namespace, |
| 76 | + }, pod) |
| 77 | + Expect(err).NotTo(HaveOccurred()) |
| 78 | + Expect(pod.Spec.Containers).NotTo(BeEmpty()) |
| 79 | + Expect(pod.Spec.Containers[0].Ports).To(HaveLen(1)) |
| 80 | + Expect(pod.Spec.Containers[0].Ports[0].Name).To(Equal("appruntime")) |
| 81 | + Expect(pod.Spec.Containers[0].Ports[0].ContainerPort).To(Equal(int32(9000))) |
| 82 | + }) |
| 83 | +}) |
0 commit comments