Add WriteIsolatedOutput() functions
WriteIsolatedOutput() functions write large content into swarming isolated output folder, which are useful to log large test data for debugging purpose. BUG=webrtc:6732 TBR=holmer@webrtc.org Review-Url: https://codereview.webrtc.org/2558693002 Cr-Commit-Position: refs/heads/master@{#15616}
This commit is contained in:
parent
fba7900f76
commit
2769ec62c0
@ -61,6 +61,21 @@ File File::Create(const Pathname& path) {
|
||||
return Create(Pathname(path));
|
||||
}
|
||||
|
||||
// static
|
||||
bool File::Remove(const std::string& path) {
|
||||
return RemoveFile(path);
|
||||
}
|
||||
|
||||
// static
|
||||
bool File::Remove(Pathname&& path) {
|
||||
return Remove(NormalizePathname(std::move(path)));
|
||||
}
|
||||
|
||||
// static
|
||||
bool File::Remove(const Pathname& path) {
|
||||
return Remove(Pathname(path));
|
||||
}
|
||||
|
||||
File::File(File&& other) : file_(other.file_) {
|
||||
other.file_ = kInvalidPlatformFileValue;
|
||||
}
|
||||
|
||||
@ -47,6 +47,11 @@ class File {
|
||||
static File Create(Pathname&& path);
|
||||
static File Create(const Pathname& path);
|
||||
|
||||
// Remove a file in the file system.
|
||||
static bool Remove(const std::string& path);
|
||||
static bool Remove(Pathname&& path);
|
||||
static bool Remove(const Pathname& path);
|
||||
|
||||
size_t Write(const uint8_t* data, size_t length);
|
||||
size_t Read(uint8_t* buffer, size_t length);
|
||||
|
||||
|
||||
@ -189,4 +189,13 @@ TEST_F(FileTest, CreateFromPathname) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(FileTest, ShouldBeAbleToRemoveFile) {
|
||||
{
|
||||
File file = File::Open(Pathname(path_));
|
||||
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
|
||||
}
|
||||
|
||||
ASSERT_TRUE(File::Remove(Pathname(path_))) << "Error: " << LastError();
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
@ -128,6 +128,8 @@ rtc_source_set("test_support") {
|
||||
"testsupport/frame_reader.h",
|
||||
"testsupport/frame_writer.cc",
|
||||
"testsupport/frame_writer.h",
|
||||
"testsupport/isolated_output.cc",
|
||||
"testsupport/isolated_output.h",
|
||||
"testsupport/metrics/video_metrics.cc",
|
||||
"testsupport/metrics/video_metrics.h",
|
||||
"testsupport/mock/mock_frame_reader.h",
|
||||
@ -148,6 +150,7 @@ rtc_source_set("test_support") {
|
||||
"../system_wrappers",
|
||||
"//testing/gmock",
|
||||
"//testing/gtest",
|
||||
"//third_party/gflags",
|
||||
]
|
||||
|
||||
public_deps = [
|
||||
@ -213,6 +216,7 @@ rtc_test("test_support_unittests") {
|
||||
"testsupport/always_passing_unittest.cc",
|
||||
"testsupport/frame_reader_unittest.cc",
|
||||
"testsupport/frame_writer_unittest.cc",
|
||||
"testsupport/isolated_output_unittest.cc",
|
||||
"testsupport/metrics/video_metrics_unittest.cc",
|
||||
"testsupport/packet_reader_unittest.cc",
|
||||
"testsupport/perf_test_unittest.cc",
|
||||
@ -248,6 +252,7 @@ rtc_test("test_support_unittests") {
|
||||
"../modules/video_capture",
|
||||
"//testing/gmock",
|
||||
"//testing/gtest",
|
||||
"//third_party/gflags",
|
||||
]
|
||||
}
|
||||
rtc_source_set("fileutils_unittests") {
|
||||
|
||||
53
webrtc/test/testsupport/isolated_output.cc
Normal file
53
webrtc/test/testsupport/isolated_output.cc
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "webrtc/test/testsupport/isolated_output.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "gflags/gflags.h"
|
||||
#include "webrtc/base/file.h"
|
||||
#include "webrtc/base/logging.h"
|
||||
#include "webrtc/base/pathutils.h"
|
||||
#include "webrtc/test/testsupport/fileutils.h"
|
||||
|
||||
DEFINE_string(isolated_out_dir, webrtc::test::OutputPath(),
|
||||
"The isolated output folder provided by swarming test framework.");
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
bool WriteIsolatedOutput(const char* filename,
|
||||
const uint8_t* buffer,
|
||||
size_t length) {
|
||||
if (FLAGS_isolated_out_dir.empty()) {
|
||||
LOG(LS_WARNING) << "No isolated_out_dir defined.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filename == nullptr || strlen(filename) == 0) {
|
||||
LOG(LS_WARNING) << "filename must be provided.";
|
||||
return false;
|
||||
}
|
||||
|
||||
rtc::File output =
|
||||
rtc::File::Create(rtc::Pathname(FLAGS_isolated_out_dir, filename));
|
||||
|
||||
return output.IsOpen() && output.Write(buffer, length) == length;
|
||||
}
|
||||
|
||||
bool WriteIsolatedOutput(const char* filename, const std::string& content) {
|
||||
return WriteIsolatedOutput(filename,
|
||||
reinterpret_cast<const uint8_t*>(content.c_str()),
|
||||
content.length());
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
35
webrtc/test/testsupport/isolated_output.h
Normal file
35
webrtc/test/testsupport/isolated_output.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef WEBRTC_TEST_TESTSUPPORT_ISOLATED_OUTPUT_H_
|
||||
#define WEBRTC_TEST_TESTSUPPORT_ISOLATED_OUTPUT_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
// Writes a |length| bytes array |buffer| to |filename| in isolated output
|
||||
// directory defined by swarming. If the file is existing, content will be
|
||||
// appended. Otherwise a new file will be created. This function returns false
|
||||
// if isolated output directory has not been defined, or |filename| indicates an
|
||||
// invalid or non-writable file, or underlying file system errors.
|
||||
bool WriteIsolatedOutput(const char* filename,
|
||||
const uint8_t* buffer,
|
||||
size_t length);
|
||||
|
||||
bool WriteIsolatedOutput(const char* filename, const std::string& content);
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_TEST_TESTSUPPORT_ISOLATED_OUTPUT_H_
|
||||
61
webrtc/test/testsupport/isolated_output_unittest.cc
Normal file
61
webrtc/test/testsupport/isolated_output_unittest.cc
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "webrtc/test/testsupport/isolated_output.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gflags/gflags.h"
|
||||
#include "webrtc/base/file.h"
|
||||
#include "webrtc/base/pathutils.h"
|
||||
#include "webrtc/base/platform_file.h"
|
||||
#include "webrtc/test/gtest.h"
|
||||
|
||||
DECLARE_string(isolated_out_dir);
|
||||
|
||||
namespace webrtc {
|
||||
namespace test {
|
||||
|
||||
TEST(IsolatedOutputTest, ShouldRejectInvalidIsolatedOutDir) {
|
||||
std::string backup = FLAGS_isolated_out_dir;
|
||||
FLAGS_isolated_out_dir = "";
|
||||
ASSERT_FALSE(WriteIsolatedOutput("a-file", "some-contents"));
|
||||
FLAGS_isolated_out_dir = backup;
|
||||
}
|
||||
|
||||
TEST(IsolatedOutputTest, ShouldRejectInvalidFileName) {
|
||||
ASSERT_FALSE(WriteIsolatedOutput(nullptr, "some-contents"));
|
||||
ASSERT_FALSE(WriteIsolatedOutput("", "some-contents"));
|
||||
}
|
||||
|
||||
// Sets isolated_out_dir=<a-writable-path> to execute this test.
|
||||
TEST(IsolatedOutputTest, ShouldBeAbleToWriteContent) {
|
||||
const char* filename = "a-file";
|
||||
const char* content = "some-contents";
|
||||
if (WriteIsolatedOutput(filename, content)) {
|
||||
rtc::Pathname out_file(FLAGS_isolated_out_dir, filename);
|
||||
rtc::File input = rtc::File::Open(out_file);
|
||||
EXPECT_TRUE(input.IsOpen());
|
||||
EXPECT_TRUE(input.Seek(0));
|
||||
uint8_t buffer[32];
|
||||
EXPECT_EQ(input.Read(buffer, strlen(content)), strlen(content));
|
||||
buffer[strlen(content)] = 0;
|
||||
EXPECT_EQ(std::string(content),
|
||||
std::string(reinterpret_cast<char*>(buffer)));
|
||||
input.Close();
|
||||
|
||||
EXPECT_TRUE(rtc::File::Remove(out_file));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace webrtc
|
||||
Loading…
x
Reference in New Issue
Block a user