8167548: [TESTBUG] Logging tests put log files in source tree
authorhseigel
Wed, 20 Feb 2019 08:10:40 -0500
changeset 53845 1807da9ad196
parent 53844 8323fdac6da5
child 53846 fe95464806a7
8167548: [TESTBUG] Logging tests put log files in source tree Summary: Create log files in temp directory, instead of cwd. Reviewed-by: coleenp, dholmes
src/hotspot/share/logging/logFileOutput.cpp
src/hotspot/share/logging/logFileOutput.hpp
test/hotspot/gtest/logging/logTestFixture.cpp
test/hotspot/gtest/logging/logTestUtils.inline.hpp
test/hotspot/gtest/logging/test_logConfiguration.cpp
test/hotspot/gtest/logging/test_logFileOutput.cpp
test/hotspot/gtest/logging/test_logTagSetDescriptions.cpp
--- a/src/hotspot/share/logging/logFileOutput.cpp	Wed Feb 20 13:43:28 2019 +0100
+++ b/src/hotspot/share/logging/logFileOutput.cpp	Wed Feb 20 08:10:40 2019 -0500
@@ -51,6 +51,14 @@
   _file_name = make_file_name(name + strlen(Prefix), _pid_str, _vm_start_time_str);
 }
 
+const char* LogFileOutput::cur_log_file_name() {
+  if (strlen(_archive_name) == 0) {
+    return _file_name;
+  } else {
+    return _archive_name;
+  }
+}
+
 void LogFileOutput::set_file_name_parameters(jlong vm_start_time) {
   int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id());
   assert(res > 0, "PID buffer too small");
@@ -234,6 +242,7 @@
     _file_count_max_digits = number_of_digits(_file_count - 1);
     _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
     _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
+    _archive_name[0] = 0;
   }
 
   log_trace(logging)("Initializing logging to file '%s' (filecount: %u"
--- a/src/hotspot/share/logging/logFileOutput.hpp	Wed Feb 20 13:43:28 2019 +0100
+++ b/src/hotspot/share/logging/logFileOutput.hpp	Wed Feb 20 08:10:40 2019 -0500
@@ -92,6 +92,7 @@
     return _name;
   }
 
+  const char* cur_log_file_name();
   static const char* const Prefix;
   static void set_file_name_parameters(jlong start_time);
 };
--- a/test/hotspot/gtest/logging/logTestFixture.cpp	Wed Feb 20 13:43:28 2019 +0100
+++ b/test/hotspot/gtest/logging/logTestFixture.cpp	Wed Feb 20 08:10:40 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,11 +33,12 @@
 #include "utilities/ostream.hpp"
 
 LogTestFixture::LogTestFixture() : _n_snapshots(0), _configuration_snapshot(NULL) {
-  // Set up TestLogFileName to include PID, testcase name and test name
-  int ret = jio_snprintf(_filename, sizeof(_filename), "testlog.pid%d.%s.%s.log",
-                         os::current_process_id(),
-                         ::testing::UnitTest::GetInstance()->current_test_info()->test_case_name(),
-                         ::testing::UnitTest::GetInstance()->current_test_info()->name());
+
+  // Set up TestLogFileName to include temp_dir, PID, testcase name and test name.
+  const testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
+  int ret = jio_snprintf(_filename, sizeof(_filename), "%s%stestlog.pid%d.%s.%s.log",
+                         os::get_temp_directory(), os::file_separator(), os::current_process_id(),
+                         test_info->test_case_name(), test_info->name());
   EXPECT_GT(ret, 0) << "_filename buffer issue";
   TestLogFileName = _filename;
 
--- a/test/hotspot/gtest/logging/logTestUtils.inline.hpp	Wed Feb 20 13:43:28 2019 +0100
+++ b/test/hotspot/gtest/logging/logTestUtils.inline.hpp	Wed Feb 20 08:10:40 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -88,6 +88,30 @@
   guarantee(success, "Failed to disable logging to file '%s'", filename);
 }
 
+static const char* tmp_dir = os::get_temp_directory();
+static const char* file_sep = os::file_separator();
+
+// Prepend filename with the temp directory and pid and return the result as a
+// resource allocated string.
+static inline char* prepend_temp_dir(const char* filename) {
+  size_t temp_file_len = strlen(tmp_dir) + strlen(file_sep) + strlen(filename) + 28;
+  char* temp_file = NEW_RESOURCE_ARRAY(char, temp_file_len);
+  int ret = jio_snprintf(temp_file, temp_file_len, "%s%spid%d.%s",
+                         tmp_dir, file_sep,
+                         os::current_process_id(), filename);
+  return temp_file;
+}
+
+// Prepend filename with specified prefix and the temp directory and return the
+// result as a malloc allocated string.  This is used by test_logFileOutput.cpp.
+static inline char* prepend_prefix_temp_dir(const char* prefix, const char* filename) {
+  size_t temp_file_len = strlen(prefix) + strlen(tmp_dir) + strlen(file_sep) + strlen(filename) + 1;
+  char* temp_file = (char*)os::malloc(temp_file_len, mtLogging);
+  int ret = jio_snprintf(temp_file, temp_file_len, "%s%s%s%s",
+                         prefix, tmp_dir, file_sep, filename);
+  return temp_file;
+}
+
 // Read a complete line from fp and return it as a resource allocated string.
 // Returns NULL on EOF.
 static inline char* read_line(FILE* fp) {
--- a/test/hotspot/gtest/logging/test_logConfiguration.cpp	Wed Feb 20 13:43:28 2019 +0100
+++ b/test/hotspot/gtest/logging/test_logConfiguration.cpp	Wed Feb 20 08:10:40 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -403,16 +403,15 @@
 
   // Make sure prefixes are ignored when used within quotes
   // (this should create a log with "file=" in its filename)
-  int ret = jio_snprintf(buf, sizeof(buf), "\"file=%s\"", TestLogFileName);
-  ASSERT_NE(-1, ret);
-  set_log_config(buf, "logging=trace");
+  // Note that the filename cannot contain directories because
+  // it is being prefixed with "file=".
+  const char* leafFileName = "\"file=leaf_file_name\"";
+  set_log_config(leafFileName, "logging=trace");
   EXPECT_TRUE(is_described("#3: ")) << "prefix within quotes not ignored as it should be";
-  set_log_config(buf, "all=off");
+  set_log_config(leafFileName, "all=off");
 
   // Remove the extra log file created
-  ret = jio_snprintf(buf, sizeof(buf), "file=%s", TestLogFileName);
-  ASSERT_NE(-1, ret);
-  delete_file(buf);
+  delete_file("file=leaf_file_name");
 }
 
 static size_t count_occurrences(const char* haystack, const char* needle) {
--- a/test/hotspot/gtest/logging/test_logFileOutput.cpp	Wed Feb 20 13:43:28 2019 +0100
+++ b/test/hotspot/gtest/logging/test_logFileOutput.cpp	Wed Feb 20 08:10:40 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,7 @@
 #include "utilities/globalDefinitions.hpp"
 #include "utilities/ostream.hpp"
 
-static const char* name = "file=testlog.pid%p.%t.log";
+static const char* name = prepend_prefix_temp_dir("file=", "testlog.pid%p.%t.log");
 
 // Test parsing a bunch of valid file output options
 TEST_VM(LogFileOutput, parse_valid) {
@@ -46,11 +46,6 @@
 
   // Override LogOutput's vm_start time to get predictable file name
   LogFileOutput::set_file_name_parameters(0);
-  char expected_filename[1 * K];
-  int ret = jio_snprintf(expected_filename, sizeof(expected_filename),
-                         "testlog.pid%d.1970-01-01_01-00-00.log",
-                         os::current_process_id());
-  ASSERT_GT(ret, 0) << "Buffer too small";
 
   for (size_t i = 0; i < ARRAY_SIZE(valid_options); i++) {
     ResourceMark rm;
@@ -60,8 +55,8 @@
       EXPECT_STREQ(name, fo.name());
       EXPECT_TRUE(fo.initialize(valid_options[i], &ss))
         << "Did not accept valid option(s) '" << valid_options[i] << "': " << ss.as_string();
+      remove(fo.cur_log_file_name());
     }
-    remove(expected_filename);
   }
 }
 
@@ -105,11 +100,11 @@
 }
 
 TEST_VM(LogFileOutput, startup_rotation) {
+  ResourceMark rm;
   const size_t rotations = 5;
-  const char* filename = "start-rotate-test";
+  const char* filename = prepend_temp_dir("start-rotate-test");
   char* rotated_file[rotations];
 
-  ResourceMark rm;
   for (size_t i = 0; i < rotations; i++) {
     size_t len = strlen(filename) + 3;
     rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
@@ -142,8 +137,9 @@
 }
 
 TEST_VM(LogFileOutput, startup_truncation) {
-  const char* filename = "start-truncate-test";
-  const char* archived_filename = "start-truncate-test.0";
+  ResourceMark rm;
+  const char* filename = prepend_temp_dir("start-truncate-test");
+  const char* archived_filename = prepend_temp_dir("start-truncate-test.0");
 
   delete_file(filename);
   delete_file(archived_filename);
--- a/test/hotspot/gtest/logging/test_logTagSetDescriptions.cpp	Wed Feb 20 13:43:28 2019 +0100
+++ b/test/hotspot/gtest/logging/test_logTagSetDescriptions.cpp	Wed Feb 20 08:10:40 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -48,7 +48,8 @@
 }
 
 TEST_VM(LogTagSetDescriptions, command_line_help) {
-  const char* filename = "logtagset_descriptions";
+  ResourceMark rm;
+  const char* filename = prepend_temp_dir("logtagset_descriptions");
   FILE* fp = fopen(filename, "w+");
   ASSERT_NE((void*)NULL, fp);
   fileStream stream(fp);