Skip to content

Commit

Permalink
HDDS-1480. Add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Patel <ptlrshb@gmail.com>
  • Loading branch information
ptlrs committed Nov 27, 2024
1 parent 8c9fbd9 commit 85546fa
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static org.apache.hadoop.hdds.HddsUtils.processForDebug;
import static org.apache.hadoop.ozone.container.ContainerTestHelper.getDummyCommandRequestProto;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
Expand Down Expand Up @@ -100,6 +101,13 @@ public void testDatanodeIDPersistent(@TempDir File tempDir) throws Exception {
InetAddress mockedInetAddress = mock(InetAddress.class);
mockedStaticInetAddress.when(() -> InetAddress.getByName(id1.getHostName()))
.thenReturn(mockedInetAddress);

// If persisted ip address is different from resolved ip address,
// DatanodeDetails should be set with the resolved ip address
when(mockedInetAddress.getHostAddress())
.thenReturn("127.0.0.1");
assertWriteReadWithChangedIpAddress(tempDir, id1);

when(mockedInetAddress.getHostAddress())
.thenReturn(id1.getIpAddress());

Expand Down Expand Up @@ -146,6 +154,17 @@ private void assertWriteRead(@TempDir File tempDir,
assertEquals(details.getCurrentVersion(), read.getCurrentVersion());
}

private void assertWriteReadWithChangedIpAddress(@TempDir File tempDir,
DatanodeDetails details) throws IOException {
// Write a single ID to the file and read it out
File file = new File(tempDir, "valid-values.id");
ContainerUtils.writeDatanodeDetailsTo(details, file, conf);

DatanodeDetails read = ContainerUtils.readDatanodeDetailsFrom(file);

assertNotEquals(details.toString(), read.toString());
}

private void createMalformedIDFile(File malformedFile)
throws IOException {
DatanodeDetails id = randomDatanodeDetails();
Expand Down

0 comments on commit 85546fa

Please sign in to comment.