File: catch2_telemetry.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (184 lines) | stat: -rw-r--r-- 7,170 bytes parent folder | download | duplicates (3)
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
#include "catch2_common.h"

namespace
{
constexpr double SERVER_VALUE = 8.888;
}

template <class Base>
class TelemetryDS : public Base
{
  public:
    using Base::Base;

    void init_device() override { }

    void read_attribute(Tango::Attribute &att)
    {
        auto span = TANGO_TELEMETRY_SPAN(TANGO_CURRENT_FUNCTION, {{"myKey", "myValue"}});
        auto scope = TANGO_TELEMETRY_SCOPE(span);

        attr_dq_double = SERVER_VALUE;
        att.set_value_date_quality(&attr_dq_double, std::chrono::system_clock::now(), Tango::ATTR_VALID);
    }

    static void attribute_factory(std::vector<Tango::Attr *> &attrs)
    {
        attrs.push_back(new TangoTest::AutoAttr<&TelemetryDS::read_attribute>("attr_dq_db", Tango::DEV_DOUBLE));
    }

  private:
    Tango::DevDouble attr_dq_double;
};

TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(TelemetryDS, 3)

SCENARIO("Telemetry traces are outputted")
{
    int idlver = GENERATE(TangoTest::idlversion(3));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        std::vector<std::string> env{"TANGO_TELEMETRY_ENABLE=on",
                                     "TANGO_TELEMETRY_KERNEL_ENABLE=on",
                                     "TANGO_TELEMETRY_TRACES_EXPORTER=console",
                                     "TANGO_TELEMETRY_LOGS_EXPORTER=console"};
        TangoTest::Context ctx{"telemetry", "TelemetryDS", idlver, env};
        auto device = ctx.get_proxy();

        REQUIRE(idlver == device->get_idl_version());

        WHEN("we read the attribute")
        {
            std::string att{"attr_dq_db"};

            Tango::DeviceAttribute da;
            REQUIRE_NOTHROW(da = device->read_attribute(att));

            THEN("the read value matches the value on the server")
            {
                double att_value;
                da >> att_value;
                REQUIRE(att_value == SERVER_VALUE);
            }

            auto contents = load_file(ctx.get_redirect_file());
            REQUIRE(!contents.empty());

            using Catch::Matchers::ContainsSubstring;
            REQUIRE_THAT(contents, ContainsSubstring("code.filepath:"));

            if(idlver > 3)
            {
                REQUIRE_THAT(contents,
                             ContainsSubstring("TelemetryDS") && ContainsSubstring("read_attribute") &&
                                 ContainsSubstring("myKey") && ContainsSubstring("myValue"));
            }
        }
    }
}

SCENARIO("Telemetry does complain about invalid environment variables")
{
    int idlver = GENERATE(TangoTest::idlversion(6));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        struct TestData
        {
            std::string telemetry, kernel, traces_exporter, logs_exporter, traces_endpoint, logs_endpoint;
        };

        auto data = GENERATE(TestData{"bogus", "on", "console", "console", "cout", "cout"},
                             TestData{"on", "bogus", "console", "console", "cout", "cout"},
                             TestData{"on", "on", "bogus", "console", "cout", "cout"},
                             TestData{"on", "on", "console", "bogus", "cout", "cout"},
                             TestData{"on", "on", "console", "console", "bogus", "cout"},
                             TestData{"on", "on", "console", "console", "cout", "bogus"});

        WHEN("set the environment variables")
        {
            std::vector<std::string> env{"TANGO_TELEMETRY_ENABLE=" + data.telemetry,
                                         "TANGO_TELEMETRY_KERNEL_ENABLE=" + data.kernel,
                                         "TANGO_TELEMETRY_TRACES_EXPORTER=" + data.traces_exporter,
                                         "TANGO_TELEMETRY_LOGS_EXPORTER=" + data.logs_exporter,
                                         "TANGO_TELEMETRY_TRACES_ENDPOINT=" + data.traces_endpoint,
                                         "TANGO_TELEMETRY_LOGS_ENDPOINT=" + data.logs_endpoint};

            INFO(Catch::StringMaker<std::vector<std::string>>::convert(env));

            // avoid parsing issues in REQUIRE_XXX macro
            auto f = [&idlver, &env]() { TangoTest::Context ctx{"telemetry", "TelemetryDS", idlver, env}; };

            using Catch::Matchers::ContainsSubstring;
            using Catch::Matchers::MessageMatches;
            REQUIRE_THROWS_MATCHES(
                f(), std::runtime_error, MessageMatches(ContainsSubstring("Error reason = API_InvalidArgs")));
        }
    }
}

SCENARIO("Telemetry can be configured for all variants")
{
    int idlver = GENERATE(TangoTest::idlversion(6));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        struct TestData
        {
            std::string traces_exporter, logs_exporter, traces_endpoint, logs_endpoint;
        };

        auto data =
            GENERATE(TestData{"console", "console", "cout", "cerr"},
                     TestData{"http", "http", "http://localhost:4711/v1/traces", "https://localhost:4712/v1/traces"},
                     TestData{"grpc", "grpc", "grpc://localhost:4711", "grpc://localhost:4712"});

        WHEN("set the environment variables")
        {
            std::vector<std::string> env{"TANGO_TELEMETRY_ENABLE=ON",
                                         "TANGO_TELEMETRY_KERNEL_ENABLE=OFF",
                                         "TANGO_TELEMETRY_TRACES_EXPORTER=" + data.traces_exporter,
                                         "TANGO_TELEMETRY_LOGS_EXPORTER=" + data.logs_exporter,
                                         "TANGO_TELEMETRY_TRACES_ENDPOINT=" + data.traces_endpoint,
                                         "TANGO_TELEMETRY_LOGS_ENDPOINT=" + data.logs_endpoint};

            auto f = [&idlver, &env]() { TangoTest::Context ctx{"telemetry", "TelemetryDS", idlver, env}; };
            REQUIRE_NOTHROW(f());
        }
    }
}

SCENARIO("Telemetry traces/logs can be turned off")
{
    int idlver = GENERATE(TangoTest::idlversion(6));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        std::vector<std::string> env{"TANGO_TELEMETRY_ENABLE=on",
                                     "TANGO_TELEMETRY_KERNEL_ENABLE=on",
                                     "TANGO_TELEMETRY_TRACES_EXPORTER=none",
                                     "TANGO_TELEMETRY_LOGS_EXPORTER=none"};
        TangoTest::Context ctx{"telemetry", "TelemetryDS", idlver, env};
        auto device = ctx.get_proxy();

        REQUIRE(idlver == device->get_idl_version());

        WHEN("we read the attribute")
        {
            std::string att{"attr_dq_db"};

            Tango::DeviceAttribute da;
            REQUIRE_NOTHROW(da = device->read_attribute(att));

            THEN("the read value matches the value on the server")
            {
                double att_value;
                da >> att_value;
                REQUIRE(att_value == SERVER_VALUE);
            }

            auto contents = load_file(ctx.get_redirect_file());
            REQUIRE(!contents.empty());

            using Catch::Matchers::ContainsSubstring;
            REQUIRE_THAT(contents, !ContainsSubstring("code.filepath:"));
        }
    }
}