mirror of
				https://github.com/kata-containers/kata-containers.git
				synced 2025-10-31 09:26:52 +00:00 
			
		
		
		
	runtime-rs: add unit test and eliminate raw string
Add two unit tests for coverage and eliminate raw strings to constant. Fixes: #5068 Signed-Off-By: Ji-Xinyou <jerryji0414@outlook.com>
This commit is contained in:
		| @@ -18,9 +18,11 @@ lazy_static! { | |||||||
|         "/usr/share/defaults/kata-containers/configuration.toml", |         "/usr/share/defaults/kata-containers/configuration.toml", | ||||||
|     ]; |     ]; | ||||||
| } | } | ||||||
|  |  | ||||||
| pub const DEFAULT_AGENT_NAME: &str = "kata-agent"; | pub const DEFAULT_AGENT_NAME: &str = "kata-agent"; | ||||||
| pub const DEFAULT_AGENT_VSOCK_PORT: u32 = 1024; | pub const DEFAULT_AGENT_VSOCK_PORT: u32 = 1024; | ||||||
| pub const DEFAULT_AGENT_LOG_PORT: u32 = 1025; | pub const DEFAULT_AGENT_LOG_PORT: u32 = 1025; | ||||||
|  | pub const DEFAULT_AGENT_DBG_CONSOLE_PORT: u32 = 1026; | ||||||
| pub const DEFAULT_AGENT_TYPE_NAME: &str = AGENT_NAME_KATA; | pub const DEFAULT_AGENT_TYPE_NAME: &str = AGENT_NAME_KATA; | ||||||
|  |  | ||||||
| pub const DEFAULT_RUNTIME_NAME: &str = RUNTIME_NAME_VIRTCONTAINER; | pub const DEFAULT_RUNTIME_NAME: &str = RUNTIME_NAME_VIRTCONTAINER; | ||||||
|   | |||||||
| @@ -243,9 +243,13 @@ impl BootInfo { | |||||||
|     /// to let the original one takes priority |     /// to let the original one takes priority | ||||||
|     pub fn add_kparams(&mut self, params: Vec<String>) { |     pub fn add_kparams(&mut self, params: Vec<String>) { | ||||||
|         let mut p = params; |         let mut p = params; | ||||||
|  |         if self.kernel_params.is_empty() { | ||||||
|  |             self.kernel_params = p.join(KERNEL_PARAM_DELIMITER); | ||||||
|  |         } else { | ||||||
|             p.push(self.kernel_params.clone()); // [new_params0, new_params1, ..., original_params] |             p.push(self.kernel_params.clone()); // [new_params0, new_params1, ..., original_params] | ||||||
|             self.kernel_params = p.join(KERNEL_PARAM_DELIMITER); |             self.kernel_params = p.join(KERNEL_PARAM_DELIMITER); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /// Validate guest kernel image annotaion |     /// Validate guest kernel image annotaion | ||||||
|     pub fn validate_boot_path(&self, path: &str) -> Result<()> { |     pub fn validate_boot_path(&self, path: &str) -> Result<()> { | ||||||
| @@ -1077,4 +1081,31 @@ mod tests { | |||||||
|         assert!(get_hypervisor_plugin("dragonball").is_some()); |         assert!(get_hypervisor_plugin("dragonball").is_some()); | ||||||
|         assert!(get_hypervisor_plugin("dragonball2").is_none()); |         assert!(get_hypervisor_plugin("dragonball2").is_none()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn test_add_kparams() { | ||||||
|  |         let mut boot_info = BootInfo { | ||||||
|  |             ..Default::default() | ||||||
|  |         }; | ||||||
|  |         let params = vec![ | ||||||
|  |             String::from("foo"), | ||||||
|  |             String::from("bar"), | ||||||
|  |             String::from("baz=faz"), | ||||||
|  |         ]; | ||||||
|  |         boot_info.add_kparams(params); | ||||||
|  |  | ||||||
|  |         assert_eq!(boot_info.kernel_params, String::from("foo bar baz=faz")); | ||||||
|  |  | ||||||
|  |         let new_params = vec![ | ||||||
|  |             String::from("boo=far"), | ||||||
|  |             String::from("a"), | ||||||
|  |             String::from("b=c"), | ||||||
|  |         ]; | ||||||
|  |         boot_info.add_kparams(new_params); | ||||||
|  |  | ||||||
|  |         assert_eq!( | ||||||
|  |             boot_info.kernel_params, | ||||||
|  |             String::from("boo=far a b=c foo bar baz=faz") | ||||||
|  |         ); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -23,6 +23,7 @@ mod drop_in; | |||||||
| pub mod hypervisor; | pub mod hypervisor; | ||||||
|  |  | ||||||
| pub use self::agent::Agent; | pub use self::agent::Agent; | ||||||
|  | use self::default::DEFAULT_AGENT_DBG_CONSOLE_PORT; | ||||||
| pub use self::hypervisor::{ | pub use self::hypervisor::{ | ||||||
|     BootInfo, DragonballConfig, Hypervisor, QemuConfig, HYPERVISOR_NAME_DRAGONBALL, |     BootInfo, DragonballConfig, Hypervisor, QemuConfig, HYPERVISOR_NAME_DRAGONBALL, | ||||||
|     HYPERVISOR_NAME_QEMU, |     HYPERVISOR_NAME_QEMU, | ||||||
| @@ -33,6 +34,24 @@ pub use self::runtime::{Runtime, RuntimeVendor, RUNTIME_NAME_VIRTCONTAINER}; | |||||||
|  |  | ||||||
| pub use self::agent::AGENT_NAME_KATA; | pub use self::agent::AGENT_NAME_KATA; | ||||||
|  |  | ||||||
|  | // TODO: let agent use the constants here for consistency | ||||||
|  | /// Debug console enabled flag for agent | ||||||
|  | pub const DEBUG_CONSOLE_FLAG: &str = "agent.debug_console"; | ||||||
|  | /// Tracing enabled flag for agent | ||||||
|  | pub const TRACE_MODE_OPTION: &str = "agent.trace"; | ||||||
|  | /// Tracing enabled | ||||||
|  | pub const TRACE_MODE_ENABLE: &str = "true"; | ||||||
|  | /// Log level setting key for agent, if debugged mode on, set to debug | ||||||
|  | pub const LOG_LEVEL_OPTION: &str = "agent.log"; | ||||||
|  | /// logging level: debug | ||||||
|  | pub const LOG_LEVEL_DEBUG: &str = "debug"; | ||||||
|  | /// Option of which port will the debug console connect to | ||||||
|  | pub const DEBUG_CONSOLE_VPORT_OPTION: &str = "agent.debug_console_vport"; | ||||||
|  | /// Option of which port the agent's log will connect to | ||||||
|  | pub const LOG_VPORT_OPTION: &str = "agent.log_vport"; | ||||||
|  | /// Option of setting the container's pipe size | ||||||
|  | pub const CONTAINER_PIPE_SIZE_OPTION: &str = "agent.container_pipe_size"; | ||||||
|  |  | ||||||
| /// Trait to manipulate global Kata configuration information. | /// Trait to manipulate global Kata configuration information. | ||||||
| pub trait ConfigPlugin: Send + Sync { | pub trait ConfigPlugin: Send + Sync { | ||||||
|     /// Get the plugin name. |     /// Get the plugin name. | ||||||
| @@ -156,18 +175,21 @@ impl TomlConfig { | |||||||
|         let mut kv = HashMap::new(); |         let mut kv = HashMap::new(); | ||||||
|         if let Some(cfg) = self.agent.get(&self.runtime.agent_name) { |         if let Some(cfg) = self.agent.get(&self.runtime.agent_name) { | ||||||
|             if cfg.debug { |             if cfg.debug { | ||||||
|                 kv.insert("agent.log".to_string(), "debug".to_string()); |                 kv.insert(LOG_LEVEL_OPTION.to_string(), LOG_LEVEL_DEBUG.to_string()); | ||||||
|             } |             } | ||||||
|             if cfg.enable_tracing { |             if cfg.enable_tracing { | ||||||
|                 kv.insert("agent.trace".to_string(), "true".to_string()); |                 kv.insert(TRACE_MODE_OPTION.to_string(), TRACE_MODE_ENABLE.to_string()); | ||||||
|             } |             } | ||||||
|             if cfg.container_pipe_size > 0 { |             if cfg.container_pipe_size > 0 { | ||||||
|                 let container_pipe_size = cfg.container_pipe_size.to_string(); |                 let container_pipe_size = cfg.container_pipe_size.to_string(); | ||||||
|                 kv.insert("agent.container_pipe_size".to_string(), container_pipe_size); |                 kv.insert(CONTAINER_PIPE_SIZE_OPTION.to_string(), container_pipe_size); | ||||||
|             } |             } | ||||||
|             if cfg.debug_console_enabled { |             if cfg.debug_console_enabled { | ||||||
|                 kv.insert("agent.debug_console".to_string(), "".to_string()); |                 kv.insert(DEBUG_CONSOLE_FLAG.to_string(), "".to_string()); | ||||||
|                 kv.insert("agent.debug_console_vport".to_string(), "1026".to_string()); |                 kv.insert( | ||||||
|  |                     DEBUG_CONSOLE_VPORT_OPTION.to_string(), | ||||||
|  |                     DEFAULT_AGENT_DBG_CONSOLE_PORT.to_string(), | ||||||
|  |                 ); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         Ok(kv) |         Ok(kv) | ||||||
| @@ -325,4 +347,28 @@ mod tests { | |||||||
|         let patterns = ["/usr/share".to_string(), "/bin/*".to_string()]; |         let patterns = ["/usr/share".to_string(), "/bin/*".to_string()]; | ||||||
|         validate_path_pattern(&patterns, "/bin/ls").unwrap(); |         validate_path_pattern(&patterns, "/bin/ls").unwrap(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn test_get_agent_kparams() { | ||||||
|  |         let mut config = TomlConfig { | ||||||
|  |             ..Default::default() | ||||||
|  |         }; | ||||||
|  |         let agent_config = Agent { | ||||||
|  |             debug: true, | ||||||
|  |             enable_tracing: true, | ||||||
|  |             container_pipe_size: 20, | ||||||
|  |             debug_console_enabled: true, | ||||||
|  |             ..Default::default() | ||||||
|  |         }; | ||||||
|  |         let agent_name = "test_agent"; | ||||||
|  |         config.runtime.agent_name = agent_name.to_string(); | ||||||
|  |         config.agent.insert(agent_name.to_owned(), agent_config); | ||||||
|  |  | ||||||
|  |         let kv = config.get_agent_kparams().unwrap(); | ||||||
|  |         assert_eq!(kv.get("agent.log").unwrap(), "debug"); | ||||||
|  |         assert_eq!(kv.get("agent.trace").unwrap(), "true"); | ||||||
|  |         assert_eq!(kv.get("agent.container_pipe_size").unwrap(), "20"); | ||||||
|  |         kv.get("agent.debug_console").unwrap(); | ||||||
|  |         assert_eq!(kv.get("agent.debug_console_vport").unwrap(), "1026"); // 1026 is the default port | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ | |||||||
| use anyhow::{anyhow, Result}; | use anyhow::{anyhow, Result}; | ||||||
|  |  | ||||||
| use crate::{VM_ROOTFS_DRIVER_BLK, VM_ROOTFS_DRIVER_PMEM}; | use crate::{VM_ROOTFS_DRIVER_BLK, VM_ROOTFS_DRIVER_PMEM}; | ||||||
|  | use kata_types::config::LOG_VPORT_OPTION; | ||||||
|  |  | ||||||
| // Port where the agent will send the logs. Logs are sent through the vsock in cases | // Port where the agent will send the logs. Logs are sent through the vsock in cases | ||||||
| // where the hypervisor has no console.sock, i.e dragonball | // where the hypervisor has no console.sock, i.e dragonball | ||||||
| @@ -60,7 +61,7 @@ impl KernelParams { | |||||||
|         ]; |         ]; | ||||||
|  |  | ||||||
|         if debug { |         if debug { | ||||||
|             params.push(Param::new("agent.log_vport", VSOCK_LOGS_PORT)); |             params.push(Param::new(LOG_VPORT_OPTION, VSOCK_LOGS_PORT)); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         Self { params } |         Self { params } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user