|
| 1 | +package org.checkstyle.plugins.sonar; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.sonar.orchestrator.Orchestrator; |
| 5 | +import com.sonar.orchestrator.build.Build; |
| 6 | +import com.sonar.orchestrator.build.BuildResult; |
| 7 | +import com.sonar.orchestrator.build.MavenBuild; |
| 8 | +import com.sonar.orchestrator.container.Edition; |
| 9 | +import com.sonar.orchestrator.container.Server; |
| 10 | +import com.sonar.orchestrator.http.HttpMethod; |
| 11 | +import com.sonar.orchestrator.http.HttpResponse; |
| 12 | +import com.sonar.orchestrator.locator.FileLocation; |
| 13 | +import com.sonar.orchestrator.locator.MavenLocation; |
| 14 | +import org.apache.commons.io.FileUtils; |
| 15 | +import org.apache.commons.lang.StringUtils; |
| 16 | +import org.assertj.core.api.Assertions; |
| 17 | +import org.fest.util.Collections; |
| 18 | +import org.junit.AfterClass; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.rules.TemporaryFolder; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.nio.file.Paths; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +import static org.junit.Assert.fail; |
| 36 | + |
| 37 | +/** |
| 38 | + * Integration testing of plugin jar inside of sonar. |
| 39 | + */ |
| 40 | +public class RunPluginTest { |
| 41 | + private static final Logger LOG = LoggerFactory.getLogger(RunPluginTest.class); |
| 42 | + private static final String SONAR_APP_VERSION = "7.9.2"; |
| 43 | + private static final int LOGS_NUMBER_LINES = 200; |
| 44 | + private static final String TRUE = "true"; |
| 45 | + private static final String PROJECT_KEY = "com.puppycrows.tools:checkstyle"; |
| 46 | + private static final String PROJECT_NAME = "integration-test-project"; |
| 47 | + private static final List<String> DEACTIVATED_RULES = Collections.list( |
| 48 | + "com.puppycrawl.tools.checkstyle.checks.coding.MissingCtorCheck", |
| 49 | + "com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck", |
| 50 | + "com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck", |
| 51 | + "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", |
| 52 | + "com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck", |
| 53 | + "com.puppycrawl.tools.checkstyle.checks.UncommentedMainCheck"); |
| 54 | + |
| 55 | + private static Orchestrator orchestrator; |
| 56 | + |
| 57 | + @Rule |
| 58 | + public final TemporaryFolder temp = new TemporaryFolder(); |
| 59 | + |
| 60 | + @BeforeClass |
| 61 | + public static void beforeAll() { |
| 62 | + orchestrator = Orchestrator.builderEnv() |
| 63 | + .setZipFile(new File("target/temp-downloads/sonar-application-" |
| 64 | + + SONAR_APP_VERSION |
| 65 | + + ".zip")) |
| 66 | + //.setSonarVersion(System.getProperty("sonar.runtimeVersion", SONAR_APP_VERSION)) |
| 67 | + .setEdition(Edition.COMMUNITY) |
| 68 | + .addPlugin(FileLocation.byWildcardMavenFilename(new File("target"), |
| 69 | + "checkstyle-sonar-plugin-*.jar")) |
| 70 | + .addPlugin(MavenLocation.of("org.sonarsource.sonar-lits-plugin", |
| 71 | + "sonar-lits-plugin", |
| 72 | + "0.8.0.1209")) |
| 73 | + .addPlugin(MavenLocation.of("org.sonarsource.java", |
| 74 | + "sonar-java-plugin", |
| 75 | + "6.0.0.20538")) |
| 76 | + .setServerProperty("sonar.web.javaOpts", "-Xmx1G") |
| 77 | + .build(); |
| 78 | + |
| 79 | + orchestrator.start(); |
| 80 | + } |
| 81 | + |
| 82 | + @AfterClass |
| 83 | + public static void afterAll() { |
| 84 | + orchestrator.stop(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testSonarExecution() { |
| 89 | + try { |
| 90 | + MavenBuild build = testProjectBuild(); |
| 91 | + executeBuildWithCommonProperties(build, true); |
| 92 | + } catch(IOException exception) { |
| 93 | + LOG.error("Build execution error.", exception); |
| 94 | + fail("Failed to execute build."); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static void executeBuildWithCommonProperties(Build<?> build, boolean buildQuietly) |
| 99 | + throws IOException { |
| 100 | + final String dumpOldLocation = FileLocation.of("target/old/" + PROJECT_NAME) |
| 101 | + .getFile() |
| 102 | + .getAbsolutePath(); |
| 103 | + final String dumpNewLocation = FileLocation.of("target/actual/" + PROJECT_NAME) |
| 104 | + .getFile() |
| 105 | + .getAbsolutePath(); |
| 106 | + build |
| 107 | + .setProperty("sonar.login", "admin") |
| 108 | + .setProperty("sonar.password", "admin") |
| 109 | + .setProperty("sonar.cpd.exclusions", "**/*") |
| 110 | + .setProperty("sonar.import_unknown_files", TRUE) |
| 111 | + .setProperty("sonar.skipPackageDesign", TRUE) |
| 112 | + .setProperty("dump.old", dumpOldLocation) |
| 113 | + .setProperty("dump.new", dumpNewLocation) |
| 114 | + .setProperty("lits.differences", litsDifferencesPath()) |
| 115 | + .setProperty("sonar.java.xfile", TRUE) |
| 116 | + .setProperty("sonar.java.failOnException", TRUE); |
| 117 | + |
| 118 | + BuildResult buildResult; |
| 119 | + // if build fail, job is not violently interrupted, allowing time to dump SQ logs |
| 120 | + if(buildQuietly) { |
| 121 | + buildResult = orchestrator.executeBuildQuietly(build); |
| 122 | + } else { |
| 123 | + buildResult = orchestrator.executeBuild(build); |
| 124 | + } |
| 125 | + |
| 126 | + if(buildResult.isSuccess()) { |
| 127 | + assertNoDifferences(); |
| 128 | + } else { |
| 129 | + dumpServerLogs(); |
| 130 | + fail("Build failure for project: " + PROJECT_NAME); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static void assertNoDifferences() { |
| 135 | + try { |
| 136 | + String differences = Files.readString(Paths.get(litsDifferencesPath())); |
| 137 | + Assertions.assertThat(differences) |
| 138 | + .isEmpty(); |
| 139 | + } catch(IOException exception) { |
| 140 | + LOG.error("Failed to read LITS differences.", exception); |
| 141 | + fail("LITS differences not computed."); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static String litsDifferencesPath() { |
| 146 | + return FileLocation.of("target/" + PROJECT_NAME + "_differences") |
| 147 | + .getFile() |
| 148 | + .getAbsolutePath(); |
| 149 | + } |
| 150 | + |
| 151 | + private static void dumpServerLogs() throws IOException { |
| 152 | + Server server = orchestrator.getServer(); |
| 153 | + LOG.error(":::::::::::::::: DUMPING SERVER LOGS ::::::::::::::::"); |
| 154 | + dumpServerLogLastLines(server.getAppLogs()); |
| 155 | + dumpServerLogLastLines(server.getCeLogs()); |
| 156 | + dumpServerLogLastLines(server.getEsLogs()); |
| 157 | + dumpServerLogLastLines(server.getWebLogs()); |
| 158 | + } |
| 159 | + |
| 160 | + private static void dumpServerLogLastLines(File logFile) throws IOException { |
| 161 | + if(!logFile.exists()) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + List<String> logs = Files.readAllLines(logFile.toPath()); |
| 166 | + int nbLines = logs.size(); |
| 167 | + if(nbLines > LOGS_NUMBER_LINES) { |
| 168 | + logs = logs.subList(nbLines - LOGS_NUMBER_LINES, nbLines); |
| 169 | + } |
| 170 | + String collectedLogs = logs.stream() |
| 171 | + .collect(Collectors.joining(System.lineSeparator())); |
| 172 | + |
| 173 | + LOG.error("============= START {} =============", logFile.getName()); |
| 174 | + LOG.error("{} {}", System.lineSeparator(), collectedLogs); |
| 175 | + LOG.error("============= END {} =============", logFile.getName()); |
| 176 | + } |
| 177 | + |
| 178 | + private MavenBuild testProjectBuild() throws IOException { |
| 179 | + File targetDir = prepareProject(); |
| 180 | + |
| 181 | + String pomLocation = targetDir.getCanonicalPath() + "/pom.xml"; |
| 182 | + File pomFile = FileLocation.of(pomLocation) |
| 183 | + .getFile() |
| 184 | + .getCanonicalFile(); |
| 185 | + MavenBuild mavenBuild = MavenBuild.create() |
| 186 | + .setPom(pomFile) |
| 187 | + .setCleanPackageSonarGoals() |
| 188 | + .addArgument("-Dmaven.test.skip=true") |
| 189 | + .addArgument("-DskipTests") |
| 190 | + .addArgument("-DskipITs"); |
| 191 | + mavenBuild.setProperty("sonar.projectKey", PROJECT_KEY); |
| 192 | + return mavenBuild; |
| 193 | + } |
| 194 | + |
| 195 | + @SuppressWarnings("unchecked") |
| 196 | + private File prepareProject() throws IOException { |
| 197 | + // set severities of all active rules to INFO |
| 198 | + String profilesResponse = orchestrator.getServer() |
| 199 | + .newHttpCall("api/qualityprofiles/create") |
| 200 | + .setAdminCredentials() |
| 201 | + .setMethod(HttpMethod.POST) |
| 202 | + .setParam("language", "java") |
| 203 | + .setParam("name", "checkstyle") |
| 204 | + .execute() |
| 205 | + .getBodyAsString(); |
| 206 | + Map<String, Object> map = new Gson().fromJson(profilesResponse, Map.class); |
| 207 | + String profileKey = ((Map<String, String>) map.get("profile")).get("key"); |
| 208 | + if(StringUtils.isEmpty(profileKey)) { |
| 209 | + fail("Could not retrieve profile key: setting up quality profile failed."); |
| 210 | + } else { |
| 211 | + HttpResponse activateRulesResponse = orchestrator.getServer() |
| 212 | + .newHttpCall("api/qualityprofiles/activate_rules") |
| 213 | + .setAdminCredentials() |
| 214 | + .setMethod(HttpMethod.POST) |
| 215 | + .setParam("activation_severity", "INFO") |
| 216 | + .setParam("languages", "java") |
| 217 | + .setParam("profile_key", profileKey) |
| 218 | + .setParam("repositories", "checkstyle") |
| 219 | + .executeUnsafely(); |
| 220 | + if(!activateRulesResponse.isSuccessful()) { |
| 221 | + fail(String.format("Failed to activate all rules. %s", |
| 222 | + activateRulesResponse.getBodyAsString())); |
| 223 | + } |
| 224 | + // deactivate some rules for test project |
| 225 | + for(String ruleKey : DEACTIVATED_RULES) { |
| 226 | + HttpResponse deactivateRulesResponse = orchestrator.getServer() |
| 227 | + .newHttpCall("api/qualityprofiles/deactivate_rule") |
| 228 | + .setAdminCredentials() |
| 229 | + .setMethod(HttpMethod.POST) |
| 230 | + .setParam("rule_key", "checkstyle:" + ruleKey) |
| 231 | + .setParam("profile_key", profileKey) |
| 232 | + .executeUnsafely(); |
| 233 | + if(!deactivateRulesResponse.isSuccessful()) { |
| 234 | + fail(String.format("Failed to deactivate rule %s. %s", |
| 235 | + ruleKey, |
| 236 | + deactivateRulesResponse.getBodyAsString())); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + // associate CS profile |
| 242 | + orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_NAME); |
| 243 | + HttpResponse assignQPResponse = orchestrator.getServer() |
| 244 | + .newHttpCall("api/qualityprofiles/add_project") |
| 245 | + .setAdminCredentials() |
| 246 | + .setMethod(HttpMethod.POST) |
| 247 | + .setParam("language", "java") |
| 248 | + .setParam("profileName", "checkstyle") |
| 249 | + .setParam("projectKey", PROJECT_KEY) |
| 250 | + .executeUnsafely(); |
| 251 | + if(!assignQPResponse.isSuccessful()) { |
| 252 | + fail(String.format("Failed to add project to quality profile. %s", |
| 253 | + assignQPResponse.getBodyAsString())); |
| 254 | + } |
| 255 | + |
| 256 | + // copy project to analysis space |
| 257 | + Path projectRoot = Paths.get("src/it/resources/" + PROJECT_NAME); |
| 258 | + File targetDir = temp.newFolder(PROJECT_NAME); |
| 259 | + FileUtils.copyDirectory(projectRoot.toFile(), targetDir); |
| 260 | + return targetDir; |
| 261 | + } |
| 262 | +} |
0 commit comments