From patchwork Tue Jul 16 07:05:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 1709 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5511C61AC4 for ; Tue, 16 Jul 2019 09:05:23 +0200 (CEST) Received: from emerald.amanokami.net (unknown [IPv6:2a00:79e1:abc:3602:b57a:2dda:be67:ac6e]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D1E51564; Tue, 16 Jul 2019 09:05:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1563260722; bh=PXEJGWmjtr53BuygWSH12LWZDyh1PzFNMoJSnjKf3G0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o2/elEmEFMP612VBTk4FAPGQTK3QZQgXDsDY/gX+WTToC7JS/Zc3Rr5fvArurNAV8 nbx4qfLzHs/LQqE5vkJlwUJiV+Q56HVKhGfSk+cOJVJc0CfvGEH8GaF07EHyU81I4Z zrYwhIQdmis52XSWwKpcfFRvRas2muIBEu5Bb8w8= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Tue, 16 Jul 2019 16:05:07 +0900 Message-Id: <20190716070508.24589-3-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190716070508.24589-1-paul.elder@ideasonboard.com> References: <20190716070508.24589-1-paul.elder@ideasonboard.com> Subject: [libcamera-devel] [PATCH v2 3/4] test: logging: add logging process test X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Jul 2019 07:05:23 -0000 Add a test to test that logging works in isolated child processes, Only logSetFile is tested, because stdout and stderr are closed for isolated child processes, and syslog and the none logging destinations are expected to be the same as non-isolated processes. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart --- Changes in v2: - unlink the log file - move random number generation and process completion connection to init - other minor changes test/log_process.cpp | 153 +++++++++++++++++++++++++++++++++++++++++++ test/meson.build | 1 + 2 files changed, 154 insertions(+) create mode 100644 test/log_process.cpp diff --git a/test/log_process.cpp b/test/log_process.cpp new file mode 100644 index 0000000..499a5a8 --- /dev/null +++ b/test/log_process.cpp @@ -0,0 +1,153 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * log_process.cpp - Logging in isolated child process test + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "log.h" +#include "process.h" +#include "test.h" +#include "utils.h" + +using namespace std; +using namespace libcamera; + +static string message("hello from the child"); + +LOG_DEFINE_CATEGORY(LogProcessTest) + +class LogProcessTestChild +{ +public: + int run(int status, int num) + { + usleep(50000); + + string logPath = "/tmp/libcamera.worker.test." + + to_string(num) + ".log"; + if (logSetFile(logPath.c_str()) < 0) + return TestSkip; + + LOG(LogProcessTest, Warning) << message; + + return status; + } +}; + +class LogProcessTest : public Test +{ +protected: + int init() + { + random_device random; + num_ = random(); + + proc_.finished.connect(this, &LogProcessTest::procFinished); + return 0; + } + + int run() + { + EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher(); + Timer timeout; + + int exitCode = 42; + vector args; + args.push_back(to_string(exitCode)); + args.push_back(to_string(num_)); + int ret = proc_.start("/proc/self/exe", args); + if (ret) { + cerr << "failed to start process" << endl; + return TestFail; + } + + timeout.start(200); + while (timeout.isRunning()) + dispatcher->processEvents(); + + if (exitStatus_ != Process::NormalExit) { + cerr << "process did not exit normally" << endl; + return TestFail; + } + + if (exitCode_ == TestSkip) + return TestSkip; + + if (exitCode_ != exitCode) { + cerr << "exit code should be " << exitCode + << ", actual is " << exitCode_ << endl; + return TestFail; + } + + logPath_ = "/tmp/libcamera.worker.test." + + to_string(num_) + ".log"; + int fd = open(logPath_.c_str(), O_RDONLY, S_IRUSR); + if (fd < 0) { + cerr << "failed to open tmp log file" << endl; + return TestFail; + } + + char buf[200]; + memset(buf, 0, sizeof(buf)); + if (read(fd, buf, sizeof(buf)) < 0) { + cerr << "Failed to read tmp log file" << endl; + return TestFail; + } + close(fd); + + string str(buf); + if (str.find(message) == string::npos) + return TestFail; + + return TestPass; + } + + void cleanup() + { + unlink(logPath_.c_str()); + } + +private: + void procFinished(Process *proc, enum Process::ExitStatus exitStatus, int exitCode) + { + exitStatus_ = exitStatus; + exitCode_ = exitCode; + } + + Process proc_; + enum Process::ExitStatus exitStatus_; + string logPath_; + int exitCode_; + int num_; +}; + +/* + * Can't use TEST_REGISTER() as single binary needs to act as both + * parent and child processes. + */ +int main(int argc, char **argv) +{ + if (argc == 3) { + int status = std::stoi(argv[1]); + int num = std::stoi(argv[2]); + LogProcessTestChild child; + return child.run(status, num); + } + + return LogProcessTest().execute(); +} diff --git a/test/meson.build b/test/meson.build index ad1a2f2..658f283 100644 --- a/test/meson.build +++ b/test/meson.build @@ -23,6 +23,7 @@ public_tests = [ internal_tests = [ ['camera-sensor', 'camera-sensor.cpp'], ['log', 'log.cpp'], + ['log_process', 'log_process.cpp'], ['message', 'message.cpp'], ['signal-threads', 'signal-threads.cpp'], ['threads', 'threads.cpp'],