@@ -2,24 +2,26 @@ package usage_test
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"testing"
6
7
"time"
7
8
8
9
. "github.com./onsi/gomega"
9
10
appsv1 "k8s.io/api/apps/v1"
10
11
v1 "k8s.io/api/core/v1"
11
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
+ "k8s.io/apimachinery/pkg/types"
14
+ "sigs.k8s.io/controller-runtime/pkg/client"
12
15
"sigs.k8s.io/controller-runtime/pkg/client/fake"
13
16
"sigs.k8s.io/controller-runtime/pkg/log/zap"
14
17
18
+ "github.com./nginxinc/nginx-gateway-fabric/internal/framework/events/eventsfakes"
15
19
"github.com./nginxinc/nginx-gateway-fabric/internal/mode/static/config"
16
20
"github.com./nginxinc/nginx-gateway-fabric/internal/mode/static/usage"
17
21
"github.com./nginxinc/nginx-gateway-fabric/internal/mode/static/usage/usagefakes"
18
22
)
19
23
20
24
func TestCreateUsageJobWorker (t * testing.T ) {
21
- g := NewWithT (t )
22
-
23
25
replicas := int32 (1 )
24
26
ngfReplicaSet := & appsv1.ReplicaSet {
25
27
ObjectMeta : metav1.ObjectMeta {
@@ -34,64 +36,145 @@ func TestCreateUsageJobWorker(t *testing.T) {
34
36
},
35
37
}
36
38
37
- ngfPod := & v1.Pod {
38
- ObjectMeta : metav1.ObjectMeta {
39
- Namespace : "nginx-gateway" ,
40
- Name : "ngf-pod" ,
41
- OwnerReferences : []metav1.OwnerReference {
42
- {
43
- Kind : "ReplicaSet" ,
44
- Name : "ngf-replicaset" ,
39
+ tests := []struct {
40
+ name string
41
+ listCalls func (_ context.Context , object client.ObjectList , _ ... client.ListOption ) error
42
+ getCalls func (_ context.Context , _ types.NamespacedName , object client.Object , _ ... client.GetOption ) error
43
+ expData usage.ClusterDetails
44
+ expErr bool
45
+ }{
46
+ {
47
+ name : "succeeds" ,
48
+ listCalls : func (_ context.Context , object client.ObjectList , _ ... client.ListOption ) error {
49
+ switch typedList := object .(type ) {
50
+ case * v1.NodeList :
51
+ typedList .Items = append (typedList .Items , v1.Node {})
52
+ return nil
53
+ case * appsv1.ReplicaSetList :
54
+ typedList .Items = append (typedList .Items , * ngfReplicaSet )
55
+ return nil
56
+ }
57
+ return nil
58
+ },
59
+ getCalls : func (_ context.Context , _ types.NamespacedName , object client.Object , _ ... client.GetOption ) error {
60
+ switch typedObject := object .(type ) {
61
+ case * v1.Namespace :
62
+ typedObject .Name = metav1 .NamespaceSystem
63
+ typedObject .UID = "1234abcd"
64
+ return nil
65
+ }
66
+ return nil
67
+ },
68
+ expData : usage.ClusterDetails {
69
+ Metadata : usage.Metadata {
70
+ UID : "1234abcd" ,
71
+ DisplayName : "my-cluster" ,
72
+ },
73
+ NodeCount : 1 ,
74
+ PodDetails : usage.PodDetails {
75
+ CurrentPodCounts : usage.CurrentPodsCount {
76
+ PodCount : 1 ,
77
+ },
45
78
},
46
79
},
80
+ expErr : false ,
47
81
},
48
- }
49
-
50
- kubeSystem := & v1.Namespace {
51
- ObjectMeta : metav1.ObjectMeta {
52
- Name : metav1 .NamespaceSystem ,
53
- UID : "1234abcd" ,
54
- },
55
- }
56
-
57
- k8sClient := fake .NewFakeClient (& v1.Node {}, ngfReplicaSet , ngfPod , kubeSystem )
58
- reporter := & usagefakes.FakeReporter {}
59
-
60
- worker := usage .CreateUsageJobWorker (
61
- zap .New (),
62
- k8sClient ,
63
- reporter ,
64
- config.Config {
65
- GatewayPodConfig : config.GatewayPodConfig {
66
- Namespace : "nginx-gateway" ,
67
- Name : "ngf-pod" ,
82
+ {
83
+ name : "collect node count fails" ,
84
+ listCalls : func (_ context.Context , object client.ObjectList , _ ... client.ListOption ) error {
85
+ switch object .(type ) {
86
+ case * v1.NodeList :
87
+ return errors .New ("failed to collect node list" )
88
+ }
89
+ return nil
68
90
},
69
- UsageReportConfig : & config. UsageReportConfig {
70
- ClusterDisplayName : "my-cluster" ,
91
+ getCalls : func ( _ context. Context , _ types. NamespacedName , _ client. Object , _ ... client. GetOption ) error {
92
+ return nil
71
93
},
94
+ expData : usage.ClusterDetails {},
95
+ expErr : true ,
72
96
},
73
- )
74
-
75
- expData := usage.ClusterDetails {
76
- Metadata : usage.Metadata {
77
- UID : "1234abcd" ,
78
- DisplayName : "my-cluster" ,
97
+ {
98
+ name : "collect replica count fails" ,
99
+ listCalls : func (_ context.Context , object client.ObjectList , _ ... client.ListOption ) error {
100
+ switch typedList := object .(type ) {
101
+ case * v1.NodeList :
102
+ typedList .Items = append (typedList .Items , v1.Node {})
103
+ return nil
104
+ case * appsv1.ReplicaSetList :
105
+ return errors .New ("failed to collect replica set list" )
106
+ }
107
+ return nil
108
+ },
109
+ getCalls : func (_ context.Context , _ types.NamespacedName , _ client.Object , _ ... client.GetOption ) error {
110
+ return nil
111
+ },
112
+ expData : usage.ClusterDetails {},
113
+ expErr : true ,
79
114
},
80
- NodeCount : 1 ,
81
- PodDetails : usage.PodDetails {
82
- CurrentPodCounts : usage.CurrentPodsCount {
83
- PodCount : 1 ,
115
+ {
116
+ name : "collect cluster UID fails" ,
117
+ listCalls : func (_ context.Context , object client.ObjectList , _ ... client.ListOption ) error {
118
+ switch typedList := object .(type ) {
119
+ case * v1.NodeList :
120
+ typedList .Items = append (typedList .Items , v1.Node {})
121
+ return nil
122
+ case * appsv1.ReplicaSetList :
123
+ typedList .Items = append (typedList .Items , * ngfReplicaSet )
124
+ return nil
125
+ }
126
+ return nil
127
+ },
128
+ getCalls : func (_ context.Context , _ types.NamespacedName , object client.Object , _ ... client.GetOption ) error {
129
+ switch object .(type ) {
130
+ case * v1.Namespace :
131
+ return errors .New ("failed to collect namespace" )
132
+ }
133
+ return nil
84
134
},
135
+ expData : usage.ClusterDetails {},
136
+ expErr : true ,
85
137
},
86
138
}
87
139
88
- timeout := 10 * time .Second
89
- ctx , cancel := context .WithTimeout (context .Background (), timeout )
90
- defer cancel ()
140
+ for _ , test := range tests {
141
+ t .Run (test .name , func (t * testing.T ) {
142
+ g := NewWithT (t )
143
+
144
+ k8sClientReader := & eventsfakes.FakeReader {}
145
+ k8sClientReader .ListCalls (test .listCalls )
146
+ k8sClientReader .GetCalls (test .getCalls )
91
147
92
- worker (ctx )
93
- _ , data := reporter .ReportArgsForCall (0 )
94
- g .Expect (data ).To (Equal (expData ))
148
+ reporter := & usagefakes.FakeReporter {}
149
+
150
+ worker := usage .CreateUsageJobWorker (
151
+ zap .New (),
152
+ k8sClientReader ,
153
+ reporter ,
154
+ config.Config {
155
+ GatewayPodConfig : config.GatewayPodConfig {
156
+ Namespace : "nginx-gateway" ,
157
+ Name : "ngf-pod" ,
158
+ },
159
+ UsageReportConfig : & config.UsageReportConfig {
160
+ ClusterDisplayName : "my-cluster" ,
161
+ },
162
+ },
163
+ )
164
+
165
+ timeout := 10 * time .Second
166
+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
167
+ defer cancel ()
168
+
169
+ worker (ctx )
170
+ if test .expErr {
171
+ g .Expect (reporter .ReportCallCount ()).To (Equal (0 ))
172
+ } else {
173
+ _ , data := reporter .ReportArgsForCall (0 )
174
+ g .Expect (data ).To (Equal (test .expData ))
175
+ }
176
+ })
177
+ }
95
178
}
96
179
97
180
func TestGetTotalNGFPodCount (t * testing.T ) {
0 commit comments