Skip to content

Commit

Permalink
Try fix flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sophio-japharidze-sonarsource authored and jblievremont committed Feb 7, 2025
1 parent ec54e63 commit da70f0c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ public static List<SonarQubeConnectionConfigurationDto> extractSonarQubeConnecti
public static List<SonarCloudConnectionConfigurationDto> extractSonarCloudConnections(Map<String, ServerConnectionSettings> connections) {
return connections.entrySet().stream()
.filter(it -> it.getValue().isSonarCloudAlias())
.map(it -> new SonarCloudConnectionConfigurationDto(it.getKey(), it.getValue().getOrganizationKey(), it.getValue().getRegion() != null ? it.getValue().getRegion() : SonarCloudRegion.EU, it.getValue().isSmartNotificationsDisabled()))
.map(it -> new SonarCloudConnectionConfigurationDto(it.getKey(), it.getValue().getOrganizationKey(),
it.getValue().getRegion() != null ? it.getValue().getRegion() : SonarCloudRegion.EU, it.getValue().isSmartNotificationsDisabled()))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,7 @@ private void parseDeprecatedServerEntries(Map<String, Object> connectedModeMap,
var token = (String) m.get(TOKEN);
var organization = (String) m.get(ORGANIZATION_KEY);
var region = (String) m.getOrDefault(REGION_KEY, SonarCloudRegion.EU.name());
// TODO check for parsing the region properly
var connectionSettings = new ServerConnectionSettings(connectionId, url, token, organization, false, SonarCloudRegion.valueOf(region));
var connectionSettings = new ServerConnectionSettings(connectionId, url, token, organization, false, parseRegion(region));
addIfUniqueConnectionId(serverConnections, connectionId, connectionSettings);
}
});
Expand Down Expand Up @@ -466,13 +465,21 @@ private void parseSonarCloudConnections(Map<String, Object> connectionsMap, Map<
var token = getTokenFromClient(organizationKey);
var disableNotifs = (Boolean) m.getOrDefault(DISABLE_NOTIFICATIONS, false);
var region = (String) m.getOrDefault(REGION_KEY, SonarCloudRegion.EU.name());
// TODO check for parsing the region properly
addIfUniqueConnectionId(serverConnections, connectionId,
new ServerConnectionSettings(connectionId, ServerConnectionSettings.SONARCLOUD_URL, token, organizationKey, disableNotifs, SonarCloudRegion.valueOf(region)));
new ServerConnectionSettings(connectionId, ServerConnectionSettings.SONARCLOUD_URL, token, organizationKey, disableNotifs, parseRegion(region)));
}
});
}

private SonarCloudRegion parseRegion(String region) {
try {
return SonarCloudRegion.valueOf(region);
} catch (IllegalArgumentException e) {
logOutput.error(format("Unknown SonarQube Cloud region '%s'. Using default region '%s'", region, SonarCloudRegion.EU.name()));
return SonarCloudRegion.EU;
}
}

private String getTokenFromClient(String serverUrlOrOrganization) {
try {
return client.getTokenForServer(serverUrlOrOrganization).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static void initialize() throws Exception {

@BeforeEach
public void mockSonarQube() {
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"9.9\", \"id\": \"xzy\"}");
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.7\", \"id\": \"xzy\"}");
mockWebServerExtension.addResponse("/api/authentication/validate?format=json", new MockResponse().setResponseCode(200));
mockWebServerExtension.addResponse("/api/developers/search_events?projects=&from=", new MockResponse().setResponseCode(200));
mockWebServerExtension.addProtobufResponse("/api/components/search.protobuf?qualifiers=TRK&ps=500&p=1",
Expand Down Expand Up @@ -234,6 +234,10 @@ public void mockSonarQube() {
Issues.IssuesPullQueryTimestamp.newBuilder()
.setQueryTimestamp(CURRENT_TIME)
.build());
mockWebServerExtension.addProtobufResponseDelimited(
"/api/hotspots/pull?projectKey=myProject&branchName=master&languages=" + LANGUAGES_LIST,
Hotspots.HotspotPullQueryTimestamp.newBuilder().setQueryTimestamp(CURRENT_TIME).build()
);
client.clearHotspotsAndIssuesAndConfigScopeReadiness();
}

Expand Down Expand Up @@ -368,7 +372,7 @@ void analysisConnected_find_tracked_hotspot_before_sq_10_1() {

@Test
void analysisConnected_find_tracked_hotspot_after_sq_10_1() {
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.1\", \"id\": \"xzy\"}");
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.7\", \"id\": \"xzy\"}");
mockNoIssueAndNoTaintInIncrementalSync();
mockWebServerExtension.addProtobufResponse(
"/api/hotspots/search.protobuf?projectKey=myProject&files=hotspot.py&branch=master&ps=500&p=1",
Expand Down Expand Up @@ -528,7 +532,7 @@ void analysisConnected_scan_all_hotspot_then_forget() throws IOException {

@Test
void analysisConnected_no_matching_server_issues() {
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"9.9\", \"id\": \"xzy\"}");
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.7\", \"id\": \"xzy\"}");
mockNoIssuesNoHotspotsForProject();
mockWebServerExtension.addStringResponse("/api/authentication/validate?format=json", "{\"valid\": true}");
mockWebServerExtension.addProtobufResponse("/api/measures/component.protobuf?additionalFields=period&metricKeys=projects&component=myProject",
Expand Down Expand Up @@ -573,11 +577,12 @@ private void addConfigScope(String configScopeId) {

@Test
void analysisConnected_matching_server_issues() throws Exception {
var issueKey = UUID.randomUUID().toString();
mockWebServerExtension.addProtobufResponse(
"/api/issues/search.protobuf?issues=xyz&additionalFields=transitions&ps=1&p=1",
"/api/issues/search.protobuf?issues=" + issueKey + "&additionalFields=transitions&ps=1&p=1",
Issues.SearchWsResponse.newBuilder()
.addIssues(Issues.Issue.newBuilder()
.setKey("xyz")
.setKey(issueKey)
.setTransitions(Issues.Transitions.newBuilder()
.addAllTransitions(List.of("wontfix", "falsepositive"))
.build())
Expand All @@ -604,7 +609,7 @@ void analysisConnected_matching_server_issues() throws Exception {
.setQueryTimestamp(CURRENT_TIME)
.build(),
Issues.IssueLite.newBuilder()
.setKey("xyz")
.setKey(issueKey)
.setRuleKey(PYTHON_S1481)
.setType(Common.RuleType.CODE_SMELL)
.setUserSeverity(Common.Severity.BLOCKER)
Expand Down Expand Up @@ -859,7 +864,7 @@ void shouldChangeIssueStatus() {

@Test
void shouldNotChangeIssueStatus() {
var issueKey = "xyz";
var issueKey = UUID.randomUUID().toString();

mockWebServerExtension.addResponse("/api/issues/do_transition", new MockResponse().setResponseCode(400));
mockWebServerExtension.addResponse("/api/issues/add_comment", new MockResponse().setResponseCode(400));
Expand Down Expand Up @@ -894,7 +899,7 @@ void shouldNotChangeIssueStatus() {
void change_hotspot_status_to_resolved() {
var analyzedFileName = "hotspot_resolved.py";

mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.1\", \"id\": \"xzy\"}");
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.7\", \"id\": \"xzy\"}");
mockWebServerExtension.addResponse("/api/hotspots/change_status", new MockResponse().setResponseCode(200));
mockNoIssueAndNoTaintInIncrementalSync();
mockWebServerExtension.addProtobufResponse(
Expand Down Expand Up @@ -976,7 +981,7 @@ void change_hotspot_status_to_resolved() {
void should_not_change_hotspot_status_to_resolved() {
var analyzedFileName = "hotspot_not_resolved.py";

mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.1\", \"id\": \"xzy\"}");
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.7\", \"id\": \"xzy\"}");
mockWebServerExtension.addResponse("/api/hotspots/change_status", new MockResponse().setResponseCode(400));
mockNoIssueAndNoTaintInIncrementalSync();
mockWebServerExtension.addProtobufResponse(
Expand Down Expand Up @@ -1057,7 +1062,7 @@ void should_not_change_hotspot_status_to_resolved() {
@Test
void change_hotspot_status_permission_check() throws ExecutionException, InterruptedException {
var analyzedFileName = "hotspot_permissions.py";
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.1\", \"id\": \"xzy\"}");
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.7\", \"id\": \"xzy\"}");
mockWebServerExtension.addResponse("/api/hotspots/change_status", new MockResponse().setResponseCode(200));
mockNoIssueAndNoTaintInIncrementalSync();
mockWebServerExtension.addProtobufResponse(
Expand Down Expand Up @@ -1154,7 +1159,7 @@ void change_hotspot_status_permission_check() throws ExecutionException, Interru
@Test
void change_hotspot_status_permission_check_fail() throws ExecutionException, InterruptedException {
var analyzedFileName = "hotspot_no_permissions.py";
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.1\", \"id\": \"xzy\"}");
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.7\", \"id\": \"xzy\"}");
mockWebServerExtension.addResponse("/api/hotspots/change_status", new MockResponse().setResponseCode(400));

var uriInFolder = folder1BaseDir.resolve(analyzedFileName).toUri().toString();
Expand All @@ -1181,7 +1186,7 @@ void change_issue_status_permission_check() throws ExecutionException, Interrupt
"/api/issues/search.protobuf?issues=" + issueKey + "&additionalFields=transitions&ps=1&p=1",
Issues.SearchWsResponse.newBuilder()
.addIssues(Issues.Issue.newBuilder()
.setKey("xyz")
.setKey(issueKey)
.setTransitions(Issues.Transitions.newBuilder()
.addAllTransitions(List.of("wontfix", "falsepositive"))
.build())
Expand All @@ -1196,7 +1201,7 @@ void change_issue_status_permission_check() throws ExecutionException, Interrupt
.setQueryTimestamp(CURRENT_TIME)
.build(),
Issues.IssueLite.newBuilder()
.setKey("xyz")
.setKey(issueKey)
.setRuleKey(PYTHON_S1481)
.setType(Common.RuleType.CODE_SMELL)
.setUserSeverity(Common.Severity.BLOCKER)
Expand Down Expand Up @@ -1279,7 +1284,7 @@ private void mockNoIssueAndNoTaintInIncrementalSync() {
}

private void mockNoIssuesNoHotspotsForProject() {
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"9.9\", \"id\": \"xzy\"}");
mockWebServerExtension.addStringResponse("/api/system/status", "{\"status\": \"UP\", \"version\": \"10.7\", \"id\": \"xzy\"}");
mockWebServerExtension.addProtobufResponseDelimited(
"/api/issues/pull?projectKey=myProject&branchName=master&languages=" + LANGUAGES_LIST,
Issues.IssuesPullQueryTimestamp.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ void optOutTelemetry() {
notifyConfigurationChangeOnClient();

assertLogContains(
String.format("Global settings updated: WorkspaceSettings[analysisExcludes=,connections={%s=ServerConnectionSettings[connectionId=%s,disableNotifications=false,organizationKey=<null>,serverUrl=%s]},disableTelemetry=false,excludedRules=[],focusOnNewCode=false,includedRules=[],pathToNodeExecutable=<null>,ruleParameters={},showAnalyzerLogs=false,showVerboseLogs=true]",
String.format("Global settings updated: WorkspaceSettings[analysisExcludes=,connections={%s=ServerConnectionSettings[connectionId=%s,disableNotifications=false,organizationKey=<null>,region=<null>,serverUrl=%s]},disableTelemetry=false,excludedRules=[],focusOnNewCode=false,includedRules=[],pathToNodeExecutable=<null>,ruleParameters={},showAnalyzerLogs=false,showVerboseLogs=true]",
CONNECTION_ID, CONNECTION_ID, mockWebServerExtension.url("/")));
// We are using the global system property to disable telemetry in tests, so this assertion do not pass
// assertLogContainsInOrder( "Telemetry enabled");
Expand Down

0 comments on commit da70f0c

Please sign in to comment.