Skip to content

Commit 075855d

Browse files
committed
unit tests
1 parent c08104e commit 075855d

File tree

7 files changed

+853
-268
lines changed

7 files changed

+853
-268
lines changed

api/v3/shared_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ValidateUpdate[W WMSWFS](c client.Client, newW, oldW W, validate func(W, *[
4646
allErrs := field.ErrorList{}
4747

4848
// Make sure no ingressRouteURLs have been removed
49-
sharedValidation.ValidateIngressRouteURLsNotRemoved(oldW.IngressRouteURLs(true), newW.IngressRouteURLs(true), &allErrs, nil)
49+
sharedValidation.ValidateIngressRouteURLsNotRemoved(oldW.IngressRouteURLs(false), newW.IngressRouteURLs(true), &allErrs, nil)
5050

5151
if len(newW.IngressRouteURLs(false)) == 0 {
5252
// There are no ingressRouteURLs given, spec.service.url is immutable is that case.

api/v3/wfs_validation.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@ func ValidateWFS(wfs *WFS, warnings *[]string, allErrs *field.ErrorList) {
3434
path := field.NewPath("spec").Child("service")
3535

3636
if service.Mapfile == nil && service.DefaultCrs != "EPSG:28992" && service.Bbox == nil {
37-
*allErrs = append(*allErrs, field.Required(path.Child("bbox").Child("defaultCRS"), "when service.defaultCRS is not 'EPSG:28992'"))
37+
*allErrs = append(*allErrs, field.Required(
38+
path.Child("bbox").Child("defaultCRS"),
39+
"when service.defaultCRS is not 'EPSG:28992'",
40+
))
3841
}
3942

4043
if service.Mapfile != nil && service.Bbox != nil {
41-
sharedValidation.AddWarning(warnings, *path.Child("bbox"), "is not used when service.mapfile is configured", wfs.GroupVersionKind(), wfs.GetName())
44+
sharedValidation.AddWarning(
45+
warnings,
46+
*path.Child("bbox"),
47+
"is not used when service.mapfile is configured",
48+
wfs.GroupVersionKind(),
49+
wfs.GetName(),
50+
)
4251
}
4352

4453
ValidateInspire(wfs, allErrs)

api/v3/wms_validation.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,6 @@ func ValidateWMS(wms *WMS, warnings *[]string, allErrs *field.ErrorList) {
5151
wms.GetName(),
5252
)
5353
}
54-
} else {
55-
styleFile := false
56-
for _, key := range wms.Spec.Service.StylingAssets.GetAllConfigMapRefKeys() {
57-
if strings.HasSuffix(key, ".style") {
58-
styleFile = true
59-
break
60-
}
61-
}
62-
if !styleFile {
63-
*allErrs = append(*allErrs, field.Required(
64-
field.NewPath("spec").Child("service").Child("stylingAssets[*]").Child("configMapRefs[*]").Child("keys"),
65-
"at least one .style file is required",
66-
))
67-
}
6854
}
6955

7056
ValidateInspire(wms, allErrs)
@@ -129,7 +115,9 @@ func validateLayer(layer AnnotatedLayer, path *field.Path, groupStyles []string,
129115
validateLayerWithMapfile(layer, path, wms, warnings, allErrs)
130116

131117
if layer.Visible {
132-
*hasVisibleLayer = true
118+
if !layer.IsTopLayer {
119+
*hasVisibleLayer = true
120+
}
133121
} else {
134122
validateNotVisibleLayer(layer, path, wms, warnings, allErrs)
135123
}
@@ -150,9 +138,6 @@ func validateLayer(layer AnnotatedLayer, path *field.Path, groupStyles []string,
150138
))
151139
}
152140
}
153-
if tif := layer.Data.TIF; tif != nil {
154-
tifWarnings(tif, path, wms, warnings)
155-
}
156141
}
157142

158143
for i, subLayer := range layer.Layers {
@@ -180,12 +165,19 @@ func validateLayerWithMapfile(layer AnnotatedLayer, path *field.Path, wms *WMS,
180165
wms.GetName(),
181166
)
182167
}
183-
if !hasCustomMapfile && service.DataEPSG != "EPSG:28992" && !layer.hasBoundingBoxForCRS(service.DataEPSG) {
168+
if !hasCustomMapfile && service.DataEPSG != "EPSG:28992" && !layer.hasBoundingBoxForCRS(service.DataEPSG) && layer.Name != nil {
184169
*allErrs = append(*allErrs, field.Required(
185170
path.Child("boundingBoxes").Child("crs"),
186171
fmt.Sprintf("must contain a boundingBox for CRS %s when service.dataEPSG is not 'EPSG:28992'", service.DataEPSG),
187172
))
188173
}
174+
175+
if layer.IsDataLayer && hasCustomMapfile {
176+
if tif := layer.Data.TIF; tif != nil {
177+
tifWarnings(tif, path, wms, warnings)
178+
}
179+
}
180+
189181
}
190182

191183
func tifWarnings(tif *TIF, path *field.Path, wms *WMS, warnings *[]string) {
@@ -241,7 +233,7 @@ func validateStyle(style Style, path *field.Path, styleNames *[]string, groupSty
241233
if layer.IsGroupLayer {
242234
if slices.Contains(*groupStyles, style.Name) {
243235
*allErrs = append(*allErrs, field.Invalid(
244-
path,
236+
path.Child("name"),
245237
style.Name,
246238
"A GroupLayer can't redefine the same style as a parent layer",
247239
))

internal/webhook/v3/test_data/v3_wms.yaml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ spec:
1212
resources:
1313
limits:
1414
ephemeral-storage: 1G
15+
ingressRouteUrls:
16+
- url: "https://test.test/path"
1517
service:
1618
prefix: "prefix"
1719
url: "https://test.test/path"
@@ -50,14 +52,20 @@ spec:
5052
- name: "name"
5153
title: "title"
5254
visualization: "file.style"
53-
- name: "not visible"
54-
visible: false
55-
data:
56-
postgis:
57-
columns:
58-
- name: "column"
59-
geometryType: "Point"
60-
tableName: "table"
55+
- name: "visible Group Layer"
56+
visible: true
6157
styles:
62-
- name: "name"
63-
visualization: "file.style"
58+
- name: style
59+
title: style
60+
layers:
61+
- name: "not visible"
62+
visible: false
63+
data:
64+
postgis:
65+
columns:
66+
- name: "column"
67+
geometryType: "Point"
68+
tableName: "table"
69+
styles:
70+
- name: "style"
71+
visualization: "file.style"

internal/webhook/v3/webhook_suite_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ import (
3838
"testing"
3939
"time"
4040

41+
"github.com/pdok/smooth-operator/pkg/validation"
42+
apierrors "k8s.io/apimachinery/pkg/api/errors"
43+
"k8s.io/apimachinery/pkg/runtime/schema"
44+
"k8s.io/apimachinery/pkg/util/validation/field"
45+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
46+
4147
smoothoperatorv1 "github.com/pdok/smooth-operator/api/v1"
4248
"golang.org/x/tools/go/packages"
4349

@@ -229,3 +235,16 @@ func must[T any](t T, err error) T {
229235
}
230236
return t
231237
}
238+
239+
func getValidationError[O pdoknlv3.WMSWFS](obj O, errorList *field.Error) error {
240+
return apierrors.NewInvalid(obj.GroupKind(), obj.GetName(), field.ErrorList{errorList})
241+
}
242+
243+
func getValidationWarnings[O pdoknlv3.WMSWFS](obj O, path field.Path, warning string, warnings []string) admission.Warnings {
244+
validation.AddWarning(&warnings, path, warning, schema.GroupVersionKind{
245+
Group: obj.GroupKind().Group,
246+
Version: "v3",
247+
Kind: obj.GroupKind().Kind,
248+
}, obj.GetName())
249+
return warnings
250+
}

0 commit comments

Comments
 (0)