Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.base.Strings;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -87,7 +88,13 @@ public void execute(ScmClient scmClient) throws IOException {
.getFromProtoBuf(node.getNodeID()),
node.getNodeOperationalStates(0),
node.getNodeStates(0));
printDatanodeInfo(dwa);

if (json) {
List<DatanodeWithAttributes> singleList = Collections.singletonList(dwa);
System.out.println(JsonUtils.toJsonStringWithDefaultPrettyPrinter(singleList));
} else {
printDatanodeInfo(dwa);
}
return;
}
Stream<DatanodeWithAttributes> allNodes = getAllNodes(scmClient).stream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@

package org.apache.hadoop.hdds.scm.cli.datanode;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
Expand All @@ -49,6 +55,7 @@ public class TestListInfoSubcommand {
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private final PrintStream originalErr = System.err;
private final ObjectMapper mapper = new ObjectMapper();
private static final String DEFAULT_ENCODING = StandardCharsets.UTF_8.name();

@BeforeEach
Expand Down Expand Up @@ -99,6 +106,54 @@ public void testDataNodeOperationalStateAndHealthIncludedInOutput()

m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

// ----- with JSON flag -----
outContent.reset();

CommandLine cmdWithJsonFlag = new CommandLine(cmd);
cmdWithJsonFlag.parseArgs("--json");
cmd.execute(scmClient);

String jsonOutput = outContent.toString(DEFAULT_ENCODING);
JsonNode root;
try {
root = mapper.readTree(jsonOutput);
} catch (IOException e) {
fail("Invalid JSON output:\n" + jsonOutput + "\nError: " + e.getMessage());
return;
}

assertTrue(root.isArray(), "JSON output should be an array");
assertEquals(4, root.size(), "Expected 4 nodes in JSON output");

List<String> healthStates = new ArrayList<>();
List<String> operationalStates = new ArrayList<>();
for (JsonNode node : root) {
healthStates.add(node.get("healthState").asText());
operationalStates.add(node.get("opState").asText());
}

// Check expected operational states are present
List<String> expectedOpStates = Arrays.asList("IN_SERVICE", "DECOMMISSIONING");
for (String expectedState : expectedOpStates) {
assertTrue(operationalStates.contains(expectedState),
"Expected operational state: " + expectedState + " but not found");
}

// Check all expected health states are present
for (HddsProtos.NodeState state : HddsProtos.NodeState.values()) {
assertTrue(healthStates.contains(state.toString()),
"Expected health state: " + state + " but not found");
}

// Check order: HEALTHY -> STALE -> DEAD -> HEALTHY_READONLY
List<String> expectedOrder = Arrays.asList("HEALTHY", "STALE", "DEAD", "HEALTHY_READONLY");
int lastIndex = -1;
for (String state : healthStates) {
int index = expectedOrder.indexOf(state);
assertTrue(index >= lastIndex, "Health states not in expected order: " + healthStates);
lastIndex = index;
}
}

@Test
Expand All @@ -125,6 +180,34 @@ public void testDataNodeByUuidOutput()
Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

outContent.reset();

// ----- with JSON flag -----
CommandLine cmdWithJson = new CommandLine(cmd);
cmdWithJson.parseArgs("--id", nodes.get(0).getNodeID().getUuid(), "--json");
cmd.execute(scmClient);

String jsonOutput = outContent.toString(DEFAULT_ENCODING);
JsonNode root;
try {
root = mapper.readTree(jsonOutput);
} catch (IOException e) {
fail("Invalid JSON output:\n" + jsonOutput + "\nError: " + e.getMessage());
return;
}

assertTrue(root.isArray(), "JSON output should be an array");
assertEquals(1, root.size(), "Expected 1 node in JSON output");

JsonNode node = root.get(0);
assertTrue(node.has("datanodeDetails"), "Missing datanodeDetails");
String opState = node.get("opState").asText();
String uuid = node.get("datanodeDetails").get("uuid").asText();

assertEquals("IN_SERVICE", opState, "Expected opState IN_SERVICE but got: " + opState);
assertEquals(nodes.get(0).getNodeID().getUuid(), uuid,
"Expected UUID " + nodes.get(0).getNodeID().getUuid() + " but got: " + uuid);
}

private List<HddsProtos.Node> getNodeDetails() {
Expand Down
Loading