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.

TestFailureAspect.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.sonarqube.monitoring.test.aspect;
  21. import com.google.gson.Gson;
  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.nio.file.LinkOption;
  25. import java.nio.file.Path;
  26. import java.nio.file.Paths;
  27. import java.nio.file.StandardOpenOption;
  28. import java.time.LocalDateTime;
  29. import java.time.ZoneId;
  30. import java.time.format.DateTimeFormatter;
  31. import org.aspectj.lang.JoinPoint;
  32. import org.aspectj.lang.annotation.After;
  33. import org.aspectj.lang.annotation.Aspect;
  34. import org.junit.runner.Description;
  35. import org.junit.runner.notification.Failure;
  36. import org.sonarqube.monitoring.test.Measure;
  37. import static java.nio.charset.StandardCharsets.UTF_8;
  38. import static org.sonarqube.monitoring.test.Measure.MeasureBuilder.newMeasureBuilder;
  39. @Aspect
  40. public class TestFailureAspect {
  41. public static final String BRANCH_NAME = System.getenv("GITHUB_BRANCH");
  42. public static final String COMMIT_HASH = System.getenv("GIT_SHA1");
  43. public static final String BUILD_NUMBER = System.getenv("BUILD_NUMBER");
  44. public static final String QA_CATEGORY = System.getenv("QA_CATEGORY");
  45. private static final Path PATH = Paths.get("/tmp/test-monitoring.log");
  46. private static final Gson GSON = new Gson();
  47. static {
  48. try {
  49. if (!Files.exists(PATH, LinkOption.NOFOLLOW_LINKS)) {
  50. Files.createFile(PATH);
  51. }
  52. Files.write(PATH, "".getBytes(UTF_8));
  53. } catch (IOException e) {
  54. // Ignore
  55. }
  56. }
  57. @After("execution(public * org.junit.runner.notification.RunNotifier+.fireTestFailure(..))")
  58. public void afterFireTestFailure(JoinPoint joinPoint) {
  59. Object[] args = joinPoint.getArgs();
  60. if (args.length == 1) {
  61. Object arg = args[0];
  62. if (arg instanceof Failure) {
  63. Failure failure = (Failure) arg;
  64. persistMeasure(buildMeasure(failure));
  65. }
  66. }
  67. }
  68. private static Measure buildMeasure(Failure failure) {
  69. Throwable throwable = failure.getException();
  70. Description description = failure.getDescription();
  71. return newMeasureBuilder()
  72. .setTimestamp(LocalDateTime.now(ZoneId.of("UTC")).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
  73. .setBranchName(BRANCH_NAME)
  74. .setCommit(COMMIT_HASH)
  75. .setBuild(BUILD_NUMBER)
  76. .setCategory(QA_CATEGORY)
  77. .setTestClass(description.getClassName())
  78. .setTestMethod(description.getMethodName())
  79. .setExceptionClass(throwable.getClass().getName())
  80. .setExceptionMessage(failure.getMessage())
  81. .setExceptionLogs(failure.getTrimmedTrace())
  82. .build();
  83. }
  84. public static void persistMeasure(Measure measure) {
  85. try {
  86. Files.write(PATH, GSON.toJson(measure).getBytes(UTF_8), StandardOpenOption.APPEND);
  87. Files.write(PATH, "\n".getBytes(UTF_8), StandardOpenOption.APPEND);
  88. } catch (IOException e) {
  89. // Ignore
  90. }
  91. }
  92. }