deb: cross-compilation, ephemeral contexts, local builds
All checks were successful
CI / build (push) Successful in 7m18s
All checks were successful
CI / build (push) Successful in 7m18s
Multiple changes: - New contexts (schroot, unshare) - Cross-building quirks, with ephemeral contexts and repositories management - Contexts with parents, global context manager, better lifetime handling - Local building of binary packages - Pull: pulling dsc files by default - Many small bugfixes and changes Co-authored-by: Valentin Haudiquet <valentin.haudiquet@canonical.com> Co-committed-by: Valentin Haudiquet <valentin.haudiquet@canonical.com>
This commit was merged in pull request #1.
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
mod api;
|
||||
mod local;
|
||||
mod manager;
|
||||
mod schroot;
|
||||
mod ssh;
|
||||
mod unshare;
|
||||
|
||||
pub use api::{Context, ContextCommand};
|
||||
pub use api::{Context, ContextCommand, ContextConfig};
|
||||
pub use manager::ContextManager;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn current_context() -> Context {
|
||||
match ContextManager::new() {
|
||||
Ok(mgr) => mgr.current(),
|
||||
Err(_) => Context::Local,
|
||||
}
|
||||
pub fn manager() -> &'static ContextManager {
|
||||
&manager::MANAGER
|
||||
}
|
||||
|
||||
pub fn current() -> Arc<Context> {
|
||||
manager::MANAGER.current()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -25,11 +29,16 @@ mod tests {
|
||||
let src_file = temp_dir.path().join("src.txt");
|
||||
fs::write(&src_file, "local").unwrap();
|
||||
|
||||
let ctx = Context::Local;
|
||||
let ctx = Context::new(ContextConfig::Local);
|
||||
let dest = ctx.ensure_available(&src_file, "/tmp").unwrap();
|
||||
|
||||
// Should return canonical path
|
||||
assert_eq!(dest, src_file.canonicalize().unwrap());
|
||||
// Should return a path that exists and has the same content
|
||||
assert!(dest.exists());
|
||||
let content = fs::read_to_string(&dest).unwrap();
|
||||
assert_eq!(content, "local");
|
||||
|
||||
// The dest should be in the /tmp directory
|
||||
assert!(dest.starts_with("/tmp"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -37,18 +46,17 @@ mod tests {
|
||||
let temp_file = NamedTempFile::new().unwrap();
|
||||
let path = temp_file.path().to_path_buf();
|
||||
|
||||
let mut mgr = ContextManager::with_path(path.clone());
|
||||
let mgr = ContextManager::with_path(path.clone());
|
||||
|
||||
// Add
|
||||
let ssh_ctx = Context::Ssh {
|
||||
let ssh_cfg = ContextConfig::Ssh {
|
||||
host: "10.0.0.1".into(),
|
||||
user: Some("admin".into()),
|
||||
port: Some(2222),
|
||||
};
|
||||
mgr.add_context("myserver", ssh_ctx.clone()).unwrap();
|
||||
mgr.add_context("myserver", ssh_cfg.clone()).unwrap();
|
||||
|
||||
assert!(mgr.get_context("myserver").is_some());
|
||||
assert_eq!(mgr.get_context("myserver").unwrap(), &ssh_ctx);
|
||||
assert!(mgr.list_contexts().contains(&"myserver".to_string()));
|
||||
|
||||
// List
|
||||
let list = mgr.list_contexts();
|
||||
@@ -56,13 +64,11 @@ mod tests {
|
||||
|
||||
// Set Current
|
||||
mgr.set_current("myserver").unwrap();
|
||||
assert_eq!(mgr.current(), ssh_ctx);
|
||||
assert_eq!(mgr.current_name(), Some("myserver".to_string()));
|
||||
assert_eq!(mgr.current_name(), "myserver".to_string());
|
||||
|
||||
// Remove
|
||||
mgr.remove_context("myserver").unwrap();
|
||||
assert!(mgr.get_context("myserver").is_none());
|
||||
assert_eq!(mgr.current(), Context::Local);
|
||||
assert!(!mgr.list_contexts().contains(&"myserver".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -71,18 +77,72 @@ mod tests {
|
||||
let config_path = temp_dir.path().join("contexts.json");
|
||||
|
||||
{
|
||||
let mut mgr = ContextManager::with_path(config_path.clone());
|
||||
mgr.add_context("persistent", Context::Local).unwrap();
|
||||
let mgr = ContextManager::with_path(config_path.clone());
|
||||
mgr.add_context("persistent", ContextConfig::Local).unwrap();
|
||||
mgr.set_current("persistent").unwrap();
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(&config_path).unwrap();
|
||||
let loaded_config: super::manager::Config = serde_json::from_str(&content).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
loaded_config.current_context,
|
||||
Some("persistent".to_string())
|
||||
);
|
||||
assert_eq!(loaded_config.context, "persistent".to_string());
|
||||
assert!(loaded_config.contexts.contains_key("persistent"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_context_fallback_on_removal() {
|
||||
let temp_file = NamedTempFile::new().unwrap();
|
||||
let path = temp_file.path().to_path_buf();
|
||||
let mgr = ContextManager::with_path(path);
|
||||
|
||||
// 1. Add and set a context
|
||||
mgr.add_context("temp", ContextConfig::Local).unwrap();
|
||||
mgr.set_current("temp").unwrap();
|
||||
assert_eq!(mgr.current_name(), "temp");
|
||||
|
||||
// 2. Remove it
|
||||
mgr.remove_context("temp").unwrap();
|
||||
|
||||
// 3. Should have fallen back to local
|
||||
assert_eq!(mgr.current_name(), "local");
|
||||
assert!(mgr.list_contexts().contains(&"local".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_context_file_ops() {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let ctx = Context::new(ContextConfig::Local);
|
||||
|
||||
let file_path = temp_dir.path().join("test.txt");
|
||||
let content = "hello world";
|
||||
|
||||
// 1. Write file
|
||||
ctx.write_file(&file_path, content).unwrap();
|
||||
|
||||
// 2. Read file
|
||||
let read_content = ctx.read_file(&file_path).unwrap();
|
||||
assert_eq!(read_content, content);
|
||||
|
||||
// 3. Copy path
|
||||
let dest_path = temp_dir.path().join("test_copy.txt");
|
||||
ctx.copy_path(&file_path, &dest_path).unwrap();
|
||||
let copied_content = ctx.read_file(&dest_path).unwrap();
|
||||
assert_eq!(copied_content, content);
|
||||
|
||||
// 4. Recursive copy
|
||||
let subdir = temp_dir.path().join("subdir");
|
||||
std::fs::create_dir_all(&subdir).unwrap();
|
||||
let subfile = subdir.join("subfile.txt");
|
||||
ctx.write_file(&subfile, "subcontent").unwrap();
|
||||
|
||||
let subdir_copy = temp_dir.path().join("subdir_copy");
|
||||
ctx.copy_path(&subdir, &subdir_copy).unwrap();
|
||||
|
||||
assert!(subdir_copy.exists());
|
||||
assert!(subdir_copy.join("subfile.txt").exists());
|
||||
assert_eq!(
|
||||
ctx.read_file(&subdir_copy.join("subfile.txt")).unwrap(),
|
||||
"subcontent"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user