aboutsummaryrefslogtreecommitdiffstats
path: root/engine/tap_test.cpp
blob: f4253a68e727134a69bf79c66960b8ed61788423 (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
// Copyright 2015 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 "engine/tap.hpp"

extern "C" {
#include <signal.h>
}

#include <atf-c++.hpp>

#include "engine/config.hpp"
#include "engine/scheduler.hpp"
#include "model/metadata.hpp"
#include "model/test_case.hpp"
#include "model/test_program.hpp"
#include "model/test_result.hpp"
#include "utils/config/tree.ipp"
#include "utils/datetime.hpp"
#include "utils/env.hpp"
#include "utils/format/containers.ipp"
#include "utils/format/macros.hpp"
#include "utils/fs/operations.hpp"
#include "utils/fs/path.hpp"
#include "utils/optional.ipp"

namespace config = utils::config;
namespace datetime = utils::datetime;
namespace fs = utils::fs;
namespace scheduler = engine::scheduler;

using utils::none;


namespace {


/// Copies the tap helper to the work directory, selecting a specific helper.
///
/// \param tc Pointer to the calling test case, to obtain srcdir.
/// \param name Name of the new binary to create.  Must match the name of a
///     valid helper, as the binary name is used to select it.
static void
copy_tap_helper(const atf::tests::tc* tc, const char* name)
{
    const fs::path srcdir(tc->get_config_var("srcdir"));
    atf::utils::copy_file((srcdir / "tap_helpers").str(), name);
}


/// Runs one tap test program and checks its result.
///
/// \param tc Pointer to the calling test case, to obtain srcdir.
/// \param test_case_name Name of the "test case" to select from the helper
///     program.
/// \param exp_result The expected result.
/// \param metadata The test case metadata.
/// \param user_config User-provided configuration variables.
static void
run_one(const atf::tests::tc* tc, const char* test_case_name,
        const model::test_result& exp_result,
        const model::metadata& metadata = model::metadata_builder().build(),
        const config::tree& user_config = engine::empty_config())
{
    copy_tap_helper(tc, test_case_name);
    const model::test_program_ptr program = model::test_program_builder(
        "tap", fs::path(test_case_name), fs::current_path(), "the-suite")
        .add_test_case("main", metadata).build_ptr();

    scheduler::scheduler_handle handle = scheduler::setup();
    (void)handle.spawn_test(program, "main", user_config);

    scheduler::result_handle_ptr result_handle = handle.wait_any();
    const scheduler::test_result_handle* test_result_handle =
        dynamic_cast< const scheduler::test_result_handle* >(
            result_handle.get());
    atf::utils::cat_file(result_handle->stdout_file().str(), "stdout: ");
    atf::utils::cat_file(result_handle->stderr_file().str(), "stderr: ");
    ATF_REQUIRE_EQ(exp_result, test_result_handle->test_result());
    result_handle->cleanup();
    result_handle.reset();

    handle.cleanup();
}


}  // anonymous namespace


ATF_TEST_CASE_WITHOUT_HEAD(list);
ATF_TEST_CASE_BODY(list)
{
    const model::test_program program = model::test_program_builder(
        "tap", fs::path("non-existent"), fs::path("."), "unused-suite")
        .build();

    scheduler::scheduler_handle handle = scheduler::setup();
    const model::test_cases_map test_cases = handle.list_tests(
        &program, engine::empty_config());
    handle.cleanup();

    const model::test_cases_map exp_test_cases = model::test_cases_map_builder()
        .add("main").build();
    ATF_REQUIRE_EQ(exp_test_cases, test_cases);
}


ATF_TEST_CASE_WITHOUT_HEAD(test__all_tests_pass);
ATF_TEST_CASE_BODY(test__all_tests_pass)
{
    const model::test_result exp_result(model::test_result_passed);
    run_one(this, "pass", exp_result);
}


ATF_TEST_CASE_WITHOUT_HEAD(test__some_tests_fail);
ATF_TEST_CASE_BODY(test__some_tests_fail)
{
    const model::test_result exp_result(model::test_result_failed,
                                        "2 of 5 tests failed");
    run_one(this, "fail", exp_result);
}


ATF_TEST_CASE_WITHOUT_HEAD(test__all_tests_pass_but_exit_failure);
ATF_TEST_CASE_BODY(test__all_tests_pass_but_exit_failure)
{
    const model::test_result exp_result(
        model::test_result_broken,
        "Dubious test program: reported all tests as passed but returned exit "
        "code 70");
    run_one(this, "pass_but_exit_failure", exp_result);
}


ATF_TEST_CASE_WITHOUT_HEAD(test__signal_is_broken);
ATF_TEST_CASE_BODY(test__signal_is_broken)
{
    const model::test_result exp_result(model::test_result_broken,
                                        F("Received signal %s") % SIGABRT);
    run_one(this, "crash", exp_result);
}


ATF_TEST_CASE(test__timeout_is_broken);
ATF_TEST_CASE_HEAD(test__timeout_is_broken)
{
    set_md_var("timeout", "60");
}
ATF_TEST_CASE_BODY(test__timeout_is_broken)
{
    utils::setenv("CONTROL_DIR", fs::current_path().str());

    const model::metadata metadata = model::metadata_builder()
        .set_timeout(datetime::delta(1, 0)).build();
    const model::test_result exp_result(model::test_result_broken,
                                        "Test case timed out");
    run_one(this, "timeout", exp_result, metadata);

    ATF_REQUIRE(!atf::utils::file_exists("cookie"));
}


ATF_TEST_CASE_WITHOUT_HEAD(test__configuration_variables);
ATF_TEST_CASE_BODY(test__configuration_variables)
{
    config::tree user_config = engine::empty_config();
    user_config.set_string("test_suites.a-suite.first", "unused");
    user_config.set_string("test_suites.the-suite.first", "some value");
    user_config.set_string("test_suites.the-suite.second", "some other value");
    user_config.set_string("test_suites.other-suite.first", "unused");

    const model::test_result exp_result(model::test_result_passed);
    run_one(this, "check_configuration_variables", exp_result,
            model::metadata_builder().build(), user_config);
}


ATF_INIT_TEST_CASES(tcs)
{
    scheduler::register_interface(
        "tap", std::shared_ptr< scheduler::interface >(
            new engine::tap_interface()));

    ATF_ADD_TEST_CASE(tcs, list);

    ATF_ADD_TEST_CASE(tcs, test__all_tests_pass);
    ATF_ADD_TEST_CASE(tcs, test__all_tests_pass_but_exit_failure);
    ATF_ADD_TEST_CASE(tcs, test__some_tests_fail);
    ATF_ADD_TEST_CASE(tcs, test__signal_is_broken);
    ATF_ADD_TEST_CASE(tcs, test__timeout_is_broken);
    ATF_ADD_TEST_CASE(tcs, test__configuration_variables);
}