Skip to content

Commit e355328

Browse files
feat: add package install integration tests (#2075)
* feat: add package install integration tests
1 parent 428cdc1 commit e355328

File tree

9 files changed

+471
-47
lines changed

9 files changed

+471
-47
lines changed

agent-control/src/agent_control/run/on_host.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::sub_agent::on_host::builder::SupervisorBuilderOnHost;
3737
use crate::sub_agent::remote_config_parser::AgentRemoteConfigParser;
3838
use crate::values::ConfigRepo;
3939
use fs::directory_manager::DirectoryManagerFs;
40-
use oci_client::client::ClientConfig;
40+
use oci_client::client::{ClientConfig, ClientProtocol};
4141
use opamp_client::operation::settings::DescriptionValueType;
4242
use resource_detection::cloud::http_client::DEFAULT_CLIENT_TIMEOUT;
4343
use std::collections::HashMap;
@@ -164,11 +164,17 @@ impl AgentControlRunner {
164164
&self.base_paths.remote_dir,
165165
));
166166

167+
// We are setting client http in debug_assertions mode for tests
168+
let oci_client_config = ClientConfig {
169+
#[cfg(debug_assertions)]
170+
protocol: ClientProtocol::Http,
171+
..Default::default()
172+
};
173+
167174
let packages_downloader =
168-
OCIArtifactDownloader::try_new(self.proxy, self.runtime, ClientConfig::default())
169-
.map_err(|err| {
170-
RunError(format!("failed to create OCIRefDownloader client: {err}"))
171-
})?;
175+
OCIArtifactDownloader::try_new(self.proxy, self.runtime, oci_client_config).map_err(
176+
|err| RunError(format!("failed to create OCIRefDownloader client: {err}")),
177+
)?;
172178

173179
let package_manager = OCIPackageManager::new(
174180
packages_downloader,
@@ -227,6 +233,7 @@ impl AgentControlRunner {
227233
.map_err(|err| RunError(err.to_string()))
228234
}
229235
}
236+
230237
pub fn agent_control_opamp_non_identifying_attributes(
231238
identifiers: &Identifiers,
232239
) -> HashMap<String, DescriptionValueType> {

agent-control/tests/common/attributes.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ pub fn check_latest_non_identifying_attributes_match_expected(
3636
.map_err(|e| format!("Non identifying attributes don't match: {e}"))
3737
}
3838

39+
pub fn check_identifying_attributes_contains_expected(
40+
opamp_server: &FakeServer,
41+
instance_id: &InstanceID,
42+
expected_subset: Vec<KeyValue>,
43+
) -> Result<(), String> {
44+
let current_attributes = opamp_server
45+
.get_attributes(instance_id)
46+
.ok_or_else(|| "Identifying attributes not found".to_string())?;
47+
48+
check_opamp_attributes_contains(
49+
expected_subset,
50+
current_attributes.identifying_attributes.clone(),
51+
)
52+
.map_err(|e| format!("Identifying attributes missing required elements: {e}"))
53+
}
54+
3955
fn check_opamp_attributes(
4056
mut expected_vec: Vec<KeyValue>,
4157
mut current_vec: Vec<KeyValue>,
@@ -50,6 +66,25 @@ fn check_opamp_attributes(
5066
Ok(())
5167
}
5268

69+
fn check_opamp_attributes_contains(
70+
subset_vec: Vec<KeyValue>,
71+
superset_vec: Vec<KeyValue>,
72+
) -> Result<(), String> {
73+
for expected in subset_vec {
74+
let found = superset_vec
75+
.iter()
76+
.find(|&current| current.key == expected.key && current.value == expected.value);
77+
78+
if found.is_none() {
79+
return Err(format!(
80+
"Required attribute key '{}' with value '{:?}' not found in actual attributes.",
81+
expected.key, expected.value
82+
));
83+
}
84+
}
85+
Ok(())
86+
}
87+
5388
pub fn convert_to_vec_key_value(data: Vec<(&str, Value)>) -> Vec<KeyValue> {
5489
data.into_iter()
5590
.map(|(k, v)| KeyValue {

agent-control/tests/on_host/oci_package_management.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1+
use crate::on_host::tools::oci_artifact::REGISTRY_URL;
12
use crate::on_host::tools::oci_package_manager::TestDataHelper;
23
use crate::on_host::tools::{
34
oci_artifact::push_agent_package, oci_package_manager::new_testing_oci_package_manager,
45
};
56
use newrelic_agent_control::agent_control::agent_id::AgentID;
67
use newrelic_agent_control::package::manager::{PackageData, PackageManager};
8+
use newrelic_agent_control::package::oci::artifact_definitions::PackageMediaType;
79
use newrelic_agent_control::package::oci::package_manager::get_package_path;
810
use tempfile::tempdir;
911

10-
// Registry created in the make target executing oci-registry.sh
11-
const REGISTRY_URL: &str = "localhost:5001";
12-
1312
#[test]
1413
#[ignore = "needs oci registry (use *with_oci_registry suffix), needs elevated privileges on Windows"]
1514
fn test_install_and_uninstall_with_oci_registry() {
15+
const FILENAME: &str = "file1.txt";
1616
let dir = tempdir().unwrap();
1717
let tmp_dir_to_compress = tempdir().unwrap();
1818
let file_to_push = dir.path().join("layer_digest.tar.gz");
1919

20-
TestDataHelper::compress_tar_gz(tmp_dir_to_compress.path(), file_to_push.as_path());
20+
TestDataHelper::compress_tar_gz(
21+
tmp_dir_to_compress.path(),
22+
file_to_push.as_path(),
23+
"important content",
24+
FILENAME,
25+
);
2126

22-
let (_artifact_digest, reference) = push_agent_package(&file_to_push, REGISTRY_URL);
27+
let (_artifact_digest, reference) = push_agent_package(
28+
&file_to_push,
29+
REGISTRY_URL,
30+
PackageMediaType::AgentPackageLayerTarGz,
31+
);
2332

2433
let temp_dir = tempdir().unwrap();
2534
let base_path = temp_dir.path().to_path_buf();
@@ -43,7 +52,10 @@ fn test_install_and_uninstall_with_oci_registry() {
4352
);
4453

4554
let installed_package = installed_package_result.unwrap();
46-
TestDataHelper::test_data_uncompressed(installed_package.installation_path.as_path());
55+
TestDataHelper::test_tar_gz_uncompressed(
56+
installed_package.installation_path.as_path(),
57+
FILENAME,
58+
);
4759
// Verify location
4860
// The path should be base_path/agent_id/oci_registry__port__repo_tag
4961
let expected_path = get_package_path(&base_path, &agent_id, &pkg_id, &reference).unwrap();
@@ -61,17 +73,25 @@ fn test_install_and_uninstall_with_oci_registry() {
6173
#[test]
6274
#[ignore = "needs oci registry, needs elevated privileges on Windows"]
6375
fn test_install_skips_download_if_exists_with_oci_registry() {
76+
const FILENAME: &str = "payload.txt";
77+
6478
let dir = tempdir().unwrap();
6579
let content_dir = tempdir().unwrap();
6680

67-
let payload_file_source = content_dir.path().join("payload.txt");
68-
std::fs::write(&payload_file_source, "ORIGINAL_CONTENT").unwrap();
69-
7081
let file_to_push = dir.path().join("layer_digest.tar.gz");
7182

72-
TestDataHelper::compress_tar_gz(content_dir.path(), file_to_push.as_path());
83+
TestDataHelper::compress_tar_gz(
84+
content_dir.path(),
85+
file_to_push.as_path(),
86+
"ORIGINAL_CONTENT",
87+
FILENAME,
88+
);
7389

74-
let (_artifact_digest, reference) = push_agent_package(&file_to_push, REGISTRY_URL);
90+
let (_artifact_digest, reference) = push_agent_package(
91+
&file_to_push,
92+
REGISTRY_URL,
93+
PackageMediaType::AgentPackageLayerTarGz,
94+
);
7595

7696
let temp_dir = tempdir().unwrap();
7797
let base_path = temp_dir.path().to_path_buf();
@@ -89,7 +109,7 @@ fn test_install_skips_download_if_exists_with_oci_registry() {
89109
.install(&agent_id, package_data.clone())
90110
.expect("First install failed");
91111

92-
let installed_file_path = installed_1.installation_path.join("payload.txt");
112+
let installed_file_path = installed_1.installation_path.join(FILENAME);
93113
assert!(
94114
installed_file_path.exists(),
95115
"Payload file should exist after install"

agent-control/tests/on_host/oci_registry.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1-
use crate::on_host::tools::oci_artifact::push_agent_package;
1+
use crate::on_host::tools::oci_artifact::{REGISTRY_URL, push_agent_package};
22
use crate::on_host::tools::oci_package_manager::TestDataHelper;
33
use httpmock::{MockServer, When};
44
use newrelic_agent_control::http::config::ProxyConfig;
5+
use newrelic_agent_control::package::oci::artifact_definitions::PackageMediaType;
56
use newrelic_agent_control::package::oci::downloader::{OCIAgentDownloader, OCIArtifactDownloader};
67
use oci_client::client::{ClientConfig, ClientProtocol};
78
use std::sync::{Arc, Mutex};
89
use std::time::Duration;
910
use tempfile::tempdir;
1011

11-
// Registry created in the make target executing oci-registry.sh
12-
const REGISTRY_URL: &str = "localhost:5001";
13-
1412
#[test]
1513
#[ignore = "needs oci registry (use *with_oci_registry suffix)"]
1614
fn test_download_artifact_from_local_registry_with_oci_registry() {
1715
let dir = tempdir().unwrap();
1816
let tmp_dir_to_compress = tempdir().unwrap();
1917
let file_to_push = dir.path().join("layer_digest.tar.gz");
20-
TestDataHelper::compress_tar_gz(tmp_dir_to_compress.path(), file_to_push.as_path());
18+
TestDataHelper::compress_tar_gz(
19+
tmp_dir_to_compress.path(),
20+
file_to_push.as_path(),
21+
"important content",
22+
"file1.txt",
23+
);
2124

22-
let (artifact_digest, reference) = push_agent_package(&file_to_push, REGISTRY_URL);
25+
let (artifact_digest, reference) = push_agent_package(
26+
&file_to_push,
27+
REGISTRY_URL,
28+
PackageMediaType::AgentPackageLayerTarGz,
29+
);
2330

2431
let temp_dir = tempdir().unwrap();
2532
let local_agent_data_dir = temp_dir.path();
@@ -56,9 +63,18 @@ fn test_download_artifact_from_local_registry_using_proxy_with_retries_with_oci_
5663
let dir = tempdir().unwrap();
5764
let tmp_dir_to_compress = tempdir().unwrap();
5865
let file_to_push = dir.path().join("layer_digest.tar.gz");
59-
TestDataHelper::compress_tar_gz(tmp_dir_to_compress.path(), file_to_push.as_path());
66+
TestDataHelper::compress_tar_gz(
67+
tmp_dir_to_compress.path(),
68+
file_to_push.as_path(),
69+
"important content",
70+
"file1.txt",
71+
);
6072

61-
let (artifact_digest, reference) = push_agent_package(&file_to_push, REGISTRY_URL);
73+
let (artifact_digest, reference) = push_agent_package(
74+
&file_to_push,
75+
REGISTRY_URL,
76+
PackageMediaType::AgentPackageLayerTarGz,
77+
);
6278

6379
// Proxy server will request the target server, allowing requests to that host only
6480
let proxy_server = MockServer::start();

agent-control/tests/on_host/scenarios.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod attributes;
22
mod empty_config;
33
mod filesystem_ops;
44
mod health_check;
5+
mod install_remote_package;
56
mod invalid_remote_config;
67
mod multiple_executables;
78
mod multiple_remote_config;

0 commit comments

Comments
 (0)