Skip to content

Commit fa7ca7b

Browse files
committed
update remote configuration
- Add and rework remote configuration tests. - Rework when client state are updated.
1 parent ebc6c37 commit fa7ca7b

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

src/datadog/remote_config/remote_config.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,29 +168,11 @@ nlohmann::json Manager::make_request_payload() {
168168

169169
void Manager::process_response(const nlohmann::json& json) {
170170
state_.error_message = nullopt;
171-
struct UpdateTargetsVersion {
172-
decltype(state_)& state;
173-
std::uint64_t new_targets_version;
174-
175-
~UpdateTargetsVersion() noexcept {
176-
// if there was a global error, the old targets_version should be sent in
177-
// the next request (at least this is what the system-tests expect)
178-
if ((!state.error_message || state.error_message->empty()) &&
179-
new_targets_version != std::uint64_t(-1)) {
180-
state.targets_version = new_targets_version;
181-
}
182-
}
183-
} update_targets_version{state_, std::uint64_t(-1)};
184171

185172
try {
186173
const auto targets = nlohmann::json::parse(
187174
base64_decode(json.at("targets").get<StringView>()));
188175

189-
update_targets_version.new_targets_version =
190-
targets.at("/signed/version"_json_pointer).get<std::uint64_t>();
191-
state_.opaque_backend_state =
192-
targets.at("/signed/custom/opaque_backend_state"_json_pointer);
193-
194176
const auto client_configs_it = json.find("client_configs");
195177

196178
// `client_configs` is absent => remove previously applied configuration if
@@ -210,6 +192,11 @@ void Manager::process_response(const nlohmann::json& json) {
210192
for (const auto& listener : listeners_) {
211193
listener->on_post_process();
212194
}
195+
196+
state_.targets_version =
197+
targets.at("/signed/version"_json_pointer).get<std::uint64_t>();
198+
state_.opaque_backend_state =
199+
targets.at("/signed/custom/opaque_backend_state"_json_pointer);
213200
return;
214201
}
215202

@@ -297,6 +284,10 @@ void Manager::process_response(const nlohmann::json& json) {
297284
for (const auto& listener : listeners_) {
298285
listener->on_post_process();
299286
}
287+
state_.targets_version =
288+
targets.at("/signed/version"_json_pointer).get<std::uint64_t>();
289+
state_.opaque_backend_state =
290+
targets.at("/signed/custom/opaque_backend_state"_json_pointer);
300291
} catch (const nlohmann::json::exception& json_exception) {
301292
std::string reason = "Failed to parse the response: ";
302293
reason += json_exception.what();

test/remote_config/test_remote_config.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ REMOTE_CONFIG_TEST("response processing") {
172172
"client_configs": ["employee/APM_TRACING/valid_conf_path/config"],
173173
"target_files": [{"path": "employee/APM_TRACING/valid_conf_path/config", "raw": "eyJmb28iOiAiYmFyIn0="}]
174174
})",
175+
/// invalid configuration path
176+
R"({
177+
"targets": "eyJzaWduZWQiOiB7InZlcnNpb24iOiAyLCAidGFyZ2V0cyI6IHsiZW1wbG95ZWUvQVBNX1RSQUNJTkcvdmFsaWRfY29uZl9wYXRoL2NvbmZpZyI6IHt9LCAiYmFyIjoge319LCJjdXN0b20iOiB7Im9wYXF1ZV9iYWNrZW5kX3N0YXRlIjogIjE1In19fQ==",
178+
"client_configs": ["foo"],
179+
"target_files": [{"path": "foo", "raw": "eyJmb28iOiAiYmFyIn0="}]
180+
})",
175181
}));
176182
// clang-format on
177183

@@ -191,8 +197,10 @@ REMOTE_CONFIG_TEST("response processing") {
191197
CHECK(payload.contains("/client/state/has_error"_json_pointer) == true);
192198
CHECK(payload.contains("/client/state/error"_json_pointer) == true);
193199

194-
// targets_version should not have been updated.
200+
// `targets_version` and `backend_client_state` should not have been
201+
// updated.
195202
CHECK(payload["client"]["state"]["targets_version"] == 0);
203+
CHECK(payload["client"]["state"]["backend_client_state"] == "");
196204
}
197205

198206
SECTION("update dispatch") {
@@ -252,8 +260,22 @@ REMOTE_CONFIG_TEST("response processing") {
252260
CHECK(agent_listener->count_on_revert == 0);
253261
CHECK(agent_listener->count_on_post_process == 1);
254262

255-
SECTION("config states are reported on next payload") {
263+
SECTION("next request payload is correctly populated") {
256264
const auto payload = rc.make_request_payload();
265+
266+
// Verify client state is reported
267+
REQUIRE(payload.contains("/client/state"_json_pointer) == true);
268+
const auto& client_state = payload.at("/client/state"_json_pointer);
269+
CHECK(client_state.at("targets_version") == 66204320);
270+
CHECK(
271+
client_state.at("backend_client_state") ==
272+
"eyJ2ZXJzaW9uIjoyLCJzdGF0ZSI6eyJmaWxlX2hhc2hlcyI6eyJkYXRhZG9nLzEwMDAx"
273+
"MjU4NDAvQVBNX1RSQUNJTkcvODI3ZWFjZjhkYmMzYWIxNDM0ZDMyMWNiODFkZmJmN2Fm"
274+
"ZTY1NGE0YjYxMTFjZjE2NjBiNzFjY2Y4OTc4MTkzOC8yOTA4NmJkYmU1MDZlNjhiNTBm"
275+
"MzA1NTgyM2EzZGE1Y2UwNTI4ZjE2NDBkNTJjZjg4NjE4MTZhYWE5ZmNlYWY0IjpbIm9Y"
276+
"ZDJpeUMzeC9oRWsxeXVhY1hGN1lqcXJpTk9BWUtuZzFtWE01NVZKTHc9Il19fX0=");
277+
278+
// Verify config states are reported
257279
REQUIRE(payload.contains("/client/state/config_states"_json_pointer) ==
258280
true);
259281

@@ -273,36 +295,25 @@ REMOTE_CONFIG_TEST("response processing") {
273295
CHECK(!config_state.contains("apply_error"));
274296
}
275297
}
276-
}
277-
278-
SECTION("cached_target_files is correctly populated") {
279-
auto payload = rc.make_request_payload();
280298

299+
// Verify `cached_target_files` is reported
281300
auto cached_target_files = payload.find("cached_target_files");
282301
REQUIRE(cached_target_files != payload.end());
283-
284302
REQUIRE(cached_target_files->is_array());
285303
REQUIRE(cached_target_files->size() == 3);
286304

287-
std::sort(cached_target_files->begin(), cached_target_files->end(),
288-
[](const auto& a, const auto& b) {
289-
return a.at("path").template get<std::string_view>() <
290-
b.at("path").template get<std::string_view>();
291-
});
292-
293305
const auto ctf = cached_target_files->at(0);
294-
REQUIRE(ctf.at("path").get<std::string_view>() ==
295-
"employee/AGENT_CONFIG/test_rc_update/flare_conf");
296-
REQUIRE(ctf.at("length").get<std::uint64_t>() == 381UL);
306+
CHECK(ctf.at("path").get<std::string_view>() ==
307+
"employee/AGENT_CONFIG/test_rc_update/flare_conf");
308+
CHECK(ctf.at("length").get<std::uint64_t>() == 381UL);
297309

298310
auto hashes = ctf.at("hashes");
299-
300311
REQUIRE(hashes.is_array());
301312
REQUIRE(hashes.size() == 1);
302313

303314
const auto h = hashes.at(0);
304-
REQUIRE(h.at("algorithm").get<std::string_view>() == "sha256");
305-
REQUIRE(h.at("hash").get<std::string_view>().size() == 64U);
315+
CHECK(h.at("algorithm").get<std::string_view>() == "sha256");
316+
CHECK(h.at("hash").get<std::string_view>().size() == 64U);
306317
}
307318

308319
SECTION("same config update should not trigger listeners") {

0 commit comments

Comments
 (0)