aboutsummaryrefslogtreecommitdiffstats
path: root/logd/src/tests/watcher/watcher_test.cpp
blob: fa8c2ffe932ecc69f4ccad2a18a4de3bc2a2d049 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/gtest/gtest.h>
#include <logd/config_subscriber.h>
#include <vespa/config/common/configcontext.h>
#include <logd/watcher.h>
#include <vespa/vespalib/io/fileutil.h>
#include <vespa/vespalib/util/threadstackexecutor.h>
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/vespalib/util/size_literals.h>
#include <filesystem>
#include <fstream>
#include <regex>
#include <thread>
#include <mutex>
#include <condition_variable>

using cloud::config::log::LogdConfigBuilder;
using cloud::config::log::LogdConfig;
using config::ConfigSet;
using config::ConfigUri;
using config::IConfigContext;
using config::ConfigContext;
using vespalib::ThreadStackExecutor;
using vespalib::makeLambdaTask;
using namespace std::chrono_literals;

std::regex rotated_log(R"(vespa.log-[0-9]*-[0-9]*-[0-9]*\.[0-9]*-[0-9]*-[0-9]*)");

namespace logdemon {

namespace {

struct ConfigFixture {
    const std::string configId;
    LogdConfigBuilder logdBuilder;
    ConfigSet set;
    std::shared_ptr<IConfigContext> context;
    int idcounter;

    ConfigFixture(const std::string & id);
    ~ConfigFixture();
    void reload() { context->reload(); }
};

ConfigFixture::ConfigFixture(const std::string & id)
    : configId(id),
      logdBuilder(),
      set(),
      context(std::make_shared<ConfigContext>(set)),
      idcounter(-1)
{
    logdBuilder.logserver.use = false;
    logdBuilder.rotate.size = 1024;
    set.addBuilder(configId, &logdBuilder);
}

ConfigFixture::~ConfigFixture() = default;

struct DummyForwarder : public Forwarder {
    std::mutex lock;
    std::condition_variable cond;
    std::vector<std::string> lines;
    DummyForwarder();
    ~DummyForwarder() override;
    void forwardLine(std::string_view log_line) override {
        std::lock_guard guard(lock);
        lines.emplace_back(log_line);
        cond.notify_all();
    }
    void flush() override { }
    int badLines() const override { return 0; }
    void resetBadLines() override { }
    std::vector<std::string> getLines() {
        std::lock_guard guard(lock);
        return lines;
    }
    void waitLineCount(size_t lineCount) {
        std::unique_lock guard(lock);
        cond.wait_for(guard, 10s, [this, lineCount]() { return lineCount <= lines.size(); });
    }
};

DummyForwarder::DummyForwarder()
    : Forwarder(),
      lock(),
      cond(),
      lines()
{ }
DummyForwarder::~DummyForwarder() = default;

struct WatcherFixture
{
    DummyForwarder fwd;
    ConfigSubscriber subscriber;
    Watcher watcher;

    WatcherFixture(ConfigFixture &cfg);
    ~WatcherFixture();
};

WatcherFixture::WatcherFixture(ConfigFixture &cfg)
    : fwd(),
      subscriber(config::ConfigUri(cfg.configId, cfg.context)),
      watcher(subscriber, fwd)
{
    subscriber.latch();
}

WatcherFixture::~WatcherFixture() = default;

}

class WatcherTest : public ::testing::Test {
protected:
    std::unique_ptr<ConfigFixture> _cfg;
    std::unique_ptr<WatcherFixture> _watcher;
    ThreadStackExecutor _executor;

    void setup_watcher();
    void run_watcher();
    void stop_watcher();
    void log_line(const std::string &line);
    void assert_lines(const std::vector<std::string> &lines);
    void remove_files();
    void remove_rotated();
    int count_rotated();
public:
    WatcherTest();
    ~WatcherTest();
};

WatcherTest::WatcherTest()
    : _executor(1)
{
    remove_files();
    setenv("VESPA_LOG_TARGET", "file:vespa.log", true);
    std::filesystem::create_directories(std::filesystem::path("var/db/vespa")); // for logd.donestate
    _cfg = std::make_unique<ConfigFixture>("testconfigid");
}

WatcherTest::~WatcherTest()
{
    remove_files();
}

void
WatcherTest::setup_watcher()
{
    _watcher = std::make_unique<WatcherFixture>(*_cfg);
}

void
WatcherTest::run_watcher()
{
    // Spin off watcher task
    _executor.execute(makeLambdaTask([this]() { _watcher->watcher.watchfile(); }));
}

void
WatcherTest::stop_watcher()
{
    _cfg->reload();
    _executor.sync();
}

void
WatcherTest::log_line(const std::string &line)
{
    std::ofstream log_file("vespa.log", std::ios::out | std::ios::app);
    log_file << line << std::endl;
}

void
WatcherTest::assert_lines(const std::vector<std::string> &lines)
{
    EXPECT_EQ(lines, _watcher->fwd.getLines());
}

void
WatcherTest::remove_files()
{
    std::filesystem::remove_all(std::filesystem::path("var"));
    remove_rotated();
    std::filesystem::remove(std::filesystem::path("vespa.log"));
}

void
WatcherTest::remove_rotated()
{
    auto dirlist = vespalib::listDirectory(".");
    for (const auto &entry : dirlist) {
        if (std::regex_match(entry.data(), entry.data() + entry.size(), rotated_log)) {
            std::filesystem::remove(std::filesystem::path(entry));
        }
    }
}
    
int
WatcherTest::count_rotated()
{
    int result = 0;
    auto dirlist = vespalib::listDirectory(".");
    for (const auto &entry : dirlist) {
        if (std::regex_match(entry.data(), entry.data() + entry.size(), rotated_log)) {
            ++result;
        }
    }
    return result;
}

TEST_F(WatcherTest, require_that_watching_no_logging_works)
{
    setup_watcher();
    run_watcher();
    stop_watcher();
    assert_lines({});
    EXPECT_EQ(0, count_rotated());
}

TEST_F(WatcherTest, require_that_watching_simple_logging_works)
{
    setup_watcher();
    run_watcher();
    log_line("foo");
    _watcher->fwd.waitLineCount(1);
    stop_watcher();
    EXPECT_EQ(0, count_rotated());
    assert_lines({"foo"});
}

TEST_F(WatcherTest, require_that_watching_can_resume)
{
    setup_watcher();
    run_watcher();
    log_line("foo");
    _watcher->fwd.waitLineCount(1);
    stop_watcher();
    assert_lines({"foo"});
    setup_watcher();
    run_watcher();
    log_line("bar");
    log_line("baz");
    _watcher->fwd.waitLineCount(2);
    stop_watcher();
    assert_lines({"bar", "baz"});
    // remove state file. Old entry will resurface
    std::filesystem::remove(std::filesystem::path("var/db/vespa/logd.donestate"));
    setup_watcher();
    run_watcher();
    _watcher->fwd.waitLineCount(3);
    stop_watcher();
    assert_lines({"foo", "bar", "baz"});
}

TEST_F(WatcherTest, require_that_watching_can_rotate_log_files)
{
    setup_watcher();
    run_watcher();
    std::vector<std::string> exp_lines;
    for (int i = 0; i < 100; ++i) {
        std::ostringstream os;
        os << "this is a malformatted " << std::setw(3) << i <<
            std::setw(0) << " line but who cares ?";
        log_line(os.str());
        exp_lines.push_back(os.str());
        std::this_thread::sleep_for(100ms);
        if (count_rotated() > 0) {
            break;
        }
    }
    _watcher->fwd.waitLineCount(exp_lines.size());
    stop_watcher();
    assert_lines(exp_lines);
    EXPECT_LT(0, count_rotated());
}

}

GTEST_MAIN_RUN_ALL_TESTS()