]> source.dussan.org Git - sonarqube.git/blob
f12190b6a76af99864e6d32f0964415d84189d6a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.db.migration;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.sonar.api.utils.log.LogTester;
26 import org.sonar.api.utils.log.LoggerLevel;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.dialect.Dialect;
29 import org.sonar.db.dialect.H2;
30 import org.sonar.db.dialect.MsSql;
31 import org.sonar.db.dialect.Oracle;
32 import org.sonar.db.dialect.PostgreSql;
33 import org.sonar.server.platform.DefaultServerUpgradeStatus;
34 import org.sonar.server.platform.db.migration.engine.MigrationEngine;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.verifyNoInteractions;
40 import static org.mockito.Mockito.when;
41
42 public class AutoDbMigrationTest {
43   @Rule
44   public LogTester logTester = new LogTester();
45
46   private DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
47   private DefaultServerUpgradeStatus serverUpgradeStatus = mock(DefaultServerUpgradeStatus.class);
48   private MigrationEngine migrationEngine = mock(MigrationEngine.class);
49   private AutoDbMigration underTest = new AutoDbMigration(serverUpgradeStatus, migrationEngine);
50
51   @Test
52   public void start_runs_MigrationEngine_on_h2_if_fresh_install() {
53     start_runs_MigrationEngine_for_dialect_if_fresh_install(new H2());
54   }
55
56   @Test
57   public void start_runs_MigrationEngine_on_postgre_if_fresh_install() {
58     start_runs_MigrationEngine_for_dialect_if_fresh_install(new PostgreSql());
59   }
60
61   @Test
62   public void start_runs_MigrationEngine_on_Oracle_if_fresh_install() {
63     start_runs_MigrationEngine_for_dialect_if_fresh_install(new Oracle());
64   }
65
66   @Test
67   public void start_runs_MigrationEngine_on_MsSQL_if_fresh_install() {
68     start_runs_MigrationEngine_for_dialect_if_fresh_install(new MsSql());
69   }
70
71   private void start_runs_MigrationEngine_for_dialect_if_fresh_install(Dialect dialect) {
72     mockDialect(dialect);
73     mockFreshInstall(true);
74
75     underTest.start();
76
77     verify(migrationEngine).execute();
78     verifyInfoLog();
79   }
80
81   @Test
82   public void start_does_nothing_if_not_fresh_install() {
83     mockFreshInstall(false);
84
85     underTest.start();
86
87     verifyNoInteractions(migrationEngine);
88     assertThat(logTester.logs(LoggerLevel.INFO)).isEmpty();
89   }
90
91   @Test
92   public void start_runs_MigrationEngine_if_blue_green_upgrade() {
93     mockFreshInstall(false);
94     when(serverUpgradeStatus.isUpgraded()).thenReturn(true);
95     when(serverUpgradeStatus.isBlueGreen()).thenReturn(true);
96
97     underTest.start();
98
99     verify(migrationEngine).execute();
100     assertThat(logTester.logs(LoggerLevel.INFO)).contains("Automatically perform DB migration on blue/green deployment");
101   }
102
103   @Test
104   public void start_does_nothing_if_blue_green_but_no_upgrade() {
105     mockFreshInstall(false);
106     when(serverUpgradeStatus.isUpgraded()).thenReturn(false);
107     when(serverUpgradeStatus.isBlueGreen()).thenReturn(true);
108
109     underTest.start();
110
111     verifyNoInteractions(migrationEngine);
112     assertThat(logTester.logs(LoggerLevel.INFO)).isEmpty();
113   }
114
115   @Test
116   public void start_runs_MigrationEngine_if_autoDbMigration_enabled() {
117     mockFreshInstall(false);
118     when(serverUpgradeStatus.isUpgraded()).thenReturn(true);
119     when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
120
121     underTest.start();
122
123     verify(migrationEngine).execute();
124     assertThat(logTester.logs(LoggerLevel.INFO)).contains("Automatically perform DB migration, as automatic database upgrade is enabled");
125   }
126
127   @Test
128   public void start_does_nothing_if_autoDbMigration_but_no_upgrade() {
129     mockFreshInstall(false);
130     when(serverUpgradeStatus.isUpgraded()).thenReturn(false);
131     when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
132
133     underTest.start();
134
135     verifyNoInteractions(migrationEngine);
136     assertThat(logTester.logs(LoggerLevel.INFO)).isEmpty();
137   }
138
139   @Test
140   public void stop_has_no_effect() {
141     underTest.stop();
142   }
143
144   private void mockFreshInstall(boolean value) {
145     when(serverUpgradeStatus.isFreshInstall()).thenReturn(value);
146   }
147
148   private void mockDialect(Dialect dialect) {
149     when(dbClient.getDatabase().getDialect()).thenReturn(dialect);
150   }
151
152   private void verifyInfoLog() {
153     assertThat(logTester.logs()).hasSize(1);
154     assertThat(logTester.logs(LoggerLevel.INFO)).containsExactly("Automatically perform DB migration on fresh install");
155   }
156
157 }