aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-main/src/test/java/org/sonar/application/es/EsLoggingTest.java
blob: 659d52f818f8a06645a09eb2ed2240b5874cdb9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.application.es;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.process.Props;

import static org.assertj.core.api.Assertions.assertThat;

public class EsLoggingTest {
  @Rule
  public TemporaryFolder temporaryFolder = new TemporaryFolder();

  private final EsLogging underTest = new EsLogging();

  @Test
  public void createProperties_with_empty_props() throws IOException {
    File logDir = temporaryFolder.newFolder();
    Properties properties = underTest.createProperties(newProps(), logDir);

    verifyProperties(properties,
      "status", "ERROR",
      "appender.file_es.type", "RollingFile",
      "appender.file_es.name", "file_es",
      "appender.file_es.filePattern", new File(logDir, "es.%d{yyyy-MM-dd}.log").getAbsolutePath(),
      "appender.file_es.fileName", new File(logDir, "es.log").getAbsolutePath(),
      "appender.file_es.layout.type", "PatternLayout",
      "appender.file_es.layout.pattern", "%d{yyyy.MM.dd HH:mm:ss} %-5level es[][%logger{1.}] %msg%n",
      "appender.file_es.policies.type", "Policies",
      "appender.file_es.policies.time.type", "TimeBasedTriggeringPolicy",
      "appender.file_es.policies.time.interval", "1",
      "appender.file_es.policies.time.modulate", "true",
      "appender.file_es.strategy.type", "DefaultRolloverStrategy",
      "appender.file_es.strategy.fileIndex", "nomax",
      "appender.file_es.strategy.action.type", "Delete",
      "appender.file_es.strategy.action.basepath", logDir.getAbsolutePath(),
      "appender.file_es.strategy.action.maxDepth", "1",
      "appender.file_es.strategy.action.condition.type", "IfFileName",
      "appender.file_es.strategy.action.condition.glob", "es*",
      "appender.file_es.strategy.action.condition.nested_condition.type", "IfAccumulatedFileCount",
      "appender.file_es.strategy.action.condition.nested_condition.exceeds", "7",
      "rootLogger.level", "INFO",
      "rootLogger.appenderRef.file_es.ref", "file_es"
    );
  }

  @Test
  public void createProperties_sets_root_logger_to_INFO_if_no_property_is_set() throws IOException {
    File logDir = temporaryFolder.newFolder();
    Properties properties = underTest.createProperties(newProps(), logDir);

    assertThat(properties.getProperty("rootLogger.level")).isEqualTo("INFO");
  }

  @Test
  public void createProperties_sets_root_logger_to_global_property_if_set() throws IOException {
    File logDir = temporaryFolder.newFolder();
    Properties properties = underTest.createProperties(newProps("sonar.log.level", "TRACE"), logDir);

    assertThat(properties.getProperty("rootLogger.level")).isEqualTo("TRACE");
  }

  @Test
  public void createProperties_sets_root_logger_to_process_property_if_set() throws IOException {
    File logDir = temporaryFolder.newFolder();
    Properties properties = underTest.createProperties(newProps("sonar.log.level.es", "DEBUG"), logDir);

    assertThat(properties.getProperty("rootLogger.level")).isEqualTo("DEBUG");
  }

  @Test
  public void createProperties_sets_root_logger_to_process_property_over_global_property_if_both_set() throws IOException {
    File logDir = temporaryFolder.newFolder();
    Properties properties = underTest.createProperties(
      newProps(
        "sonar.log.level", "DEBUG",
        "sonar.log.level.es", "TRACE"),
      logDir);

    assertThat(properties.getProperty("rootLogger.level")).isEqualTo("TRACE");
  }

  @Test
  public void createProperties_adds_nodename_if_cluster_property_is_set() throws IOException {
    File logDir = temporaryFolder.newFolder();
    Properties properties = underTest.createProperties(
      newProps(
        "sonar.cluster.enabled", "true",
          "sonar.cluster.node.name", "my-node"),
      logDir);

    assertThat(properties.getProperty("appender.file_es.layout.pattern")).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level my-node es[][%logger{1.}] %msg%n");
  }

  private static Props newProps(String... propertyKeysAndValues) {
    assertThat(propertyKeysAndValues.length % 2).describedAs("Number of parameters must be even").isZero();
    Properties properties = new Properties();
    for (int i = 0; i < propertyKeysAndValues.length; i++) {
      properties.put(propertyKeysAndValues[i++], propertyKeysAndValues[i]);
    }
    return new Props(properties);
  }

  private void verifyProperties(Properties properties, String... expectedPropertyKeysAndValuesOrdered) {
    if (expectedPropertyKeysAndValuesOrdered.length == 0) {
      assertThat(properties).isEmpty();
    } else {
      assertThat(expectedPropertyKeysAndValuesOrdered.length % 2).describedAs("Number of parameters must be even").isZero();
      Set<String> keys = new HashSet<>(expectedPropertyKeysAndValuesOrdered.length / 2 + 1);
      keys.add("status");
      for (int i = 0; i < expectedPropertyKeysAndValuesOrdered.length; i++) {
        String key = expectedPropertyKeysAndValuesOrdered[i++];
        String value = expectedPropertyKeysAndValuesOrdered[i];
        assertThat(properties.get(key)).describedAs("Unexpected value for property " + key).isEqualTo(value);
        keys.add(key);
      }
      assertThat(properties).containsOnlyKeys(keys.toArray());
    }
  }
}