A Discrete-Event Network Simulator
API
example-as-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Lawrence Livermore National Laboratory
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18 */
19
20#include "example-as-test.h"
21
22#include "ascii-test.h"
23#include "assert.h"
24#include "log.h"
25
26#include <cstdlib> // itoa(), system (), getenv ()
27#include <cstring>
28#include <sstream>
29#include <string>
30
37namespace ns3
38{
39
40NS_LOG_COMPONENT_DEFINE("ExampleAsTestCase");
41
42// Running tests as examples currently requires Python.
43#if defined(NS3_ENABLE_EXAMPLES)
44
45ExampleAsTestCase::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
57ExampleAsTestCase::~ExampleAsTestCase()
58{
60}
61
62std::string
63ExampleAsTestCase::GetCommandTemplate() const
64{
66 std::string command("%s ");
67 command += m_args;
68 return command;
69}
70
71std::string
72ExampleAsTestCase::GetPostProcessingCommand() const
73{
75 std::string command("");
76 return command;
77}
78
79void
80ExampleAsTestCase::DoRun()
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 ss << "python3 ./ns3 run " << m_program << " --no-build --command-template=\""
91 << GetCommandTemplate()
92 << "\""
93
94 // redirect std::clog, std::cerr to std::cout
95 << " 2>&1 "
96 // Suppress the waf lines from output; waf output contains directory paths which will
97 // obviously differ during a test run
98 << " " << GetPostProcessingCommand() << " > " << testFile;
99
100 int status = std::system(ss.str().c_str());
101
102 std::cout << "command: " << ss.str() << "\n"
103 << "status: " << status << "\n"
104 << "refFile: " << refFile << "\n"
105 << "testFile: " << testFile << "\n"
106 << std::endl;
107 std::cout << "testFile contents:" << std::endl;
108
109 std::ifstream logF(testFile);
110 std::string line;
111 while (getline(logF, line))
112 {
113 std::cout << line << "\n";
114 }
115 logF.close();
116
117 // Make sure the example didn't outright crash
118 NS_TEST_ASSERT_MSG_EQ(status, 0, "example " + m_program + " failed");
119
120 // Check that we're not just introspecting the command-line
121 const char* envVar = std::getenv("NS_COMMANDLINE_INTROSPECTION");
122 if (envVar != nullptr && std::strlen(envVar) != 0)
123 {
124 return;
125 }
126
127 // Compare the testFile to the reference file
128 NS_ASCII_TEST_EXPECT_EQ(testFile, refFile);
129}
130
131ExampleAsTestSuite::ExampleAsTestSuite(const std::string name,
132 const std::string program,
133 const std::string dataDir,
134 const std::string args /* = "" */,
135 const TestDuration duration /* =QUICK */)
136 : TestSuite(name, EXAMPLE)
137{
138 NS_LOG_FUNCTION(this << name << program << dataDir << args << duration);
139 AddTestCase(new ExampleAsTestCase(name, program, dataDir, args), duration);
140}
141
142#endif // NS3_ENABLE_EXAMPLES
143
144} // 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:39
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:202
#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:144
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.