Skip to content

Commit 654940f

Browse files
committed
PDOK-18065 Atom-Operator - validation unit-tests
1 parent 3950263 commit 654940f

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

api/v3/atom_validation_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package v3
22

33
import (
4+
"os"
5+
"testing"
6+
47
"github.com/google/go-cmp/cmp"
58
"k8s.io/apimachinery/pkg/util/validation/field"
6-
"os"
79
"sigs.k8s.io/yaml"
8-
"testing"
910
)
1011

1112
func TestValidateAtomWithoutClusterChecks(t *testing.T) {

internal/webhook/v3/atom_webhook_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ SOFTWARE.
2525
package v3
2626

2727
import (
28-
"fmt"
28+
"errors"
29+
"os"
30+
2931
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo bdd
3032
. "github.com/onsi/gomega" //nolint:revive // ginkgo bdd
31-
"os"
3233
"sigs.k8s.io/yaml"
3334

3435
pdoknlv3 "github.com/pdok/atom-operator/api/v3"
35-
// TODO (user): Add any additional imports if needed
3636
)
3737

3838
var _ = Describe("Atom Webhook", func() {
@@ -79,11 +79,11 @@ var _ = Describe("Atom Webhook", func() {
7979
atom := &pdoknlv3.Atom{}
8080
err = yaml.Unmarshal(input, atom)
8181
Expect(err).NotTo(HaveOccurred())
82-
warnings, errors := validator.ValidateCreate(ctx, atom)
82+
warnings, errorsCreate := validator.ValidateCreate(ctx, atom)
8383

84-
expectedError := fmt.Errorf("Atom.pdok.nl \"asis-readonly-prod\" is invalid: metadata.labels: Required value: can't be empty")
84+
expectedError := errors.New("Atom.pdok.nl \"asis-readonly-prod\" is invalid: metadata.labels: Required value: can't be empty")
8585
Expect(len(warnings)).To(Equal(0))
86-
Expect(expectedError.Error()).To(Equal(errors.Error()))
86+
Expect(expectedError.Error()).To(Equal(errorsCreate.Error()))
8787
})
8888

8989
It("Should create and update atom without errors or warnings", func() {
@@ -115,21 +115,21 @@ var _ = Describe("Atom Webhook", func() {
115115
atomOld := &pdoknlv3.Atom{}
116116
err = yaml.Unmarshal(input, atomOld)
117117
Expect(err).NotTo(HaveOccurred())
118-
warnings, errors := validator.ValidateCreate(ctx, atomOld)
119-
Expect(errors).To(BeNil())
120-
Expect(len(warnings)).To(Equal(0))
118+
warningsCreate, errorsCreate := validator.ValidateCreate(ctx, atomOld)
119+
Expect(errorsCreate).To(BeNil())
120+
Expect(len(warningsCreate)).To(Equal(0))
121121

122122
By("simulating an invalid update scenario. error label names cannot be added or deleted")
123123
input, err = os.ReadFile("test_data/input/4-update-error-add-or-delete-labels.yaml")
124124
Expect(err).NotTo(HaveOccurred())
125125
atomNew := &pdoknlv3.Atom{}
126126
err = yaml.Unmarshal(input, atomNew)
127127
Expect(err).NotTo(HaveOccurred())
128-
warnings, errors = validator.ValidateUpdate(ctx, atomOld, atomNew)
128+
warningsUpdate, errorsUpdate := validator.ValidateUpdate(ctx, atomOld, atomNew)
129129

130-
expectedError := fmt.Errorf("Atom.pdok.nl \"asis-readonly-prod\" is invalid: [metadata.labels.pdok.nl/dataset-id: Required value: labels cannot be removed, metadata.labels.pdok.nl/dataset-idsssssssss: Forbidden: new labels cannot be added]")
131-
Expect(len(warnings)).To(Equal(0))
132-
Expect(expectedError.Error()).To(Equal(errors.Error()))
130+
expectedError := errors.New("Atom.pdok.nl \"asis-readonly-prod\" is invalid: [metadata.labels.pdok.nl/dataset-id: Required value: labels cannot be removed, metadata.labels.pdok.nl/dataset-idsssssssss: Forbidden: new labels cannot be added]")
131+
Expect(len(warningsUpdate)).To(Equal(0))
132+
Expect(expectedError.Error()).To(Equal(errorsUpdate.Error()))
133133
})
134134

135135
It("Should deny update atom with error label names are immutable", func() {
@@ -139,21 +139,21 @@ var _ = Describe("Atom Webhook", func() {
139139
atomOld := &pdoknlv3.Atom{}
140140
err = yaml.Unmarshal(input, atomOld)
141141
Expect(err).NotTo(HaveOccurred())
142-
warnings, errors := validator.ValidateCreate(ctx, atomOld)
143-
Expect(errors).To(BeNil())
144-
Expect(len(warnings)).To(Equal(0))
142+
warningsCreate, errorsCreate := validator.ValidateCreate(ctx, atomOld)
143+
Expect(errorsCreate).To(BeNil())
144+
Expect(len(warningsCreate)).To(Equal(0))
145145

146146
By("simulating an invalid update scenario. Lablels are immutable")
147147
input, err = os.ReadFile("test_data/input/5-update-error-labels-immutable.yaml")
148148
Expect(err).NotTo(HaveOccurred())
149149
atomNew := &pdoknlv3.Atom{}
150150
err = yaml.Unmarshal(input, atomNew)
151151
Expect(err).NotTo(HaveOccurred())
152-
warnings, errors = validator.ValidateUpdate(ctx, atomOld, atomNew)
152+
warningsUpdate, errorsUpdate := validator.ValidateUpdate(ctx, atomOld, atomNew)
153153

154-
expectedError := fmt.Errorf("Atom.pdok.nl \"asis-readonly-prod\" is invalid: metadata.labels.pdok.nl/dataset-id: Invalid value: \"wetlands-changed\": immutable: should be wetlands")
155-
Expect(len(warnings)).To(Equal(0))
156-
Expect(expectedError.Error()).To(Equal(errors.Error()))
154+
expectedError := errors.New("Atom.pdok.nl \"asis-readonly-prod\" is invalid: metadata.labels.pdok.nl/dataset-id: Invalid value: \"wetlands-changed\": immutable: should be wetlands")
155+
Expect(len(warningsUpdate)).To(Equal(0))
156+
Expect(expectedError.Error()).To(Equal(errorsUpdate.Error()))
157157
})
158158

159159
It("Should deny update atom with error URL are immutable", func() {
@@ -163,8 +163,8 @@ var _ = Describe("Atom Webhook", func() {
163163
atomOld := &pdoknlv3.Atom{}
164164
err = yaml.Unmarshal(input, atomOld)
165165
Expect(err).NotTo(HaveOccurred())
166-
warnings, errors := validator.ValidateCreate(ctx, atomOld)
167-
Expect(errors).To(BeNil())
166+
warnings, errorsCreate := validator.ValidateCreate(ctx, atomOld)
167+
Expect(errorsCreate).To(BeNil())
168168
Expect(len(warnings)).To(Equal(0))
169169

170170
By("simulating an invalid update scenario. URL is immutable")
@@ -173,11 +173,11 @@ var _ = Describe("Atom Webhook", func() {
173173
atomNew := &pdoknlv3.Atom{}
174174
err = yaml.Unmarshal(input, atomNew)
175175
Expect(err).NotTo(HaveOccurred())
176-
warnings, errors = validator.ValidateUpdate(ctx, atomOld, atomNew)
176+
warnings, errorsUpdate := validator.ValidateUpdate(ctx, atomOld, atomNew)
177177

178-
expectedError := fmt.Errorf("Atom.pdok.nl \"asis-readonly-prod\" is invalid: spec.service.baseUrl: Forbidden: is immutable")
178+
expectedError := errors.New("Atom.pdok.nl \"asis-readonly-prod\" is invalid: spec.service.baseUrl: Forbidden: is immutable")
179179
Expect(len(warnings)).To(Equal(0))
180-
Expect(expectedError.Error()).To(Equal(errors.Error()))
180+
Expect(expectedError.Error()).To(Equal(errorsUpdate.Error()))
181181
})
182182
})
183183
})

internal/webhook/v3/ownerinfo-test/samples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package samples
22

33
import (
44
_ "embed"
5+
56
v1 "github.com/pdok/smooth-operator/api/v1"
67
"sigs.k8s.io/yaml"
78
)

internal/webhook/v3/webhook_suite_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ import (
2929
"crypto/tls"
3030
"errors"
3131
"fmt"
32-
samples "github.com/pdok/atom-operator/internal/webhook/v3/ownerinfo-test"
33-
smoothoperatorv1 "github.com/pdok/smooth-operator/api/v1"
34-
"golang.org/x/tools/go/packages"
35-
corev1 "k8s.io/api/core/v1"
36-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37-
"k8s.io/apimachinery/pkg/util/json"
3832
"net"
3933
"os"
4034
"os/exec"
4135
"path/filepath"
4236
"testing"
4337
"time"
4438

39+
samples "github.com/pdok/atom-operator/internal/webhook/v3/ownerinfo-test"
40+
smoothoperatorv1 "github.com/pdok/smooth-operator/api/v1"
41+
"golang.org/x/tools/go/packages"
42+
corev1 "k8s.io/api/core/v1"
43+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
44+
"k8s.io/apimachinery/pkg/util/json"
45+
4546
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo bdd
4647
. "github.com/onsi/gomega" //nolint:revive // ginkgo bdd
4748

@@ -134,12 +135,14 @@ var _ = BeforeSuite(func() {
134135
By("creating manager namespace")
135136

136137
clientset, err := kubernetes.NewForConfig(cfg)
138+
Expect(err).NotTo(HaveOccurred())
139+
Expect(clientset).NotTo(BeNil())
137140
namespace := &corev1.Namespace{
138141
ObjectMeta: metav1.ObjectMeta{
139142
Name: "services",
140143
},
141144
}
142-
namespace, err = clientset.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{})
145+
_, err = clientset.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{})
143146
Expect(err).NotTo(HaveOccurred(), "Failed to create namespace")
144147
ownerInfo, err := samples.OwnerInfoSample()
145148
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)