|
| 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 topic |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/streamnative/pulsarctl/pkg/ctl/cluster" |
| 27 | + "github.com/streamnative/pulsarctl/pkg/ctl/tenant" |
| 28 | + "github.com/streamnative/pulsarctl/pkg/test" |
| 29 | + |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | +) |
| 32 | + |
| 33 | +func TestReplicationClustersCmd(t *testing.T) { |
| 34 | + topicName := fmt.Sprintf("persistent://public/default/test-replication-clusters-topic-%s", test.RandomSuffix()) |
| 35 | + |
| 36 | + // Create the topic first |
| 37 | + args := []string{"create", topicName, "0"} |
| 38 | + _, execErr, _, err := TestTopicCommands(CreateTopicCmd, args) |
| 39 | + assert.Nil(t, err) |
| 40 | + assert.Nil(t, execErr) |
| 41 | + |
| 42 | + // Create a test cluster for replication |
| 43 | + clusterArgs := []string{"create", "test-replication-cluster", "--url", "http://192.168.12.11:8080"} |
| 44 | + _, execErr, _, err = cluster.TestClusterCommands(cluster.CreateClusterCmd, clusterArgs) |
| 45 | + assert.Nil(t, err) |
| 46 | + assert.Nil(t, execErr) |
| 47 | + |
| 48 | + // Update tenant to allow the new cluster |
| 49 | + updateTenantArgs := []string{"update", "--allowed-clusters", "test-replication-cluster", |
| 50 | + "--allowed-clusters", "standalone", "public"} |
| 51 | + _, execErr, _, err = tenant.TestTenantCommands(tenant.UpdateTenantCmd, updateTenantArgs) |
| 52 | + assert.Nil(t, err) |
| 53 | + assert.Nil(t, execErr) |
| 54 | + |
| 55 | + // Set replication clusters for the topic |
| 56 | + setArgs := []string{"set-replication-clusters", topicName, "--clusters", "test-replication-cluster"} |
| 57 | + setOut, execErr, _, _ := TestTopicCommands(SetReplicationClustersCmd, setArgs) |
| 58 | + assert.Nil(t, execErr) |
| 59 | + assert.True(t, strings.Contains(setOut.String(), "Set replication clusters successfully")) |
| 60 | + |
| 61 | + // Get replication clusters for the topic |
| 62 | + getArgs := []string{"get-replication-clusters", topicName} |
| 63 | + getOut, execErr, _, _ := TestTopicCommands(GetReplicationClustersCmd, getArgs) |
| 64 | + assert.Nil(t, execErr) |
| 65 | + |
| 66 | + var clusters []string |
| 67 | + err = json.Unmarshal(getOut.Bytes(), &clusters) |
| 68 | + assert.Nil(t, err) |
| 69 | + assert.Contains(t, clusters, "test-replication-cluster") |
| 70 | + |
| 71 | + // Reset tenant clusters for other test cases |
| 72 | + updateTenantArgs = []string{"update", "--allowed-clusters", "standalone", "public"} |
| 73 | + _, execErr, _, err = tenant.TestTenantCommands(tenant.UpdateTenantCmd, updateTenantArgs) |
| 74 | + assert.Nil(t, err) |
| 75 | + assert.Nil(t, execErr) |
| 76 | +} |
| 77 | + |
| 78 | +func TestSetReplicationClustersArgError(t *testing.T) { |
| 79 | + // Test with no topic name |
| 80 | + args := []string{"set-replication-clusters", "--clusters", "standalone"} |
| 81 | + _, _, nameErr, _ := TestTopicCommands(SetReplicationClustersCmd, args) |
| 82 | + assert.NotNil(t, nameErr) |
| 83 | + assert.Equal(t, "the topic name is not specified or the topic name is specified more than one", nameErr.Error()) |
| 84 | +} |
| 85 | + |
| 86 | +func TestGetReplicationClustersArgError(t *testing.T) { |
| 87 | + // Test with no topic name |
| 88 | + args := []string{"get-replication-clusters"} |
| 89 | + _, _, nameErr, _ := TestTopicCommands(GetReplicationClustersCmd, args) |
| 90 | + assert.NotNil(t, nameErr) |
| 91 | + assert.Equal(t, "the topic name is not specified or the topic name is specified more than one", nameErr.Error()) |
| 92 | +} |
| 93 | + |
| 94 | +func TestSetReplicationClustersInvalidCluster(t *testing.T) { |
| 95 | + topicName := fmt.Sprintf("persistent://public/default/test-invalid-cluster-topic-%s", test.RandomSuffix()) |
| 96 | + |
| 97 | + // Create the topic first |
| 98 | + args := []string{"create", topicName, "0"} |
| 99 | + _, execErr, _, err := TestTopicCommands(CreateTopicCmd, args) |
| 100 | + assert.Nil(t, err) |
| 101 | + assert.Nil(t, execErr) |
| 102 | + |
| 103 | + // Try to set an invalid cluster |
| 104 | + setArgs := []string{"set-replication-clusters", topicName, "--clusters", "invalid-cluster"} |
| 105 | + _, execErr, _, _ = TestTopicCommands(SetReplicationClustersCmd, setArgs) |
| 106 | + assert.NotNil(t, execErr) |
| 107 | +} |
0 commit comments