Skip to content

Commit

Permalink
[cli] Fix output format of string by pulsar-admin command (apache#11878)
Browse files Browse the repository at this point in the history
* Fix output format of list of strings by pulsar-admin command

* Fix test code

* Fix docs
  • Loading branch information
Masahiro Sakamoto authored Oct 25, 2021
1 parent 9137e6b commit 7037c5e
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ <K,V> void print(Map<K,V> items) {

<T> void print(T item) {
try {
System.out.println(writer.writeValueAsString(item));
if (item instanceof String) {
System.out.println(item);
} else {
System.out.println(writer.writeValueAsString(item));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@
*/
package org.apache.pulsar.admin.cli;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.Lists;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.Topics;
import org.apache.pulsar.client.impl.MessageIdImpl;
import org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats.LedgerInfo;
import org.testng.Assert;
Expand Down Expand Up @@ -54,4 +65,27 @@ public void testFindFirstLedgerWithinThreshold() throws Exception {
Assert.assertEquals(CmdTopics.findFirstLedgerWithinThreshold(ledgers, 5000),
new MessageIdImpl(1, 0, -1));
}

@Test
public void testListCmd() throws Exception {
List<String> topicList = Lists.newArrayList("persistent://public/default/t1", "persistent://public/default/t2",
"persistent://public/default/t3");

Topics topics = mock(Topics.class);
doReturn(topicList).when(topics).getList(anyString(), any());

PulsarAdmin admin = mock(PulsarAdmin.class);
when(admin.topics()).thenReturn(topics);

CmdTopics cmd = new CmdTopics(() -> admin);

PrintStream defaultSystemOut = System.out;
try (ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(out)) {
System.setOut(ps);
cmd.run("list public/default".split("\\s+"));
Assert.assertEquals(out.toString(), String.join("\n", topicList) + "\n");
} finally {
System.setOut(defaultSystemOut);
}
}
}
10 changes: 5 additions & 5 deletions site2/docs/io-quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ This sink connector runs as a Pulsar Function and writes the messages produced i
The sink has been created successfully if the following message appears.
```bash
"Created successfully"
Created successfully
```
### Inspect a JDBC sink
Expand Down Expand Up @@ -682,7 +682,7 @@ $ bin/pulsar-admin sinks stop \
The sink instance has been stopped successfully if the following message disappears.
```bash
"Stopped successfully"
Stopped successfully
```
### Restart a JDBC sink
Expand All @@ -704,7 +704,7 @@ $ bin/pulsar-admin sinks restart \
The sink instance has been started successfully if the following message disappears.
```bash
"Started successfully"
Started successfully
```
> #### Tip
Expand Down Expand Up @@ -735,7 +735,7 @@ $ bin/pulsar-admin sinks update \
The sink connector has been updated successfully if the following message disappears.
```bash
"Updated successfully"
Updated successfully
```
This example double-checks the information.
Expand Down Expand Up @@ -794,7 +794,7 @@ $ bin/pulsar-admin sinks delete \
The sink connector has been deleted successfully if the following message appears.
```text
"Deleted successfully"
Deleted successfully
```
This example double-checks the status of the sink connector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String uploadFunction() throws Exception {
};
ContainerExecResult output = pulsarCluster.getAnyWorker().execCmd(commands);
assertEquals(0, output.getExitCode());
assertTrue(output.getStdout().contains("\"Uploaded successfully\""));
assertTrue(output.getStdout().contains("Uploaded successfully"));
return bkPkgPath;
}

Expand All @@ -77,7 +77,7 @@ public void testUploadDownload() throws Exception {
WorkerContainer container = pulsarCluster.getAnyWorker();
ContainerExecResult output = container.execCmd(commands);
assertEquals(0, output.getExitCode());
assertTrue(output.getStdout().contains("\"Downloaded successfully\""));
assertTrue(output.getStdout().contains("Downloaded successfully"));
String[] diffCommand = {
"diff",
PulsarCluster.ADMIN_SCRIPT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void testWindowFunction(String type, String[] expectedResults) throws Exc
};

ContainerExecResult containerExecResult = pulsarCluster.getAnyWorker().execCmd(commands);
assertTrue(containerExecResult.getStdout().contains("\"Created successfully\""));
assertTrue(containerExecResult.getStdout().contains("Created successfully"));

// get function info
getFunctionInfoSuccess(functionName);
Expand Down Expand Up @@ -848,7 +848,7 @@ private <T> void submitFunction(Runtime runtime,
};
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(
commands);
assertTrue(result.getStdout().contains("\"Created successfully\""));
assertTrue(result.getStdout().contains("Created successfully"));

if (StringUtils.isNotEmpty(inputTopicName)) {
ensureSubscriptionCreated(
Expand All @@ -869,7 +869,7 @@ private void updateFunctionParallelism(String functionName, int parallelism) thr
};
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(
commands);
assertTrue(result.getStdout().contains("\"Updated successfully\""));
assertTrue(result.getStdout().contains("Updated successfully"));
}

protected <T> void submitFunction(Runtime runtime,
Expand Down Expand Up @@ -912,7 +912,7 @@ protected <T> void submitFunction(Runtime runtime,
};
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(
commands);
assertTrue(result.getStdout().contains("\"Created successfully\""));
assertTrue(result.getStdout().contains("Created successfully"));
}

private <T> void ensureSubscriptionCreated(String inputTopicName,
Expand Down Expand Up @@ -1514,7 +1514,7 @@ private void submitJavaLoggingFunction(String inputTopicName,
};
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(
commands);
assertTrue(result.getStdout().contains("\"Created successfully\""));
assertTrue(result.getStdout().contains("Created successfully"));

ensureSubscriptionCreated(inputTopicName, String.format("public/default/%s", functionName), schema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private void submitSourceConnector(String sourceName,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = container.execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand All @@ -218,7 +218,7 @@ private void submitSinkConnector(String sinkName,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = container.execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand Down Expand Up @@ -267,7 +267,7 @@ private <T> void submitFunction(Runtime runtime,
};
ContainerExecResult result = container.execCmd(
commands);
assertTrue(result.getStdout().contains("\"Created successfully\""));
assertTrue(result.getStdout().contains("Created successfully"));

ensureSubscriptionCreated(inputTopicName, String.format("public/default/%s", functionName), inputTopicSchema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private void submitSinkConnector(String sinkName,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = container.execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected void submitSinkConnector(@SuppressWarnings("rawtypes") SinkTester test
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand Down Expand Up @@ -212,7 +212,7 @@ protected void updateSinkConnector(@SuppressWarnings("rawtypes") SinkTester test
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(commands);
assertTrue(
result.getStdout().contains("\"Updated successfully\""),
result.getStdout().contains("Updated successfully"),
result.getStdout());
}

Expand Down Expand Up @@ -450,4 +450,4 @@ protected Map<String, String> produceSchemaDeleteMessagesToInputTopic(String inp
}
return kvs;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ protected void submitSourceConnector(String tenant,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void submitSourceConnector(String sourceName,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = container.execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand Down Expand Up @@ -224,4 +224,4 @@ private static void getSourceInfoNotFound(StandaloneContainer container, String
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void submitSourceConnector(String sourceName,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = container.execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand Down Expand Up @@ -255,4 +255,4 @@ private static void getSourceInfoNotFound(StandaloneContainer container, String
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected void submitSourceConnector(SourceTester tester,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand All @@ -162,7 +162,7 @@ protected void updateSourceConnector(SourceTester tester,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(commands);
assertTrue(
result.getStdout().contains("\"Updated successfully\""),
result.getStdout().contains("Updated successfully"),
result.getStdout());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void submitSourceConnector(String sourceName,
log.info("Run command : {}", StringUtils.join(commands, ' '));
ContainerExecResult result = container.execCmd(commands);
assertTrue(
result.getStdout().contains("\"Created successfully\""),
result.getStdout().contains("Created successfully"),
result.getStdout());
}

Expand Down

0 comments on commit 7037c5e

Please sign in to comment.