blob: 8bb47b8965a3a4b5b5426c1754fc7de05b991889 [file] [log] [blame]
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "crash-reporter/arc_service_failure_collector.h"
#include <unistd.h>
#include <string>
#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "crash-reporter/test_util.h"
using base::FilePath;
namespace {
bool s_metrics = false;
// Source tree log config file name.
const char kLogConfigFileName[] = "crash_reporter_logs.conf";
const char kTestFilename[] = "test-arc-service-failure";
const char kTestCrashDirectory[] = "test-crash-directory";
bool IsMetrics() {
return s_metrics;
}
} // namespace
class ArcServiceFailureCollectorMock : public ArcServiceFailureCollector {
public:
MOCK_METHOD0(SetUpDBus, void());
};
class ArcServiceFailureCollectorTest : public ::testing::Test {
void SetUp() {
s_metrics = true;
EXPECT_CALL(collector_, SetUpDBus()).WillRepeatedly(testing::Return());
collector_.Initialize(IsMetrics);
ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
test_path_ = scoped_temp_dir_.GetPath().Append(kTestFilename);
collector_.failure_report_path_ = test_path_.value();
test_crash_directory_ =
scoped_temp_dir_.GetPath().Append(kTestCrashDirectory);
CreateDirectory(test_crash_directory_);
collector_.set_crash_directory_for_test(test_crash_directory_);
collector_.set_log_config_path(kLogConfigFileName);
}
protected:
ArcServiceFailureCollectorMock collector_;
base::ScopedTempDir scoped_temp_dir_;
FilePath test_path_;
FilePath test_crash_directory_;
};
TEST_F(ArcServiceFailureCollectorTest, CollectOK) {
// Collector produces an ARC service failure report.
ASSERT_TRUE(test_util::CreateFile(
test_path_, "arc-crash main process (2563) terminated with status 2\n"));
collector_.SetServiceName("arc-crash");
EXPECT_TRUE(collector_.Collect());
EXPECT_TRUE(test_util::DirectoryHasFileWithPattern(
test_crash_directory_, "arc_service_failure_arc_crash.*.meta", NULL));
EXPECT_TRUE(test_util::DirectoryHasFileWithPattern(
test_crash_directory_, "arc_service_failure_arc_crash.*.log", NULL));
}
TEST_F(ArcServiceFailureCollectorTest, FailureReportDoesNotExist) {
// ARC service failure report file doesn't exist without a relevant log
// message.
EXPECT_TRUE(collector_.Collect());
EXPECT_TRUE(IsDirectoryEmpty(test_crash_directory_));
}