Skip to content

Commit b257552

Browse files
committed
TDD: Write Linux server monitoring test code
1 parent 110d43f commit b257552

File tree

2 files changed

+102
-65
lines changed

2 files changed

+102
-65
lines changed

Repository/src/main/java/root/repository/implement/LinuxServerMonitoringRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
3232
try {
3333
String command = String.format("cat %s | wc -l", alc.getReadFilePath());
3434
String executeResult = jsch.executeCommand(command);
35-
fileLineCnt = Integer.parseInt(executeResult);
35+
StringTokenizer st = new StringTokenizer(executeResult);
36+
String lastToken = "0";
37+
while (st.hasMoreTokens()) {
38+
lastToken = st.nextToken();
39+
}
40+
41+
fileLineCnt = Integer.parseInt(lastToken) + 1;
3642
} catch (Exception e) {
3743
log.error(e.getMessage());
3844
}
Lines changed: 95 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,141 @@
11
package root.repository.implement;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.mockito.Mockito.mock;
5-
import static org.mockito.Mockito.mockStatic;
6-
import static org.mockito.Mockito.when;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.fail;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
12+
import java.util.List;
713

8-
import org.apache.commons.io.IOUtils;
914
import org.junit.jupiter.api.AfterAll;
1015
import org.junit.jupiter.api.BeforeAll;
11-
import org.junit.jupiter.api.BeforeEach;
1216
import org.junit.jupiter.api.Test;
13-
import org.mockito.MockedStatic;
17+
18+
import com.jcraft.jsch.Channel;
19+
import com.jcraft.jsch.ChannelSftp;
20+
import com.jcraft.jsch.Session;
1421

1522
import root.common.server.implement.AlertLogCommand;
23+
import root.common.server.implement.JschConnectionInfo;
1624
import root.common.server.implement.JschServer;
25+
import root.common.server.implement.ServerOS;
26+
import root.core.domain.OSDiskUsage;
1727
import root.core.repository.constracts.ServerMonitoringRepository;
1828

1929
public class LinuxServerMonitoringRepositoryTest {
2030

21-
public JschServer jschServer;
22-
23-
public ServerMonitoringRepository repo;
24-
25-
public static MockedStatic<IOUtils> ioUtilsMock;
31+
public static JschServer jschServer;
32+
public static ServerMonitoringRepository repo;
2633

2734
public static String alertLogString = "";
28-
2935
public static String[] alertLogLines;
3036

3137
@BeforeAll
32-
public static void before() {
33-
ioUtilsMock = mockStatic(IOUtils.class);
34-
StringBuffer sb = new StringBuffer();
35-
sb.append("2022-03-24T13:38:35.065184+09:00").append("\n");
36-
sb.append("Thread 1 advanced to log sequence 7606 (LGWR switch)").append("\n");
37-
sb.append(" Current log# 5 seq# 7606 mem# 0: +REDO/1003346093").append("\n");
38-
sb.append("2022-03-24T13:39:00.180206+09:00").append("\n");
39-
sb.append("Archived Log entry 13398 added for T-1.S-7605 ID 0x8155080b LAD:1").append("\n");
40-
sb.append("2022-03-25T14:24:57.291344+09:00").append("\n");
41-
sb.append("Session (223,56276): RECO logon successful: Inbound connection from client").append("\n");
42-
sb.append("Session (223,56276): RECO logon successful: DB Logon User: RECO, ").append("\n");
43-
sb.append("Session (223,56276): RECO logon successful: Client IP Address: -").append("\n");
44-
sb.append("2022-03-26T18:04:53.572965+09:00").append("\n");
45-
sb.append("Thread 1 advanced to log sequence 7607 (LGWR switch)").append("\n");
46-
sb.append(" Current log# 1 seq# 7607 mem# 0: +REDO/DBERP/ONLINELOG/group_1.257.966360593").append("\n");
47-
sb.append("2022-03-27T18:05:16.929231+09:00").append("\n");
48-
sb.append("Archived Log entry 13400 added for T-1.S-7606 ID 0x8155080b LAD:1").append("\n");
49-
sb.append("2022-03-28T00:02:21.629284+09:00").append("\n");
50-
sb.append("TABLE SYS.WRI$_OPTSTAT_HISTHEAD_HISTORY: ADDED INTERVAL PARTITION SYS_P38333)").append("\n");
51-
sb.append("TABLE SYS.WRI$_OPTSTAT_HISTGRM_HISTORY: ADDED INTERVAL PARTITION SYS_P38336)").append("\n");
52-
sb.append("2022-03-28T01:00:18.971650+09:00").append("\n");
53-
sb.append("ALTER SYSTEM ARCHIVE LOG").append("\n");
54-
sb.append("2022-03-29T01:00:18.980968+09:00").append("\n");
55-
sb.append("Thread 1 advanced to log sequence 7608 (LGWR switch)").append("\n");
56-
sb.append(" Current log# 6 seq# 7608 mem# 0: +REDO/1003346037").append("\n");
57-
sb.append("2022-03-30T01:00:38.903080+09:00").append("\n");
58-
sb.append("Archived Log entry 13401 added for T-1.S-7607 ID 0x8155080b LAD:1").append("\n");
59-
sb.append("2022-03-31T02:56:51.984291+09:00").append("\n");
60-
sb.append("Starting control autobackup").append("\n");
61-
alertLogString = sb.toString();
62-
38+
public static void setup() {
39+
// Load alert log test file
40+
try {
41+
alertLogString = Files.readString(Paths.get("src/test/resources/alertlog_sample.txt"));
42+
} catch (IOException e) {
43+
}
6344
alertLogLines = alertLogString.split("\n");
64-
}
6545

66-
@BeforeEach
67-
public void setup() {
68-
jschServer = mock(JschServer.class);
46+
String serverName = "testServer";
47+
ServerOS serverOS = ServerOS.LINUX;
48+
String host = "192.168.109.129";
49+
int port = 22;
50+
String id = "dky";
51+
String password = "ehruddbs1!";
52+
53+
jschServer = new JschServer(new JschConnectionInfo(serverName, serverOS, host, port, id, password));
54+
jschServer.init();
6955
repo = new LinuxServerMonitoringRepository(jschServer);
56+
57+
// Send test alert log file to remote server
58+
try {
59+
Session session = jschServer.getSession();
60+
Channel channel = session.openChannel("sftp");
61+
channel.connect();
62+
ChannelSftp channelSftp = (ChannelSftp) channel;
63+
InputStream is = new ByteArrayInputStream(alertLogString.getBytes());
64+
channelSftp.put(is, "/home/dky/test.txt");
65+
channelSftp.disconnect();
66+
channel.disconnect();
67+
} catch (Exception e) {
68+
fail(e.getMessage());
69+
}
7070
}
7171

7272
@AfterAll
73-
public static void after() {
74-
ioUtilsMock.close();
73+
public static void teardown() {
74+
try {
75+
jschServer.executeCommand("rm -rf /home/dky/test.txt");
76+
} catch (Exception e) {
77+
}
78+
}
79+
80+
@Test
81+
public void testGetServerName() {
82+
// Act
83+
String result = repo.getServerName();
84+
85+
// Assert
86+
assertEquals("testServer", result);
7587
}
7688

7789
@Test
78-
public void checkAlertLogTest() throws Exception {
90+
public void testGetAlertLogFileLineCount() {
7991
// Arrange
80-
AlertLogCommand alc = mock(AlertLogCommand.class);
81-
alc.setReadLine(10);
82-
alc.setReadFilePath("/test/alert_DB.log");
92+
AlertLogCommand alc = new AlertLogCommand(10, "/home/dky/test.txt");
8393

84-
String command = String.format("tail -%d %s", alc.getReadLine(), alc.getReadFilePath());
85-
when(jschServer.executeCommand(command)).thenReturn(alertLogString);
94+
// Act
95+
int lineCount = repo.getAlertLogFileLineCount(alc);
96+
97+
// Assert
98+
assertEquals(alertLogLines.length, lineCount);
99+
}
100+
101+
@Test
102+
public void testCheckAlertLog() throws Exception {
103+
// Arrange
104+
AlertLogCommand alc = new AlertLogCommand(alertLogLines.length, "/home/dky/test.txt");
86105

87106
// Act
88107
String result = repo.checkAlertLog(alc);
89108

90109
// Assert
91-
assertEquals(result, alertLogString);
110+
assertEquals(alertLogString, result);
92111
}
93112

94113
@Test
95-
public void getAlertLogFileLineCountTest() throws Exception {
114+
public void testCheckAlertLog_Last10Lines() throws Exception {
96115
// Arrange
97-
AlertLogCommand alc = mock(AlertLogCommand.class);
98-
alc.setReadLine(10);
99-
alc.setReadFilePath("/test/alert_DB.log");
116+
AlertLogCommand alc = new AlertLogCommand(10, "/home/dky/test.txt");
100117

101-
String command = String.format("cat %s | wc -l", alc.getReadFilePath());
102-
when(jschServer.executeCommand(command)).thenReturn(String.valueOf(alertLogLines.length));
118+
// Act
119+
String result = repo.checkAlertLog(alc);
120+
121+
// Assert
122+
StringBuffer expected = new StringBuffer();
123+
for (int i = alertLogLines.length - 10; i < alertLogLines.length; i++) {
124+
expected.append(alertLogLines[i]);
125+
if(i != alertLogLines.length -1) {
126+
expected.append("\n");
127+
}
128+
}
129+
assertEquals(expected.toString(), result);
130+
}
103131

132+
@Test
133+
public void testCheckOSDiskUsage() {
134+
// Arrange
104135
// Act
105-
int lineCount = repo.getAlertLogFileLineCount(alc);
136+
List<OSDiskUsage> result = repo.checkOSDiskUsage();
106137

107138
// Assert
108-
assertEquals(lineCount, alertLogLines.length);
139+
assertTrue(result.size() != 0);
109140
}
110141
}

0 commit comments

Comments
 (0)