-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommitrequest_controller_test.go
More file actions
100 lines (89 loc) · 4.01 KB
/
Copy pathcommitrequest_controller_test.go
File metadata and controls
100 lines (89 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: Apache-2.0
package controller
import (
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
configbutleraiv1alpha3 "github.com/ConfigButler/gitops-reverser/api/v1alpha3"
)
var _ = Describe("CommitRequest controller", func() {
const namespace = "default"
// The suite registers the reconciler in attributed-author mode (a non-nil AuthorLookup
// that never resolves) with a Finalizer that never resolves, so these specs cover
// the in-progress stamp and the terminal short-circuit only; the full
// attribute → attach → terminal flow and the configured-author path are covered by
// the unit tests in commitrequest_controller_unit_test.go.
It("stamps a freshly created CommitRequest with in-progress conditions", func() {
commitRequest := &configbutleraiv1alpha3.CommitRequest{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "save-",
Namespace: namespace,
},
Spec: configbutleraiv1alpha3.CommitRequestSpec{
TargetRef: configbutleraiv1alpha3.LocalTargetReference{
Name: "team-a-config",
},
Message: "increase checkout API memory",
},
}
Expect(k8sClient.Create(ctx, commitRequest)).To(Succeed())
key := client.ObjectKeyFromObject(commitRequest)
Eventually(func(g Gomega) {
var fetched configbutleraiv1alpha3.CommitRequest
g.Expect(k8sClient.Get(ctx, key, &fetched)).To(Succeed())
reconciling := apimeta.FindStatusCondition(fetched.Status.Conditions, ConditionTypeReconciling)
g.Expect(reconciling).NotTo(BeNil())
g.Expect(reconciling.Status).To(Equal(metav1.ConditionTrue))
ready := apimeta.FindStatusCondition(fetched.Status.Conditions, ConditionTypeReady)
g.Expect(ready).NotTo(BeNil())
g.Expect(ready.Status).To(Equal(metav1.ConditionFalse))
}, 10*time.Second, 200*time.Millisecond).Should(Succeed())
})
It("does not overwrite a terminal outcome that is already recorded", func() {
commitRequest := &configbutleraiv1alpha3.CommitRequest{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "save-",
Namespace: namespace,
},
Spec: configbutleraiv1alpha3.CommitRequestSpec{
TargetRef: configbutleraiv1alpha3.LocalTargetReference{
Name: "team-a-config",
},
},
}
Expect(k8sClient.Create(ctx, commitRequest)).To(Succeed())
key := client.ObjectKeyFromObject(commitRequest)
// Wait for the initial in-progress stamp.
Eventually(func(g Gomega) {
var fetched configbutleraiv1alpha3.CommitRequest
g.Expect(k8sClient.Get(ctx, key, &fetched)).To(Succeed())
ready := apimeta.FindStatusCondition(fetched.Status.Conditions, ConditionTypeReady)
g.Expect(ready).NotTo(BeNil())
g.Expect(ready.Status).To(Equal(metav1.ConditionFalse))
}, 10*time.Second, 200*time.Millisecond).Should(Succeed())
// Simulate a finalize having recorded the terminal outcome (Ready=True).
var fetched configbutleraiv1alpha3.CommitRequest
Expect(k8sClient.Get(ctx, key, &fetched)).To(Succeed())
apimeta.SetStatusCondition(&fetched.Status.Conditions, metav1.Condition{
Type: ConditionTypeReady, Status: metav1.ConditionTrue, Reason: crReasonCommitted, Message: "committed",
})
apimeta.SetStatusCondition(&fetched.Status.Conditions, metav1.Condition{
Type: ConditionTypeReconciling, Status: metav1.ConditionFalse, Reason: crReasonCommitted, Message: "done",
})
fetched.Status.Branch = "main"
fetched.Status.SHA = "abc123"
Expect(k8sClient.Status().Update(ctx, &fetched)).To(Succeed())
// The controller must leave the terminal outcome intact.
Consistently(func(g Gomega) {
var checked configbutleraiv1alpha3.CommitRequest
g.Expect(k8sClient.Get(ctx, key, &checked)).To(Succeed())
ready := apimeta.FindStatusCondition(checked.Status.Conditions, ConditionTypeReady)
g.Expect(ready).NotTo(BeNil())
g.Expect(ready.Status).To(Equal(metav1.ConditionTrue))
g.Expect(checked.Status.SHA).To(Equal("abc123"))
}, 2*time.Second, 200*time.Millisecond).Should(Succeed())
})
})