A Discrete-Event Network Simulator
API
example-as-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 Lawrence Livermore National Laboratory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  */
20 
21 #include "example-as-test.h"
22 #include "ascii-test.h"
23 #include "log.h"
24 #include "assert.h"
25 
26 #include <string>
27 #include <sstream>
28 #include <cstdlib> // itoa(), system (), getenv ()
29 #include <cstring>
30 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("ExampleAsTestCase");
40 
41 // Running tests as examples currently requires bash shell; uses Unix
42 // piping that does not work on Windows.
43 #if defined(NS3_ENABLE_EXAMPLES) && !defined (__win32__)
44 
45 ExampleAsTestCase::ExampleAsTestCase (const std::string name,
46  const std::string program,
47  const std::string dataDir,
48  const std::string args /* = "" */)
49  : TestCase (name),
50  m_program (program),
51  m_dataDir (dataDir),
52  m_args (args)
53 {
54  NS_LOG_FUNCTION (this << name << program << dataDir << args);
55 }
56 
57 ExampleAsTestCase::~ExampleAsTestCase (void)
58 {
60 }
61 
62 std::string
63 ExampleAsTestCase::GetCommandTemplate (void) const
64 {
66  std::string command ("%s ");
67  command += m_args;
68  return command;
69 }
70 
71 std::string
72 ExampleAsTestCase::GetPostProcessingCommand (void) const
73 {
75  std::string command ("");
76  return command;
77 }
78 
79 void
80 ExampleAsTestCase::DoRun (void)
81 {
83  // Set up the output file names
84  SetDataDir (m_dataDir);
85  std::string refFile = CreateDataDirFilename (GetName () + ".reflog");
86  std::string testFile = CreateTempDirFilename (GetName () + ".reflog");
87 
88  std::stringstream ss;
89 
90  // Use bash as shell to allow use of PIPESTATUS
91  ss << "bash -c './ns3 run " << m_program
92  << " --no-build --command-template=\"" << GetCommandTemplate () << "\""
93 
94  // redirect std::clog, std::cerr to std::cout
95  << " 2>&1 "
96 
97  // Suppress the waf lines from output; waf output contains directory paths which will
98  // obviously differ during a test run
99  << " | grep -v 'Waf:' "
100  << GetPostProcessingCommand ()
101  << " > " << testFile
102 
103  // Get the status of ns3
104  << "; exit ${PIPESTATUS[0]}'";
105 
106  int status = std::system (ss.str ().c_str ());
107 
108  std::cout << "command: " << ss.str () << "\n"
109  << "status: " << status << "\n"
110  << "refFile: " << refFile << "\n"
111  << "testFile: " << testFile << "\n"
112  << std::endl;
113  std::cout << "testFile contents:" << std::endl;
114 
115  std::ifstream logF (testFile);
116  std::string line;
117  while (getline (logF, line))
118  {
119  std::cout << line << "\n";
120  }
121  logF.close ();
122 
123  // Make sure the example didn't outright crash
124  NS_TEST_ASSERT_MSG_EQ (status, 0, "example " + m_program + " failed");
125 
126  // Check that we're not just introspecting the command-line
127  const char * envVar = std::getenv ("NS_COMMANDLINE_INTROSPECTION");
128  if (envVar != 0 && std::strlen (envVar) != 0)
129  {
130  return;
131  }
132 
133  // Compare the testFile to the reference file
134  NS_ASCII_TEST_EXPECT_EQ (testFile, refFile);
135 }
136 
137 ExampleAsTestSuite::ExampleAsTestSuite (const std::string name,
138  const std::string program,
139  const std::string dataDir,
140  const std::string args /* = "" */,
141  const TestDuration duration /* =QUICK */)
142  : TestSuite (name, EXAMPLE)
143 {
144  NS_LOG_FUNCTION (this << name << program << dataDir << args << duration);
145  AddTestCase (new ExampleAsTestCase (name, program, dataDir, args), duration);
146 }
147 
148 #endif // NS3_ENABLE_EXAMPLES && !defined (__win32__)
149 
150 } // namespace ns3
#define NS_ASCII_TEST_EXPECT_EQ(gotFilename, expectedFilename)
Test that a pair of new/reference ascii files are equal.
Definition: ascii-test.h:38
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ExampleAsTestCase(const std::string name, const std::string program, const std::string dataDir, const std::string args="")
Constructor.
Enable examples to be run as meaningful tests.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:141
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.