Skip to content

Commit 371e948

Browse files
Add onhost remote config integration test (#708)
1 parent b89337c commit 371e948

File tree

13 files changed

+255
-12
lines changed

13 files changed

+255
-12
lines changed

super-agent/src/super_agent/defaults.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ generate_const_getter!(STDOUT_LOG_PREFIX, "stdout.log");
128128
generate_const_getter!(STDERR_LOG_PREFIX, "stderr.log");
129129

130130
#[cfg(debug_assertions)]
131-
pub(super) fn set_local_dir(path: &Path) {
131+
pub fn set_local_dir(path: &Path) {
132132
// The Err variant in `set` just contains the value we attempted to set,
133133
// so we can just ignore the Result
134134
_ = SUPER_AGENT_LOCAL_DATA_DIR_STATIC
@@ -139,7 +139,7 @@ pub(super) fn set_local_dir(path: &Path) {
139139
}
140140

141141
#[cfg(debug_assertions)]
142-
pub(super) fn set_remote_dir(path: &Path) {
142+
pub fn set_remote_dir(path: &Path) {
143143
_ = SUPER_AGENT_DATA_DIR_STATIC
144144
.set(path.to_string_lossy().to_string())
145145
.inspect_err(|_| {

super-agent/src/super_agent/run.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ use std::sync::Arc;
2121
use tokio::runtime::Runtime;
2222
use tracing::{debug, error, info, trace};
2323

24+
// k8s and on_host need to be public to allow integration tests to access the fn run_super_agent.
2425
#[cfg(feature = "k8s")]
25-
mod k8s;
26+
pub mod k8s;
2627
#[cfg(feature = "onhost")]
27-
mod on_host;
28+
pub mod on_host;
2829

2930
/// Structures for running the super-agent provided by CLI inputs
3031
pub struct SuperAgentRunConfig {

super-agent/tests/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ pub(super) mod health;
33
pub(super) mod opamp;
44
/// Includes helpers to handle the _async_ code execution in non-tokio-tests.
55
pub(super) mod runtime;
6+
7+
pub(super) mod retry;

super-agent/tests/k8s/no_opamp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::common::runtime::block_on;
1+
use crate::common::{retry::retry, runtime::block_on};
22

33
use super::tools::{
4-
k8s_api::check_deployments_exist, k8s_env::K8sEnv, retry,
4+
k8s_api::check_deployments_exist, k8s_env::K8sEnv,
55
super_agent::start_super_agent_with_testdata_config,
66
};
77
use std::time::Duration;

super-agent/tests/k8s/opamp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::common::{
22
health::check_latest_health_status_was_healthy,
33
opamp::{ConfigResponse, FakeServer},
4+
retry::retry,
45
runtime::block_on,
56
};
67

78
use super::tools::{
89
instance_id,
910
k8s_api::{check_deployments_exist, check_helmrelease_spec_values},
1011
k8s_env::K8sEnv,
11-
retry,
1212
super_agent::{
1313
start_super_agent_with_testdata_config, wait_until_super_agent_with_opamp_is_started,
1414
},

super-agent/tests/k8s/tools/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ pub mod instance_id;
55
pub mod k8s_api;
66
/// Provides a k8s testing environment.
77
pub mod k8s_env;
8-
mod retry;
98
/// Contains helpers to execute the super-agent binary (compiled with the k8s feature)
109
/// and specific initial configuration. Any helper receiving a `folder_name` assumes that the folder exists
1110
/// in the path `tests/k8s/data/`.
1211
pub mod super_agent;
13-
14-
pub use retry::retry;

super-agent/tests/k8s/tools/super_agent.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use std::time::Duration;
1212
use std::{collections::BTreeMap, path::PathBuf};
1313
use std::{fs::File, io::Write};
1414

15-
use crate::common::runtime::block_on;
15+
use crate::common::{retry::retry, runtime::block_on};
1616

17-
use super::{k8s_api::check_config_map_exist, retry};
17+
use super::k8s_api::check_config_map_exist;
1818

1919
pub const TEST_CLUSTER_NAME: &str = "minikube";
2020

super-agent/tests/on_host/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ mod consts;
55
mod id;
66
mod logging;
77
mod opamp_auth;
8+
mod remote_config;
89
mod restarting_processes;
910
mod supervisor;
11+
mod tools;
1012
mod values_repository;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use crate::common::opamp::{ConfigResponse, FakeServer};
2+
use crate::common::retry::retry;
3+
use crate::on_host::tools::instance_id::get_instance_id;
4+
use crate::on_host::tools::super_agent::start_super_agent_with_custom_config;
5+
use newrelic_super_agent::super_agent::config::{AgentID, SuperAgentDynamicConfig};
6+
use newrelic_super_agent::super_agent::defaults::{set_local_dir, set_remote_dir};
7+
use std::error::Error;
8+
use std::fs::File;
9+
use std::io::Write;
10+
use std::thread;
11+
use std::time::Duration;
12+
use tempfile::tempdir;
13+
use url::Url;
14+
15+
#[cfg(unix)]
16+
#[test]
17+
fn onhost_opamp_superagent_configuration_change() {
18+
// Given a super-agent without agents and opamp configured.
19+
let mut server = FakeServer::start_new();
20+
let server_endpoint = Url::try_from(server.endpoint().as_str()).unwrap();
21+
22+
let local_dir = tempdir().expect("failed to create local temp dir");
23+
let remote_dir = tempdir().expect("failed to create remote temp dir");
24+
25+
set_local_dir(local_dir.path());
26+
set_remote_dir(remote_dir.path());
27+
28+
let config_file_path = local_dir.path().join("config.yaml");
29+
let mut local_file =
30+
File::create(config_file_path.clone()).expect("failed to create local config file");
31+
let local_config = r#"
32+
host_id: integration-test
33+
fleet_id: integration
34+
opamp:
35+
endpoint: http://127.0.0.1/v1/opamp
36+
agents: {}
37+
"#;
38+
write!(local_file, "{}", local_config).unwrap();
39+
40+
// We won't join and wait for the thread to finish because we want the super_agent to exit
41+
// if our assertions were not ok.
42+
let _super_agent_join = thread::spawn(move || {
43+
start_super_agent_with_custom_config(config_file_path.as_path(), server_endpoint)
44+
});
45+
46+
let super_agent_instance_id = get_instance_id(&AgentID::new_super_agent_id());
47+
48+
// When a new config with two agents is received from OpAMP
49+
server.set_config_response(
50+
super_agent_instance_id,
51+
ConfigResponse::from(
52+
r#"
53+
agents:
54+
nr-infra-agent:
55+
agent_type: "newrelic/com.newrelic.infrastructure_agent:0.1.2"
56+
otel-collector:
57+
agent_type: "newrelic/io.opentelemetry.collector:0.0.1"
58+
"#,
59+
),
60+
);
61+
62+
// Then the config should be updated in the remote filesystem.
63+
let expected_config = r#"agents:
64+
nr-infra-agent:
65+
agent_type: newrelic/com.newrelic.infrastructure_agent:0.1.2
66+
otel-collector:
67+
agent_type: newrelic/io.opentelemetry.collector:0.0.1
68+
"#;
69+
let expected_config_parsed =
70+
serde_yaml::from_str::<SuperAgentDynamicConfig>(expected_config).unwrap();
71+
72+
retry(20, Duration::from_secs(5), || {
73+
|| -> Result<(), Box<dyn Error>> {
74+
let remote_file = remote_dir.path().join("config.yaml");
75+
let content =
76+
std::fs::read_to_string(remote_file.as_path()).unwrap_or("agents:".to_string());
77+
let content_parsed =
78+
serde_yaml::from_str::<SuperAgentDynamicConfig>(content.as_str()).unwrap();
79+
if content_parsed != expected_config_parsed {
80+
return Err(format!(
81+
"Super agent config not as expected, Expected: {:?}, Found: {:?}",
82+
expected_config, content,
83+
)
84+
.into());
85+
}
86+
Ok(())
87+
}()
88+
});
89+
90+
// TODO: Then OpAMP receives applied (& applying?) AgentToServer (check state on the server).
91+
// TODO: Then the two agent processes are running (we should create custom agent_types for custom binary).
92+
}

0 commit comments

Comments
 (0)