]> source.dussan.org Git - sonarqube.git/blob
25b06a0dcbf089ba5ae3337e28e7d3ed0398b744
[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.monitoring;
21
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.sonar.api.SonarEdition;
30 import org.sonar.api.SonarRuntime;
31 import org.sonar.api.config.Configuration;
32 import org.sonar.api.config.internal.MapSettings;
33 import org.sonar.api.platform.Server;
34 import org.sonar.api.utils.log.LoggerLevel;
35 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
36 import org.sonar.server.log.ServerLogging;
37 import org.sonar.server.platform.DockerSupport;
38 import org.sonar.server.platform.OfficialDistribution;
39 import org.sonar.server.platform.StatisticsSupport;
40
41 import static java.util.Collections.emptyList;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.api.SonarEdition.COMMUNITY;
46 import static org.sonar.api.SonarEdition.DATACENTER;
47 import static org.sonar.api.SonarEdition.DEVELOPER;
48 import static org.sonar.api.SonarEdition.ENTERPRISE;
49 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
50 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeDoesNotExist;
51 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
52
53 @RunWith(DataProviderRunner.class)
54 public class StandaloneSystemSectionTest {
55
56   private final MapSettings settings = new MapSettings();
57   private final Configuration config = settings.asConfig();
58   private final Server server = mock(Server.class);
59   private final ServerLogging serverLogging = mock(ServerLogging.class);
60   private final OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
61   private final DockerSupport dockerSupport = mock(DockerSupport.class);
62   private final StatisticsSupport statisticsSupport = mock(StatisticsSupport.class);
63   private final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
64   private final CommonSystemInformation commonSystemInformation = mock(CommonSystemInformation.class);
65
66   private final StandaloneSystemSection underTest = new StandaloneSystemSection(config, server, serverLogging,
67     officialDistribution, dockerSupport, statisticsSupport, sonarRuntime, commonSystemInformation);
68
69   @Before
70   public void setUp() {
71     when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
72     when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
73   }
74
75   @Test
76   public void name_is_not_empty() {
77     assertThat(underTest.name()).isNotEmpty();
78   }
79
80   @Test
81   public void test_getServerId() {
82     when(server.getId()).thenReturn("ABC");
83     assertThat(underTest.getServerId()).isEqualTo("ABC");
84   }
85
86   @Test
87   public void official_distribution() {
88     when(officialDistribution.check()).thenReturn(true);
89
90     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
91     assertThatAttributeIs(protobuf, "Official Distribution", true);
92   }
93
94   @Test
95   public void not_an_official_distribution() {
96     when(officialDistribution.check()).thenReturn(false);
97
98     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
99     assertThatAttributeIs(protobuf, "Official Distribution", false);
100   }
101
102   @Test
103   public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() {
104     when(commonSystemInformation.getExternalUserAuthentication()).thenReturn(null);
105
106     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
107     assertThatAttributeDoesNotExist(protobuf, "External User Authentication");
108   }
109
110   @Test
111   public void toProtobuf_whenExternalUserAuthentication_shouldWriteIt() {
112     when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("LDAP");
113     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
114     assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
115   }
116
117   @Test
118   public void toProtobuf_whenNoIdentityProviders_shouldWriteNothing() {
119     when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(emptyList());
120
121     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
122     assertThatAttributeDoesNotExist(protobuf, "Accepted external identity providers");
123   }
124
125   @Test
126   public void toProtobuf_whenEnabledIdentityProviders_shouldWriteThem() {
127     when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(List.of("Bitbucket, GitHub"));
128
129     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
130     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
131   }
132
133   @Test
134   public void toProtobuf_whenNoAllowsToSignUpEnabledIdentityProviders_shouldWriteNothing() {
135     when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(emptyList());
136
137     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
138     assertThatAttributeDoesNotExist(protobuf, "External identity providers whose users are allowed to sign themselves up");
139   }
140
141   @Test
142   public void toProtobuf_whenAllowsToSignUpEnabledIdentityProviders_shouldWriteThem() {
143     when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(List.of("GitHub"));
144
145     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
146     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
147   }
148
149   @Test
150   public void return_nb_of_processors() {
151     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
152     assertThat(attribute(protobuf, "Processors").getLongValue()).isPositive();
153   }
154
155   @Test
156   public void toProtobuf_whenForceAuthentication_returnIt() {
157     when(commonSystemInformation.getForceAuthentication()).thenReturn(false);
158     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
159     assertThatAttributeIs(protobuf, "Force authentication", false);
160   }
161
162   @Test
163   public void return_Lines_of_Codes_from_StatisticsSupport(){
164     when(statisticsSupport.getLinesOfCode()).thenReturn(17752L);
165     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
166     assertThatAttributeIs(protobuf,"Lines of Code", 17752L);
167   }
168
169   @Test
170   @UseDataProvider("trueOrFalse")
171   public void return_docker_flag_from_DockerSupport(boolean flag) {
172     when(dockerSupport.isRunningInDocker()).thenReturn(flag);
173     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
174     assertThat(attribute(protobuf, "Docker").getBooleanValue()).isEqualTo(flag);
175   }
176
177   @Test
178   @UseDataProvider("editions")
179   public void get_edition(SonarEdition sonarEdition, String editionLabel) {
180     when(sonarRuntime.getEdition()).thenReturn(sonarEdition);
181     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
182     assertThatAttributeIs(protobuf, "Edition", editionLabel);
183   }
184
185   @Test
186   public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
187     when(commonSystemInformation.getManagedInstanceProviderName()).thenReturn(null);
188     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
189
190     assertThatAttributeDoesNotExist(protobuf, "External Users and Groups Provisioning");
191   }
192
193   @Test
194   public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
195     when(commonSystemInformation.getManagedInstanceProviderName()).thenReturn("Okta");
196
197     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
198     assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "Okta");
199   }
200
201   @DataProvider
202   public static Object[][] trueOrFalse() {
203     return new Object[][] {
204       {true},
205       {false}
206     };
207   }
208
209   @DataProvider
210   public static Object[][] editions() {
211     return new Object[][] {
212       {COMMUNITY, COMMUNITY.getLabel()},
213       {DEVELOPER, DEVELOPER.getLabel()},
214       {ENTERPRISE, ENTERPRISE.getLabel()},
215       {DATACENTER, DATACENTER.getLabel()},
216     };
217   }
218 }