Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions agent-control/tests/common/custom_agent_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use fs::file::LocalFile;
use fs::file::writer::FileWriter;
use newrelic_agent_control::agent_control::defaults::DYNAMIC_AGENT_TYPES_DIR;
use newrelic_agent_control::agent_type::agent_type_id::AgentTypeID;
use newrelic_agent_control::agent_type::definition::AgentTypeDefinition;
use std::path::Path;

/// Fields and builder methods shared by [`crate::k8s::tools::custom_agent_type::K8sCustomAgentType`]
/// and [`crate::on_host::tools::custom_agent_type::OnHostCustomAgentType`].
pub struct CommonCustomAgentType {
pub agent_type_id: AgentTypeID,
pub variables: Option<serde_json::Value>,
}

impl CommonCustomAgentType {
pub fn new(agent_type_id: AgentTypeID) -> Self {
Self {
agent_type_id,
variables: None,
}
}

pub fn with_variables(self, variables: &str) -> Self {
Self {
variables: Some(serde_saphyr::from_str(variables).unwrap()),
..self
}
}

pub fn with_agent_type_id(self, agent_type_id: &str) -> Self {
Self {
agent_type_id: AgentTypeID::try_from(agent_type_id).unwrap(),
..self
}
}

/// Validates `content` (the caller's rendered YAML) against [`AgentTypeDefinition`] and writes
/// it under `local_dir`'s dynamic agent types directory. The file name is derived from the full
/// agent type id (namespace, name and version) so several distinct types can coexist in the
/// dynamic agent types dir (the loader reads every file there); two ids sharing only a name
/// would otherwise clobber each other. Returns the agent type id as a string.
pub fn build(&self, local_dir: &Path, content: &str) -> String {
// The id (`namespace/name:version`) has `/` and `:`, which are not portable in file names.
let file_stem = self.agent_type_id.to_string().replace(['/', ':'], "_");
let agent_type_file_path = local_dir
.join(DYNAMIC_AGENT_TYPES_DIR)
.join(format!("{file_stem}.yaml"));

let parsed_agent_type = AgentTypeDefinition::from_slice(content.as_bytes());
assert!(
parsed_agent_type.is_ok(),
"CustomAgentType did not produce valid AgentTypeDefinition: {}\n{content}",
parsed_agent_type.err().unwrap(),
);

std::fs::create_dir_all(agent_type_file_path.parent().unwrap()).unwrap();
LocalFile
.write(&agent_type_file_path, content.to_string())
.expect("failed to write custom agent type");
self.agent_type_id.to_string()
}
}
1 change: 1 addition & 0 deletions agent-control/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub(super) mod agent_control;
pub(crate) mod attributes;
pub(super) mod base_paths;
pub(crate) mod config;
pub(crate) mod custom_agent_type;
/// Includes a OpAMP mock server to test scenarios involving OpAMP.
pub(super) mod effective_config;
pub(super) mod global_logger;
Expand Down
25 changes: 0 additions & 25 deletions agent-control/tests/k8s/data/bar_cr_agent_type.yml

This file was deleted.

26 changes: 0 additions & 26 deletions agent-control/tests/k8s/data/config_map_type.yml

This file was deleted.

63 changes: 0 additions & 63 deletions agent-control/tests/k8s/data/custom_agent_type.yml

This file was deleted.

This file was deleted.

36 changes: 0 additions & 36 deletions agent-control/tests/k8s/data/custom_agent_type_secrets.yml

This file was deleted.

73 changes: 0 additions & 73 deletions agent-control/tests/k8s/data/custom_agent_type_split_ns.yml

This file was deleted.

25 changes: 0 additions & 25 deletions agent-control/tests/k8s/data/foo_cr_agent_type.yml

This file was deleted.

This file was deleted.

Loading
Loading