Skip to content

Commit ebc6c37

Browse files
committed
Fix bugs in remote config
1 parent 3e0244f commit ebc6c37

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/datadog/remote_config/remote_config.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ nlohmann::json Manager::make_request_payload() {
154154
nlohmann::json cached_file = {
155155
{"path", config.path},
156156
{"length", config.content.size()},
157-
{"hashes", {{"algorithm", "sha256"}, {"hash", config.hash}}}};
157+
{"hashes", {{{"algorithm", "sha256"}, {"hash", config.hash}}}}};
158158

159159
cached_target_files.emplace_back(std::move(cached_file));
160160
}
161161

162-
j["cached_target"] = cached_target_files;
162+
j["cached_target_files"] = cached_target_files;
163163
j["client"]["state"]["config_states"] = config_states;
164164
}
165165

@@ -168,12 +168,26 @@ 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)};
171184

172185
try {
173186
const auto targets = nlohmann::json::parse(
174187
base64_decode(json.at("targets").get<StringView>()));
175188

176-
state_.targets_version = targets.at("/signed/version"_json_pointer);
189+
update_targets_version.new_targets_version =
190+
targets.at("/signed/version"_json_pointer).get<std::uint64_t>();
177191
state_.opaque_backend_state =
178192
targets.at("/signed/custom/opaque_backend_state"_json_pointer);
179193

test/remote_config/test_remote_config.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <algorithm>
2+
13
#include "catch.hpp"
24
#include "datadog/json.hpp"
35
#include "datadog/remote_config/remote_config.h"
@@ -184,10 +186,13 @@ REMOTE_CONFIG_TEST("response processing") {
184186

185187
rc.process_response(response_json);
186188

187-
// Next payload should contains an error.
189+
// Next payload should contain an error.
188190
const auto payload = rc.make_request_payload();
189191
CHECK(payload.contains("/client/state/has_error"_json_pointer) == true);
190192
CHECK(payload.contains("/client/state/error"_json_pointer) == true);
193+
194+
// targets_version should not have been updated.
195+
CHECK(payload["client"]["state"]["targets_version"] == 0);
191196
}
192197

193198
SECTION("update dispatch") {
@@ -270,6 +275,36 @@ REMOTE_CONFIG_TEST("response processing") {
270275
}
271276
}
272277

278+
SECTION("cached_target_files is correctly populated") {
279+
auto payload = rc.make_request_payload();
280+
281+
auto cached_target_files = payload.find("cached_target_files");
282+
REQUIRE(cached_target_files != payload.end());
283+
284+
REQUIRE(cached_target_files->is_array());
285+
REQUIRE(cached_target_files->size() == 3);
286+
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+
293+
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);
297+
298+
auto hashes = ctf.at("hashes");
299+
300+
REQUIRE(hashes.is_array());
301+
REQUIRE(hashes.size() == 1);
302+
303+
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);
306+
}
307+
273308
SECTION("same config update should not trigger listeners") {
274309
rc.process_response(response_json);
275310
CHECK(tracing_listener->count_on_update == 1);

0 commit comments

Comments
 (0)