3 * Copyright (C) 2009-2021 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.junit.rules.ExpectedException;
26 import org.sonar.api.config.internal.MapSettings;
27 import org.sonar.api.utils.MessageException;
28 import org.sonar.api.utils.log.LogTester;
29 import org.sonar.api.utils.log.LoggerLevel;
30 import org.sonar.server.platform.db.migration.version.DatabaseVersion;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
36 public class DatabaseServerCompatibilityTest {
39 public ExpectedException thrown = ExpectedException.none();
41 public LogTester logTester = new LogTester();
43 public ExpectedException expectedException = ExpectedException.none();
44 private MapSettings settings = new MapSettings();
47 public void fail_if_requires_downgrade() {
48 thrown.expect(MessageException.class);
49 thrown.expectMessage("Database was upgraded to a more recent version of SonarQube. "
50 + "A backup must probably be restored or the DB settings are incorrect.");
52 DatabaseVersion version = mock(DatabaseVersion.class);
53 when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE);
54 new DatabaseServerCompatibility(version, settings.asConfig()).start();
58 public void fail_if_requires_firstly_to_upgrade_to_lts() {
59 thrown.expect(MessageException.class);
60 thrown.expectMessage("Current version is too old. Please upgrade to Long Term Support version firstly.");
62 DatabaseVersion version = mock(DatabaseVersion.class);
63 when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
64 when(version.getVersion()).thenReturn(Optional.of(12L));
65 new DatabaseServerCompatibility(version, settings.asConfig()).start();
69 public void log_warning_if_requires_upgrade() {
70 DatabaseVersion version = mock(DatabaseVersion.class);
71 when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
72 when(version.getVersion()).thenReturn(Optional.of(DatabaseVersion.MIN_UPGRADE_VERSION));
73 new DatabaseServerCompatibility(version, settings.asConfig()).start();
75 assertThat(logTester.logs()).hasSize(4);
76 assertThat(logTester.logs(LoggerLevel.WARN)).contains(
77 "The database must be manually upgraded. Please backup the database and browse /setup. "
78 + "For more information: https://docs.sonarqube.org/latest/setup/upgrading",
79 "################################################################################",
80 "The database must be manually upgraded. Please backup the database and browse /setup. "
81 + "For more information: https://docs.sonarqube.org/latest/setup/upgrading",
82 "################################################################################");
86 public void do_nothing_if_up_to_date() {
87 DatabaseVersion version = mock(DatabaseVersion.class);
88 when(version.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE);
89 new DatabaseServerCompatibility(version, settings.asConfig()).start();
94 public void upgrade_automatically_if_blue_green_deployment() {
95 settings.setProperty("sonar.blueGreenEnabled", "true");
96 DatabaseVersion version = mock(DatabaseVersion.class);
97 when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
98 when(version.getVersion()).thenReturn(Optional.of(DatabaseVersion.MIN_UPGRADE_VERSION));
100 new DatabaseServerCompatibility(version, settings.asConfig()).start();
102 assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();