Skip to content

Commit d1142b4

Browse files
refactor: config build (#2617)
1 parent 56810be commit d1142b4

21 files changed

Lines changed: 323 additions & 425 deletions

agent-control/tests/on_host/proxy.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![cfg(target_family = "unix")]
2-
use fake_opamp_server::FakeServer;
32
use crate::common::agent_control::start_agent_control_with_custom_config;
43
use crate::common::effective_config::check_latest_effective_config_is_expected;
54
use crate::common::health::check_latest_health_status_was_healthy;
6-
use crate::common::{runtime::tokio_runtime, retry::retry};
7-
use crate::on_host::tools::config::create_agent_control_config_with_proxy;
5+
use crate::common::{retry::retry, runtime::tokio_runtime};
6+
use crate::on_host::tools::config::AgentControlConfigBuilder;
87
use crate::on_host::tools::instance_id::get_instance_id;
8+
use fake_opamp_server::FakeServer;
99
use newrelic_agent_control::agent_control::agent_id::AgentID;
1010
use newrelic_agent_control::agent_control::run::BasePaths;
1111
use newrelic_agent_control::agent_control::run::on_host::AGENT_CONTROL_MODE_ON_HOST;
@@ -48,17 +48,11 @@ fn proxy_onhost_opamp_agent_control_local_effective_config() {
4848
.jwks_endpoint()
4949
.replace("localhost", host_gateway.as_str());
5050

51-
let agents = "{}";
52-
53-
create_agent_control_config_with_proxy(
54-
opamp_server_endpoint,
55-
jwks_endpoint,
56-
agents.to_string(),
57-
local_dir.path().to_path_buf(),
58-
Some(format!(
51+
AgentControlConfigBuilder::basic(opamp_server_endpoint, jwks_endpoint)
52+
.with_proxy(format!(
5953
"{{\"url\": \"{proxy_url}\", \"ca_bundle_dir\": \"{proxy_ca_dir}\"}}"
60-
)),
61-
);
54+
))
55+
.write(local_dir.path().to_path_buf());
6256

6357
let base_paths = BasePaths {
6458
local_dir: local_dir.path().to_path_buf(),

agent-control/tests/on_host/scenarios/ac_self_update.rs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ use crate::common::remote_config_status::check_latest_remote_config_status_is_ex
44
use crate::common::retry::retry;
55
use crate::common::retry::retry_never;
66
use crate::common::runtime::tokio_runtime;
7-
use crate::on_host::tools::config::create_local_config;
7+
use crate::on_host::tools::config::AgentControlConfigBuilder;
88
use crate::on_host::tools::fake_binary::assert_is_fake_binary;
99
use crate::on_host::tools::fake_binary::build_fake_ac_binary;
1010
use crate::on_host::tools::fake_binary::build_invalid_fake_ac_binary;
1111
use crate::on_host::tools::instance_id::get_instance_id;
1212
use crate::on_host::tools::oci_package_manager::TestDataHelper;
1313
use fake_opamp_server::FakeServer;
1414
use newrelic_agent_control::agent_control::agent_id::AgentID;
15-
use newrelic_agent_control::agent_control::defaults::AGENT_CONTROL_ID;
1615
use newrelic_agent_control::agent_control::defaults::AGENT_CONTROL_VERSION;
1716
use newrelic_agent_control::agent_control::run::BasePaths;
1817
use newrelic_agent_control::agent_control::run::on_host::AGENT_CONTROL_MODE_ON_HOST;
@@ -326,32 +325,14 @@ fn create_self_update_local_config(
326325
local_dir: &Path,
327326
signature_verification_enabled: bool,
328327
) {
329-
let config = format!(
330-
r#"
331-
host_id: integration-test
332-
fleet_control:
333-
endpoint: {}
334-
poll_interval: 5s
335-
signature_validation:
336-
public_key_server_url: {}
337-
agents: {{}}
338-
oci:
339-
registry: {OCI_TEST_REGISTRY_URL}
340-
self_update:
341-
enabled: true
342-
signature_verification_enabled: {signature_verification_enabled}
343-
package:
344-
download:
345-
oci:
346-
registry: {OCI_TEST_REGISTRY_URL}
347-
repository: test
348-
public_key_url: {}
349-
"#,
350-
opamp_server.endpoint(),
351-
opamp_server.jwks_endpoint(),
352-
signer.jwks_url()
353-
);
354-
create_local_config(AGENT_CONTROL_ID, config, local_dir.to_path_buf());
328+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
329+
.with_oci_registry(OCI_TEST_REGISTRY_URL)
330+
.with_self_update(
331+
signature_verification_enabled,
332+
"test",
333+
signer.jwks_url().to_string(),
334+
)
335+
.write(local_dir.to_path_buf());
355336
}
356337

357338
/// Pushes an invalid fake agent-control binary package to the OCI registry and signs it.

agent-control/tests/on_host/scenarios/attributes.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::common::attributes::{
66
use crate::common::retry::retry;
77
use crate::common::runtime::tokio_runtime;
88
use crate::on_host::consts::NO_CONFIG;
9-
use crate::on_host::tools::config::{create_agent_control_config, create_local_config};
9+
use crate::on_host::tools::config::{AgentControlConfigBuilder, create_local_config};
1010
use crate::on_host::tools::custom_agent_type::CustomAgentType;
1111
use crate::on_host::tools::instance_id::get_instance_id;
1212
use fake_opamp_server::FakeServer;
@@ -46,12 +46,9 @@ fn test_attributes_from_non_existing_agent_type() {
4646
"#
4747
);
4848

49-
create_agent_control_config(
50-
opamp_server.endpoint(),
51-
opamp_server.jwks_endpoint(),
52-
agents.to_string(),
53-
local_dir.path().to_path_buf(),
54-
);
49+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
50+
.with_agents(agents.to_string())
51+
.write(local_dir.path().to_path_buf());
5552

5653
let base_paths = BasePaths {
5754
local_dir: local_dir.path().to_path_buf(),
@@ -139,12 +136,9 @@ agents:
139136
"#
140137
);
141138

142-
create_agent_control_config(
143-
opamp_server.endpoint(),
144-
opamp_server.jwks_endpoint(),
145-
agents.to_string(),
146-
local_dir.path().to_path_buf(),
147-
);
139+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
140+
.with_agents(agents.to_string())
141+
.write(local_dir.path().to_path_buf());
148142

149143
// And the custom-agent has empty config values
150144
create_local_config(

agent-control/tests/on_host/scenarios/empty_config.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
runtime::tokio_runtime,
77
},
88
on_host::tools::{
9-
config::{create_agent_control_config, create_local_config},
9+
config::{AgentControlConfigBuilder, create_local_config},
1010
custom_agent_type::CustomAgentType,
1111
instance_id::get_instance_id,
1212
},
@@ -38,12 +38,9 @@ fn onhost_opamp_sub_agent_set_empty_config_defaults_to_local() {
3838
"#
3939
);
4040

41-
create_agent_control_config(
42-
opamp_server.endpoint(),
43-
opamp_server.jwks_endpoint(),
44-
agents.to_string(),
45-
local_dir.path().to_path_buf(),
46-
);
41+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
42+
.with_agents(agents.to_string())
43+
.write(local_dir.path().to_path_buf());
4744

4845
// And the custom-agent has local config values
4946
let agent_id = "nr-sleep-agent";
@@ -116,12 +113,9 @@ fn onhost_opamp_sub_agent_with_no_local_config() {
116113
);
117114

118115
let agent_id = "nr-sleep-agent";
119-
create_agent_control_config(
120-
opamp_server.endpoint(),
121-
opamp_server.jwks_endpoint(),
122-
agents.to_string(),
123-
local_dir.path().to_path_buf(),
124-
);
116+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
117+
.with_agents(agents.to_string())
118+
.write(local_dir.path().to_path_buf());
125119

126120
// There is no local configuration for the sub-agent
127121

agent-control/tests/on_host/scenarios/file_logging.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
agent_control::start_agent_control_with_custom_config, retry::retry, runtime::tokio_runtime,
66
},
77
on_host::tools::{
8-
config::{create_agent_control_config, create_file, create_local_config},
8+
config::{AgentControlConfigBuilder, create_file, create_local_config},
99
custom_agent_type::DYNAMIC_AGENT_TYPE_FILENAME,
1010
instance_id::get_instance_id,
1111
},
@@ -145,12 +145,9 @@ fn run_file_logging_scenario(
145145
agent_type: "test/file_logging_agent:0.0.0"
146146
"#
147147
);
148-
create_agent_control_config(
149-
opamp_server.endpoint(),
150-
opamp_server.jwks_endpoint(),
151-
agents,
152-
local_dir.to_path_buf(),
153-
);
148+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
149+
.with_agents(agents)
150+
.write(local_dir.to_path_buf());
154151

155152
// Provide the initial local config for the sub-agent
156153
create_local_config(

agent-control/tests/on_host/scenarios/filesystem_ops.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
agent_control::start_agent_control_with_custom_config, retry::retry, runtime::tokio_runtime,
77
},
88
on_host::tools::{
9-
config::{create_agent_control_config, create_file, create_local_config},
9+
config::{AgentControlConfigBuilder, create_file, create_local_config},
1010
custom_agent_type::DYNAMIC_AGENT_TYPE_FILENAME,
1111
},
1212
};
@@ -58,12 +58,9 @@ deployment:
5858
"#
5959
);
6060

61-
create_agent_control_config(
62-
opamp_server.endpoint(),
63-
opamp_server.jwks_endpoint(),
64-
agents.to_string(),
65-
local_dir.to_path_buf(),
66-
);
61+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
62+
.with_agents(agents.to_string())
63+
.write(local_dir.to_path_buf());
6764
create_local_config(
6865
agent_id.to_string(),
6966
NO_CONFIG.to_string(),
@@ -172,17 +169,14 @@ deployment:
172169
);
173170

174171
// Create AC config
175-
create_agent_control_config(
176-
opamp_server.endpoint(),
177-
opamp_server.jwks_endpoint(),
178-
format!(
172+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
173+
.with_agents(format!(
179174
r#"
180175
{agent_id}:
181176
agent_type: "test/test:0.0.0"
182177
"#
183-
),
184-
local_dir.to_path_buf(),
185-
);
178+
))
179+
.write(local_dir.to_path_buf());
186180
// Values. Contains 3 variables: a YAML, a string, and a map[string]yaml (to create files in a directory)
187181
create_local_config(
188182
agent_id.to_string(),
@@ -337,12 +331,9 @@ deployment:
337331
"#
338332
);
339333

340-
create_agent_control_config(
341-
opamp_server.endpoint(),
342-
opamp_server.jwks_endpoint(),
343-
agents.to_string(),
344-
local_dir.to_path_buf(),
345-
);
334+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
335+
.with_agents(agents.to_string())
336+
.write(local_dir.to_path_buf());
346337

347338
create_local_config(
348339
agent_id.to_string(),

agent-control/tests/on_host/scenarios/health_check.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use crate::common::agent_control::start_agent_control_with_custom_config;
22
use crate::common::retry::retry;
33
use crate::common::runtime::tokio_runtime;
44
use crate::on_host::consts::NO_CONFIG;
5-
use crate::on_host::tools::config::{
6-
create_agent_control_config, create_file, create_local_config,
7-
};
5+
use crate::on_host::tools::config::{AgentControlConfigBuilder, create_file, create_local_config};
86
use crate::on_host::tools::custom_agent_type::CustomAgentType;
97
use crate::on_host::tools::instance_id::get_instance_id;
108
use fake_opamp_server::FakeServer;
@@ -54,12 +52,9 @@ file:
5452
NO_CONFIG.to_string(),
5553
local_dir.path().into(),
5654
);
57-
create_agent_control_config(
58-
opamp_server.endpoint(),
59-
opamp_server.jwks_endpoint(),
60-
agents.to_string(),
61-
local_dir.path().to_path_buf(),
62-
);
55+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
56+
.with_agents(agents.to_string())
57+
.write(local_dir.path().to_path_buf());
6358

6459
let base_paths = BasePaths {
6560
local_dir: local_dir.path().to_path_buf(),
@@ -162,12 +157,9 @@ http:
162157
"#
163158
);
164159

165-
create_agent_control_config(
166-
opamp_server.endpoint(),
167-
opamp_server.jwks_endpoint(),
168-
agents.to_string(),
169-
local_dir.path().to_path_buf(),
170-
);
160+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
161+
.with_agents(agents.to_string())
162+
.write(local_dir.path().to_path_buf());
171163
create_local_config(
172164
sub_agent_id.to_string(),
173165
NO_CONFIG.to_string(),

agent-control/tests/on_host/scenarios/http_status_server.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use crate::common::http_port::{available_port, status_server_url};
33
use crate::common::retry::retry;
44
use crate::common::runtime::tokio_runtime;
55
use crate::on_host::consts::NO_CONFIG;
6-
use crate::on_host::tools::config::{
7-
create_agent_control_config_with_status_server, create_local_config,
8-
};
6+
use crate::on_host::tools::config::{AgentControlConfigBuilder, create_local_config};
97
use crate::on_host::tools::custom_agent_type::CustomAgentType;
108
use fake_opamp_server::FakeServer;
119
use newrelic_agent_control::agent_control::defaults::{
@@ -43,14 +41,10 @@ fn test_http_status_endpoint_response() {
4341
);
4442

4543
let status_server_port = available_port();
46-
create_agent_control_config_with_status_server(
47-
HOST_ID,
48-
opamp_server.endpoint(),
49-
opamp_server.jwks_endpoint(),
50-
agents,
51-
local_dir.path().to_path_buf(),
52-
status_server_port,
53-
);
44+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
45+
.with_agents(agents)
46+
.with_status_server(status_server_port)
47+
.write(local_dir.path().to_path_buf());
5448

5549
create_local_config(
5650
AGENT_ID.to_string(),

agent-control/tests/on_host/scenarios/install_agent_type.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::common::attributes::{
44
};
55
use crate::common::retry::{retry, retry_never};
66
use crate::common::runtime::tokio_runtime;
7-
use crate::on_host::tools::config::create_local_config;
7+
use crate::on_host::tools::config::AgentControlConfigBuilder;
88
use crate::on_host::tools::instance_id::get_instance_id;
99
use crate::on_host::tools::oci_package_manager::TestDataHelper;
1010
use fake_opamp_server::FakeServer;
1111
use newrelic_agent_control::agent_control::agent_id::AgentID;
1212
use newrelic_agent_control::agent_control::defaults::{
13-
AGENT_CONTROL_ID, DYNAMIC_AGENT_TYPES_DIR, OPAMP_AGENT_VERSION_ATTRIBUTE_KEY,
13+
DYNAMIC_AGENT_TYPES_DIR, OPAMP_AGENT_VERSION_ATTRIBUTE_KEY,
1414
};
1515
use newrelic_agent_control::agent_control::run::BasePaths;
1616
use newrelic_agent_control::agent_control::run::on_host::{
@@ -77,28 +77,10 @@ fn start_agent_control_for_test(
7777
signer: &OCISigner,
7878
base_paths: &BasePaths,
7979
) -> StartedAgentControl {
80-
let config = format!(
81-
r#"
82-
host_id: integration-test
83-
fleet_control:
84-
endpoint: {}
85-
poll_interval: 5s
86-
signature_validation:
87-
public_key_server_url: {}
88-
oci:
89-
registry: "{OCI_TEST_REGISTRY_URL}"
90-
agent_types:
91-
default_remote:
92-
repository: test
93-
signature_verification_enabled: true
94-
public_key_url: {}
95-
agents: {{}}
96-
"#,
97-
opamp_server.endpoint(),
98-
opamp_server.jwks_endpoint(),
99-
signer.jwks_url(),
100-
);
101-
create_local_config(AGENT_CONTROL_ID, config, base_paths.local_dir.clone());
80+
AgentControlConfigBuilder::basic(opamp_server.endpoint(), opamp_server.jwks_endpoint())
81+
.with_oci_registry(OCI_TEST_REGISTRY_URL)
82+
.with_agent_types(true, "test", signer.jwks_url().to_string())
83+
.write(base_paths.local_dir.clone());
10284

10385
start_agent_control_with_custom_config(base_paths.clone(), AGENT_CONTROL_MODE_ON_HOST)
10486
}

0 commit comments

Comments
 (0)