aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-main/src/test/java/org/sonar/application/process/StreamGobblerTest.java
blob: 68fd65b51b5cfbc702d3eb2c629504f0fb549de5 (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
/*
 * 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.process;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.sonar.application.config.AppSettings;
import org.sonar.process.Props;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.sonar.process.ProcessProperties.Property.LOG_JSON_OUTPUT;

public class StreamGobblerTest {

  private AppSettings appSettings = mock(AppSettings.class);
  private Props props = mock(Props.class);

  @Before
  public void before() {
    when(props.valueAsBoolean(LOG_JSON_OUTPUT.getKey(), false)).thenReturn(false);
    when(appSettings.getProps()).thenReturn(props);
  }

  @Test
  public void forward_stream_to_log() {
    InputStream stream = IOUtils.toInputStream("one\nsecond log\nthird log\n", StandardCharsets.UTF_8);
    Logger logger = mock(Logger.class);
    Logger startupLogger = mock(Logger.class);

    StreamGobbler gobbler = new StreamGobbler(stream, "WEB", appSettings, logger, startupLogger);
    verifyNoInteractions(logger);

    gobbler.start();
    StreamGobbler.waitUntilFinish(gobbler);

    verify(logger).info("one");
    verify(logger).info("second log");
    verify(logger).info("third log");
    verifyNoMoreInteractions(logger);
    verifyNoInteractions(startupLogger);
  }

  @Test
  public void startupLogIsLoggedWhenJSONFormatIsNotActive() {
    InputStream stream = IOUtils.toInputStream("[startup] Admin is still using default credentials\nsecond log\n",
      StandardCharsets.UTF_8);
    Logger startupLogger = mock(Logger.class);
    Logger logger = mock(Logger.class);

    StreamGobbler gobbler = new StreamGobbler(stream, "WEB", appSettings, logger, startupLogger);
    verifyNoInteractions(startupLogger);

    gobbler.start();
    StreamGobbler.waitUntilFinish(gobbler);

    verify(startupLogger).warn("Admin is still using default credentials");
    verifyNoMoreInteractions(startupLogger);
  }

  /*
   * This is scenario for known limitation of our approach when we detect more than we should - logs here are not really coming
   * from a startup log from subprocess but from some other log but the message contains '[startup]'
   */
  @Test
  public void startupLogIsLoggedWhenJSONFormatNotActiveAndMatchingStringIsIntMiddleOfTheTest() {
    InputStream stream = IOUtils.toInputStream("Some other not [startup] log\nsecond log\n",
      StandardCharsets.UTF_8);
    Logger startupLogger = mock(Logger.class);
    Logger logger = mock(Logger.class);

    StreamGobbler gobbler = new StreamGobbler(stream, "WEB", appSettings, logger, startupLogger);
    verifyNoInteractions(startupLogger);

    gobbler.start();
    StreamGobbler.waitUntilFinish(gobbler);

    verify(startupLogger).warn("log");
    verifyNoMoreInteractions(startupLogger);
  }

  @Test
  public void startupLogIsLoggedWhenJSONFormatIsActive() {
    when(props.valueAsBoolean(LOG_JSON_OUTPUT.getKey(), false)).thenReturn(true);
    InputStream stream = IOUtils.toInputStream("{ \"logger\": \"startup\", \"message\": \"Admin is still using default credentials\"}\n",
      StandardCharsets.UTF_8);
    Logger startupLogger = mock(Logger.class);
    Logger logger = mock(Logger.class);

    StreamGobbler gobbler = new StreamGobbler(stream, "WEB", appSettings, logger, startupLogger);
    verifyNoInteractions(startupLogger);

    gobbler.start();
    StreamGobbler.waitUntilFinish(gobbler);

    verify(startupLogger).warn("Admin is still using default credentials");
    verifyNoMoreInteractions(startupLogger);
  }

  @Test
  public void startupLogIsNotLoggedWhenJSONFormatIsActiveAndLogHasWrongName() {
    when(props.valueAsBoolean(LOG_JSON_OUTPUT.getKey(), false)).thenReturn(true);
    InputStream stream = IOUtils.toInputStream("{ \"logger\": \"wrong-logger\", \"message\": \"Admin 'startup' is still using default credentials\"}\n",
      StandardCharsets.UTF_8);
    Logger startupLogger = mock(Logger.class);
    Logger logger = mock(Logger.class);

    StreamGobbler gobbler = new StreamGobbler(stream, "WEB", appSettings, logger, startupLogger);
    verifyNoInteractions(startupLogger);

    gobbler.start();
    StreamGobbler.waitUntilFinish(gobbler);

    verifyNoMoreInteractions(startupLogger);
  }
}