0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TestHarness.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 #ifndef Common_TestHarness_h
28 #define Common_TestHarness_h
29 
30 #include "Logger.h"
31 
32 #include <iostream>
33 #include <fstream>
34 #include <string>
35 
36 extern "C" {
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 }
43 
44 namespace Hypertable {
45 
54  class TestHarness {
55  public:
61  TestHarness(const char *name)
62  : m_error(0) {
63  Logger::initialize(name);
64 
65  // open temporary output file
66  sprintf(m_output_file, "%s%d", name, (int)getpid());
67 
68  if ((m_fd = open(m_output_file, O_CREAT | O_TRUNC | O_WRONLY, 0644))
69  < 0) {
70  HT_ERRORF("open(%s) failed - %s", m_output_file, strerror(errno));
71  exit(EXIT_FAILURE);
72  }
73 
75  }
76 
79  if (!m_error)
80  unlink(m_output_file);
81  }
82 
84  int get_log_file_descriptor() { return m_fd; }
85 
92  void validate_and_exit(const char *golden_file) {
93  validate(golden_file);
94  std::quick_exit(m_error ? 1 : 0);
95  }
96 
102  int validate(const char *golden_file) {
103  close(m_fd);
104  String command = (String)"diff " + m_output_file + " " + golden_file;
105  m_error = system(command.c_str());
106 
107  if (m_error)
108  std::cerr << "Command: "<< command << " exited with "<< m_error
109  << ", see '" << m_output_file << "'" << std::endl;
110 
111  return m_error;
112  }
113 
119  void regenerate_golden_file(const char *golden_file) {
120  String command = (String)"mv " + m_output_file + " " + golden_file;
121  HT_ASSERT(system(command.c_str()) == 0);
122  }
123 
126  close(m_fd);
127  std::cerr << "Error, see '" << m_output_file << "'" << std::endl;
128  std::quick_exit(EXIT_FAILURE);
129  }
130 
131  private:
133  char m_output_file[128];
134 
136  int m_fd;
137 
139  int m_error;
140  };
141 
144 } // namespace Hypertable
145 
146 #endif // Common_TestHarness_h
void initialize(const String &name)
Public initialization function - creates a singleton instance of LogWriter.
Definition: Logger.cc:45
~TestHarness()
Destructor; if the test was successful then the logfile is deleted.
Definition: TestHarness.h:78
void regenerate_golden_file(const char *golden_file)
Regenerates the golden file by renaming the output file to the golden file.
Definition: TestHarness.h:119
int validate(const char *golden_file)
Validates the log file against the golden file.
Definition: TestHarness.h:102
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
A simple test framework which sets up the logging subsystem, can compare its output against a golden ...
Definition: TestHarness.h:54
void display_error_and_exit()
Prints an error and exits with exit code 1.
Definition: TestHarness.h:125
void set_test_mode(int fd=-1)
The test mode disables line numbers and timestamps and can redirect the output to a separate file des...
Definition: Logger.h:100
#define HT_ASSERT(_e_)
Definition: Logger.h:396
char m_output_file[128]
The output filename; the application name concatenated with the pid.
Definition: TestHarness.h:133
TestHarness(const char *name)
Constructor; redirects the logging output to a file.
Definition: TestHarness.h:61
int get_log_file_descriptor()
Returns the file descriptor of the log file.
Definition: TestHarness.h:84
Logging routines and macros.
int m_error
The error from the golden file validation.
Definition: TestHarness.h:139
Hypertable definitions
LogWriter * get()
Accessor for the LogWriter singleton instance.
Definition: Logger.cc:49
int m_fd
The logfile file descriptor.
Definition: TestHarness.h:136
#define HT_ERRORF(msg,...)
Definition: Logger.h:300
void validate_and_exit(const char *golden_file)
Validates the log file output with the golden file, then exits.
Definition: TestHarness.h:92