Skip to content

Commit

Permalink
Simplify binding webhook unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Mar 17, 2021
1 parent 7433675 commit 9cbea51
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 89 deletions.
108 changes: 21 additions & 87 deletions api/v1alpha1/binding_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var _ = Describe("Binding webhook", func() {
Name: "update-binding",
},
Spec: BindingSpec{
Vhost: "/test",
Source: "test",
Destination: "test",
DestinationType: "queue",
Expand All @@ -25,114 +26,47 @@ var _ = Describe("Binding webhook", func() {
},
}

It("does not allow updates on vhost", func() {
newBinding := oldBinding.DeepCopy()
newBinding.Spec.Vhost = "/new-vhost"
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
})

It("does not allow updates on source", func() {
newBinding := Binding{
ObjectMeta: metav1.ObjectMeta{
Name: "update-binding",
},
Spec: BindingSpec{
Source: "updated-source",
Destination: "test",
DestinationType: "queue",
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "some-cluster",
Namespace: "default",
},
},
}
newBinding := oldBinding.DeepCopy()
newBinding.Spec.Source = "updated-source"
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
})

It("does not allow updates on destination", func() {
newBinding := Binding{
ObjectMeta: metav1.ObjectMeta{
Name: "update-binding",
},
Spec: BindingSpec{
Source: "test",
Destination: "updated-des",
DestinationType: "queue",
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "some-cluster",
Namespace: "default",
},
},
}
newBinding := oldBinding.DeepCopy()
newBinding.Spec.Destination = "updated-des"
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
})

It("does not allow updates on destination type", func() {
newBinding := Binding{
ObjectMeta: metav1.ObjectMeta{
Name: "update-binding",
},
Spec: BindingSpec{
Source: "test",
Destination: "test",
DestinationType: "exchange",
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "some-cluster",
Namespace: "default",
},
},
}
newBinding := oldBinding.DeepCopy()
newBinding.Spec.DestinationType = "exchange"
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
})

It("does not allow updates on routing key", func() {
newBinding := Binding{
ObjectMeta: metav1.ObjectMeta{
Name: "update-binding",
},
Spec: BindingSpec{
Source: "test",
Destination: "test",
DestinationType: "queue",
RoutingKey: "not-allowed",
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "some-cluster",
Namespace: "default",
},
},
}
newBinding := oldBinding.DeepCopy()
newBinding.Spec.RoutingKey = "not-allowed"
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
})

It("does not allow updates on binding arguments", func() {
newBinding := Binding{
ObjectMeta: metav1.ObjectMeta{
Name: "update-binding",
},
Spec: BindingSpec{
Source: "test",
Destination: "test",
DestinationType: "queue",
Arguments: &runtime.RawExtension{
Raw: []byte(`{"new":"new-value"}`),
},
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "some-cluster",
Namespace: "default",
},
},
}
newBinding := oldBinding.DeepCopy()
newBinding.Spec.Arguments = &runtime.RawExtension{Raw: []byte(`{"new":"new-value"}`)}
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
})

It("does not allow updates on RabbitmqClusterReference", func() {
newBinding := Binding{
ObjectMeta: metav1.ObjectMeta{
Name: "update-binding",
},
Spec: BindingSpec{
Source: "test",
Destination: "test",
DestinationType: "queue",
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "new-cluster",
Namespace: "default",
},
},
newBinding := oldBinding.DeepCopy()
newBinding.Spec.RabbitmqClusterReference = RabbitmqClusterReference{
Name: "new-cluster",
Namespace: "default",
}
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
})
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func init() {
_ = rabbitmqv1beta1.AddToScheme(scheme)

_ = topologyv1alpha1.AddToScheme(scheme)
_ = rabbitmqcomv1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}

Expand Down
2 changes: 1 addition & 1 deletion system_tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/messaging-topology-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down

0 comments on commit 9cbea51

Please sign in to comment.