8165702: Convert LogFileOutput internal tests to GTest

Reviewed-by: rehn, rprotacio
This commit is contained in:
Marcus Larsson 2016-09-08 15:43:20 +02:00
parent 12fae773c0
commit e544c70170
4 changed files with 100 additions and 156 deletions

View file

@ -50,72 +50,6 @@
#define assert_char_not_in(c, s) \
assert(strchr(s, c) == NULL, "Expected '%s' to *not* contain character '%c'", s, c)
// Read a complete line from fp and return it as a resource allocated string.
// Returns NULL on EOF.
static char* read_line(FILE* fp) {
assert(fp != NULL, "bad fp");
int buflen = 512;
char* buf = NEW_RESOURCE_ARRAY(char, buflen);
long pos = ftell(fp);
char* ret = fgets(buf, buflen, fp);
while (ret != NULL && buf[strlen(buf) - 1] != '\n' && !feof(fp)) {
// retry with a larger buffer
buf = REALLOC_RESOURCE_ARRAY(char, buf, buflen, buflen * 2);
buflen *= 2;
// rewind to beginning of line
fseek(fp, pos, SEEK_SET);
// retry read with new buffer
ret = fgets(buf, buflen, fp);
}
return ret;
}
static size_t number_of_lines_with_substring_in_file(const char* filename,
const char* substr) {
FILE* fp = fopen(filename, "r");
assert(fp != NULL, "error opening file %s: %s", filename, strerror(errno));
size_t ret = 0;
for (;;) {
ResourceMark rm;
char* line = read_line(fp);
if (line == NULL) {
break;
}
if (strstr(line, substr) != NULL) {
ret++;
}
}
fclose(fp);
return ret;
}
static bool file_exists(const char* filename) {
struct stat st;
return os::stat(filename, &st) == 0;
}
static void delete_file(const char* filename) {
if (!file_exists(filename)) {
return;
}
int ret = remove(filename);
assert(ret == 0, "failed to remove file '%s': %s", filename, strerror(errno));
}
static void create_directory(const char* name) {
assert(!file_exists(name), "can't create directory: %s already exists", name);
bool failed;
#ifdef _WINDOWS
failed = !CreateDirectory(name, NULL);
#else
failed = mkdir(name, 0777);
#endif
assert(!failed, "failed to create directory %s", name);
}
class TestLogFile {
private:
char file_name[256];
@ -201,77 +135,6 @@ void Test_configure_stdout() {
assert_str_eq("all=off", stdoutput->config_string());
}
static const char* ExpectedLine = "a (hopefully) unique log line for testing";
static void init_file(const char* filename, const char* options = "") {
LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options,
Log(logging)::error_stream());
log_debug(logging)("%s", ExpectedLine);
LogConfiguration::parse_log_arguments(filename, "all=off", "", "",
Log(logging)::error_stream());
}
void Test_log_file_startup_rotation() {
ResourceMark rm;
const size_t rotations = 5;
const char* filename = "start-rotate-test";
char* rotated_file[rotations];
for (size_t i = 0; i < rotations; i++) {
size_t len = strlen(filename) + 3;
rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);
delete_file(rotated_file[i]);
};
delete_file(filename);
init_file(filename);
assert(file_exists(filename),
"configured logging to file '%s' but file was not found", filename);
// Initialize the same file a bunch more times to trigger rotations
for (size_t i = 0; i < rotations; i++) {
init_file(filename);
assert(file_exists(rotated_file[i]), "existing file was not rotated");
}
// Remove a file and expect its slot to be re-used
delete_file(rotated_file[1]);
init_file(filename);
assert(file_exists(rotated_file[1]), "log file not properly rotated");
// Clean up after test
delete_file(filename);
for (size_t i = 0; i < rotations; i++) {
delete_file(rotated_file[i]);
}
}
void Test_log_file_startup_truncation() {
ResourceMark rm;
const char* filename = "start-truncate-test";
const char* archived_filename = "start-truncate-test.0";
delete_file(filename);
delete_file(archived_filename);
// Use the same log file twice and expect it to be overwritten/truncated
init_file(filename, "filecount=0");
assert(file_exists(filename), "couldn't find log file: %s", filename);
init_file(filename, "filecount=0");
assert(file_exists(filename), "couldn't find log file: %s", filename);
assert(!file_exists(archived_filename),
"existing log file %s was not properly truncated when filecount was 0",
filename);
// Verify that the file was really truncated and not just appended
assert(number_of_lines_with_substring_in_file(filename, ExpectedLine) == 1,
"log file %s appended rather than truncated", filename);
delete_file(filename);
delete_file(archived_filename);
}
static int Test_logconfiguration_subscribe_triggered = 0;
static void Test_logconfiguration_subscribe_helper() {
@ -608,20 +471,4 @@ void Test_logtargethandle() {
Test_logtargethandle_off();
}
void Test_invalid_log_file() {
ResourceMark rm;
stringStream ss;
const char* target_name = "tmplogdir";
// Attempt to log to a directory (existing log not a regular file)
create_directory(target_name);
LogFileOutput bad_file("file=tmplogdir");
assert(bad_file.initialize("", &ss) == false, "file was initialized "
"when there was an existing directory with the same name");
assert(strstr(ss.as_string(), "tmplogdir is not a regular file") != NULL,
"missing expected error message, received msg: %s", ss.as_string());
ss.reset();
remove(target_name);
}
#endif // PRODUCT

View file

@ -67,9 +67,6 @@ void InternalVMTests::run() {
run_unit_test(Test_logconfiguration_subscribe);
run_unit_test(Test_log_prefix);
run_unit_test(Test_log_big);
run_unit_test(Test_log_file_startup_rotation);
run_unit_test(Test_log_file_startup_truncation);
run_unit_test(Test_invalid_log_file);
run_unit_test(DirectivesParser_test);
#if INCLUDE_VM_STRUCTS
run_unit_test(VMStructs_test);

View file

@ -48,6 +48,26 @@ static inline void delete_file(const char* filename) {
<< os::strerror(errno) << " (" << errno << ")";
}
static inline void create_directory(const char* name) {
assert(!file_exists(name), "can't create directory: %s already exists", name);
bool failed;
#ifdef _WINDOWS
failed = !CreateDirectory(name, NULL);
#else
failed = mkdir(name, 0777);
#endif
assert(!failed, "failed to create directory %s", name);
}
static inline void init_log_file(const char* filename, const char* options = "") {
LogStreamHandle(Error, logging) stream;
bool success = LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options, &stream);
guarantee(success, "Failed to initialize log file '%s' with options '%s'", filename, options);
log_debug(logging)("%s", LOG_TEST_STRING_LITERAL);
success = LogConfiguration::parse_log_arguments(filename, "all=off", "", "", &stream);
guarantee(success, "Failed to disable logging to file '%s'", filename);
}
// 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) {

View file

@ -22,6 +22,7 @@
*
*/
#include "precompiled.hpp"
#include "logTestUtils.inline.hpp"
#include "logging/logFileOutput.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/os.hpp"
@ -101,3 +102,82 @@ TEST_VM(LogFileOutput, filesize_overflow) {
LogFileOutput fo(name);
EXPECT_FALSE(fo.initialize(buf, &ss)) << "Accepted filesize that overflows";
}
TEST(LogFileOutput, startup_rotation) {
const size_t rotations = 5;
const char* filename = "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);
int ret = jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);
ASSERT_NE(-1, ret);
delete_file(rotated_file[i]);
}
delete_file(filename);
init_log_file(filename);
ASSERT_TRUE(file_exists(filename))
<< "configured logging to file '" << filename << "' but file was not found";
// Initialize the same file a bunch more times to trigger rotations
for (size_t i = 0; i < rotations; i++) {
init_log_file(filename);
EXPECT_TRUE(file_exists(rotated_file[i]));
}
// Remove a file and expect its slot to be re-used
delete_file(rotated_file[1]);
init_log_file(filename);
EXPECT_TRUE(file_exists(rotated_file[1]));
// Clean up after test
delete_file(filename);
for (size_t i = 0; i < rotations; i++) {
delete_file(rotated_file[i]);
}
}
TEST(LogFileOutput, startup_truncation) {
const char* filename = "start-truncate-test";
const char* archived_filename = "start-truncate-test.0";
delete_file(filename);
delete_file(archived_filename);
// Use the same log file twice and expect it to be overwritten/truncated
init_log_file(filename, "filecount=0");
ASSERT_TRUE(file_exists(filename))
<< "configured logging to file '" << filename << "' but file was not found";
init_log_file(filename, "filecount=0");
ASSERT_TRUE(file_exists(filename))
<< "configured logging to file '" << filename << "' but file was not found";
EXPECT_FALSE(file_exists(archived_filename))
<< "existing log file was not properly truncated when filecount was 0";
// Verify that the file was really truncated and not just appended
EXPECT_TRUE(file_contains_substring(filename, LOG_TEST_STRING_LITERAL));
const char* repeated[] = { LOG_TEST_STRING_LITERAL, LOG_TEST_STRING_LITERAL };
EXPECT_FALSE(file_contains_substrings_in_order(filename, repeated))
<< "log file " << filename << " appended rather than truncated";
delete_file(filename);
delete_file(archived_filename);
}
TEST(LogFileOutput, invalid_file) {
ResourceMark rm;
stringStream ss;
// Attempt to log to a directory (existing log not a regular file)
create_directory("tmplogdir");
LogFileOutput bad_file("file=tmplogdir");
EXPECT_FALSE(bad_file.initialize("", &ss))
<< "file was initialized when there was an existing directory with the same name";
EXPECT_TRUE(string_contains_substring(ss.as_string(), "tmplogdir is not a regular file"))
<< "missing expected error message, received msg: %s" << ss.as_string();
remove("tmplogdir");
}