summaryrefslogtreecommitdiff
path: root/src/mongo/unittest/golden_test.cpp
blob: f06286cc3c5dd80a24e115884124ccbb89055ece (plain)
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
/**
 *    Copyright (C) 2022-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest

#include <boost/filesystem.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/stream_buffer.hpp>
#include <boost/lexical_cast.hpp>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <pcrecpp.h>

#include "mongo/base/init.h"
#include "mongo/base/status.h"
#include "mongo/logv2/log.h"
#include "mongo/unittest/golden_test.h"
#include "mongo/util/ctype.h"
#include "mongo/util/options_parser/option_section.h"
#include "mongo/util/options_parser/startup_option_init.h"
#include "mongo/util/options_parser/startup_options.h"

namespace mongo::unittest {

namespace fs = ::boost::filesystem;
using namespace fmt::literals;

static const pcrecpp::RE validNameRegex(R"([[:alnum:]_\-]*)");

GoldenTestEnvironment* GoldenTestEnvironment::getInstance() {
    static GoldenTestEnvironment instance;
    return &instance;
}

GoldenTestEnvironment::GoldenTestEnvironment()
    : _goldenDataRoot("."), _outputPathPrefix("test_output") {
    // Parse environment variables
    auto opts = GoldenTestOptions::parseEnvironment();

    fs::path outputRoot;
    if (opts.output) {
        outputRoot = fs::path(opts.output.get());
    } else {
        fs::path tempRoot = fs::temp_directory_path();
        fs::path uniqueDir = fs::unique_path(_outputPathPrefix + "-%%%%-%%%%-%%%%-%%%%");
        outputRoot = tempRoot / uniqueDir;
    }

    _actualOutputRoot = outputRoot / "actual";
    _expectedOutputRoot = outputRoot / "expected";
}

std::string GoldenTestContext::toSnakeCase(const std::string& str) {
    std::string result;
    bool lastAlpha = false;
    for (char c : str) {
        if (ctype::isUpper(c)) {
            if (lastAlpha) {
                result += '_';
            }

            result += ctype::toLower(c);
        } else {
            result += c;
        }

        lastAlpha = ctype::isAlpha(c);
    }

    return result;
}

std::string GoldenTestContext::sanitizeName(const std::string& str) {
    if (!validNameRegex.FullMatch(str)) {
        FAIL("Unsupported characters in name '{}'"_format(str));
    }

    return toSnakeCase(str);
}

void GoldenTestContext::verifyOutput() {
    std::string actualStr = _outStream.str();

    fs::path goldenDataPath = getGoldedDataPath();
    if (!fs::exists(goldenDataPath)) {
        failResultMismatch(actualStr, boost::none, "Golden data file doesn't exist.");
    }

    std::string expectedStr = readFile(goldenDataPath);
    if (actualStr != expectedStr) {
        failResultMismatch(actualStr, expectedStr, "Actual result doesn't match golden data.");
    }
}

void GoldenTestContext::failResultMismatch(const std::string& actualStr,
                                           const boost::optional<std::string>& expectedStr,
                                           const std::string& message) {
    fs::path actualOutputFilePath = getActualOutputPath();
    fs::path expectedOutputFilePath = getExpectedOutputPath();

    writeFile(actualOutputFilePath, actualStr);
    if (expectedStr != boost::none) {
        writeFile(expectedOutputFilePath, *expectedStr);
    }

    LOGV2_ERROR(6273501,
                "Test output verification failed",
                "message"_attr = message,
                "actualOutput"_attr = actualStr,
                "expectedOutput"_attr = expectedStr,
                "actualOutputPath"_attr = actualOutputFilePath.string(),
                "expectedOutputPath"_attr = expectedOutputFilePath.string(),
                "actualOutputRoot"_attr = _env->actualOutputRoot().string(),
                "expectedOutputRoot"_attr = _env->expectedOutputRoot().string());

    throwAssertionFailureException(
        "Test output verification failed: {}, "
        "actual output file: {}, "
        "expected output file: {}"
        ""_format(message, actualOutputFilePath, expectedOutputFilePath));
}

std::string GoldenTestContext::readFile(const fs::path& path) {
    ASSERT_FALSE(is_directory(path));
    ASSERT_TRUE(is_regular_file(path));

    std::ostringstream os;
    os << fs::ifstream(path).rdbuf();
    return os.str();
}

void GoldenTestContext::writeFile(const fs::path& path, const std::string& contents) {
    create_directories(path.parent_path());
    fs::ofstream ofs(path);
    ofs << contents;
}

void GoldenTestContext::throwAssertionFailureException(const std::string& message) {
    throw TestAssertionFailureException(_testInfo->file().toString(), _testInfo->line(), message);
}

fs::path GoldenTestContext::getActualOutputPath() const {
    return _env->actualOutputRoot() / _config->relativePath / getTestPath();
}

fs::path GoldenTestContext::getExpectedOutputPath() const {
    return _env->expectedOutputRoot() / _config->relativePath / getTestPath();
}

fs::path GoldenTestContext::getGoldedDataPath() const {
    return _env->goldenDataRoot() / _config->relativePath / getTestPath();
}

fs::path GoldenTestContext::getTestPath() const {
    return fs::path(sanitizeName(_testInfo->suiteName().toString())) /
        fs::path(sanitizeName(_testInfo->testName().toString()) + ".txt");
}

GoldenTestOptions GoldenTestOptions::parseEnvironment() {
    GoldenTestOptions opts;
    namespace po = ::boost::program_options;
    po::options_description desc_env;
    desc_env.add_options()  //
        ("output", po::value<std::string>()->notifier([&opts](auto v) { opts.output = v; }));

    po::variables_map vm_env;
    po::store(po::parse_environment(desc_env, "GOLDEN_TEST_"), vm_env);
    po::notify(vm_env);

    return opts;
}

}  // namespace mongo::unittest