diff options
author | Eric Hartmann <hartmann.eric@gmail.Com> | 2017-03-15 10:12:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-15 10:12:18 +0100 |
commit | 5f95ea5b105dcd39813d832acfbbecb81fe33dbb (patch) | |
tree | 95ebbd9505d59cabc2e41e167a7f0444d712ce1d /server/sonar-process-monitor/src/test | |
parent | 051c0509fde15354ec02f6a86eddb0ce493458b2 (diff) | |
download | sonarqube-5f95ea5b105dcd39813d832acfbbecb81fe33dbb.tar.gz sonarqube-5f95ea5b105dcd39813d832acfbbecb81fe33dbb.zip |
SONAR-8817 Add SonarQube version safeguard in cluster mode (#1781)
SONAR-8817 Add SonarQube version safeguard in cluster mode
Diffstat (limited to 'server/sonar-process-monitor/src/test')
6 files changed, 165 insertions, 92 deletions
diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/TestAppState.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/TestAppState.java index e69624382eb..40d4196cdd8 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/application/TestAppState.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/TestAppState.java @@ -71,6 +71,11 @@ public class TestAppState implements AppState { } @Override + public void registerSonarQubeVersion(String sonarqubeVersion) { + // nothing to do + } + + @Override public void close() { // nothing to do } diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/AppStateClusterImplTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/AppStateClusterImplTest.java index 1aaf3bcd6f4..98d44cb0a4a 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/AppStateClusterImplTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/AppStateClusterImplTest.java @@ -40,6 +40,8 @@ import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.sonar.application.cluster.AppStateClusterImpl.OPERATIONAL_PROCESSES; +import static org.sonar.application.cluster.AppStateClusterImpl.SONARQUBE_VERSION; +import static org.sonar.application.cluster.HazelcastTestHelper.createHazelcastClient; public class AppStateClusterImplTest { @@ -95,7 +97,7 @@ public class AppStateClusterImplTest { try (AppStateClusterImpl appStateCluster = new AppStateClusterImpl(settings)) { appStateCluster.addListener(listener); - HazelcastInstance hzInstance = HazelcastHelper.createHazelcastClient(appStateCluster); + HazelcastInstance hzInstance = createHazelcastClient(appStateCluster); String uuid = UUID.randomUUID().toString(); ReplicatedMap<ClusterProcess, Boolean> replicatedMap = hzInstance.getReplicatedMap(OPERATIONAL_PROCESSES); // process is not up yet --> no events are sent to listeners @@ -116,6 +118,41 @@ public class AppStateClusterImplTest { } } + @Test + public void registerSonarQubeVersion_publishes_version_on_first_call() { + TestAppSettings settings = newClusterSettings(); + + try (AppStateClusterImpl appStateCluster = new AppStateClusterImpl(settings)) { + appStateCluster.registerSonarQubeVersion("6.4.1.5"); + + HazelcastInstance hzInstance = createHazelcastClient(appStateCluster); + assertThat(hzInstance.getAtomicReference(SONARQUBE_VERSION).get()) + .isNotNull() + .isInstanceOf(String.class) + .isEqualTo("6.4.1.5"); + } + } + + @Test + public void registerSonarQubeVersion_throws_ISE_if_initial_version_is_different() throws Exception { + // Now launch an instance that try to be part of the hzInstance cluster + TestAppSettings settings = new TestAppSettings(); + settings.set(ProcessProperties.CLUSTER_ENABLED, "true"); + settings.set(ProcessProperties.CLUSTER_NAME, "sonarqube"); + + + try (AppStateClusterImpl appStateCluster = new AppStateClusterImpl(settings)) { + // Register first version + appStateCluster.registerSonarQubeVersion("1.0.0"); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("The local version 2.0.0 is not the same as the cluster 1.0.0"); + + // Registering a second different version must trigger an exception + appStateCluster.registerSonarQubeVersion("2.0.0"); + } + } + private static TestAppSettings newClusterSettings() { TestAppSettings settings = new TestAppSettings(); settings.set(ProcessProperties.CLUSTER_ENABLED, "true"); diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/ClusterProcessTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/ClusterProcessTest.java new file mode 100644 index 00000000000..5472d575746 --- /dev/null +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/ClusterProcessTest.java @@ -0,0 +1,40 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.application.cluster; + +import org.junit.Test; +import org.sonar.process.ProcessId; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ClusterProcessTest { + @Test + public void test_equality() { + ClusterProcess clusterProcess = new ClusterProcess("A", ProcessId.WEB_SERVER); + + assertThat(clusterProcess) + .isNotEqualTo(null) + .isEqualTo(clusterProcess) + .isNotEqualTo(new ClusterProcess("B", ProcessId.WEB_SERVER)) + .isNotEqualTo(new ClusterProcess("A", ProcessId.ELASTICSEARCH)) + .isEqualTo(new ClusterProcess("A", ProcessId.WEB_SERVER)); + } +} diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastHelper.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastHelper.java deleted file mode 100644 index 9cfe2559e67..00000000000 --- a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastHelper.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.application.cluster; - -import com.hazelcast.client.HazelcastClient; -import com.hazelcast.client.config.ClientConfig; -import com.hazelcast.config.Config; -import com.hazelcast.config.JoinConfig; -import com.hazelcast.config.NetworkConfig; -import com.hazelcast.core.Hazelcast; -import com.hazelcast.core.HazelcastInstance; -import java.net.InetSocketAddress; -import java.util.Collection; - -public class HazelcastHelper { - static HazelcastInstance createHazelcastNode(AppStateClusterImpl appStateCluster) { - Config hzConfig = new Config() - .setInstanceName(appStateCluster.hzInstance.getName() + "_1"); - - // Configure the network instance - NetworkConfig netConfig = hzConfig.getNetworkConfig(); - netConfig.setPort(9003).setPortAutoIncrement(true); - Collection<String> interfaces = appStateCluster.hzInstance.getConfig().getNetworkConfig().getInterfaces().getInterfaces(); - if (!interfaces.isEmpty()) { - netConfig.getInterfaces().addInterface( - interfaces.iterator().next() - ); - } - - // Only allowing TCP/IP configuration - JoinConfig joinConfig = netConfig.getJoin(); - joinConfig.getAwsConfig().setEnabled(false); - joinConfig.getMulticastConfig().setEnabled(false); - joinConfig.getTcpIpConfig().setEnabled(true); - - InetSocketAddress socketAddress = (InetSocketAddress) appStateCluster.hzInstance.getLocalEndpoint().getSocketAddress(); - joinConfig.getTcpIpConfig().addMember( - String.format("%s:%d", - socketAddress.getHostString(), - socketAddress.getPort() - ) - ); - - // Tweak HazelCast configuration - hzConfig - // Increase the number of tries - .setProperty("hazelcast.tcp.join.port.try.count", "10") - // Don't bind on all interfaces - .setProperty("hazelcast.socket.bind.any", "false") - // Don't phone home - .setProperty("hazelcast.phone.home.enabled", "false") - // Use slf4j for logging - .setProperty("hazelcast.logging.type", "slf4j"); - - // We are not using the partition group of Hazelcast, so disabling it - hzConfig.getPartitionGroupConfig().setEnabled(false); - - return Hazelcast.newHazelcastInstance(hzConfig); - } - - static HazelcastInstance createHazelcastClient(AppStateClusterImpl appStateCluster) { - ClientConfig clientConfig = new ClientConfig(); - InetSocketAddress socketAddress = (InetSocketAddress) appStateCluster.hzInstance.getLocalEndpoint().getSocketAddress(); - - clientConfig.getNetworkConfig().getAddresses().add( - String.format("%s:%d", - socketAddress.getHostString(), - socketAddress.getPort() - )); - clientConfig.getGroupConfig().setName(appStateCluster.hzInstance.getConfig().getGroupConfig().getName()); - return HazelcastClient.newHazelcastClient(clientConfig); - } -} diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastTestHelper.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastTestHelper.java new file mode 100644 index 00000000000..106390dc2d1 --- /dev/null +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/cluster/HazelcastTestHelper.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.application.cluster; + +import com.hazelcast.client.HazelcastClient; +import com.hazelcast.client.config.ClientConfig; +import com.hazelcast.core.HazelcastInstance; +import java.net.InetSocketAddress; + +public class HazelcastTestHelper { + + static HazelcastInstance createHazelcastClient(AppStateClusterImpl appStateCluster) { + ClientConfig clientConfig = new ClientConfig(); + InetSocketAddress socketAddress = (InetSocketAddress) appStateCluster.hzInstance.getLocalEndpoint().getSocketAddress(); + + clientConfig.getNetworkConfig().getAddresses().add( + String.format("%s:%d", + socketAddress.getHostString(), + socketAddress.getPort() + )); + clientConfig.getGroupConfig().setName(appStateCluster.hzInstance.getConfig().getGroupConfig().getName()); + return HazelcastClient.newHazelcastClient(clientConfig); + } +} diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/config/SonarQubeVersionHelperTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/config/SonarQubeVersionHelperTest.java new file mode 100644 index 00000000000..93c1aa065c0 --- /dev/null +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/config/SonarQubeVersionHelperTest.java @@ -0,0 +1,40 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.application.config; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SonarQubeVersionHelperTest { + @Test + public void getSonarQubeVersion_must_not_return_an_empty_string() { + assertThat(SonarQubeVersionHelper.getSonarqubeVersion()).isNotEmpty(); + } + + @Test + public void getSonarQubeVersion_must_always_return_same_value() { + String sonarqubeVersion = SonarQubeVersionHelper.getSonarqubeVersion(); + for (int i = 0; i < 3; i++) { + assertThat(SonarQubeVersionHelper.getSonarqubeVersion()).isEqualTo(sonarqubeVersion); + } + } +} |