File: test_telemetry.cc

package info (click to toggle)
eckit 1.32.4-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 305,876 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 42
file content (114 lines) | stat: -rw-r--r-- 3,371 bytes parent folder | download | duplicates (2)
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
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <cstring>

#include "eckit/config/Resource.h"
#include "eckit/config/YAMLConfiguration.h"
#include "eckit/filesystem/TmpFile.h"
#include "eckit/io/DataHandle.h"
#include "eckit/io/FileHandle.h"
#include "eckit/runtime/Telemetry.h"
#include "eckit/runtime/Tool.h"
#include "eckit/testing/Test.h"
#include "eckit/types/Types.h"

using namespace std;
using namespace eckit;
using namespace eckit::runtime;
using namespace eckit::testing;

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

class Tester {
public:

    Tester() {
        format(std::cout, Log::fullFormat);
        std::string base = Resource<std::string>("$TMPDIR", "/tmp");
        config_          = PathName::unique(base + "/telemetry.yaml");
    }

    ~Tester() { config_.unlink(); }

    void setConfig(const std::string& cfg) {
        std::unique_ptr<DataHandle> dh(config_.fileHandle());
        dh->openForWrite(0);
        dh->write(cfg.data(), cfg.size());
        dh->close();

        ::setenv("TELEMETRY_CONFIG_FILE", config_.asString().c_str(), 1);
    }

    PathName config_;
};

//----------------------------------------------------------------------------------------------------------------------

CASE("Telemetry standard configuration") {

    Tester tester;

    const char* config_str = R"XXX(
---
service_type: myServiceType
service_name: myServiceName
service_groups:
  - group1
  - group2
servers:
  - host: localhost
    port: 30302
)XXX";

    eckit::testing::SetEnv env("TELEMETRY_CONFIG", config_str);

    SECTION("All messages sent") {

        std::string msg;

        EXPECT_NO_THROW(msg = Telemetry::report(Report::APPSTART));
        {
            // std::cout << msg << std::endl;

            YAMLConfiguration cfg(msg);
            EXPECT(cfg.getInt("version") == 1);
            EXPECT(cfg.getString("type") == "appstart");
            EXPECT(cfg.getString("service_type") == "myServiceType");
            EXPECT(cfg.getString("service_name") == "myServiceName");
            EXPECT(cfg.getString("service_name") == "myServiceName");
            std::vector<std::string> groups = {"group1", "group2"};
            EXPECT(cfg.getStringVector("service_groups") == groups);
        }

        EXPECT_EQUAL(Telemetry::countSent(), 1);

        EXPECT_NO_THROW(Telemetry::report(Report::INFO));
        EXPECT_NO_THROW(Telemetry::report(Report::METER));
        EXPECT_NO_THROW(Telemetry::report(Report::KEEPALIVE));

        EXPECT_EQUAL(Telemetry::countSent(), 4);

        EXPECT_NO_THROW(Telemetry::report(Report::COUNTER));
        EXPECT_NO_THROW(Telemetry::report(Report::APPSTOP));

        EXPECT_EQUAL(Telemetry::countSent(), 6);
    }
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

int main(int argc, char** argv) {
    return run_tests(argc, argv);
}