@@ -18,67 +18,92 @@ package controller
18
18
19
19
import (
20
20
"context"
21
+ "net/http"
22
+ "testing"
21
23
22
- . "github.com/onsi/ginkgo/v2 "
24
+ "github.com/fluxcd/pkg/runtime/conditions "
23
25
. "github.com/onsi/gomega"
24
- "k8s.io/apimachinery/pkg/api/errors"
25
- "k8s.io/apimachinery/pkg/types"
26
- "sigs.k8s.io/controller-runtime/pkg/reconcile"
26
+
27
+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
27
28
28
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
30
30
31
cloudflareoperatoriov1 "github.com/containeroo/cloudflare-operator/api/v1"
31
32
)
32
33
33
- var _ = Describe ("IP Controller" , func () {
34
- Context ("When reconciling a resource" , func () {
35
- const resourceName = "test-resource"
36
-
37
- ctx := context .Background ()
38
-
39
- typeNamespacedName := types.NamespacedName {
40
- Name : resourceName ,
41
- Namespace : "default" , // TODO(user):Modify as needed
42
- }
43
- ip := & cloudflareoperatoriov1.IP {}
44
-
45
- BeforeEach (func () {
46
- By ("creating the custom resource for the Kind IP" )
47
- err := k8sClient .Get (ctx , typeNamespacedName , ip )
48
- if err != nil && errors .IsNotFound (err ) {
49
- resource := & cloudflareoperatoriov1.IP {
50
- ObjectMeta : metav1.ObjectMeta {
51
- Name : resourceName ,
52
- Namespace : "default" ,
53
- },
54
- // TODO(user): Specify other spec details if needed.
55
- }
56
- Expect (k8sClient .Create (ctx , resource )).To (Succeed ())
57
- }
58
- })
59
-
60
- AfterEach (func () {
61
- // TODO(user): Cleanup logic after each test, like removing the resource instance.
62
- resource := & cloudflareoperatoriov1.IP {}
63
- err := k8sClient .Get (ctx , typeNamespacedName , resource )
64
- Expect (err ).NotTo (HaveOccurred ())
65
-
66
- By ("Cleanup the specific resource instance IP" )
67
- Expect (k8sClient .Delete (ctx , resource )).To (Succeed ())
68
- })
69
- It ("should successfully reconcile the resource" , func () {
70
- By ("Reconciling the created resource" )
71
- controllerReconciler := & IPReconciler {
72
- Client : k8sClient ,
73
- Scheme : k8sClient .Scheme (),
74
- }
75
-
76
- _ , err := controllerReconciler .Reconcile (ctx , reconcile.Request {
77
- NamespacedName : typeNamespacedName ,
78
- })
79
- Expect (err ).NotTo (HaveOccurred ())
80
- // TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
81
- // Example: If you expect a certain status condition after reconciliation, verify it here.
82
- })
34
+ func StartIPSource () {
35
+ http .HandleFunc ("/plain" , func (w http.ResponseWriter , r * http.Request ) {
36
+ _ , _ = w .Write ([]byte ("1.1.1.1" ))
37
+ })
38
+ http .HandleFunc ("/json" , func (w http.ResponseWriter , r * http.Request ) {
39
+ _ , _ = w .Write ([]byte (`{"ip":"1.1.1.1"}` ))
40
+ })
41
+ _ = http .ListenAndServe (":8080" , nil )
42
+ }
43
+
44
+ func TestIPReconciler_reconcileIP (t * testing.T ) {
45
+ g := NewWithT (t )
46
+
47
+ ip := & cloudflareoperatoriov1.IP {
48
+ ObjectMeta : metav1.ObjectMeta {
49
+ Name : "ip" ,
50
+ },
51
+ Spec : cloudflareoperatoriov1.IPSpec {
52
+ Type : "dynamic" ,
53
+ },
54
+ }
55
+
56
+ r := & IPReconciler {
57
+ Client : fake .NewClientBuilder ().
58
+ WithScheme (NewTestScheme ()).
59
+ WithStatusSubresource (& cloudflareoperatoriov1.Account {}).
60
+ WithObjects ().
61
+ Build (),
62
+ }
63
+
64
+ go StartIPSource ()
65
+
66
+ t .Run ("reconciles dynamic ip plain text" , func (t * testing.T ) {
67
+ ip .Spec .IPSources = []cloudflareoperatoriov1.IPSpecIPSources {{
68
+ URL : "http://localhost:8080/plain" ,
69
+ }}
70
+
71
+ _ = r .reconcileIP (context .TODO (), ip )
72
+
73
+ g .Expect (ip .Status .Conditions ).To (conditions .MatchConditions ([]metav1.Condition {
74
+ * conditions .TrueCondition (cloudflareoperatoriov1 .ConditionTypeReady , cloudflareoperatoriov1 .ConditionReasonReady , "IP is ready" ),
75
+ }))
76
+
77
+ g .Expect (ip .Spec .Address ).To (Equal ("1.1.1.1" ))
78
+ })
79
+
80
+ t .Run ("reconciles dynamic ip jq filter" , func (t * testing.T ) {
81
+ ip .Spec .IPSources = []cloudflareoperatoriov1.IPSpecIPSources {{
82
+ URL : "http://localhost:8080/json" ,
83
+ ResponseJQFilter : ".ip" ,
84
+ }}
85
+
86
+ _ = r .reconcileIP (context .TODO (), ip )
87
+
88
+ g .Expect (ip .Status .Conditions ).To (conditions .MatchConditions ([]metav1.Condition {
89
+ * conditions .TrueCondition (cloudflareoperatoriov1 .ConditionTypeReady , cloudflareoperatoriov1 .ConditionReasonReady , "IP is ready" ),
90
+ }))
91
+
92
+ g .Expect (ip .Spec .Address ).To (Equal ("1.1.1.1" ))
93
+ })
94
+
95
+ t .Run ("reconciles dynamic ip regex" , func (t * testing.T ) {
96
+ ip .Spec .IPSources = []cloudflareoperatoriov1.IPSpecIPSources {{
97
+ URL : "http://localhost:8080/json" ,
98
+ PostProcessingRegex : "([0-9]+\\ .[0-9]+\\ .[0-9]+\\ .[0-9]+)" ,
99
+ }}
100
+
101
+ _ = r .reconcileIP (context .TODO (), ip )
102
+
103
+ g .Expect (ip .Status .Conditions ).To (conditions .MatchConditions ([]metav1.Condition {
104
+ * conditions .TrueCondition (cloudflareoperatoriov1 .ConditionTypeReady , cloudflareoperatoriov1 .ConditionReasonReady , "IP is ready" ),
105
+ }))
106
+
107
+ g .Expect (ip .Spec .Address ).To (Equal ("1.1.1.1" ))
83
108
})
84
- })
109
+ }
0 commit comments