xref: /aosp_15_r20/external/federated-compute/fcp/tracing/test/text_tracing_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <fstream>
16 #include <string>
17 
18 #include "gtest/gtest.h"
19 #include "absl/strings/str_cat.h"
20 #include "fcp/base/monitoring.h"
21 #include "fcp/base/platform.h"
22 #include "fcp/testing/testing.h"
23 #include "fcp/tracing/test/tracing_schema.h"
24 #include "fcp/tracing/text_tracing_recorder.h"
25 #include "re2/re2.h"
26 
27 namespace fcp {
28 namespace {
29 
30 constexpr char kBaselineDir[] = "fcp/tracing/test/testdata";
31 
PostProcessOutput(std::string * input)32 bool PostProcessOutput(std::string* input) {
33   RE2 timestamp_pattern("\\d{4}-\\d{2}-\\d{2}T[[:^blank:]]*");
34   return RE2::GlobalReplace(input, timestamp_pattern, "${TIME}") > 0;
35 }
36 
TEST(Tracing,Basic)37 TEST(Tracing, Basic) {
38   std::string out_file =
39       ConcatPath(testing::TempDir(), absl::StrCat(TestName(), ".out"));
40   {
41     TextTracingRecorder p(out_file, absl::UTCTimeZone());
42     p.InstallAsGlobal();
43     Trace<EventFoo>(10, 20);
44     {
45       TracingSpan<SpanWithId> inner(111);
46       Trace<EventFoo>(222, 333);
47       auto ignored = TraceError<ErrorEvent>("Oops!");
48       (void)ignored;
49       {
50         TracingSpan<SpanWithId> very_inner(999);
51         Trace<EventFoo>(555, 666);
52       }
53     }
54     {
55       TracingSpan<SpanWithNoData> inner;
56       Trace<EventWithNoData>();
57     }
58     {
59       auto long_running_span =
60           std::make_unique<UnscopedTracingSpan<SpanWithNoData>>(
61               TracingSpanRef::Top());
62       TracingSpan<SpanWithId> foo_inner(long_running_span->Ref(), 222);
63       Trace<EventBar>(333, "Hello world!");
64     }
65   }
66 
67   // Reading out file
68   std::string report = ReadFileToString(out_file).value();
69   ASSERT_TRUE(PostProcessOutput(&report));
70   // Producing report which is expected to precisely match .baseline file.
71   std::ostringstream expected;
72   expected << "" << std::endl;
73 
74   // Compare produced report with baseline.
75   std::string baseline_path =
76       ConcatPath(kBaselineDir, absl::StrCat(TestName(), ".baseline"));
77   auto status_s = VerifyAgainstBaseline(baseline_path, report);
78   ASSERT_TRUE(status_s.ok()) << status_s.status();
79   auto& diff = status_s.value();
80   if (!diff.empty()) {
81     FAIL() << diff;
82   }
83 }
84 
TEST(Tracing,TimestampReplace)85 TEST(Tracing, TimestampReplace) {
86   std::string timestamp = "2019-10-24T22:07:07.916321247+00:00";
87   ASSERT_TRUE(PostProcessOutput(&timestamp));
88   ASSERT_EQ(timestamp, "${TIME}");
89 }
90 
TEST(Tracing,DefaultProvider)91 TEST(Tracing, DefaultProvider) {
92   // This just triggers default stderr logging codepath, without verifying it
93   Trace<EventBar>(444, "Hello world!");
94   TracingSpan<SpanWithId> inner(111);
95   Trace<EventFoo>(222, 333);
96 }
97 
98 }  // namespace
99 }  // namespace fcp
100