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
|
//===- llvm/unittest/Telemetry/TelemetryTest.cpp - Telemetry unittests ---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Telemetry/Telemetry.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
#include "gtest/gtest.h"
#include <optional>
#include <vector>
namespace llvm {
namespace telemetry {
// Testing parameters.
//
// These are set by each test to force certain outcomes.
struct TestContext {
// Controlling whether there is vendor plugin. In "real" implementation, the
// plugin-registration framework will handle the overrides but for tests, we
// just use a bool flag to decide which function to call.
bool HasVendorPlugin = false;
// This field contains data emitted by the framework for later
// verification by the tests.
std::string Buffer = "";
// The expected Uuid generated by the fake tool.
std::string ExpectedUuid = "";
};
class StringSerializer : public Serializer {
public:
const std::string &getString() { return Buffer; }
Error init() override {
if (Started)
return createStringError("Serializer already in use");
Started = true;
Buffer.clear();
return Error::success();
}
void write(StringRef KeyName, bool Value) override {
writeHelper(KeyName, Value);
}
void write(StringRef KeyName, StringRef Value) override {
writeHelper(KeyName, Value);
}
void write(StringRef KeyName, int Value) override {
writeHelper(KeyName, Value);
}
void write(StringRef KeyName, long Value) override {
writeHelper(KeyName, Value);
}
void write(StringRef KeyName, long long Value) override {
writeHelper(KeyName, Value);
}
void write(StringRef KeyName, unsigned int Value) override {
writeHelper(KeyName, Value);
}
void write(StringRef KeyName, unsigned long Value) override {
writeHelper(KeyName, Value);
}
void write(StringRef KeyName, unsigned long long Value) override {
writeHelper(KeyName, Value);
}
void beginObject(StringRef KeyName) override {
Children.push_back(std::string("\n"));
ChildrenNames.push_back(KeyName.str());
}
void endObject() override {
assert(!Children.empty() && !ChildrenNames.empty());
std::string ChildBuff = Children.back();
std::string Name = ChildrenNames.back();
Children.pop_back();
ChildrenNames.pop_back();
writeHelper(Name, ChildBuff);
}
Error finalize() override {
assert(Children.empty() && ChildrenNames.empty());
if (!Started)
return createStringError("Serializer not currently in use");
Started = false;
return Error::success();
}
private:
template <typename T> void writeHelper(StringRef Name, T Value) {
assert(Started && "serializer not started");
if (Children.empty())
Buffer.append((Name + ":" + Twine(Value) + "\n").str());
else
Children.back().append((Name + ":" + Twine(Value) + "\n").str());
}
bool Started = false;
std::string Buffer;
std::vector<std::string> Children;
std::vector<std::string> ChildrenNames;
};
namespace vendor {
struct VendorConfig : public Config {
VendorConfig(bool Enable) : Config(Enable) {}
std::optional<std::string> makeSessionId() override {
static int seed = 0;
return std::to_string(seed++);
}
};
std::shared_ptr<Config> getTelemetryConfig(const TestContext &Ctxt) {
return std::make_shared<VendorConfig>(/*EnableTelemetry=*/true);
}
class TestStorageDestination : public Destination {
public:
TestStorageDestination(TestContext *Ctxt) : CurrentContext(Ctxt) {}
Error receiveEntry(const TelemetryInfo *Entry) override {
if (Error Err = serializer.init())
return Err;
Entry->serialize(serializer);
if (Error Err = serializer.finalize())
return Err;
CurrentContext->Buffer.append(serializer.getString());
return Error::success();
}
StringLiteral name() const override { return "TestDestination"; }
private:
TestContext *CurrentContext;
StringSerializer serializer;
};
struct StartupInfo : public TelemetryInfo {
std::string ToolName;
std::map<std::string, std::string> MetaData;
void serialize(Serializer &serializer) const override {
TelemetryInfo::serialize(serializer);
serializer.write("ToolName", ToolName);
serializer.write("MetaData", MetaData);
}
};
struct ExitInfo : public TelemetryInfo {
int ExitCode;
std::string ExitDesc;
void serialize(Serializer &serializer) const override {
TelemetryInfo::serialize(serializer);
serializer.write("ExitCode", ExitCode);
serializer.write("ExitDesc", ExitDesc);
}
};
class TestManager : public Manager {
public:
static std::unique_ptr<TestManager>
createInstance(Config *Config, TestContext *CurrentContext) {
if (!Config->EnableTelemetry)
return nullptr;
CurrentContext->ExpectedUuid = *(Config->makeSessionId());
std::unique_ptr<TestManager> Ret = std::make_unique<TestManager>(
CurrentContext, CurrentContext->ExpectedUuid);
// Add a destination.
Ret->addDestination(
std::make_unique<TestStorageDestination>(CurrentContext));
return Ret;
}
TestManager(TestContext *Ctxt, std::string Id)
: CurrentContext(Ctxt), SessionId(Id) {}
Error preDispatch(TelemetryInfo *Entry) override {
Entry->SessionId = SessionId;
(void)CurrentContext;
return Error::success();
}
std::string getSessionId() { return SessionId; }
private:
TestContext *CurrentContext;
const std::string SessionId;
};
} // namespace vendor
std::shared_ptr<Config> getTelemetryConfig(const TestContext &Ctxt) {
if (Ctxt.HasVendorPlugin)
return vendor::getTelemetryConfig(Ctxt);
return std::make_shared<Config>(false);
}
#if LLVM_ENABLE_TELEMETRY
#define TELEMETRY_TEST(suite, test) TEST(suite, test)
#else
#define TELEMETRY_TEST(suite, test) TEST(DISABLED_##suite, test)
#endif
TELEMETRY_TEST(TelemetryTest, TelemetryDisabled) {
TestContext Context;
Context.HasVendorPlugin = false;
std::shared_ptr<Config> Config = getTelemetryConfig(Context);
auto Manager = vendor::TestManager::createInstance(Config.get(), &Context);
EXPECT_EQ(nullptr, Manager);
}
TELEMETRY_TEST(TelemetryTest, TelemetryEnabled) {
const std::string ToolName = "TelemetryTestTool";
// Preset some params.
TestContext Context;
Context.HasVendorPlugin = true;
Context.Buffer.clear();
std::shared_ptr<Config> Config = getTelemetryConfig(Context);
auto Manager = vendor::TestManager::createInstance(Config.get(), &Context);
EXPECT_STREQ(Manager->getSessionId().c_str(), Context.ExpectedUuid.c_str());
vendor::StartupInfo S;
S.ToolName = ToolName;
S.MetaData["a"] = "A";
S.MetaData["b"] = "B";
Error startupEmitStatus = Manager->dispatch(&S);
EXPECT_FALSE(startupEmitStatus);
std::string ExpectedBuffer =
"SessionId:0\nToolName:TelemetryTestTool\nMetaData:\na:A\nb:B\n\n";
EXPECT_EQ(ExpectedBuffer, Context.Buffer);
Context.Buffer.clear();
vendor::ExitInfo E;
E.ExitCode = 0;
E.ExitDesc = "success";
Error exitEmitStatus = Manager->dispatch(&E);
EXPECT_FALSE(exitEmitStatus);
ExpectedBuffer = "SessionId:0\nExitCode:0\nExitDesc:success\n";
EXPECT_EQ(ExpectedBuffer, Context.Buffer);
}
} // namespace telemetry
} // namespace llvm
|