]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/InfoLogLevelTest.java
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / InfoLogLevelTest.java
1 package org.rocksdb;
2
3 import org.junit.ClassRule;
4 import org.junit.Rule;
5 import org.junit.Test;
6 import org.junit.rules.TemporaryFolder;
7 import org.rocksdb.util.Environment;
8
9 import java.io.IOException;
10
11 import static java.nio.file.Files.readAllBytes;
12 import static java.nio.file.Paths.get;
13 import static org.assertj.core.api.Assertions.assertThat;
14
15 public class InfoLogLevelTest {
16
17 @ClassRule
18 public static final RocksMemoryResource rocksMemoryResource =
19 new RocksMemoryResource();
20
21 @Rule
22 public TemporaryFolder dbFolder = new TemporaryFolder();
23
24 @Test
25 public void testInfoLogLevel() throws RocksDBException,
26 IOException {
27 try (final RocksDB db =
28 RocksDB.open(dbFolder.getRoot().getAbsolutePath())) {
29 db.put("key".getBytes(), "value".getBytes());
30 db.flush(new FlushOptions().setWaitForFlush(true));
31 assertThat(getLogContentsWithoutHeader()).isNotEmpty();
32 }
33 }
34
35 @Test
36 public void testFatalLogLevel() throws RocksDBException,
37 IOException {
38 try (final Options options = new Options().
39 setCreateIfMissing(true).
40 setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
41 final RocksDB db = RocksDB.open(options,
42 dbFolder.getRoot().getAbsolutePath())) {
43 assertThat(options.infoLogLevel()).
44 isEqualTo(InfoLogLevel.FATAL_LEVEL);
45 db.put("key".getBytes(), "value".getBytes());
46 // As InfoLogLevel is set to FATAL_LEVEL, here we expect the log
47 // content to be empty.
48 assertThat(getLogContentsWithoutHeader()).isEmpty();
49 }
50 }
51
52 @Test
53 public void testFatalLogLevelWithDBOptions()
54 throws RocksDBException, IOException {
55 try (final DBOptions dbOptions = new DBOptions().
56 setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
57 final Options options = new Options(dbOptions,
58 new ColumnFamilyOptions()).
59 setCreateIfMissing(true);
60 final RocksDB db =
61 RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
62 assertThat(dbOptions.infoLogLevel()).
63 isEqualTo(InfoLogLevel.FATAL_LEVEL);
64 assertThat(options.infoLogLevel()).
65 isEqualTo(InfoLogLevel.FATAL_LEVEL);
66 db.put("key".getBytes(), "value".getBytes());
67 assertThat(getLogContentsWithoutHeader()).isEmpty();
68 }
69 }
70
71 @Test(expected = IllegalArgumentException.class)
72 public void failIfIllegalByteValueProvided() {
73 InfoLogLevel.getInfoLogLevel((byte) -1);
74 }
75
76 @Test
77 public void valueOf() {
78 assertThat(InfoLogLevel.valueOf("DEBUG_LEVEL")).
79 isEqualTo(InfoLogLevel.DEBUG_LEVEL);
80 }
81
82 /**
83 * Read LOG file contents into String.
84 *
85 * @return LOG file contents as String.
86 * @throws IOException if file is not found.
87 */
88 private String getLogContentsWithoutHeader() throws IOException {
89 final String separator = Environment.isWindows() ?
90 "\n" : System.getProperty("line.separator");
91 final String[] lines = new String(readAllBytes(get(
92 dbFolder.getRoot().getAbsolutePath() + "/LOG"))).split(separator);
93
94 int first_non_header = lines.length;
95 // Identify the last line of the header
96 for (int i = lines.length - 1; i >= 0; --i) {
97 if (lines[i].indexOf("DB pointer") >= 0) {
98 first_non_header = i + 1;
99 break;
100 }
101 }
102 StringBuilder builder = new StringBuilder();
103 for (int i = first_non_header; i < lines.length; ++i) {
104 builder.append(lines[i]).append(separator);
105 }
106 return builder.toString();
107 }
108 }