|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package admin |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" |
| 24 | + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest" |
| 25 | + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func ptr(n int) *int { |
| 31 | + return &n |
| 32 | +} |
| 33 | + |
| 34 | +func TestSetTopicAutoCreation(t *testing.T) { |
| 35 | + config := &config.Config{} |
| 36 | + admin, err := New(config) |
| 37 | + require.NoError(t, err) |
| 38 | + require.NotNil(t, admin) |
| 39 | + |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + namespace string |
| 43 | + config utils.TopicAutoCreationConfig |
| 44 | + errReason string |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "Set partitioned type topic auto creation", |
| 48 | + namespace: "public/default", |
| 49 | + config: utils.TopicAutoCreationConfig{ |
| 50 | + Allow: true, |
| 51 | + Type: utils.Partitioned, |
| 52 | + Partitions: ptr(3), |
| 53 | + }, |
| 54 | + errReason: "", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Set partitioned type topic auto creation without partitions", |
| 58 | + namespace: "public/default", |
| 59 | + config: utils.TopicAutoCreationConfig{ |
| 60 | + Allow: true, |
| 61 | + Type: utils.Partitioned, |
| 62 | + }, |
| 63 | + errReason: "Invalid configuration for autoTopicCreationOverride. the detail is [defaultNumPartitions] " + |
| 64 | + "cannot be null when the type is partitioned.", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Set partitioned type topic auto creation with partitions < 1", |
| 68 | + namespace: "public/default", |
| 69 | + config: utils.TopicAutoCreationConfig{ |
| 70 | + Allow: true, |
| 71 | + Type: utils.Partitioned, |
| 72 | + Partitions: ptr(-1), |
| 73 | + }, |
| 74 | + errReason: "Invalid configuration for autoTopicCreationOverride. the detail is [defaultNumPartitions] " + |
| 75 | + "cannot be less than 1 for partition type.", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "Set non-partitioned type topic auto creation", |
| 79 | + namespace: "public/default", |
| 80 | + config: utils.TopicAutoCreationConfig{ |
| 81 | + Allow: true, |
| 82 | + Type: utils.NonPartitioned, |
| 83 | + }, |
| 84 | + errReason: "", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "Set non-partitioned type topic auto creation with partitions", |
| 88 | + namespace: "public/default", |
| 89 | + config: utils.TopicAutoCreationConfig{ |
| 90 | + Allow: true, |
| 91 | + Type: utils.NonPartitioned, |
| 92 | + Partitions: ptr(3), |
| 93 | + }, |
| 94 | + errReason: "Invalid configuration for autoTopicCreationOverride. the detail is [defaultNumPartitions] is " + |
| 95 | + "not allowed to be set when the type is non-partition.", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Disable topic auto creation", |
| 99 | + namespace: "public/default", |
| 100 | + config: utils.TopicAutoCreationConfig{ |
| 101 | + Allow: false, |
| 102 | + }, |
| 103 | + errReason: "", |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "Set topic auto creation on a non-exist namespace", |
| 107 | + namespace: "public/nonexist", |
| 108 | + config: utils.TopicAutoCreationConfig{ |
| 109 | + Allow: true, |
| 110 | + Type: utils.NonPartitioned, |
| 111 | + }, |
| 112 | + errReason: "Namespace does not exist", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "Set topic auto creation on a non-exist tenant", |
| 116 | + namespace: "non-exist/default", |
| 117 | + config: utils.TopicAutoCreationConfig{ |
| 118 | + Allow: true, |
| 119 | + Type: utils.NonPartitioned, |
| 120 | + }, |
| 121 | + errReason: "Tenant does not exist", |
| 122 | + }, |
| 123 | + } |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + namespace, _ := utils.GetNamespaceName(tt.namespace) |
| 127 | + err := admin.Namespaces().SetTopicAutoCreation(*namespace, tt.config) |
| 128 | + if tt.errReason == "" { |
| 129 | + assert.Equal(t, nil, err) |
| 130 | + |
| 131 | + err = admin.Namespaces().RemoveTopicAutoCreation(*namespace) |
| 132 | + assert.Equal(t, nil, err) |
| 133 | + } |
| 134 | + if err != nil { |
| 135 | + restError := err.(rest.Error) |
| 136 | + assert.Equal(t, tt.errReason, restError.Reason) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestGetTopicAutoCreation(t *testing.T) { |
| 143 | + config := &config.Config{} |
| 144 | + admin, err := New(config) |
| 145 | + require.NoError(t, err) |
| 146 | + require.NotNil(t, admin) |
| 147 | + |
| 148 | + namespace, _ := utils.GetNamespaceName("public/default") |
| 149 | + |
| 150 | + // set the topic auto creation config and get it |
| 151 | + err = admin.Namespaces().SetTopicAutoCreation(*namespace, utils.TopicAutoCreationConfig{ |
| 152 | + Allow: true, |
| 153 | + Type: utils.NonPartitioned, |
| 154 | + }) |
| 155 | + assert.Equal(t, nil, err) |
| 156 | + topicAutoCreation, err := admin.Namespaces().GetTopicAutoCreation(*namespace) |
| 157 | + assert.Equal(t, nil, err) |
| 158 | + expected := utils.TopicAutoCreationConfig{ |
| 159 | + Allow: true, |
| 160 | + Type: utils.NonPartitioned, |
| 161 | + } |
| 162 | + assert.Equal(t, expected, *topicAutoCreation) |
| 163 | + |
| 164 | + // remove the topic auto creation config and get it |
| 165 | + err = admin.Namespaces().RemoveTopicAutoCreation(*namespace) |
| 166 | + assert.Equal(t, nil, err) |
| 167 | + |
| 168 | + topicAutoCreation, err = admin.Namespaces().GetTopicAutoCreation(*namespace) |
| 169 | + assert.Equal(t, nil, err) |
| 170 | + expected = utils.TopicAutoCreationConfig{ |
| 171 | + Allow: false, |
| 172 | + Type: "", |
| 173 | + } |
| 174 | + assert.Equal(t, expected, *topicAutoCreation) |
| 175 | +} |
0 commit comments