Skip to content

Commit

Permalink
Add helper GenerateBindingInfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Mar 23, 2021
1 parent d5c3710 commit 4eb332b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 72 deletions.
21 changes: 21 additions & 0 deletions internal/binding_info.go → internal/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
rabbithole "github.com/michaelklishin/rabbit-hole/v2"
topologyv1alpha1 "github.com/rabbitmq/messaging-topology-operator/api/v1alpha1"
"strings"
)

func GenerateBindingInfo(binding *topologyv1alpha1.Binding) (*rabbithole.BindingInfo, error) {
Expand All @@ -33,3 +34,23 @@ func GenerateBindingInfo(binding *topologyv1alpha1.Binding) (*rabbithole.Binding
Arguments: arguments,
}, nil
}

// Generate binding properties key which is necessary when deleting a binding
// Binding properties key is:
// when routing key and argument are not provided, properties key is "~"
// when routing key is set and no argument is provided, properties key is the routing key itself
// if routing key has character '~', it's replaced by '%7E'
// when arguments are provided, properties key is the routing key (could be empty) plus the hash of arguments
// the hash function used is 'erlang:phash2' and it's erlang specific; GeneratePropertiesKey returns empty
// string if arguments are provided (deletion not supported)

func GeneratePropertiesKey(binding *topologyv1alpha1.Binding) string {
if binding.Spec.RoutingKey == "" {
return "~"
}
if binding.Spec.Arguments == nil {
return strings.ReplaceAll(binding.Spec.RoutingKey, "~", "%7E")
}

return ""
}
72 changes: 0 additions & 72 deletions internal/binding_info_test.go

This file was deleted.

109 changes: 109 additions & 0 deletions internal/binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package internal_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
topologyv1alpha1 "github.com/rabbitmq/messaging-topology-operator/api/v1alpha1"
"github.com/rabbitmq/messaging-topology-operator/internal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

var _ = Describe("Binding", func() {
var binding *topologyv1alpha1.Binding
Context("GenerateBindingInfo", func() {
BeforeEach(func() {
binding = &topologyv1alpha1.Binding{
ObjectMeta: metav1.ObjectMeta{
Name: "exchange",
},
Spec: topologyv1alpha1.BindingSpec{
Vhost: "/avhost",
Source: "test-exchange",
Destination: "test-queue",
DestinationType: "queue",
RoutingKey: "a-key",
},
}
})

It("sets the correct vhost", func() {
info, err := internal.GenerateBindingInfo(binding)
Expect(err).NotTo(HaveOccurred())
Expect(info.Vhost).To(Equal("/avhost"))
})

It("sets the correct source", func() {
info, err := internal.GenerateBindingInfo(binding)
Expect(err).NotTo(HaveOccurred())
Expect(info.Source).To(Equal("test-exchange"))
})

It("sets the correct destination", func() {
info, err := internal.GenerateBindingInfo(binding)
Expect(err).NotTo(HaveOccurred())
Expect(info.Destination).To(Equal("test-queue"))
})

It("sets the correct destination type", func() {
info, err := internal.GenerateBindingInfo(binding)
Expect(err).NotTo(HaveOccurred())
Expect(info.DestinationType).To(Equal("queue"))
})

It("sets the correct routing key", func() {
info, err := internal.GenerateBindingInfo(binding)
Expect(err).NotTo(HaveOccurred())
Expect(info.RoutingKey).To(Equal("a-key"))
})

When("exchange arguments are provided", func() {
It("generates the correct exchange arguments", func() {
binding.Spec.Arguments = &runtime.RawExtension{
Raw: []byte(`{"argument": "argument-value"}`),
}
info, err := internal.GenerateBindingInfo(binding)
Expect(err).NotTo(HaveOccurred())
Expect(info.Arguments).To(HaveLen(1))
Expect(info.Arguments).To(HaveKeyWithValue("argument", "argument-value"))
})
})
})

Context("GeneratePropertiesKey", func() {
BeforeEach(func() {
binding = &topologyv1alpha1.Binding{
ObjectMeta: metav1.ObjectMeta{
Name: "exchange",
},
Spec: topologyv1alpha1.BindingSpec{
Vhost: "/avhost",
Source: "test-exchange",
Destination: "test-queue",
DestinationType: "queue",
},
}
})

When("routing key is not set", func() {
It("returns the default properties key value", func() {
propertiesKey := internal.GeneratePropertiesKey(binding)
Expect(propertiesKey).To(Equal("~"))
})
})

When("routing key is set", func() {
It("returns the routing key as properties key", func() {
binding.Spec.RoutingKey = "a-great-routing-key"
propertiesKey := internal.GeneratePropertiesKey(binding)
Expect(propertiesKey).To(Equal("a-great-routing-key"))
})

It("replaces character '~' if it's in the routing key", func() {
binding.Spec.RoutingKey = "special~routing~key"
propertiesKey := internal.GeneratePropertiesKey(binding)
Expect(propertiesKey).To(Equal("special%7Erouting%7Ekey"))
})
})
})
})

0 comments on commit 4eb332b

Please sign in to comment.