topic_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright 2021 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v1
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. )
  19. func TestValidateHTTPTopicSpec(t *testing.T) {
  20. topic := &CephBucketTopic{
  21. ObjectMeta: metav1.ObjectMeta{
  22. Name: "fish-topic",
  23. },
  24. Spec: BucketTopicSpec{
  25. OpaqueData: "me@email.com",
  26. Persistent: true,
  27. Endpoint: TopicEndpointSpec{
  28. HTTP: &HTTPEndpointSpec{
  29. URI: "http://myserver:9999",
  30. DisableVerifySSL: false,
  31. SendCloudEvents: false,
  32. },
  33. },
  34. },
  35. }
  36. t.Run("valid", func(t *testing.T) {
  37. err := topic.ValidateTopicSpec()
  38. assert.NoError(t, err)
  39. })
  40. t.Run("invalid endpoint host", func(t *testing.T) {
  41. topic.Spec.Endpoint.HTTP.URI = "http://my server:9999"
  42. err := topic.ValidateTopicSpec()
  43. assert.Error(t, err)
  44. })
  45. t.Run("https host", func(t *testing.T) {
  46. topic.Spec.Endpoint.HTTP.URI = "https://127.0.0.1:9999"
  47. err := topic.ValidateTopicSpec()
  48. assert.NoError(t, err)
  49. })
  50. t.Run("invalid endpoint schema", func(t *testing.T) {
  51. topic.Spec.Endpoint.HTTP.URI = "kaboom://myserver:9999"
  52. err := topic.ValidateTopicSpec()
  53. assert.Error(t, err)
  54. })
  55. }
  56. func TestValidateAMQPTopicSpec(t *testing.T) {
  57. topic := &CephBucketTopic{
  58. ObjectMeta: metav1.ObjectMeta{
  59. Name: "fish-topic",
  60. },
  61. Spec: BucketTopicSpec{
  62. OpaqueData: "me@email.com",
  63. Persistent: true,
  64. Endpoint: TopicEndpointSpec{
  65. AMQP: &AMQPEndpointSpec{
  66. URI: "amqp://myserver:9999",
  67. Exchange: "fish-ex",
  68. DisableVerifySSL: true,
  69. AckLevel: "broker",
  70. },
  71. },
  72. },
  73. }
  74. t.Run("valid", func(t *testing.T) {
  75. err := topic.ValidateTopicSpec()
  76. assert.NoError(t, err)
  77. })
  78. t.Run("amqps host", func(t *testing.T) {
  79. topic.Spec.Endpoint.AMQP.URI = "amqps://myserver:9999"
  80. err := topic.ValidateTopicSpec()
  81. assert.NoError(t, err)
  82. })
  83. t.Run("endpoint schema mismatch", func(t *testing.T) {
  84. topic.Spec.Endpoint.AMQP.URI = "http://myserver:9999"
  85. err := topic.ValidateTopicSpec()
  86. assert.Error(t, err)
  87. })
  88. }
  89. func TestValidateKafkaTopicSpec(t *testing.T) {
  90. topic := &CephBucketTopic{
  91. ObjectMeta: metav1.ObjectMeta{
  92. Name: "fish-topic",
  93. },
  94. Spec: BucketTopicSpec{
  95. OpaqueData: "me@email.com",
  96. Persistent: true,
  97. Endpoint: TopicEndpointSpec{
  98. Kafka: &KafkaEndpointSpec{
  99. URI: "kafka://myserver:9999",
  100. UseSSL: true,
  101. DisableVerifySSL: true,
  102. AckLevel: "broker",
  103. },
  104. },
  105. },
  106. }
  107. t.Run("valid", func(t *testing.T) {
  108. err := topic.ValidateTopicSpec()
  109. assert.NoError(t, err)
  110. })
  111. t.Run("endpoint schema mismatch", func(t *testing.T) {
  112. topic.Spec.Endpoint.Kafka.URI = "http://myserver:9999"
  113. err := topic.ValidateTopicSpec()
  114. assert.Error(t, err)
  115. })
  116. }
  117. func TestInvalidTopicSpec(t *testing.T) {
  118. topic := &CephBucketTopic{
  119. ObjectMeta: metav1.ObjectMeta{
  120. Name: "fish-topic",
  121. },
  122. Spec: BucketTopicSpec{
  123. OpaqueData: "me@email.com",
  124. Persistent: true,
  125. Endpoint: TopicEndpointSpec{
  126. Kafka: &KafkaEndpointSpec{
  127. URI: "kafka://myserver:9999",
  128. UseSSL: true,
  129. DisableVerifySSL: true,
  130. AckLevel: "broker",
  131. },
  132. AMQP: &AMQPEndpointSpec{
  133. URI: "amqp://myserver:9999",
  134. Exchange: "fish-ex",
  135. DisableVerifySSL: true,
  136. AckLevel: "broker",
  137. },
  138. },
  139. },
  140. }
  141. t.Run("too many endpoint specs", func(t *testing.T) {
  142. err := topic.ValidateTopicSpec()
  143. assert.Error(t, err)
  144. })
  145. t.Run("valid", func(t *testing.T) {
  146. topic.Spec.Endpoint.AMQP = nil
  147. err := topic.ValidateTopicSpec()
  148. assert.NoError(t, err)
  149. })
  150. t.Run("too few endpoint specs", func(t *testing.T) {
  151. topic.Spec.Endpoint.Kafka = nil
  152. err := topic.ValidateTopicSpec()
  153. assert.Error(t, err)
  154. })
  155. }