Skip to content

Commit

Permalink
Add unit test to OpenStackCluster webhooks
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Giese <tobias.giese@daimler.com>
  • Loading branch information
tobiasgiese committed Dec 5, 2021
1 parent 0a04a08 commit 006d55a
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
86 changes: 86 additions & 0 deletions api/v1alpha4/openstackcluster_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha4

import (
"testing"

. "github.com/onsi/gomega"
)

func TestOpenStackCluster_ValidateUpdate(t *testing.T) {
g := NewWithT(t)

tests := []struct {
name string
oldTemplate *OpenStackCluster
newTemplate *OpenStackCluster
wantErr bool
}{
{
name: "OpenStackCluster.Spec.IdentityRef with immutable spec",
oldTemplate: &OpenStackCluster{
Spec: OpenStackClusterSpec{
IdentityRef: &OpenStackIdentityReference{
Kind: "Secret",
},
CloudName: "foobar",
},
},
newTemplate: &OpenStackCluster{
Spec: OpenStackClusterSpec{
CloudName: "foobar",
IdentityRef: &OpenStackIdentityReference{
Kind: "foobar",
},
},
},
wantErr: true,
},
{
name: "OpenStackCluster.Spec.IdentityRef with mutable spec",
oldTemplate: &OpenStackCluster{
Spec: OpenStackClusterSpec{
IdentityRef: &OpenStackIdentityReference{
Kind: "Secret",
},
CloudName: "foobar",
},
},
newTemplate: &OpenStackCluster{
Spec: OpenStackClusterSpec{
CloudName: "foobar",
IdentityRef: &OpenStackIdentityReference{
Kind: "Secret",
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.newTemplate.ValidateUpdate(tt.oldTemplate)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
})
}
}
86 changes: 86 additions & 0 deletions api/v1beta1/openstackcluster_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"testing"

. "github.com/onsi/gomega"
)

func TestOpenStackCluster_ValidateUpdate(t *testing.T) {
g := NewWithT(t)

tests := []struct {
name string
oldTemplate *OpenStackCluster
newTemplate *OpenStackCluster
wantErr bool
}{
{
name: "OpenStackCluster.Spec.IdentityRef with immutable spec",
oldTemplate: &OpenStackCluster{
Spec: OpenStackClusterSpec{
IdentityRef: &OpenStackIdentityReference{
Kind: "Secret",
},
CloudName: "foobar",
},
},
newTemplate: &OpenStackCluster{
Spec: OpenStackClusterSpec{
CloudName: "foobar",
IdentityRef: &OpenStackIdentityReference{
Kind: "foobar",
},
},
},
wantErr: true,
},
{
name: "OpenStackCluster.Spec.IdentityRef with mutable spec",
oldTemplate: &OpenStackCluster{
Spec: OpenStackClusterSpec{
IdentityRef: &OpenStackIdentityReference{
Kind: "Secret",
},
CloudName: "foobar",
},
},
newTemplate: &OpenStackCluster{
Spec: OpenStackClusterSpec{
CloudName: "foobar",
IdentityRef: &OpenStackIdentityReference{
Kind: "Secret",
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.newTemplate.ValidateUpdate(tt.oldTemplate)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
})
}
}

0 comments on commit 006d55a

Please sign in to comment.