aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/common/hostreporter/hostinfotest.cpp
blob: 96211f9b2ccc90944fc261a90eefe69b66cbb0dd (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/storage/common/hostreporter/hostinfo.h>
#include <vespa/storage/common/hostreporter/hostreporter.h>
#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/util/jsonstream.h>
#include "util.h"

namespace storage {
namespace {
using Object = vespalib::JsonStream::Object;
using End = vespalib::JsonStream::End;
using JsonFormat = vespalib::slime::JsonFormat;
using Memory = vespalib::Memory;

class DummyReporter: public HostReporter {
public:
    void report(vespalib::JsonStream& jsonreport) override {
        jsonreport << "dummy" << Object()  << "foo" << "bar" << End();
    }
};
}

struct HostInfoReporterTest : public CppUnit::TestFixture
{
    void testHostInfoReporter();

    CPPUNIT_TEST_SUITE(HostInfoReporterTest);
    CPPUNIT_TEST(testHostInfoReporter);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(HostInfoReporterTest);

void
HostInfoReporterTest::testHostInfoReporter()
{
    HostInfo hostinfo;
    DummyReporter dummyReporter;
    hostinfo.registerReporter(&dummyReporter);
    vespalib::asciistream json;
    vespalib::JsonStream stream(json, true);

    stream << Object();
    hostinfo.printReport(stream);
    stream << End();

    std::string jsonData = json.str();
    vespalib::Slime slime;
    JsonFormat::decode(Memory(jsonData), slime);
    CPPUNIT_ASSERT(slime.get()["dummy"]["foo"].asString() == "bar");
    CPPUNIT_ASSERT(0 < slime.get()["network"]["lo"]["input"]["packets"].asLong());
    CPPUNIT_ASSERT(1.0 <= slime.get()["cpu"]["context switches"].asDouble());
}
} // storage