luci_test_result.h 4.6 KB

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