luci_test_result.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TESTING_PERF_LUCI_TEST_RESULT_H_
  5. #define TESTING_PERF_LUCI_TEST_RESULT_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/containers/flat_map.h"
  9. #include "base/files/file_path.h"
  10. #include "base/time/time.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace perf_test {
  13. // Generates TestResultEntry dict in LUCI Test Results format.
  14. // See: go/luci-test-results-design
  15. // //infra/go/src/go.chromium.org/luci/results/proto/v1/test_result.proto
  16. class LuciTestResult {
  17. public:
  18. // Represents a test result status.
  19. enum class Status {
  20. // The test status is unspecified.
  21. kUnspecified,
  22. // The test has passed.
  23. kPass,
  24. // The test has failed.
  25. kFail,
  26. // The test did not complete because it crashed.
  27. kCrash,
  28. // The test did not complete because it was interrupted, e.g. timeout.
  29. kAbort,
  30. // The test or test framework decided not to run the test, or the test was
  31. // not run due to previous tests timing out.
  32. kSkip
  33. };
  34. // Represents an artifact.
  35. struct Artifact {
  36. Artifact();
  37. Artifact(const Artifact& other);
  38. Artifact(const base::FilePath file_path, const std::string& content_type);
  39. Artifact(const std::string& contents, const std::string& content_type);
  40. ~Artifact();
  41. // Use only one of the two fields below.
  42. // Absolute path on the same machine running the test.
  43. absl::optional<base::FilePath> file_path;
  44. // The data of the artifact.
  45. absl::optional<std::string> contents;
  46. std::string content_type;
  47. };
  48. // Represents a tag.
  49. struct Tag {
  50. std::string key;
  51. std::string value;
  52. };
  53. LuciTestResult();
  54. LuciTestResult(const LuciTestResult& other);
  55. LuciTestResult(LuciTestResult&& other);
  56. ~LuciTestResult();
  57. // Helper to create a LuciTestResult and fill in info for the current gtest.
  58. static LuciTestResult CreateForGTest();
  59. // Adds a variant key-value pair to |extra_variant_pairs_|. See VariantDef in
  60. // //infra/go/src/go.chromium.org/luci/resultdb/proto/v1/common.proto
  61. // for more details.
  62. void AddVariant(const std::string& key, const std::string& value);
  63. // Adds an output artifact.
  64. void AddOutputArtifactFile(const std::string& artifact_name,
  65. const base::FilePath& file_path,
  66. const std::string& content_type);
  67. void AddOutputArtifactContents(const std::string& artifact_name,
  68. const std::string& contents,
  69. const std::string& content_type);
  70. // Adds a tag.
  71. void AddTag(const std::string& key, const std::string& value);
  72. // Writes to |result_file|.
  73. void WriteToFile(const base::FilePath& result_file) const;
  74. // Getters and setters.
  75. const std::string& test_path() const { return test_path_; }
  76. void set_test_path(const std::string& test_path) { test_path_ = test_path; }
  77. const base::flat_map<std::string, std::string>& extra_variant_pairs() const {
  78. return extra_variant_pairs_;
  79. }
  80. Status status() const { return status_; }
  81. void set_status(Status status) { status_ = status; }
  82. bool is_expected() const { return is_expected_; }
  83. void set_is_expected(bool is_expcted) { is_expected_ = is_expcted; }
  84. base::Time start_time() const { return start_time_; }
  85. void set_start_time(base::Time start_time) { start_time_ = start_time; }
  86. base::TimeDelta duration() const { return duration_; }
  87. void set_duration(base::TimeDelta duration) { duration_ = duration; }
  88. const base::flat_map<std::string, Artifact>& output_artifacts() const {
  89. return output_artifacts_;
  90. }
  91. const std::vector<Tag>& tags() const { return tags_; }
  92. private:
  93. // For gtest, |test_path_| is <test_suite_name>.<test_case_name>, without
  94. // the param annotations. E.g. "InstantiationName/SuiteName.CaseName/0"
  95. // will have "/0" stripped and be just "InstantiationName/SuiteName.CaseName".
  96. std::string test_path_;
  97. // For gtest, |extra_variant_pairs_| holds info about the type param and
  98. // value param for typed/parameterized tests.
  99. base::flat_map<std::string, std::string> extra_variant_pairs_;
  100. // Status of the test result.
  101. Status status_ = Status::kUnspecified;
  102. // Whether |status| is expected.
  103. bool is_expected_ = false;
  104. // Test start time.
  105. base::Time start_time_;
  106. // Duration of the test.
  107. base::TimeDelta duration_;
  108. // Artifacts of the test run.
  109. base::flat_map<std::string, Artifact> output_artifacts_;
  110. // Tags of the test run.
  111. std::vector<Tag> tags_;
  112. };
  113. } // namespace perf_test
  114. #endif // TESTING_PERF_LUCI_TEST_RESULT_H_