@@ -15,9 +15,9 @@ import (
1515 knservingv1 "knative.dev/serving/pkg/apis/serving/v1"
1616)
1717
18- // Define a valid Service configuration for tests
19- var testValidKnSvc = KnativeService {
20- BaseService : & BaseService {
18+ func TestBuildKnativeServiceConfig ( t * testing. T ) {
19+ // Test configuration
20+ baseSvc := & BaseService {
2121 Name : "test-svc" ,
2222 Namespace : "test-namespace" ,
2323 Image : "asia.gcr.io/gcp-project-id/turing-router:latest" ,
@@ -84,25 +84,10 @@ var testValidKnSvc = KnativeService{
8484 MountPath : "/var/secret" ,
8585 },
8686 },
87- },
88- ContainerPort : 8080 ,
89- MinReplicas : 1 ,
90- MaxReplicas : 2 ,
91- IsClusterLocal : true ,
92- TargetConcurrency : 1 ,
93- QueueProxyResourcePercentage : 30 ,
94- }
87+ }
9588
96- func TestBuildKnativeServiceConfig (t * testing.T ) {
97- // Build expected configuration
89+ // Expected specs
9890 var timeout int64 = 30
99- annotations := map [string ]string {
100- "autoscaling.knative.dev/minScale" : "1" ,
101- "autoscaling.knative.dev/maxScale" : "2" ,
102- "autoscaling.knative.dev/target" : "1" ,
103- "autoscaling.knative.dev/class" : "kpa.autoscaling.knative.dev" ,
104- "queue.sidecar.serving.knative.dev/resourcePercentage" : "30" ,
105- }
10691 resources := corev1.ResourceRequirements {
10792 Limits : map [corev1.ResourceName ]resource.Quantity {
10893 corev1 .ResourceCPU : resource .MustParse ("800m" ),
@@ -161,42 +146,110 @@ func TestBuildKnativeServiceConfig(t *testing.T) {
161146 TimeoutSeconds : 5 ,
162147 FailureThreshold : 5 ,
163148 },
164- VolumeMounts : testValidKnSvc .VolumeMounts ,
149+ VolumeMounts : baseSvc .VolumeMounts ,
165150 Env : envs ,
166151 },
167152 },
168- Volumes : testValidKnSvc .Volumes ,
153+ Volumes : baseSvc .Volumes ,
169154 }
170- expected := & knservingv1.Service {
171- ObjectMeta : metav1.ObjectMeta {
172- Name : "test-svc" ,
173- Namespace : "test-namespace" ,
174- Labels : map [string ]string {
175- "labelKey" : "labelVal" ,
176- "serving.knative.dev/visibility" : "cluster-local" ,
155+
156+ tests := map [string ]struct {
157+ serviceCfg KnativeService
158+ expectedSpec knservingv1.Service
159+ }{
160+ "basic" : {
161+ serviceCfg : KnativeService {
162+ BaseService : baseSvc ,
163+ ContainerPort : 8080 ,
164+ MinReplicas : 1 ,
165+ MaxReplicas : 2 ,
166+ IsClusterLocal : true ,
167+ TargetConcurrency : 1 ,
168+ QueueProxyResourcePercentage : 30 ,
177169 },
178- },
179- Spec : knservingv1.ServiceSpec {
180- ConfigurationSpec : knservingv1.ConfigurationSpec {
181- Template : knservingv1.RevisionTemplateSpec {
182- ObjectMeta : metav1.ObjectMeta {
183- Name : "test-svc-0" ,
184- Labels : map [string ]string {
185- "labelKey" : "labelVal" ,
170+ expectedSpec : knservingv1.Service {
171+ ObjectMeta : metav1.ObjectMeta {
172+ Name : "test-svc" ,
173+ Namespace : "test-namespace" ,
174+ Labels : map [string ]string {
175+ "labelKey" : "labelVal" ,
176+ "serving.knative.dev/visibility" : "cluster-local" ,
177+ },
178+ },
179+ Spec : knservingv1.ServiceSpec {
180+ ConfigurationSpec : knservingv1.ConfigurationSpec {
181+ Template : knservingv1.RevisionTemplateSpec {
182+ ObjectMeta : metav1.ObjectMeta {
183+ Name : "test-svc-0" ,
184+ Labels : map [string ]string {
185+ "labelKey" : "labelVal" ,
186+ },
187+ Annotations : map [string ]string {
188+ "autoscaling.knative.dev/minScale" : "1" ,
189+ "autoscaling.knative.dev/maxScale" : "2" ,
190+ "autoscaling.knative.dev/target" : "1" ,
191+ "autoscaling.knative.dev/class" : "kpa.autoscaling.knative.dev" ,
192+ "queue.sidecar.serving.knative.dev/resourcePercentage" : "30" ,
193+ },
194+ },
195+ Spec : knservingv1.RevisionSpec {
196+ PodSpec : podSpec ,
197+ TimeoutSeconds : & timeout ,
198+ },
186199 },
187- Annotations : annotations ,
188200 },
189- Spec : knservingv1.RevisionSpec {
190- PodSpec : podSpec ,
191- TimeoutSeconds : & timeout ,
201+ },
202+ },
203+ },
204+ "annotations" : {
205+ serviceCfg : KnativeService {
206+ BaseService : baseSvc ,
207+ ContainerPort : 8080 ,
208+ MinReplicas : 5 ,
209+ MaxReplicas : 6 ,
210+ IsClusterLocal : false ,
211+ TargetConcurrency : 4 ,
212+ },
213+ expectedSpec : knservingv1.Service {
214+ ObjectMeta : metav1.ObjectMeta {
215+ Name : "test-svc" ,
216+ Namespace : "test-namespace" ,
217+ Labels : map [string ]string {
218+ "labelKey" : "labelVal" ,
219+ },
220+ },
221+ Spec : knservingv1.ServiceSpec {
222+ ConfigurationSpec : knservingv1.ConfigurationSpec {
223+ Template : knservingv1.RevisionTemplateSpec {
224+ ObjectMeta : metav1.ObjectMeta {
225+ Name : "test-svc-0" ,
226+ Labels : map [string ]string {
227+ "labelKey" : "labelVal" ,
228+ },
229+ Annotations : map [string ]string {
230+ "autoscaling.knative.dev/minScale" : "5" ,
231+ "autoscaling.knative.dev/maxScale" : "6" ,
232+ "autoscaling.knative.dev/target" : "4" ,
233+ "autoscaling.knative.dev/class" : "kpa.autoscaling.knative.dev" ,
234+ },
235+ },
236+ Spec : knservingv1.RevisionSpec {
237+ PodSpec : podSpec ,
238+ TimeoutSeconds : & timeout ,
239+ },
240+ },
192241 },
193242 },
194243 },
195244 },
196245 }
197246
198- // Run test and validate
199- svc := testValidKnSvc .BuildKnativeServiceConfig ()
200- err := tu .CompareObjects (svc , expected )
201- assert .NoError (t , err )
247+ for name , data := range tests {
248+ t .Run (name , func (t * testing.T ) {
249+ // Run test and validate
250+ svc := data .serviceCfg .BuildKnativeServiceConfig ()
251+ err := tu .CompareObjects (* svc , data .expectedSpec )
252+ assert .NoError (t , err )
253+ })
254+ }
202255}
0 commit comments