This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpython2test.cc
137 lines (109 loc) · 6.15 KB
/
python2test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright 2017 Battelle Energy Alliance, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Description
-----------
HPC Software testing suite.
Python2Test subclass source code.
Date Created: Tue Dec 22 15:46:19 MST 2015
Author: Cormac Garvey
*/
#include "python2test.h"
#include <iostream> // for debugging only
#include <cstdlib> // for exit()
namespace hpcswtest {
const std::vector<std::string> Python2Test::python2_inputs_ = {
R"(
print "Hello Python"
)"
}; // vector python2_inputs_
std::vector<std::string> Python2Test::getPythonModules(void) {
return python_modules_;
}
Python2Test::Python2Test(const jobscript::JOBSCRIPT &p_s, const std::vector<std::string> &p_ms, const std::string &p_t): AppTest(p_t, ".py", p_s, python2_inputs_.size()),
log_file_name_(getHostName() + "_" + getTestName() + "_test.log"),
result_file_name_(getHostName() + "_" + getTestName() + "_results.out"),
flog_(log_file_name_,std::ios_base::app),
fresult_(result_file_name_,std::ios_base::app),
python_modules_(p_ms) {}
void Python2Test::runPythonModulesTest(std::ofstream &flog, std::ofstream &fresult, const modules::modules_type &modules, const std::vector<std::string> &python_modules) const {
std::string result;
std::string python_cmd;
std::string modules_str = modules_string(modules);
for (auto python_module: python_modules) {
#ifdef SLURM
python_cmd = " { module purge;" + modules_str + ";" + "python -c \"import " + python_module + "\"; }" + " 2>&1";
#else
python_cmd = " { module purge;module load pbs;" + modules_str + ";" + "python -c \"import " + python_module + "\"; }" + " 2>&1";
#endif
flog << "Execute : " << python_cmd << std::endl;
// std::cout << "python_cmd = " << python_cmd << std::endl;
result = exec(python_cmd.c_str());
if (strSizeZero(result)) {
fresult << module_name_version(modules[modules.size()-1]) << " " << python_module << " passed" << std::endl;
flog << module_name_version(modules[modules.size()-1]) << " " << python_module << " passed" << std::endl;
std::cout << module_name_version(modules[modules.size()-1]) << " " << python_module << " passed" << std::endl;
} else {
fresult << module_name_version(modules[modules.size()-1]) << " " << python_module << " failed" << std::endl;
flog << module_name_version(modules[modules.size()-1]) << " " << python_module << " failed" << std::endl;
std::cout << module_name_version(modules[modules.size()-1]) << " " << python_module << " failed" << std::endl;
flog << result << std::endl;
}
// std::cout << "result = " << result << std::endl;
}
}
void Python2Test::runPythonTest(std::ofstream &flog, std::ofstream &fresult, const modules::modules_type &modules, const std::string &input_file) const {
std::string result;
std::string python_cmd;
std::string modules_str = modules_string(modules);
#ifdef SLURM
python_cmd = " { module purge;" + modules_str + ";" + "python " + input_file + "; }" + " 2>&1";
#else
python_cmd = " { module purge;module load pbs;" + modules_str + ";" + "python " + input_file + "; }" + " 2>&1";
#endif
flog << "Execute : " << python_cmd << std::endl;
// std::cout << "python_cmd = " << python_cmd << std::endl;
result = exec(python_cmd.c_str());
if (result == "Hello Python") {
fresult << module_name_version(modules[modules.size()-1]) << " base passed" << std::endl;
flog << module_name_version(modules[modules.size()-1]) << " base passed" << std::endl;
std::cout << module_name_version(modules[modules.size()-1]) << " base passed" << std::endl;
} else {
fresult << module_name_version(modules[modules.size()-1]) << " base failed" << std::endl;
flog << module_name_version(modules[modules.size()-1]) << " base failed" << std::endl;
std::cout << module_name_version(modules[modules.size()-1]) << " base failed" << std::endl;
flog << result << std::endl;
}
}
void Python2Test::runTest() {
std::string cmd_result;
std::string script_cmd_result;
std::string modules_load_result;
// std::cout << "Execute runTest member function from Python2Test object " << __FILE__ << "\t" <<__LINE__ << std::endl;
if (!flog_.is_open()) {
std::cerr << "Error: (" << __FILE__ << "," << __LINE__ << ") Opening file " << log_file_name_ << std::endl;
exit(EXIT_FAILURE);
}
if (!fresult_.is_open()) {
std::cerr << "Error: (" << __FILE__ << "," << __LINE__ << ") Opening file " << result_file_name_ << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Testing: " << module_name_version(getJobScripts()[0].getModules()[getJobScripts()[0].getModules().size()-1]) << std::endl;
modules_load(flog_, getJobScripts()[0].getModules(), modules_load_result);
int c_i = 0;
for (auto python2_input: python2_inputs_) {
createFileFromStr(getInputFileNames()[c_i], python2_input);
runPythonTest(flog_, fresult_, getJobScripts()[c_i].getModules(), getInputFileNames()[c_i]);
++c_i;
}
runPythonModulesTest(flog_, fresult_, getJobScripts()[0].getModules(), getPythonModules());
}
} // namespace hpcswtest