stack_controller_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright 2026 LocoStack.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package controller
  14. import (
  15. "context"
  16. . "github.com/onsi/ginkgo/v2"
  17. . "github.com/onsi/gomega"
  18. "k8s.io/apimachinery/pkg/api/errors"
  19. "k8s.io/apimachinery/pkg/types"
  20. "sigs.k8s.io/controller-runtime/pkg/reconcile"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "github.com/LocoStack/loco-operator/api/v1alpha1"
  23. )
  24. var _ = Describe("Stack Controller", func() {
  25. Context("When reconciling a resource", func() {
  26. const resourceName = "test-resource"
  27. ctx := context.Background()
  28. typeNamespacedName := types.NamespacedName{
  29. Name: resourceName,
  30. Namespace: "default", // TODO(user):Modify as needed
  31. }
  32. stack := &v1alpha1.Stack{}
  33. BeforeEach(func() {
  34. By("creating the custom resource for the Kind Stack")
  35. err := k8sClient.Get(ctx, typeNamespacedName, stack)
  36. if err != nil && errors.IsNotFound(err) {
  37. resource := &v1alpha1.Stack{
  38. ObjectMeta: metav1.ObjectMeta{
  39. Name: resourceName,
  40. Namespace: "default",
  41. },
  42. // TODO(user): Specify other spec details if needed.
  43. }
  44. Expect(k8sClient.Create(ctx, resource)).To(Succeed())
  45. }
  46. })
  47. AfterEach(func() {
  48. // TODO(user): Cleanup logic after each test, like removing the resource instance.
  49. resource := &v1alpha1.Stack{}
  50. err := k8sClient.Get(ctx, typeNamespacedName, resource)
  51. Expect(err).NotTo(HaveOccurred())
  52. By("Cleanup the specific resource instance Stack")
  53. Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
  54. })
  55. It("should successfully reconcile the resource", func() {
  56. By("Reconciling the created resource")
  57. controllerReconciler := &StackReconciler{
  58. Client: k8sClient,
  59. Scheme: k8sClient.Scheme(),
  60. }
  61. _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
  62. NamespacedName: typeNamespacedName,
  63. })
  64. Expect(err).NotTo(HaveOccurred())
  65. // TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
  66. // Example: If you expect a certain status condition after reconciliation, verify it here.
  67. })
  68. })
  69. })