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