API
 
Loading...
Searching...
No Matches
logMap_test.cpp
Go to the documentation of this file.
1/** \file logMap_test.hpp
2 * \brief Tests for the logMap class
3 * \ingroup logger_files
4 */
5
6#include "../../../tests/testXWC.hpp"
7
8#include <filesystem>
9#include <fstream>
10
11#include "../../file/fileTimes.hpp"
12
13#include "../logMap.hpp"
14#include "../logMap.cpp"
15
16namespace libXWCTest
17{
18
19namespace loggerTest
20{
21
22/** \defgroup logMap_unit_test logMap Unit Tests
23 * \ingroup logger_unit_test
24 */
25
26/// Namespace for XWC::logger::logMap tests
27/** \ingroup logMap_unit_test
28 *
29 */
30namespace logMapTest
31{
32
33// simple time struct to enable log structure creation
34struct tmpt
35{
36 int year;
37 int month;
38 int day;
39 int hour;
40 int minute;
41 int second;
43};
44
45// create a bunch of logs on disk to process
46void createTestPaths( const std::string &basedir )
47{
48 std::filesystem::create_directory( basedir );
49
50 std::vector<std::string> devs( { "dev1", "dev2", "dev3" } );
51
52 std::vector<std::vector<tmpt>> ftimes( { /*dev1:*/ { { 2024, 11, 19, 0, 0, 0, 0 },
53 { 2024, 11, 19, 0, 0, 30, 0 },
54 { 2024, 11, 19, 2, 55, 26, 4000 },
55 { 2024, 11, 19, 5, 23, 0, 0 },
56 { 2024, 11, 21, 22, 0, 0, 0 },
57 { 2024, 11, 21, 23, 59, 59, 999999999 },
58 { 2024, 11, 23, 2, 30, 2, 2000 },
59 { 2024, 11, 23, 4, 45, 10, 12 } },
60 /*dev2:*/
61 { { 2024, 11, 19, 0, 0, 0, 0 },
62 { 2024, 11, 19, 0, 0, 30, 0 },
63 { 2024, 11, 19, 2, 55, 26, 4000 },
64 { 2024, 11, 19, 5, 23, 0, 0 },
65 { 2024, 11, 21, 22, 0, 0, 0 },
66 { 2024, 11, 21, 23, 59, 59, 999999999 },
67 { 2024, 11, 23, 2, 30, 2, 2000 },
68 { 2024, 11, 23, 4, 45, 10, 12 } },
69 /*dev3:*/
70 { { 2024, 11, 19, 0, 0, 0, 0 },
71 { 2024, 11, 19, 0, 0, 30, 0 },
72 { 2024, 11, 19, 2, 55, 26, 4000 },
73 { 2024, 11, 19, 5, 23, 0, 0 },
74 { 2024, 11, 21, 22, 0, 0, 0 },
75 { 2024, 11, 21, 23, 59, 59, 999999999 },
76 { 2024, 11, 23, 2, 30, 2, 2000 },
77 { 2024, 11, 23, 4, 45, 10, 12 } } } );
78
79 for( size_t d = 0; d < devs.size(); ++d )
80 {
81 for( size_t f = 0; f < ftimes[d].size(); ++f )
82 {
83 tm uttime;
84 uttime.tm_year = ftimes[d][f].year - 1900;
85 uttime.tm_mon = ftimes[d][f].month - 1;
86 uttime.tm_mday = ftimes[d][f].day;
87 uttime.tm_hour = ftimes[d][f].hour;
88 uttime.tm_min = ftimes[d][f].minute;
89 uttime.tm_sec = ftimes[d][f].second;
90
91 time_t secs = timegm( &uttime );
92
93 std::string fileName, relPath;
94
95 MagAOX::file::fileTimeRelPath( fileName, relPath, devs[d], "xlog", secs, ftimes[d][f].nanosec );
96
97 std::filesystem::create_directories( basedir + '/' + relPath );
98
99 std::ofstream fout;
100 fout.open( basedir + '/' + relPath + '/' + fileName );
101 fout.close();
102 }
103 }
104}
105
106/// Building the app-to-file map
107/**
108 * \ingroup logMap_unit_test
109 */
110TEST_CASE( "Building the app-to-file map", "[libMagAOX::logger::logMap]" )
111{
112 createTestPaths( "/tmp/logMap_test" );
113
114 SECTION( "File matches middle file and one later on same day" )
115 {
116 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241119030000000000000.xrif" );
117 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119040000000000000.xrif" );
118
120
121 lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".xlog", firstFile, lastFile );
122
123 REQUIRE( lm.m_appToFileMap["dev1"].size() == 2 );
124 auto it = lm.m_appToFileMap["dev1"].begin();
125 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119025526000004000.xlog" );
126 ++it;
127 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119052300000000000.xlog" );
128 }
129
130 SECTION( "File matches first file by delta-t and last file on same day" )
131 {
132 // This is inside second log, but will pick the first log to get the 60 second buffer
133 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241119000061000000000.xrif" );
134 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119042200000000000.xrif" );
135
137
138 lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".xlog", firstFile, lastFile );
139
140 REQUIRE( lm.m_appToFileMap["dev1"].size() == 4 );
141 auto it = lm.m_appToFileMap["dev1"].begin();
142 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119000000000000000.xlog" );
143 ++it;
144 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119000030000000000.xlog" );
145 ++it;
146 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119025526000004000.xlog" );
147 ++it;
148 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119052300000000000.xlog" );
149 }
150
151 SECTION( "File matches first file by delta-t and first file on next day" )
152 {
153 // This is inside second log, but will pick the first log to get the 60 second buffer
154 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241119000061000000000.xrif" );
155
156 // This is inside the 2nd to last log, but since next log is < 3600 seconds we have to go to next day
157 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119052200000000000.xrif" );
158
160
161 lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".xlog", firstFile, lastFile );
162
163 REQUIRE( lm.m_appToFileMap["dev1"].size() == 5 );
164 auto it = lm.m_appToFileMap["dev1"].begin();
165 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119000000000000000.xlog" );
166 ++it;
167 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119000030000000000.xlog" );
168 ++it;
169 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119025526000004000.xlog" );
170 ++it;
171 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119052300000000000.xlog" );
172 }
173
174 SECTION( "File matches last file on previous day and first file on current day (times are the same)" )
175 {
176 // This will pick the first log of 11/19 to get the 60 second buffer
177 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241121200030000000000.xrif" );
178
179 // Same time is more than an hour before first log of 11/21
180 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241121200030000000000.xrif" );
181
183
184 lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".xlog", firstFile, lastFile );
185
186 REQUIRE( lm.m_appToFileMap["dev1"].size() == 2 );
187 auto it = lm.m_appToFileMap["dev1"].begin();
188 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119052300000000000.xlog" );
189 ++it;
190 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_21/dev1_20241121220000000000000.xlog" );
191 }
192
193 SECTION( "Matches first and last overall files, last one is not > 1 hr" )
194 {
195 // this is 50 seconds into 2nd file, so will pick the first file which is > 60 secs
196 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241119000120000000000.xrif" );
197
198 // this is 10 minutes before end of last log
199 MagAOX::file::stdFileName lastFile( "cam1/2024_11_23/cam1_20241123044500000000000.xrif" );
200
202
203 lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".xlog", firstFile, lastFile );
204
205 REQUIRE( lm.m_appToFileMap["dev1"].size() == 8 );
206 auto it = lm.m_appToFileMap["dev1"].begin();
207 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119000000000000000.xlog" );
208 ++it;
209 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119000030000000000.xlog" );
210 ++it;
211 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119025526000004000.xlog" );
212 ++it;
213 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_19/dev1_20241119052300000000000.xlog" );
214 ++it;
215 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_21/dev1_20241121220000000000000.xlog" );
216 ++it;
217 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_21/dev1_20241121235959999999999.xlog" );
218 ++it;
219 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_23/dev1_20241123023002000002000.xlog" );
220 ++it;
221 REQUIRE( it->fullName() == "/tmp/logMap_test/dev1/2024_11_23/dev1_20241123044510000000012.xlog" );
222 }
223}
224
225/// Building the app-to-file map with bad arguments
226/**
227 * \ingroup logMap_unit_test
228 */
229TEST_CASE( "Building the app-to-file map with bad arguments", "[libMagAOX::logger::logMap]" )
230{
231 createTestPaths( "/tmp/logMap_test" );
232
233 SECTION( "bad directory permissions" )
234 {
235 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241118030000000000000.xrif" );
236 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119040000000000000.xrif" );
237
239
240 mx::error_t errc = lm.loadAppToFileMap( "/root/adlknalkejr111", "dev1", ".xlog", firstFile, lastFile );
241
242 REQUIRE(errc == mx::error_t::eacces);
243 REQUIRE( lm.m_appToFileMap.size() == 0 );
244 }
245
246 SECTION( "directory does not exist" )
247 {
248 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241118030000000000000.xrif" );
249 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119040000000000000.xrif" );
250
252
253 mx::error_t errc = lm.loadAppToFileMap( "/tmp/logMap_testX", "dev1", ".xlog", firstFile, lastFile );
254
255 REQUIRE(errc == mx::error_t::dirnotfound);
256 REQUIRE( lm.m_appToFileMap.size() == 0 );
257 }
258
259 SECTION( "firstFile is not valid" )
260 {
262 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119040000000000000.xrif" );
263
265
266 mx::error_t errc = lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".xlog", firstFile, lastFile );
267
268 REQUIRE(errc == mx::error_t::invalidconfig);
269 REQUIRE( lm.m_appToFileMap.size() == 0 );
270 }
271
272 SECTION( "lastFile is not valid" )
273 {
274 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241118030000000000000.xrif" );
276
278
279 mx::error_t errc = lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".xlog", firstFile, lastFile );
280
281 REQUIRE(errc == mx::error_t::invalidconfig);
282 REQUIRE( lm.m_appToFileMap.size() == 0 );
283 }
284
285 SECTION( "wrong device name so no files" )
286 {
287 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241118030000000000000.xrif" );
288 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119040000000000000.xrif" );
289
291
292 mx::error_t errc = lm.loadAppToFileMap( "/tmp/logMap_test", "dev6", ".xlog", firstFile, lastFile );
293
294 REQUIRE(errc == mx::error_t::noerror);
295 REQUIRE( lm.m_appToFileMap.size() == 0 );
296 }
297
298 SECTION( "wrong extension so no files" )
299 {
300 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241118030000000000000.xrif" );
301 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119040000000000000.xrif" );
302
304
305 mx::error_t errc = lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".qlog", firstFile, lastFile );
306
307 REQUIRE(errc == mx::error_t::noerror);
308 REQUIRE( lm.m_appToFileMap.size() == 0 );
309 }
310
311
312}
313
314/// Building the app-to-file map with errors
315/**
316 * \ingroup logMap_unit_test
317 */
318TEST_CASE( "Building the app-to-file map with errors", "[libMagAOX::logger::logMap]" )
319{
320 createTestPaths( "/tmp/logMap_test" );
321
322 SECTION( "No prior log" )
323 {
324 MagAOX::file::stdFileName firstFile( "cam1/2024_11_19/cam1_20241118030000000000000.xrif" );
325 MagAOX::file::stdFileName lastFile( "cam1/2024_11_19/cam1_20241119040000000000000.xrif" );
326
328
329 lm.loadAppToFileMap( "/tmp/logMap_test", "dev1", ".xlog", firstFile, lastFile );
330
331 REQUIRE( lm.m_appToFileMap.size() == 0 );
332 }
333}
334
335} // namespace logMapTest
336} // namespace loggerTest
337} // namespace libXWCTest
Organize and analyze the name of a standard file name.
TEST_CASE("Building the app-to-file map", "[libMagAOX::logger::logMap]")
Building the app-to-file map.
mx::error_t fileTimeRelPath(std::string &tstamp, std::string &relPath, time_t ts_sec, long ts_nsec)
Get the timestamp and the relative path based on a time.
void createTestPaths(const std::string &basedir)
Namespace for all libXWC tests.
Definition MagAOXApp.hpp:49
Map of log entries by application name, mapping both to files and to loaded buffers.
Definition logMap.hpp:51
mx::error_t loadAppToFileMap(const std::string &dir, const std::string &dev, const std::string &ext, const stdFileNameT &firstFile, const stdFileNameT &lastFile)
Get log file names in a directory and distribute them into the map by app-name.
Definition logMap.hpp:174
appToFileMapT m_appToFileMap
Definition logMap.hpp:62