Skip to content

Commit

Permalink
fix ostream_log_test Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo committed Feb 13, 2022
1 parent 44795b6 commit 936cbcc
Showing 1 changed file with 74 additions and 56 deletions.
130 changes: 74 additions & 56 deletions exporters/ostream/test/ostream_log_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,27 @@ TEST(OstreamLogExporter, DefaultLogRecordToCout)
// Restore cout's original stringstream
std::cout.rdbuf(original);

std::string expectedOutput =
std::vector<std::string> expected_output{
"{\n"
" timestamp : 0\n"
" severity_num : 0\n"
" severity_text : INVALID\n"
" name : \n"
" body : \n"
" resource : {{telemetry.sdk.version: " OPENTELEMETRY_VERSION
"}, {telemetry.sdk.name: opentelemetry}, {telemetry.sdk.language: cpp}}\n"
" body : \n",
" resource : {",
"{telemetry.sdk.version: " OPENTELEMETRY_VERSION "}",
"{telemetry.sdk.name: opentelemetry}",
"{telemetry.sdk.language: cpp}",
" attributes : {}\n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" trace_flags : 00\n"
"}\n";
"}\n"};

ASSERT_EQ(output.str(), expectedOutput);
for (auto &expected : expected_output)
{
ASSERT_NE(output.str().find(expected), std::string::npos);
}
}

// Testing what a log record with only the "timestamp", "severity", "name" and "message" fields set,
Expand Down Expand Up @@ -121,24 +126,29 @@ TEST(OStreamLogExporter, SimpleLogToCout)
// Reset cout's original stringstream buffer
std::cout.rdbuf(original);

std::string expectedOutput =
std::vector<std::string> expected_output{
"{\n"
" timestamp : " +
std::to_string(now.time_since_epoch().count()) +
"\n"
" severity_num : 1\n"
" severity_text : TRACE\n"
" name : Name\n"
" body : Message\n"
" resource : {{telemetry.sdk.version: " OPENTELEMETRY_VERSION
"}, {telemetry.sdk.name: opentelemetry}, "
"{telemetry.sdk.language: cpp}}\n"
std::to_string(now.time_since_epoch().count()) +
"\n"
" severity_num : 1\n"
" severity_text : TRACE\n"
" name : Name\n"
" body : Message\n",
" resource : {",
"{telemetry.sdk.version: " OPENTELEMETRY_VERSION "}",
"{telemetry.sdk.name: opentelemetry}",
"{telemetry.sdk.language: cpp}",
" attributes : {}\n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" trace_flags : 00\n"
"}\n";
ASSERT_EQ(output.str(), expectedOutput);
"}\n"};

for (auto &expected : expected_output)
{
ASSERT_NE(output.str().find(expected), std::string::npos);
}
}

// ---------------------------------- Print to cerr --------------------------
Expand Down Expand Up @@ -172,26 +182,29 @@ TEST(OStreamLogExporter, LogWithStringAttributesToCerr)
// Reset cerr's original stringstream buffer
std::cerr.rdbuf(original);

std::string expectedOutput =
std::vector<std::string> expected_output{
"{\n"
" timestamp : 0\n"
" severity_num : 0\n"
" severity_text : INVALID\n"
" name : \n"
" body : \n"
" resource : {{telemetry.sdk.version: " OPENTELEMETRY_VERSION
"}, {telemetry.sdk.name: opentelemetry}, {telemetry.sdk.language: cpp}, {service.name: "
"unknown_service}, {key1: val1}}\n"
" body : \n",
" resource : {",
"{telemetry.sdk.version: " OPENTELEMETRY_VERSION "}",
"{telemetry.sdk.name: opentelemetry}",
"{telemetry.sdk.language: cpp}",
"{service.name: unknown_service}",
"{key1: val1}",
" attributes : {{a: 1}}\n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" trace_flags : 00\n"
"}\n";
// TODO this test fails on Mac
// issue https://github.com/open-telemetry/opentelemetry-cpp/issues/1187
# if !defined(__APPLE__)
ASSERT_EQ(stdcerrOutput.str(), expectedOutput);
# endif
"}\n"};

for (auto &expected : expected_output)
{
ASSERT_NE(stdcerrOutput.str().find(expected), std::string::npos);
}
}

// ---------------------------------- Print to clog -------------------------
Expand Down Expand Up @@ -231,26 +244,29 @@ TEST(OStreamLogExporter, LogWithVariantTypesToClog)
// Reset clog's original stringstream buffer
std::clog.rdbuf(original);

std::string expectedOutput =
std::vector<std::string> expected_output{
"{\n"
" timestamp : 0\n"
" severity_num : 0\n"
" severity_text : INVALID\n"
" name : \n"
" body : \n"
" resource : {{service.name: unknown_service}, "
"{telemetry.sdk.version: " OPENTELEMETRY_VERSION
"}, {telemetry.sdk.name: opentelemetry}, {telemetry.sdk.language: cpp}, {res1: [1, 2, 3]}}\n"
" body : \n",
" resource : {",
"{service.name: unknown_service}",
"{telemetry.sdk.version: " OPENTELEMETRY_VERSION "}",
"{telemetry.sdk.name: opentelemetry}",
"{telemetry.sdk.language: cpp}",
"{res1: [1, 2, 3]}",
" attributes : {{attr1: [0, 1, 0]}}\n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" trace_flags : 00\n"
"}\n";
// TODO this test fails on Mac
// issue https://github.com/open-telemetry/opentelemetry-cpp/issues/1187
# if !defined(__APPLE__)
ASSERT_EQ(stdclogOutput.str(), expectedOutput);
# endif
"}\n"};

for (auto &expected : expected_output)
{
ASSERT_NE(stdclogOutput.str().find(expected), std::string::npos);
}
}

// // ---------------------------------- Integration Tests -------------------------
Expand Down Expand Up @@ -286,28 +302,30 @@ TEST(OStreamLogExporter, IntegrationTest)
std::cout.rdbuf(original);

// Compare actual vs expected outputs
std::string expectedOutput =
std::vector<std::string> expected_output{
"{\n"
" timestamp : " +
std::to_string(now.time_since_epoch().count()) +
"\n"
" severity_num : 5\n"
" severity_text : DEBUG\n"
" name : \n"
" body : Hello\n"
" resource : {{service.name: unknown_service}, "
"{telemetry.sdk.version: " OPENTELEMETRY_VERSION
"}, {telemetry.sdk.name: opentelemetry}, {telemetry.sdk.language: cpp}}\n"
std::to_string(now.time_since_epoch().count()) +
"\n"
" severity_num : 5\n"
" severity_text : DEBUG\n"
" name : \n"
" body : Hello\n",
" resource : {",
"{telemetry.sdk.version: " OPENTELEMETRY_VERSION "}",
"{service.name: unknown_service}",
"{telemetry.sdk.name: opentelemetry}",
"{telemetry.sdk.language: cpp}",
" attributes : {}\n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" trace_flags : 00\n"
"}\n";
// TODO this test fails on Mac
// issue https://github.com/open-telemetry/opentelemetry-cpp/issues/1187
# if !defined(__APPLE__)
ASSERT_EQ(stdcoutOutput.str(), expectedOutput);
# endif
"}\n"};

for (auto &expected : expected_output)
{
ASSERT_NE(stdcoutOutput.str().find(expected), std::string::npos);
}
}

} // namespace logs
Expand Down

0 comments on commit 936cbcc

Please sign in to comment.