You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseServerCompatibilityTest.java 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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;
  21. import java.util.Optional;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.slf4j.event.Level;
  25. import org.sonar.api.testfixtures.log.LogTester;
  26. import org.sonar.api.utils.MessageException;
  27. import org.sonar.server.platform.db.migration.version.DatabaseVersion;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.when;
  32. public class DatabaseServerCompatibilityTest {
  33. @Rule
  34. public LogTester logTester = new LogTester();
  35. @Test
  36. public void fail_if_requires_downgrade() {
  37. DatabaseVersion version = mock(DatabaseVersion.class);
  38. when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE);
  39. var compatibility = new DatabaseServerCompatibility(version);
  40. assertThatThrownBy(compatibility::start)
  41. .isInstanceOf(MessageException.class)
  42. .hasMessage("Database was upgraded to a more recent version of SonarQube. "
  43. + "A backup must probably be restored or the DB settings are incorrect.");
  44. }
  45. @Test
  46. public void fail_if_requires_firstly_to_upgrade_to_lts() {
  47. DatabaseVersion version = mock(DatabaseVersion.class);
  48. when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
  49. when(version.getVersion()).thenReturn(Optional.of(12L));
  50. var compatibility = new DatabaseServerCompatibility(version);
  51. assertThatThrownBy(compatibility::start)
  52. .isInstanceOf(MessageException.class)
  53. .hasMessage("The version of SonarQube is too old. Please upgrade to the Long Term Support version first.");
  54. }
  55. @Test
  56. public void log_warning_if_requires_upgrade() {
  57. DatabaseVersion version = mock(DatabaseVersion.class);
  58. when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
  59. when(version.getVersion()).thenReturn(Optional.of(DatabaseVersion.MIN_UPGRADE_VERSION));
  60. new DatabaseServerCompatibility(version).start();
  61. assertThat(logTester.logs()).hasSize(4);
  62. assertThat(logTester.logs(Level.WARN)).contains(
  63. "The database must be manually upgraded. Please backup the database and browse /setup. "
  64. + "For more information: https://docs.sonarsource.com/sonarqube/latest/setup/upgrading",
  65. "################################################################################",
  66. "The database must be manually upgraded. Please backup the database and browse /setup. "
  67. + "For more information: https://docs.sonarsource.com/sonarqube/latest/setup/upgrading",
  68. "################################################################################");
  69. }
  70. @Test
  71. public void do_nothing_if_up_to_date() {
  72. DatabaseVersion version = mock(DatabaseVersion.class);
  73. when(version.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE);
  74. new DatabaseServerCompatibility(version).start();
  75. // no error
  76. }
  77. }