3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform;
22 import java.util.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.config.internal.MapSettings;
26 import org.sonar.api.utils.MessageException;
27 import org.sonar.api.utils.log.LogTester;
28 import org.sonar.api.utils.log.LoggerLevel;
29 import org.sonar.server.platform.db.migration.version.DatabaseVersion;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.assertThatThrownBy;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
36 public class DatabaseServerCompatibilityTest {
39 public LogTester logTester = new LogTester();
40 private MapSettings settings = new MapSettings();
43 public void fail_if_requires_downgrade() {
44 DatabaseVersion version = mock(DatabaseVersion.class);
45 when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE);
46 var config = settings.asConfig();
47 var compatibility = new DatabaseServerCompatibility(version, config);
48 assertThatThrownBy(compatibility::start)
49 .isInstanceOf(MessageException.class)
50 .hasMessage("Database was upgraded to a more recent version of SonarQube. "
51 + "A backup must probably be restored or the DB settings are incorrect.");
55 public void fail_if_requires_firstly_to_upgrade_to_lts() {
56 DatabaseVersion version = mock(DatabaseVersion.class);
57 when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
58 when(version.getVersion()).thenReturn(Optional.of(12L));
59 var config = settings.asConfig();
60 var compatibility = new DatabaseServerCompatibility(version, config);
61 assertThatThrownBy(compatibility::start)
62 .isInstanceOf(MessageException.class)
63 .hasMessage("The version of SonarQube is too old. Please upgrade to the Long Term Support version first.");
67 public void log_warning_if_requires_upgrade() {
68 DatabaseVersion version = mock(DatabaseVersion.class);
69 when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
70 when(version.getVersion()).thenReturn(Optional.of(DatabaseVersion.MIN_UPGRADE_VERSION));
71 new DatabaseServerCompatibility(version, settings.asConfig()).start();
73 assertThat(logTester.logs()).hasSize(4);
74 assertThat(logTester.logs(LoggerLevel.WARN)).contains(
75 "The database must be manually upgraded. Please backup the database and browse /setup. "
76 + "For more information: https://docs.sonarqube.org/latest/setup/upgrading",
77 "################################################################################",
78 "The database must be manually upgraded. Please backup the database and browse /setup. "
79 + "For more information: https://docs.sonarqube.org/latest/setup/upgrading",
80 "################################################################################");
84 public void do_nothing_if_up_to_date() {
85 DatabaseVersion version = mock(DatabaseVersion.class);
86 when(version.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE);
87 new DatabaseServerCompatibility(version, settings.asConfig()).start();
92 public void upgrade_automatically_if_blue_green_deployment() {
93 settings.setProperty("sonar.blueGreenEnabled", "true");
94 DatabaseVersion version = mock(DatabaseVersion.class);
95 when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
96 when(version.getVersion()).thenReturn(Optional.of(DatabaseVersion.MIN_UPGRADE_VERSION));
98 new DatabaseServerCompatibility(version, settings.asConfig()).start();
100 assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();