|
1 | 1 | package root.repository.implement; |
2 | 2 |
|
3 | 3 | 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; |
7 | 13 |
|
8 | | -import org.apache.commons.io.IOUtils; |
9 | 14 | import org.junit.jupiter.api.AfterAll; |
10 | 15 | import org.junit.jupiter.api.BeforeAll; |
11 | | -import org.junit.jupiter.api.BeforeEach; |
12 | 16 | 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; |
14 | 21 |
|
15 | 22 | import root.common.server.implement.AlertLogCommand; |
| 23 | +import root.common.server.implement.JschConnectionInfo; |
16 | 24 | import root.common.server.implement.JschServer; |
| 25 | +import root.common.server.implement.ServerOS; |
| 26 | +import root.core.domain.OSDiskUsage; |
17 | 27 | import root.core.repository.constracts.ServerMonitoringRepository; |
18 | 28 |
|
19 | 29 | public class LinuxServerMonitoringRepositoryTest { |
20 | 30 |
|
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; |
26 | 33 |
|
27 | 34 | public static String alertLogString = ""; |
28 | | - |
29 | 35 | public static String[] alertLogLines; |
30 | 36 |
|
31 | 37 | @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 | + } |
63 | 44 | alertLogLines = alertLogString.split("\n"); |
64 | | - } |
65 | 45 |
|
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(); |
69 | 55 | 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 | + } |
70 | 70 | } |
71 | 71 |
|
72 | 72 | @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); |
75 | 87 | } |
76 | 88 |
|
77 | 89 | @Test |
78 | | - public void checkAlertLogTest() throws Exception { |
| 90 | + public void testGetAlertLogFileLineCount() { |
79 | 91 | // 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"); |
83 | 93 |
|
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"); |
86 | 105 |
|
87 | 106 | // Act |
88 | 107 | String result = repo.checkAlertLog(alc); |
89 | 108 |
|
90 | 109 | // Assert |
91 | | - assertEquals(result, alertLogString); |
| 110 | + assertEquals(alertLogString, result); |
92 | 111 | } |
93 | 112 |
|
94 | 113 | @Test |
95 | | - public void getAlertLogFileLineCountTest() throws Exception { |
| 114 | + public void testCheckAlertLog_Last10Lines() throws Exception { |
96 | 115 | // 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"); |
100 | 117 |
|
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 | + } |
103 | 131 |
|
| 132 | + @Test |
| 133 | + public void testCheckOSDiskUsage() { |
| 134 | + // Arrange |
104 | 135 | // Act |
105 | | - int lineCount = repo.getAlertLogFileLineCount(alc); |
| 136 | + List<OSDiskUsage> result = repo.checkOSDiskUsage(); |
106 | 137 |
|
107 | 138 | // Assert |
108 | | - assertEquals(lineCount, alertLogLines.length); |
| 139 | + assertTrue(result.size() != 0); |
109 | 140 | } |
110 | 141 | } |
0 commit comments