]> source.dussan.org Git - sonarqube.git/blob
c806766d108f915fb121d9920fb5434b4cf0b071
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform;
21
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;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35
36 public class DatabaseServerCompatibilityTest {
37
38   @Rule
39   public ExpectedException thrown = ExpectedException.none();
40   @Rule
41   public LogTester logTester = new LogTester();
42   @Rule
43   public ExpectedException expectedException = ExpectedException.none();
44   private MapSettings settings = new MapSettings();
45
46   @Test
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.");
51
52     DatabaseVersion version = mock(DatabaseVersion.class);
53     when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE);
54     new DatabaseServerCompatibility(version, settings.asConfig()).start();
55   }
56
57   @Test
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.");
61
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();
66   }
67
68   @Test
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();
74
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         "################################################################################");
83   }
84
85   @Test
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();
90     // no error
91   }
92
93   @Test
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));
99
100     new DatabaseServerCompatibility(version, settings.asConfig()).start();
101
102     assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
103   }
104 }