[libcamera-devel,v2,2/4] test: logging: add logSetStream test

Message ID 20190716070508.24589-2-paul.elder@ideasonboard.com
State Accepted
Headers show
Series
  • [libcamera-devel,v2,1/4] libcamera: logging: add syslog, stream, and nowhere logging targets
Related show

Commit Message

Paul Elder July 16, 2019, 7:05 a.m. UTC
Test the new logSetStream logging API call. Reorganize the logging API
tests at the same time.

logSetTarget for the syslog logging destination is not tested.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
---
Changes in v2:
- add testing for logSetTarget, to test that writing to none doesn't
  crash, and to test that setting to file or stream fail
- make verifyOutput() take an istream (instead of a string)
- make total test passage checking nicer

 test/log.cpp | 106 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 81 insertions(+), 25 deletions(-)

Comments

Laurent Pinchart July 16, 2019, 7:47 a.m. UTC | #1
Hi Paul,

Thank you for the patch.

On Tue, Jul 16, 2019 at 04:05:06PM +0900, Paul Elder wrote:
> Test the new logSetStream logging API call. Reorganize the logging API
> tests at the same time.
> 
> logSetTarget for the syslog logging destination is not tested.
> 
> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
> Changes in v2:
> - add testing for logSetTarget, to test that writing to none doesn't
>   crash, and to test that setting to file or stream fail
> - make verifyOutput() take an istream (instead of a string)
> - make total test passage checking nicer
> 
>  test/log.cpp | 106 +++++++++++++++++++++++++++++++++++++++------------
>  1 file changed, 81 insertions(+), 25 deletions(-)
> 
> diff --git a/test/log.cpp b/test/log.cpp
> index 89fb5ca..33622f8 100644
> --- a/test/log.cpp
> +++ b/test/log.cpp
> @@ -29,19 +29,8 @@ LOG_DEFINE_CATEGORY(LogAPITest)
>  class LogAPITest : public Test
>  {
>  protected:
> -	int run() override
> +	void doLogging()
>  	{
> -		int fd = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
> -		if (fd < 0) {
> -			cerr << "Failed to open tmp log file" << endl;
> -			return TestFail;
> -		}
> -
> -		char path[32];
> -		snprintf(path, sizeof(path), "/proc/self/fd/%u", fd);
> -
> -		logSetFile(path);
> -
>  		logSetLevel("LogAPITest", "DEBUG");
>  		LOG(LogAPITest, Info) << "good 1";
>  
> @@ -55,20 +44,13 @@ protected:
>  		logSetLevel("LogAPITest", "WARN");
>  		LOG(LogAPITest, Warning) << "good 5";
>  		LOG(LogAPITest, Info) << "bad";
> +	}
>  
> -		char buf[1000];
> -		memset(buf, 0, sizeof(buf));
> -		lseek(fd, 0, SEEK_SET);
> -		if (read(fd, buf, sizeof(buf)) < 0) {
> -			cerr << "Failed to read tmp log file" << endl;
> -			return TestFail;
> -		}
> -		close(fd);
> -
> -		std::list<int> goodList = { 1, 3, 5 };
> -		std::basic_istringstream<char> iss((std::string(buf)));
> -		std::string line;
> -		while (getline(iss, line)) {
> +	int verifyOutput(istream &is)
> +	{
> +		list<int> goodList = { 1, 3, 5 };
> +		string line;
> +		while (getline(is, line)) {
>  			if (goodList.empty()) {
>  				cout << "Too many log lines" << endl;
>  				return TestFail;
> @@ -90,6 +72,80 @@ protected:
>  
>  		return TestPass;
>  	}
> +
> +	int testFile()
> +	{
> +		int fd = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
> +		if (fd < 0) {
> +			cerr << "Failed to open tmp log file" << endl;
> +			return TestFail;
> +		}
> +
> +		char path[32];
> +		snprintf(path, sizeof(path), "/proc/self/fd/%u", fd);
> +
> +		if (logSetFile(path) < 0) {
> +			cerr << "Failed to set log file" << endl;
> +			return TestFail;
> +		}
> +
> +		doLogging();
> +
> +		char buf[1000];
> +		memset(buf, 0, sizeof(buf));
> +		lseek(fd, 0, SEEK_SET);
> +		if (read(fd, buf, sizeof(buf)) < 0) {
> +			cerr << "Failed to read tmp log file" << endl;
> +			return TestFail;
> +		}
> +		close(fd);
> +
> +		istringstream iss(buf);
> +		return verifyOutput(iss);
> +	}
> +
> +	int testStream()
> +	{
> +		stringstream log;
> +		/* Never fails, so no need to check return value */
> +		logSetStream(&log);
> +
> +		doLogging();
> +
> +		return verifyOutput(log);
> +	}
> +
> +	int testTarget()
> +	{
> +		logSetTarget(LoggingTargetNone);
> +		logSetLevel("LogAPITest", "DEBUG");
> +		LOG(LogAPITest, Info) << "don't crash please";
> +
> +		if (!logSetTarget(LoggingTargetFile))
> +			return TestFail;
> +
> +		if (!logSetTarget(LoggingTargetStream))
> +			return TestFail;
> +
> +		return TestPass;
> +	}
> +
> +	int run() override
> +	{
> +		int ret = testFile();
> +		if (ret != TestPass)
> +			return TestFail;
> +
> +		ret = testStream();
> +		if (ret != TestPass)
> +			return TestFail;
> +
> +		ret = testTarget();
> +		if (ret != TestPass)
> +			return TestFail;
> +
> +		return TestPass;
> +	}
>  };
>  
>  TEST_REGISTER(LogAPITest)

Patch

diff --git a/test/log.cpp b/test/log.cpp
index 89fb5ca..33622f8 100644
--- a/test/log.cpp
+++ b/test/log.cpp
@@ -29,19 +29,8 @@  LOG_DEFINE_CATEGORY(LogAPITest)
 class LogAPITest : public Test
 {
 protected:
-	int run() override
+	void doLogging()
 	{
-		int fd = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
-		if (fd < 0) {
-			cerr << "Failed to open tmp log file" << endl;
-			return TestFail;
-		}
-
-		char path[32];
-		snprintf(path, sizeof(path), "/proc/self/fd/%u", fd);
-
-		logSetFile(path);
-
 		logSetLevel("LogAPITest", "DEBUG");
 		LOG(LogAPITest, Info) << "good 1";
 
@@ -55,20 +44,13 @@  protected:
 		logSetLevel("LogAPITest", "WARN");
 		LOG(LogAPITest, Warning) << "good 5";
 		LOG(LogAPITest, Info) << "bad";
+	}
 
-		char buf[1000];
-		memset(buf, 0, sizeof(buf));
-		lseek(fd, 0, SEEK_SET);
-		if (read(fd, buf, sizeof(buf)) < 0) {
-			cerr << "Failed to read tmp log file" << endl;
-			return TestFail;
-		}
-		close(fd);
-
-		std::list<int> goodList = { 1, 3, 5 };
-		std::basic_istringstream<char> iss((std::string(buf)));
-		std::string line;
-		while (getline(iss, line)) {
+	int verifyOutput(istream &is)
+	{
+		list<int> goodList = { 1, 3, 5 };
+		string line;
+		while (getline(is, line)) {
 			if (goodList.empty()) {
 				cout << "Too many log lines" << endl;
 				return TestFail;
@@ -90,6 +72,80 @@  protected:
 
 		return TestPass;
 	}
+
+	int testFile()
+	{
+		int fd = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
+		if (fd < 0) {
+			cerr << "Failed to open tmp log file" << endl;
+			return TestFail;
+		}
+
+		char path[32];
+		snprintf(path, sizeof(path), "/proc/self/fd/%u", fd);
+
+		if (logSetFile(path) < 0) {
+			cerr << "Failed to set log file" << endl;
+			return TestFail;
+		}
+
+		doLogging();
+
+		char buf[1000];
+		memset(buf, 0, sizeof(buf));
+		lseek(fd, 0, SEEK_SET);
+		if (read(fd, buf, sizeof(buf)) < 0) {
+			cerr << "Failed to read tmp log file" << endl;
+			return TestFail;
+		}
+		close(fd);
+
+		istringstream iss(buf);
+		return verifyOutput(iss);
+	}
+
+	int testStream()
+	{
+		stringstream log;
+		/* Never fails, so no need to check return value */
+		logSetStream(&log);
+
+		doLogging();
+
+		return verifyOutput(log);
+	}
+
+	int testTarget()
+	{
+		logSetTarget(LoggingTargetNone);
+		logSetLevel("LogAPITest", "DEBUG");
+		LOG(LogAPITest, Info) << "don't crash please";
+
+		if (!logSetTarget(LoggingTargetFile))
+			return TestFail;
+
+		if (!logSetTarget(LoggingTargetStream))
+			return TestFail;
+
+		return TestPass;
+	}
+
+	int run() override
+	{
+		int ret = testFile();
+		if (ret != TestPass)
+			return TestFail;
+
+		ret = testStream();
+		if (ret != TestPass)
+			return TestFail;
+
+		ret = testTarget();
+		if (ret != TestPass)
+			return TestFail;
+
+		return TestPass;
+	}
 };
 
 TEST_REGISTER(LogAPITest)