|
| 1 | +package application |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + appsv1 "k8s.io/api/apps/v1" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + "k8s.io/client-go/rest" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client/apiutil" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 20 | +) |
| 21 | + |
| 22 | +var _ = Describe("application", func() { |
| 23 | + var stop chan struct{} |
| 24 | + |
| 25 | + BeforeEach(func() { |
| 26 | + stop = make(chan struct{}) |
| 27 | + getConfig = func() (*rest.Config, error) { return cfg, nil } |
| 28 | + newController = controller.New |
| 29 | + newManager = manager.New |
| 30 | + getGvk = apiutil.GVKForObject |
| 31 | + }) |
| 32 | + |
| 33 | + AfterEach(func() { |
| 34 | + close(stop) |
| 35 | + }) |
| 36 | + |
| 37 | + noop := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) { return reconcile.Result{}, nil }) |
| 38 | + |
| 39 | + Describe("New", func() { |
| 40 | + It("should return success if given valid objects", func() { |
| 41 | + instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build() |
| 42 | + Expect(err).NotTo(HaveOccurred()) |
| 43 | + Expect(instance).NotTo(BeNil()) |
| 44 | + }) |
| 45 | + |
| 46 | + It("should return an error if the Config is invalid", func() { |
| 47 | + getConfig = func() (*rest.Config, error) { return cfg, fmt.Errorf("expected error") } |
| 48 | + instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build() |
| 49 | + Expect(err).To(HaveOccurred()) |
| 50 | + Expect(err.Error()).To(ContainSubstring("expected error")) |
| 51 | + Expect(instance).To(BeNil()) |
| 52 | + }) |
| 53 | + |
| 54 | + It("should return an error if there is no GVK for an object", func() { |
| 55 | + instance, err := BuilderFor(fakeType("")).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build() |
| 56 | + Expect(err).To(HaveOccurred()) |
| 57 | + Expect(err.Error()).To(ContainSubstring("expected pointer, but got application.fakeType")) |
| 58 | + Expect(instance).To(BeNil()) |
| 59 | + |
| 60 | + instance, err = BuilderFor(&appsv1.ReplicaSet{}).Owns(fakeType("")).WithReconciler(noop).Build() |
| 61 | + Expect(err).To(HaveOccurred()) |
| 62 | + Expect(err.Error()).To(ContainSubstring("expected pointer, but got application.fakeType")) |
| 63 | + Expect(instance).To(BeNil()) |
| 64 | + }) |
| 65 | + |
| 66 | + It("should return an error if it cannot create the manager", func() { |
| 67 | + newManager = func(config *rest.Config, options manager.Options) (manager.Manager, error) { |
| 68 | + return nil, fmt.Errorf("expected error") |
| 69 | + } |
| 70 | + instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build() |
| 71 | + Expect(err).To(HaveOccurred()) |
| 72 | + Expect(err.Error()).To(ContainSubstring("expected error")) |
| 73 | + Expect(instance).To(BeNil()) |
| 74 | + }) |
| 75 | + |
| 76 | + It("should return an error if it cannot create the controller", func() { |
| 77 | + newController = func(name string, mgr manager.Manager, options controller.Options) ( |
| 78 | + controller.Controller, error) { |
| 79 | + return nil, fmt.Errorf("expected error") |
| 80 | + } |
| 81 | + instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build() |
| 82 | + Expect(err).To(HaveOccurred()) |
| 83 | + Expect(err.Error()).To(ContainSubstring("expected error")) |
| 84 | + Expect(instance).To(BeNil()) |
| 85 | + }) |
| 86 | + |
| 87 | + It("should return an error if there is no Reconciler", func() { |
| 88 | + // Prevent getGVK from failing so watch fails |
| 89 | + getGvk = func(obj runtime.Object, scheme *runtime.Scheme) (schema.GroupVersionKind, error) { |
| 90 | + return schema.GroupVersionKind{Kind: "foo"}, nil |
| 91 | + } |
| 92 | + instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).Build() |
| 93 | + Expect(err).To(HaveOccurred()) |
| 94 | + Expect(err.Error()).To(ContainSubstring("must call WithReconciler to set Reconciler")) |
| 95 | + Expect(instance).To(BeNil()) |
| 96 | + }) |
| 97 | + |
| 98 | + It("should use the provide config", func() { |
| 99 | + |
| 100 | + }) |
| 101 | + |
| 102 | + It("should use the provide manager", func() { |
| 103 | + |
| 104 | + }) |
| 105 | + |
| 106 | + It("should use the provide filters", func() { |
| 107 | + |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + Describe("Start", func() { |
| 112 | + It("should Reconcile objects", func(done Done) { |
| 113 | + By("Creating the application") |
| 114 | + ch := make(chan reconcile.Request) |
| 115 | + fn := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) { |
| 116 | + defer GinkgoRecover() |
| 117 | + ch <- req |
| 118 | + return reconcile.Result{}, nil |
| 119 | + }) |
| 120 | + |
| 121 | + instance, err := BuilderFor(&appsv1.Deployment{}). |
| 122 | + WithConfig(cfg). |
| 123 | + Owns(&appsv1.ReplicaSet{}). |
| 124 | + WithReconciler(fn). |
| 125 | + Build() |
| 126 | + Expect(err).NotTo(HaveOccurred()) |
| 127 | + |
| 128 | + By("Starting the application") |
| 129 | + go func() { |
| 130 | + defer GinkgoRecover() |
| 131 | + Expect(instance.Start(stop)).NotTo(HaveOccurred()) |
| 132 | + By("Stopping the application") |
| 133 | + }() |
| 134 | + |
| 135 | + By("Creating a Deployment") |
| 136 | + // Expect a Reconcile when the Deployment is managedObjects. |
| 137 | + dep := &appsv1.Deployment{ |
| 138 | + ObjectMeta: metav1.ObjectMeta{ |
| 139 | + Namespace: "default", |
| 140 | + Name: "deploy-name", |
| 141 | + }, |
| 142 | + Spec: appsv1.DeploymentSpec{ |
| 143 | + Selector: &metav1.LabelSelector{ |
| 144 | + MatchLabels: map[string]string{"foo": "bar"}, |
| 145 | + }, |
| 146 | + Template: corev1.PodTemplateSpec{ |
| 147 | + ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"foo": "bar"}}, |
| 148 | + Spec: corev1.PodSpec{ |
| 149 | + Containers: []corev1.Container{ |
| 150 | + { |
| 151 | + Name: "nginx", |
| 152 | + Image: "nginx", |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + } |
| 159 | + err = instance.GetClient().Create(context.TODO(), dep) |
| 160 | + Expect(err).NotTo(HaveOccurred()) |
| 161 | + |
| 162 | + By("Waiting for the Deployment Reconcile") |
| 163 | + Expect(<-ch).To(Equal(reconcile.Request{ |
| 164 | + NamespacedName: types.NamespacedName{Namespace: "default", Name: "deploy-name"}})) |
| 165 | + |
| 166 | + By("Creating a ReplicaSet") |
| 167 | + // Expect a Reconcile when an Owned object is managedObjects. |
| 168 | + t := true |
| 169 | + rs := &appsv1.ReplicaSet{ |
| 170 | + ObjectMeta: metav1.ObjectMeta{ |
| 171 | + Namespace: "default", |
| 172 | + Name: "rs-name", |
| 173 | + Labels: dep.Spec.Selector.MatchLabels, |
| 174 | + OwnerReferences: []metav1.OwnerReference{ |
| 175 | + { |
| 176 | + Name: "deploy-name", |
| 177 | + Kind: "Deployment", |
| 178 | + APIVersion: "apps/v1", |
| 179 | + Controller: &t, |
| 180 | + UID: dep.UID, |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + Spec: appsv1.ReplicaSetSpec{ |
| 185 | + Selector: dep.Spec.Selector, |
| 186 | + Template: dep.Spec.Template, |
| 187 | + }, |
| 188 | + } |
| 189 | + err = instance.GetClient().Create(context.TODO(), rs) |
| 190 | + Expect(err).NotTo(HaveOccurred()) |
| 191 | + |
| 192 | + By("Waiting for the ReplicaSet Reconcile") |
| 193 | + Expect(<-ch).To(Equal(reconcile.Request{ |
| 194 | + NamespacedName: types.NamespacedName{Namespace: "default", Name: "deploy-name"}})) |
| 195 | + |
| 196 | + close(done) |
| 197 | + }, 10) |
| 198 | + }) |
| 199 | +}) |
| 200 | + |
| 201 | +var _ runtime.Object = fakeType("") |
| 202 | + |
| 203 | +type fakeType string |
| 204 | + |
| 205 | +func (fakeType) GetObjectKind() schema.ObjectKind { return nil } |
| 206 | +func (fakeType) DeepCopyObject() runtime.Object { return nil } |
0 commit comments