Skip to content

Commit 645c93b

Browse files
parser/bls: Add tests for bls parser
Signed-off-by: Johan-Liebert1 <pragyanpoudyal41999@gmail.com>
1 parent dd4b41e commit 645c93b

6 files changed

Lines changed: 31 additions & 22 deletions

File tree

crates/lib/src/bls_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ where
6666
}
6767
}
6868

69+
#[allow(dead_code)]
6970
pub(crate) fn parse_bls_config(input: &str) -> Result<BLSConfig> {
7071
let mut map = HashMap::new();
7172

crates/lib/src/deploy.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ use ostree_ext::sysroot::SysrootLock;
2525
use ostree_ext::tokio_util::spawn_blocking_cancellable_flatten;
2626
use rustix::fs::{fsync, renameat_with, AtFlags, RenameFlags};
2727

28-
use crate::bls_config::{parse_bls_config, BLSConfig};
2928
use crate::composefs_consts::{
3029
BOOT_LOADER_ENTRIES, ROLLBACK_BOOT_LOADER_ENTRIES, USER_CFG, USER_CFG_ROLLBACK,
3130
};
3231
use crate::install::{get_efi_uuid_source, BootType};
32+
use crate::parsers::bls_config::{parse_bls_config, BLSConfig};
3333
use crate::parsers::grub_menuconfig::{parse_grub_menuentry_file, MenuEntry};
3434
use crate::progress_jsonl::{Event, ProgressWriter, SubTaskBytes, SubTaskStep};
3535
use crate::spec::ImageReference;
@@ -849,7 +849,7 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
849849

850850
// Update the indicies so that they're swapped
851851
for (idx, cfg) in all_configs.iter_mut().enumerate() {
852-
cfg.version = idx as u32;
852+
cfg.sort_key = Some(idx.to_string());
853853
}
854854

855855
// TODO(Johan-Liebert): Currently assuming there are only two deployments
@@ -867,7 +867,8 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
867867

868868
// Write the BLS configs in there
869869
for cfg in all_configs {
870-
let file_name = format!("bootc-composefs-{}.conf", cfg.version);
870+
// SAFETY: We set sort_key above
871+
let file_name = format!("bootc-composefs-{}.conf", cfg.sort_key.as_ref().unwrap());
871872

872873
rollback_entries_dir
873874
.atomic_write(&file_name, cfg.to_string())

crates/lib/src/install.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ use ostree_ext::composefs_oci::{
5757
image::create_filesystem as create_composefs_filesystem, pull as composefs_oci_pull,
5858
};
5959
use ostree_ext::container::deploy::ORIGIN_CONTAINER;
60-
use ostree_ext::oci_spec;
6160
use ostree_ext::ostree;
6261
use ostree_ext::ostree_prepareroot::{ComposefsState, Tristate};
6362
use ostree_ext::prelude::Cast;
@@ -74,7 +73,6 @@ use serde::{Deserialize, Serialize};
7473

7574
#[cfg(feature = "install-to-disk")]
7675
use self::baseline::InstallBlockDeviceOpts;
77-
use crate::bls_config::{parse_bls_config, BLSConfig};
7876
use crate::boundimage::{BoundImage, ResolvedBoundImage};
7977
use crate::composefs_consts::{
8078
BOOT_LOADER_ENTRIES, COMPOSEFS_CMDLINE, COMPOSEFS_STAGED_DEPLOYMENT_FNAME,
@@ -89,6 +87,7 @@ use crate::deploy::{
8987
};
9088
use crate::kernel_cmdline::Cmdline;
9189
use crate::lsm;
90+
use crate::parsers::bls_config::{parse_bls_config, BLSConfig};
9291
use crate::parsers::grub_menuconfig::MenuEntry;
9392
use crate::progress_jsonl::ProgressWriter;
9493
use crate::spec::ImageReference;
@@ -1536,8 +1535,11 @@ fn get_booted_bls() -> Result<BLSConfig> {
15361535

15371536
let bls = parse_bls_config(&std::fs::read_to_string(&entry.path())?)?;
15381537

1539-
// TODO clean this up
1540-
if bls.options.contains(booted.as_ref()) {
1538+
let Some(opts) = &bls.options else {
1539+
anyhow::bail!("options not found in bls config")
1540+
};
1541+
1542+
if opts.contains(booted.as_ref()) {
15411543
return Ok(bls);
15421544
}
15431545
}
@@ -1747,18 +1749,18 @@ pub(crate) fn setup_composefs_bls_boot(
17471749
let boot_digest = compute_boot_digest(usr_lib_modules_vmlinuz, &repo)
17481750
.context("Computing boot digest")?;
17491751

1750-
let mut bls_config = BLSConfig {
1751-
title: Some(id_hex.clone()),
1752-
version: 1,
1753-
linux: format!("/boot/{id_hex}/vmlinuz"),
1754-
initrd: format!("/boot/{id_hex}/initrd"),
1755-
options: cmdline_refs,
1756-
extra: HashMap::new(),
1757-
};
1752+
let mut bls_config = BLSConfig::default();
1753+
bls_config.title = Some(id_hex.clone());
1754+
bls_config.sort_key = Some("1".into());
1755+
bls_config.machine_id = None;
1756+
bls_config.linux = format!("/boot/{id_hex}/vmlinuz");
1757+
bls_config.initrd = vec![format!("/boot/{id_hex}/initrd")];
1758+
bls_config.options = Some(cmdline_refs);
1759+
bls_config.extra = HashMap::new();
17581760

17591761
if let Some(symlink_to) = find_vmlinuz_initrd_duplicates(&boot_digest)? {
17601762
bls_config.linux = format!("/boot/{symlink_to}/vmlinuz");
1761-
bls_config.initrd = format!("/boot/{symlink_to}/initrd");
1763+
bls_config.initrd = vec![format!("/boot/{symlink_to}/initrd")];
17621764
} else {
17631765
write_bls_boot_entries_to_disk(&boot_dir, id, usr_lib_modules_vmlinuz, &repo)?;
17641766
}
@@ -1769,7 +1771,7 @@ pub(crate) fn setup_composefs_bls_boot(
17691771

17701772
let (entries_path, booted_bls) = if is_upgrade {
17711773
let mut booted_bls = get_booted_bls()?;
1772-
booted_bls.version = 0; // entries are sorted by their filename in reverse order
1774+
booted_bls.sort_key = Some("0".into()); // entries are sorted by their filename in reverse order
17731775

17741776
// This will be atomically renamed to 'loader/entries' on shutdown/reboot
17751777
(
@@ -1787,13 +1789,15 @@ pub(crate) fn setup_composefs_bls_boot(
17871789
.with_context(|| format!("Opening {entries_path}"))?;
17881790

17891791
loader_entries_dir.atomic_write(
1790-
format!("bootc-composefs-{}.conf", bls_config.version),
1792+
// SAFETY: We set sort_key above
1793+
format!("bootc-composefs-{}.conf", bls_config.sort_key.as_ref().unwrap()),
17911794
bls_config.to_string().as_bytes(),
17921795
)?;
17931796

17941797
if let Some(booted_bls) = booted_bls {
17951798
loader_entries_dir.atomic_write(
1796-
format!("bootc-composefs-{}.conf", booted_bls.version),
1799+
// SAFETY: We set sort_key above
1800+
format!("bootc-composefs-{}.conf", booted_bls.sort_key.as_ref().unwrap()),
17971801
booted_bls.to_string().as_bytes(),
17981802
)?;
17991803
}

crates/lib/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//! to provide a fully "container native" tool for using
55
//! bootable container images.
66
7-
mod bls_config;
87
pub(crate) mod bootc_kargs;
98
mod boundimage;
109
mod cfsctl;
@@ -44,5 +43,7 @@ pub(crate) mod parsers;
4443
#[cfg(feature = "rhsm")]
4544
mod rhsm;
4645

46+
mod parsers;
47+
4748
// Re-export blockdev crate for internal use
4849
pub(crate) use bootc_blockdev as blockdev;

crates/lib/src/parsers/bls_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use uapi_version::Version;
1212
/// The boot loader should present the available boot menu entries to the user in a sorted list.
1313
/// The list should be sorted by the `sort-key` field, if it exists, otherwise by the `machine-id` field.
1414
/// If multiple entries have the same `sort-key` (or `machine-id`), they should be sorted by the `version` field in descending order.
15-
#[derive(Debug, Eq, PartialEq)]
15+
#[derive(Debug, Eq, PartialEq, Default)]
1616
#[non_exhaustive]
1717
pub(crate) struct BLSConfig {
1818
/// The title of the boot entry, to be displayed in the boot menu.

crates/lib/src/status.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ async fn boot_entry_from_composefs_deployment(
452452
store: None,
453453
ostree: None,
454454
composefs: Some(crate::spec::BootEntryComposefs { verity, boot_type }),
455-
soft_reboot_capable: false
455+
soft_reboot_capable: false,
456456
};
457457

458458
return Ok(e);
@@ -558,6 +558,8 @@ pub(crate) async fn composefs_deployment_status() -> Result<Host> {
558558
.first()
559559
.ok_or(anyhow::anyhow!("First boot entry not found"))?
560560
.options
561+
.as_ref()
562+
.ok_or(anyhow::anyhow!("options key not found in bls config"))?
561563
.contains(composefs_arg.as_ref());
562564
}
563565

0 commit comments

Comments
 (0)