summaryrefslogtreecommitdiff
path: root/drivers/report_junit_test.cpp
blob: 462dca72f9be60743f2290bf4bf1c2f4cc39a6cc (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright 2014 The Kyua Authors.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "drivers/report_junit.hpp"

#include <sstream>
#include <vector>

#include <atf-c++.hpp>

#include "drivers/scan_results.hpp"
#include "engine/filters.hpp"
#include "model/context.hpp"
#include "model/metadata.hpp"
#include "model/test_case.hpp"
#include "model/test_program.hpp"
#include "model/test_result.hpp"
#include "store/write_backend.hpp"
#include "store/write_transaction.hpp"
#include "utils/datetime.hpp"
#include "utils/format/macros.hpp"
#include "utils/fs/path.hpp"
#include "utils/optional.ipp"
#include "utils/units.hpp"

namespace datetime = utils::datetime;
namespace fs = utils::fs;
namespace units = utils::units;

using utils::none;


namespace {


/// Formatted metadata for a test case with defaults.
static const char* const default_metadata =
    "allowed_architectures is empty\n"
    "allowed_platforms is empty\n"
    "description is empty\n"
    "has_cleanup = false\n"
    "is_exclusive = false\n"
    "required_configs is empty\n"
    "required_disk_space = 0\n"
    "required_files is empty\n"
    "required_memory = 0\n"
    "required_programs is empty\n"
    "required_user is empty\n"
    "timeout = 300\n";


/// Formatted metadata for a test case constructed with the "with_metadata" flag
/// set to true in add_tests.
static const char* const overriden_metadata =
    "allowed_architectures is empty\n"
    "allowed_platforms is empty\n"
    "description = Textual description\n"
    "has_cleanup = false\n"
    "is_exclusive = false\n"
    "required_configs is empty\n"
    "required_disk_space = 0\n"
    "required_files is empty\n"
    "required_memory = 0\n"
    "required_programs is empty\n"
    "required_user is empty\n"
    "timeout = 5678\n";


/// Populates the context of the given database.
///
/// \param tx Transaction to use for the writes to the database.
/// \param env_vars Number of environment variables to add to the context.
static void
add_context(store::write_transaction& tx, const std::size_t env_vars)
{
    std::map< std::string, std::string > env;
    for (std::size_t i = 0; i < env_vars; i++)
        env[F("VAR%s") % i] = F("Value %s") % i;
    const model::context context(fs::path("/root"), env);
    (void)tx.put_context(context);
}


/// Adds a new test program with various test cases to the given database.
///
/// \param tx Transaction to use for the writes to the database.
/// \param prog Test program name.
/// \param results Collection of results for the added test cases.  The size of
///     this vector indicates the number of tests in the test program.
/// \param with_metadata Whether to add metadata overrides to the test cases.
/// \param with_output Whether to add stdout/stderr messages to the test cases.
static void
add_tests(store::write_transaction& tx,
          const char* prog,
          const std::vector< model::test_result >& results,
          const bool with_metadata, const bool with_output)
{
    model::test_program_builder test_program_builder(
        "plain", fs::path(prog), fs::path("/root"), "suite");

    for (std::size_t j = 0; j < results.size(); j++) {
        model::metadata_builder builder;
        if (with_metadata) {
            builder.set_description("Textual description");
            builder.set_timeout(datetime::delta(5678, 0));
        }
        test_program_builder.add_test_case(F("t%s") % j, builder.build());
    }

    const model::test_program test_program = test_program_builder.build();
    const int64_t tp_id = tx.put_test_program(test_program);

    for (std::size_t j = 0; j < results.size(); j++) {
        const int64_t tc_id = tx.put_test_case(test_program, F("t%s") % j,
                                               tp_id);
        const datetime::timestamp start =
            datetime::timestamp::from_microseconds(0);
        const datetime::timestamp end =
            datetime::timestamp::from_microseconds(j * 1000000 + 500000);
        tx.put_result(results[j], tc_id, start, end);

        if (with_output) {
            atf::utils::create_file("fake-out", F("stdout file %s") % j);
            tx.put_test_case_file("__STDOUT__", fs::path("fake-out"), tc_id);
            atf::utils::create_file("fake-err", F("stderr file %s") % j);
            tx.put_test_case_file("__STDERR__", fs::path("fake-err"), tc_id);
        }
    }
}


}  // anonymous namespace


ATF_TEST_CASE_WITHOUT_HEAD(junit_classname);
ATF_TEST_CASE_BODY(junit_classname)
{
    const model::test_program test_program = model::test_program_builder(
        "plain", fs::path("dir1/dir2/program"), fs::path("/root"), "suite")
        .build();

    ATF_REQUIRE_EQ("dir1.dir2.program", drivers::junit_classname(test_program));
}


ATF_TEST_CASE_WITHOUT_HEAD(junit_duration);
ATF_TEST_CASE_BODY(junit_duration)
{
    ATF_REQUIRE_EQ("0.457",
                   drivers::junit_duration(datetime::delta(0, 456700)));
    ATF_REQUIRE_EQ("3.120",
                   drivers::junit_duration(datetime::delta(3, 120000)));
    ATF_REQUIRE_EQ("5.000", drivers::junit_duration(datetime::delta(5, 0)));
}


ATF_TEST_CASE_WITHOUT_HEAD(junit_metadata__defaults);
ATF_TEST_CASE_BODY(junit_metadata__defaults)
{
    const model::metadata metadata = model::metadata_builder().build();

    const std::string expected = std::string()
        + drivers::junit_metadata_header
        + default_metadata;

    ATF_REQUIRE_EQ(expected, drivers::junit_metadata(metadata));
}


ATF_TEST_CASE_WITHOUT_HEAD(junit_metadata__overrides);
ATF_TEST_CASE_BODY(junit_metadata__overrides)
{
    const model::metadata metadata = model::metadata_builder()
        .add_allowed_architecture("arch1")
        .add_allowed_platform("platform1")
        .set_description("This is a test")
        .set_has_cleanup(true)
        .set_is_exclusive(true)
        .add_required_config("config1")
        .set_required_disk_space(units::bytes(456))
        .add_required_file(fs::path("file1"))
        .set_required_memory(units::bytes(123))
        .add_required_program(fs::path("prog1"))
        .set_required_user("root")
        .set_timeout(datetime::delta(10, 0))
        .build();

    const std::string expected = std::string()
        + drivers::junit_metadata_header
        + "allowed_architectures = arch1\n"
        + "allowed_platforms = platform1\n"
        + "description = This is a test\n"
        + "has_cleanup = true\n"
        + "is_exclusive = true\n"
        + "required_configs = config1\n"
        + "required_disk_space = 456\n"
        + "required_files = file1\n"
        + "required_memory = 123\n"
        + "required_programs = prog1\n"
        + "required_user = root\n"
        + "timeout = 10\n";

    ATF_REQUIRE_EQ(expected, drivers::junit_metadata(metadata));
}


ATF_TEST_CASE_WITHOUT_HEAD(junit_timing);
ATF_TEST_CASE_BODY(junit_timing)
{
    const std::string expected = std::string()
        + drivers::junit_timing_header +
        "Start time: 2015-06-12T01:02:35.123456Z\n"
        "End time:   2016-07-13T18:47:10.000001Z\n"
        "Duration:   34364674.877s\n";

    const datetime::timestamp start_time =
        datetime::timestamp::from_values(2015, 6, 12, 1, 2, 35, 123456);
    const datetime::timestamp end_time =
        datetime::timestamp::from_values(2016, 7, 13, 18, 47, 10, 1);

    ATF_REQUIRE_EQ(expected, drivers::junit_timing(start_time, end_time));
}


ATF_TEST_CASE_WITHOUT_HEAD(report_junit_hooks__minimal);
ATF_TEST_CASE_BODY(report_junit_hooks__minimal)
{
    store::write_backend backend = store::write_backend::open_rw(
        fs::path("test.db"));
    store::write_transaction tx = backend.start_write();
    add_context(tx, 0);
    tx.commit();
    backend.close();

    std::ostringstream output;

    drivers::report_junit_hooks hooks(output);
    drivers::scan_results::drive(fs::path("test.db"),
                                 std::set< engine::test_filter >(),
                                 hooks);

    const char* expected =
        "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
        "<testsuite>\n"
        "<properties>\n"
        "<property name=\"cwd\" value=\"/root\"/>\n"
        "</properties>\n"
        "</testsuite>\n";
    ATF_REQUIRE_EQ(expected, output.str());
}


ATF_TEST_CASE_WITHOUT_HEAD(report_junit_hooks__some_tests);
ATF_TEST_CASE_BODY(report_junit_hooks__some_tests)
{
    std::vector< model::test_result > results1;
    results1.push_back(model::test_result(
        model::test_result_broken, "Broken"));
    results1.push_back(model::test_result(
        model::test_result_expected_failure, "XFail"));
    results1.push_back(model::test_result(
        model::test_result_failed, "Failed"));
    std::vector< model::test_result > results2;
    results2.push_back(model::test_result(
        model::test_result_passed));
    results2.push_back(model::test_result(
        model::test_result_skipped, "Skipped"));

    store::write_backend backend = store::write_backend::open_rw(
        fs::path("test.db"));
    store::write_transaction tx = backend.start_write();
    add_context(tx, 2);
    add_tests(tx, "dir/prog-1", results1, false, false);
    add_tests(tx, "dir/sub/prog-2", results2, true, true);
    tx.commit();
    backend.close();

    std::ostringstream output;

    drivers::report_junit_hooks hooks(output);
    drivers::scan_results::drive(fs::path("test.db"),
                                 std::set< engine::test_filter >(),
                                 hooks);

    const std::string expected = std::string() +
        "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
        "<testsuite>\n"
        "<properties>\n"
        "<property name=\"cwd\" value=\"/root\"/>\n"
        "<property name=\"env.VAR0\" value=\"Value 0\"/>\n"
        "<property name=\"env.VAR1\" value=\"Value 1\"/>\n"
        "</properties>\n"

        "<testcase classname=\"dir.prog-1\" name=\"t0\" time=\"0.500\">\n"
        "<error message=\"Broken\"/>\n"
        "<system-err>"
        + drivers::junit_metadata_header +
        default_metadata
        + drivers::junit_timing_header +
        "Start time: 1970-01-01T00:00:00.000000Z\n"
        "End time:   1970-01-01T00:00:00.500000Z\n"
        "Duration:   0.500s\n"
        + drivers::junit_stderr_header +
        "&lt;EMPTY&gt;\n"
        "</system-err>\n"
        "</testcase>\n"

        "<testcase classname=\"dir.prog-1\" name=\"t1\" time=\"1.500\">\n"
        "<system-err>"
        "Expected failure result details\n"
        "-------------------------------\n"
        "\n"
        "XFail\n"
        "\n"
        + drivers::junit_metadata_header +
        default_metadata
        + drivers::junit_timing_header +
        "Start time: 1970-01-01T00:00:00.000000Z\n"
        "End time:   1970-01-01T00:00:01.500000Z\n"
        "Duration:   1.500s\n"
        + drivers::junit_stderr_header +
        "&lt;EMPTY&gt;\n"
        "</system-err>\n"
        "</testcase>\n"

        "<testcase classname=\"dir.prog-1\" name=\"t2\" time=\"2.500\">\n"
        "<failure message=\"Failed\"/>\n"
        "<system-err>"
        + drivers::junit_metadata_header +
        default_metadata
        + drivers::junit_timing_header +
        "Start time: 1970-01-01T00:00:00.000000Z\n"
        "End time:   1970-01-01T00:00:02.500000Z\n"
        "Duration:   2.500s\n"
        +  drivers::junit_stderr_header +
        "&lt;EMPTY&gt;\n"
        "</system-err>\n"
        "</testcase>\n"

        "<testcase classname=\"dir.sub.prog-2\" name=\"t0\" time=\"0.500\">\n"
        "<system-out>stdout file 0</system-out>\n"
        "<system-err>"
        + drivers::junit_metadata_header +
        overriden_metadata
        + drivers::junit_timing_header +
        "Start time: 1970-01-01T00:00:00.000000Z\n"
        "End time:   1970-01-01T00:00:00.500000Z\n"
        "Duration:   0.500s\n"
        + drivers::junit_stderr_header +
        "stderr file 0</system-err>\n"
        "</testcase>\n"

        "<testcase classname=\"dir.sub.prog-2\" name=\"t1\" time=\"1.500\">\n"
        "<skipped/>\n"
        "<system-out>stdout file 1</system-out>\n"
        "<system-err>"
        "Skipped result details\n"
        "----------------------\n"
        "\n"
        "Skipped\n"
        "\n"
        + drivers::junit_metadata_header +
        overriden_metadata
        + drivers::junit_timing_header +
        "Start time: 1970-01-01T00:00:00.000000Z\n"
        "End time:   1970-01-01T00:00:01.500000Z\n"
        "Duration:   1.500s\n"
        + drivers::junit_stderr_header +
        "stderr file 1</system-err>\n"
        "</testcase>\n"

        "</testsuite>\n";
    ATF_REQUIRE_EQ(expected, output.str());
}


ATF_INIT_TEST_CASES(tcs)
{
    ATF_ADD_TEST_CASE(tcs, junit_classname);

    ATF_ADD_TEST_CASE(tcs, junit_duration);

    ATF_ADD_TEST_CASE(tcs, junit_metadata__defaults);
    ATF_ADD_TEST_CASE(tcs, junit_metadata__overrides);

    ATF_ADD_TEST_CASE(tcs, junit_timing);

    ATF_ADD_TEST_CASE(tcs, report_junit_hooks__minimal);
    ATF_ADD_TEST_CASE(tcs, report_junit_hooks__some_tests);
}