Skip to main content

bootc_lib/
status.rs

1use std::borrow::Cow;
2use std::collections::VecDeque;
3use std::io::IsTerminal;
4use std::io::Read;
5use std::io::Write;
6
7use anyhow::{Context, Result};
8use canon_json::CanonJsonSerialize;
9use fn_error_context::context;
10use ostree::glib;
11use ostree_container::OstreeImageReference;
12use ostree_ext::container as ostree_container;
13use ostree_ext::keyfileext::KeyFileExt;
14use ostree_ext::oci_spec;
15use ostree_ext::oci_spec::image::Digest;
16use ostree_ext::oci_spec::image::ImageConfiguration;
17use ostree_ext::sysroot::SysrootLock;
18use unicode_width::UnicodeWidthStr;
19
20use ostree_ext::ostree;
21
22use crate::cli::OutputFormat;
23use crate::spec::BootEntryComposefs;
24use crate::spec::ImageStatus;
25use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType};
26use crate::spec::{ImageReference, ImageSignature};
27use crate::store::BootedStorage;
28use crate::store::BootedStorageKind;
29use crate::store::CachedImageStatus;
30
31impl From<ostree_container::SignatureSource> for ImageSignature {
32    fn from(sig: ostree_container::SignatureSource) -> Self {
33        use ostree_container::SignatureSource;
34        match sig {
35            SignatureSource::OstreeRemote(r) => Self::OstreeRemote(r),
36            SignatureSource::ContainerPolicy => Self::ContainerPolicy,
37            SignatureSource::ContainerPolicyAllowInsecure => Self::Insecure,
38        }
39    }
40}
41
42impl From<ImageSignature> for ostree_container::SignatureSource {
43    fn from(sig: ImageSignature) -> Self {
44        use ostree_container::SignatureSource;
45        match sig {
46            ImageSignature::OstreeRemote(r) => SignatureSource::OstreeRemote(r),
47            ImageSignature::ContainerPolicy => Self::ContainerPolicy,
48            ImageSignature::Insecure => Self::ContainerPolicyAllowInsecure,
49        }
50    }
51}
52
53/// Fixme lower serializability into ostree-ext
54fn transport_to_string(transport: ostree_container::Transport) -> String {
55    match transport {
56        // Canonicalize to registry for our own use
57        ostree_container::Transport::Registry => "registry".to_string(),
58        o => {
59            let mut s = o.to_string();
60            s.truncate(s.rfind(':').unwrap());
61            s
62        }
63    }
64}
65
66impl From<OstreeImageReference> for ImageReference {
67    fn from(imgref: OstreeImageReference) -> Self {
68        let signature = match imgref.sigverify {
69            ostree_container::SignatureSource::ContainerPolicyAllowInsecure => None,
70            v => Some(v.into()),
71        };
72        Self {
73            signature,
74            transport: transport_to_string(imgref.imgref.transport),
75            image: imgref.imgref.name,
76        }
77    }
78}
79
80impl From<ImageReference> for OstreeImageReference {
81    fn from(img: ImageReference) -> Self {
82        let sigverify = match img.signature {
83            Some(v) => v.into(),
84            None => ostree_container::SignatureSource::ContainerPolicyAllowInsecure,
85        };
86        Self {
87            sigverify,
88            imgref: ostree_container::ImageReference {
89                // SAFETY: We validated the schema in kube-rs
90                transport: img.transport.as_str().try_into().unwrap(),
91                name: img.image,
92            },
93        }
94    }
95}
96
97/// Check if SELinux policies are compatible between booted and target deployments.
98/// Returns false if SELinux is enabled and the policies differ or have mismatched presence.
99fn check_selinux_policy_compatible(
100    sysroot: &SysrootLock,
101    booted_deployment: &ostree::Deployment,
102    target_deployment: &ostree::Deployment,
103) -> Result<bool> {
104    // Only check if SELinux is enabled
105    if !crate::lsm::selinux_enabled()? {
106        return Ok(true);
107    }
108
109    let booted_fd = crate::utils::deployment_fd(sysroot, booted_deployment)
110        .context("Failed to get file descriptor for booted deployment")?;
111    let booted_policy = crate::lsm::new_sepolicy_at(&booted_fd)
112        .context("Failed to load SELinux policy from booted deployment")?;
113    let target_fd = crate::utils::deployment_fd(sysroot, target_deployment)
114        .context("Failed to get file descriptor for target deployment")?;
115    let target_policy = crate::lsm::new_sepolicy_at(&target_fd)
116        .context("Failed to load SELinux policy from target deployment")?;
117
118    let booted_csum = booted_policy.and_then(|p| p.csum());
119    let target_csum = target_policy.and_then(|p| p.csum());
120
121    match (booted_csum, target_csum) {
122        (None, None) => Ok(true), // Both absent, compatible
123        (Some(_), None) | (None, Some(_)) => {
124            // Incompatible: one has policy, other doesn't
125            Ok(false)
126        }
127        (Some(booted_csum), Some(target_csum)) => {
128            // Both have policies, checksums must match
129            Ok(booted_csum == target_csum)
130        }
131    }
132}
133
134/// Check if a deployment has soft reboot capability
135// TODO: Lower SELinux policy check into ostree's deployment_can_soft_reboot API
136fn has_soft_reboot_capability(sysroot: &SysrootLock, deployment: &ostree::Deployment) -> bool {
137    if !ostree_ext::systemd_has_soft_reboot() {
138        return false;
139    }
140
141    // When the ostree version is < 2025.7 and the deployment is
142    // missing the ostree= karg (happens during a factory reset),
143    // there is a bug that causes deployment_can_soft_reboot to crash.
144    // So in this case default to disabling soft reboot.
145    let has_ostree_karg = deployment
146        .bootconfig()
147        .and_then(|bootcfg| bootcfg.get("options"))
148        .map(|options| options.contains("ostree="))
149        .unwrap_or(false);
150
151    if !ostree::check_version(2025, 7) && !has_ostree_karg {
152        return false;
153    }
154
155    if !sysroot.deployment_can_soft_reboot(deployment) {
156        return false;
157    }
158
159    // Check SELinux policy compatibility with booted deployment
160    // Block soft reboot if SELinux policies differ, as policy is not reloaded across soft reboots
161    if let Some(booted_deployment) = sysroot.booted_deployment() {
162        // deployment_fd should not fail for valid deployments
163        if !check_selinux_policy_compatible(sysroot, &booted_deployment, deployment)
164            .expect("deployment_fd should not fail for valid deployments")
165        {
166            return false;
167        }
168    }
169
170    true
171}
172
173/// Parse an ostree origin file (a keyfile) and extract the targeted
174/// container image reference.
175fn get_image_origin(origin: &glib::KeyFile) -> Result<Option<OstreeImageReference>> {
176    origin
177        .optional_string("origin", ostree_container::deploy::ORIGIN_CONTAINER)
178        .context("Failed to load container image from origin")?
179        .map(|v| ostree_container::OstreeImageReference::try_from(v.as_str()))
180        .transpose()
181}
182
183pub(crate) struct Deployments {
184    pub(crate) staged: Option<ostree::Deployment>,
185    pub(crate) rollback: Option<ostree::Deployment>,
186    #[allow(dead_code)]
187    pub(crate) other: VecDeque<ostree::Deployment>,
188}
189
190pub(crate) fn labels_of_config(
191    config: &oci_spec::image::ImageConfiguration,
192) -> Option<&std::collections::HashMap<String, String>> {
193    config.config().as_ref().and_then(|c| c.labels().as_ref())
194}
195
196/// Convert between a subset of ostree-ext metadata and the exposed spec API.
197fn create_imagestatus(
198    image: ImageReference,
199    manifest_digest: &Digest,
200    config: &ImageConfiguration,
201) -> ImageStatus {
202    let labels = labels_of_config(config);
203    let timestamp = labels
204        .and_then(|l| {
205            l.get(oci_spec::image::ANNOTATION_CREATED)
206                .map(|s| s.as_str())
207        })
208        .or_else(|| config.created().as_deref())
209        .and_then(bootc_utils::try_deserialize_timestamp);
210
211    let version = ostree_container::version_for_config(config).map(ToOwned::to_owned);
212    let architecture = config.architecture().to_string();
213    ImageStatus {
214        image,
215        version,
216        timestamp,
217        image_digest: manifest_digest.to_string(),
218        architecture,
219    }
220}
221
222fn imagestatus(
223    sysroot: &SysrootLock,
224    deployment: &ostree::Deployment,
225    image: ostree_container::OstreeImageReference,
226) -> Result<CachedImageStatus> {
227    let repo = &sysroot.repo();
228    let imgstate = ostree_container::store::query_image_commit(repo, &deployment.csum())?;
229    let image = ImageReference::from(image);
230    let cached = imgstate
231        .cached_update
232        .map(|cached| create_imagestatus(image.clone(), &cached.manifest_digest, &cached.config));
233    let imagestatus = create_imagestatus(image, &imgstate.manifest_digest, &imgstate.configuration);
234
235    Ok(CachedImageStatus {
236        image: Some(imagestatus),
237        cached_update: cached,
238    })
239}
240
241/// Given an OSTree deployment, parse out metadata into our spec.
242#[context("Reading deployment metadata")]
243pub(crate) fn boot_entry_from_deployment(
244    sysroot: &SysrootLock,
245    deployment: &ostree::Deployment,
246) -> Result<BootEntry> {
247    let (
248        CachedImageStatus {
249            image,
250            cached_update,
251        },
252        incompatible,
253    ) = if let Some(origin) = deployment.origin().as_ref() {
254        let incompatible = crate::utils::origin_has_rpmostree_stuff(origin);
255        let cached_imagestatus = if incompatible {
256            // If there are local changes, we can't represent it as a bootc compatible image.
257            CachedImageStatus::default()
258        } else if let Some(image) = get_image_origin(origin)? {
259            imagestatus(sysroot, deployment, image)?
260        } else {
261            // The deployment isn't using a container image
262            CachedImageStatus::default()
263        };
264        (cached_imagestatus, incompatible)
265    } else {
266        // The deployment has no origin at all (this generally shouldn't happen)
267        (CachedImageStatus::default(), false)
268    };
269
270    let soft_reboot_capable = has_soft_reboot_capability(sysroot, deployment);
271    let download_only = deployment.is_staged() && deployment.is_finalization_locked();
272    let store = Some(crate::spec::Store::OstreeContainer);
273    let r = BootEntry {
274        image,
275        cached_update,
276        incompatible,
277        soft_reboot_capable,
278        download_only,
279        store,
280        pinned: deployment.is_pinned(),
281        ostree: Some(crate::spec::BootEntryOstree {
282            checksum: deployment.csum().into(),
283            // SAFETY: The deployserial is really unsigned
284            deploy_serial: deployment.deployserial().try_into().unwrap(),
285            stateroot: deployment.stateroot().into(),
286        }),
287        composefs: None,
288    };
289    Ok(r)
290}
291
292impl BootEntry {
293    /// Given a boot entry, find its underlying ostree container image
294    pub(crate) fn query_image(
295        &self,
296        repo: &ostree::Repo,
297    ) -> Result<Option<Box<ostree_container::store::LayeredImageState>>> {
298        if self.image.is_none() {
299            return Ok(None);
300        }
301        if let Some(checksum) = self.ostree.as_ref().map(|c| c.checksum.as_str()) {
302            ostree_container::store::query_image_commit(repo, checksum).map(Some)
303        } else {
304            Ok(None)
305        }
306    }
307
308    pub(crate) fn require_composefs(&self) -> Result<&BootEntryComposefs> {
309        self.composefs.as_ref().ok_or(anyhow::anyhow!(
310            "BootEntry is not a composefs native boot entry"
311        ))
312    }
313
314    /// Get the boot digest for this deployment
315    /// This is the
316    /// - SHA256SUM of kernel + initrd for Type1 booted deployments
317    /// - SHA256SUM of UKI for Type2 booted deployments
318    pub(crate) fn composefs_boot_digest(&self) -> Result<&String> {
319        self.require_composefs()?
320            .boot_digest
321            .as_ref()
322            .ok_or_else(|| anyhow::anyhow!("Could not find boot digest for deployment"))
323    }
324}
325
326/// A variant of [`get_status`] that requires a booted deployment.
327pub(crate) fn get_status_require_booted(
328    sysroot: &SysrootLock,
329) -> Result<(crate::store::BootedOstree<'_>, Deployments, Host)> {
330    let booted_deployment = sysroot.require_booted_deployment()?;
331    let booted_ostree = crate::store::BootedOstree {
332        sysroot,
333        deployment: booted_deployment,
334    };
335    let (deployments, host) = get_status(&booted_ostree)?;
336    Ok((booted_ostree, deployments, host))
337}
338
339/// Gather the ostree deployment objects, but also extract metadata from them into
340/// a more native Rust structure.
341#[context("Computing status")]
342pub(crate) fn get_status(
343    booted_ostree: &crate::store::BootedOstree<'_>,
344) -> Result<(Deployments, Host)> {
345    let sysroot = booted_ostree.sysroot;
346    let booted_deployment = Some(&booted_ostree.deployment);
347    let stateroot = booted_deployment.as_ref().map(|d| d.osname());
348    let (mut related_deployments, other_deployments) = sysroot
349        .deployments()
350        .into_iter()
351        .partition::<VecDeque<_>, _>(|d| Some(d.osname()) == stateroot);
352    let staged = related_deployments
353        .iter()
354        .position(|d| d.is_staged())
355        .map(|i| related_deployments.remove(i).unwrap());
356    tracing::debug!("Staged: {staged:?}");
357    // Filter out the booted, the caller already found that
358    if let Some(booted) = booted_deployment.as_ref() {
359        related_deployments.retain(|f| !f.equal(booted));
360    }
361    let rollback = related_deployments.pop_front();
362    let rollback_queued = match (booted_deployment.as_ref(), rollback.as_ref()) {
363        (Some(booted), Some(rollback)) => rollback.index() < booted.index(),
364        _ => false,
365    };
366    let boot_order = if rollback_queued {
367        BootOrder::Rollback
368    } else {
369        BootOrder::Default
370    };
371    tracing::debug!("Rollback queued={rollback_queued:?}");
372    let other = {
373        related_deployments.extend(other_deployments);
374        related_deployments
375    };
376    let deployments = Deployments {
377        staged,
378        rollback,
379        other,
380    };
381
382    let staged = deployments
383        .staged
384        .as_ref()
385        .map(|d| boot_entry_from_deployment(sysroot, d))
386        .transpose()
387        .context("Staged deployment")?;
388    let booted = booted_deployment
389        .as_ref()
390        .map(|d| boot_entry_from_deployment(sysroot, d))
391        .transpose()
392        .context("Booted deployment")?;
393    let rollback = deployments
394        .rollback
395        .as_ref()
396        .map(|d| boot_entry_from_deployment(sysroot, d))
397        .transpose()
398        .context("Rollback deployment")?;
399    let other_deployments = deployments
400        .other
401        .iter()
402        .map(|d| boot_entry_from_deployment(sysroot, d))
403        .collect::<Result<Vec<_>>>()
404        .context("Other deployments")?;
405    let spec = staged
406        .as_ref()
407        .or(booted.as_ref())
408        .and_then(|entry| entry.image.as_ref())
409        .map(|img| HostSpec {
410            image: Some(img.image.clone()),
411            boot_order,
412        })
413        .unwrap_or_default();
414
415    let ty = if booted
416        .as_ref()
417        .map(|b| b.image.is_some())
418        .unwrap_or_default()
419    {
420        // We're only of type BootcHost if we booted via container image
421        Some(HostType::BootcHost)
422    } else {
423        None
424    };
425
426    let usr_overlay = booted_deployment
427        .as_ref()
428        .map(|d| d.unlocked())
429        .and_then(crate::spec::deployment_unlocked_state_to_usr_overlay);
430
431    let mut host = Host::new(spec);
432    host.status = HostStatus {
433        staged,
434        booted,
435        rollback,
436        other_deployments,
437        rollback_queued,
438        ty,
439        usr_overlay,
440    };
441    Ok((deployments, host))
442}
443
444pub(crate) async fn get_host() -> Result<Host> {
445    let env = crate::store::Environment::detect()?;
446    if env.needs_mount_namespace() {
447        crate::cli::prepare_for_write()?;
448    }
449
450    let Some(storage) = BootedStorage::new(env).await? else {
451        // If we're not booted, then return a default.
452        return Ok(Host::default());
453    };
454
455    let host = match storage.kind() {
456        Ok(kind) => match kind {
457            BootedStorageKind::Ostree(booted_ostree) => {
458                let (_deployments, host) = get_status(&booted_ostree)?;
459                host
460            }
461            BootedStorageKind::Composefs(booted_cfs) => {
462                crate::bootc_composefs::status::get_composefs_status(&storage, &booted_cfs).await?
463            }
464        },
465        Err(_) => {
466            // If determining storage kind fails (e.g., no booted deployment),
467            // return a default host indicating the system is not deployed via bootc
468            Host::default()
469        }
470    };
471
472    Ok(host)
473}
474
475/// Implementation of the `bootc status` CLI command.
476#[context("Status")]
477pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
478    match opts.format_version.unwrap_or_default() {
479        // For historical reasons, both 0 and 1 mean "v1".
480        0 | 1 => {}
481        o => anyhow::bail!("Unsupported format version: {o}"),
482    };
483    let mut host = get_host().await?;
484
485    // We could support querying the staged or rollback deployments
486    // here too, but it's not a common use case at the moment.
487    if opts.booted {
488        host.filter_to_slot(Slot::Booted);
489    }
490
491    // If we're in JSON mode, then convert the ostree data into Rust-native
492    // structures that can be serialized.
493    // Filter to just the serializable status structures.
494    let out = std::io::stdout();
495    let mut out = out.lock();
496    let legacy_opt = if opts.json {
497        OutputFormat::Json
498    } else if std::io::stdout().is_terminal() {
499        OutputFormat::HumanReadable
500    } else {
501        OutputFormat::Yaml
502    };
503    let format = opts.format.unwrap_or(legacy_opt);
504    match format {
505        OutputFormat::Json => host
506            .to_canon_json_writer(&mut out)
507            .map_err(anyhow::Error::new),
508        OutputFormat::Yaml => serde_yaml::to_writer(&mut out, &host).map_err(anyhow::Error::new),
509        OutputFormat::HumanReadable => human_readable_output(&mut out, &host, opts.verbose),
510    }
511    .context("Writing to stdout")?;
512
513    Ok(())
514}
515
516#[derive(Debug, Clone, Copy)]
517pub enum Slot {
518    Staged,
519    Booted,
520    Rollback,
521}
522
523impl std::fmt::Display for Slot {
524    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
525        let s = match self {
526            Slot::Staged => "staged",
527            Slot::Booted => "booted",
528            Slot::Rollback => "rollback",
529        };
530        f.write_str(s)
531    }
532}
533
534/// Output a row title, prefixed by spaces
535fn write_row_name(mut out: impl Write, s: &str, prefix_len: usize) -> Result<()> {
536    let n = prefix_len.saturating_sub(s.chars().count());
537    let mut spaces = std::io::repeat(b' ').take(n as u64);
538    std::io::copy(&mut spaces, &mut out)?;
539    write!(out, "{s}: ")?;
540    Ok(())
541}
542
543/// Format a timestamp for human display, without nanoseconds.
544///
545/// Nanoseconds are irrelevant noise for container build timestamps;
546/// this produces the same format as RFC3339 but truncated to seconds.
547fn format_timestamp(t: &chrono::DateTime<chrono::Utc>) -> impl std::fmt::Display {
548    t.format("%Y-%m-%dT%H:%M:%SZ")
549}
550
551/// Helper function to render verbose ostree information
552fn render_verbose_ostree_info(
553    mut out: impl Write,
554    ostree: &crate::spec::BootEntryOstree,
555    slot: Option<Slot>,
556    prefix_len: usize,
557) -> Result<()> {
558    write_row_name(&mut out, "StateRoot", prefix_len)?;
559    writeln!(out, "{}", ostree.stateroot)?;
560
561    // Show deployment serial (similar to Index in rpm-ostree)
562    write_row_name(&mut out, "Deploy serial", prefix_len)?;
563    writeln!(out, "{}", ostree.deploy_serial)?;
564
565    // Show if this is staged
566    let is_staged = matches!(slot, Some(Slot::Staged));
567    write_row_name(&mut out, "Staged", prefix_len)?;
568    writeln!(out, "{}", if is_staged { "yes" } else { "no" })?;
569
570    Ok(())
571}
572
573/// Helper function to render if soft-reboot capable
574fn write_soft_reboot(
575    mut out: impl Write,
576    entry: &crate::spec::BootEntry,
577    prefix_len: usize,
578) -> Result<()> {
579    // Show soft-reboot capability
580    write_row_name(&mut out, "Soft-reboot", prefix_len)?;
581    writeln!(
582        out,
583        "{}",
584        if entry.soft_reboot_capable {
585            "yes"
586        } else {
587            "no"
588        }
589    )?;
590
591    Ok(())
592}
593
594/// Helper function to render download-only lock status
595fn write_download_only(
596    mut out: impl Write,
597    slot: Option<Slot>,
598    entry: &crate::spec::BootEntry,
599    prefix_len: usize,
600) -> Result<()> {
601    // Only staged deployments can have download-only status
602    if matches!(slot, Some(Slot::Staged)) {
603        write_row_name(&mut out, "Download-only", prefix_len)?;
604        writeln!(out, "{}", if entry.download_only { "yes" } else { "no" })?;
605    }
606    Ok(())
607}
608
609fn write_fsverity_enforcement(
610    mut out: impl Write,
611    entry: &crate::spec::BootEntry,
612    prefix_len: usize,
613) -> Result<()> {
614    if let Some(cfs) = &entry.composefs {
615        write_row_name(&mut out, "FsVerity", prefix_len)?;
616        writeln!(
617            out,
618            "{}",
619            if cfs.missing_verity_allowed {
620                "Not Enforced"
621            } else {
622                "Enforced"
623            }
624        )?;
625    };
626
627    Ok(())
628}
629
630/// Render cached update information, showing what update is available.
631///
632/// This is populated by a previous `bootc upgrade --check` that found
633/// a newer image in the registry. We only display it when the cached
634/// digest differs from the currently deployed image.
635fn render_cached_update(
636    mut out: impl Write,
637    cached: &crate::spec::ImageStatus,
638    current: &crate::spec::ImageStatus,
639    prefix_len: usize,
640) -> Result<()> {
641    if cached.image_digest == current.image_digest {
642        return Ok(());
643    }
644
645    if let Some(version) = cached.version.as_deref() {
646        write_row_name(&mut out, "UpdateVersion", prefix_len)?;
647        let timestamp_str = cached
648            .timestamp
649            .as_ref()
650            .map(|t| format!(" ({})", format_timestamp(t)))
651            .unwrap_or_default();
652        writeln!(out, "{version}{timestamp_str}")?;
653    } else {
654        write_row_name(&mut out, "Update", prefix_len)?;
655        writeln!(out, "Available")?;
656    }
657    write_row_name(&mut out, "UpdateDigest", prefix_len)?;
658    writeln!(out, "{}", cached.image_digest)?;
659
660    Ok(())
661}
662
663/// Write the data for a container image based status.
664fn human_render_slot(
665    mut out: impl Write,
666    slot: Option<Slot>,
667    entry: &crate::spec::BootEntry,
668    image: &crate::spec::ImageStatus,
669    host_status: &crate::spec::HostStatus,
670    verbose: bool,
671) -> Result<()> {
672    let transport = &image.image.transport;
673    let imagename = &image.image.image;
674    // Registry is the default, so don't show that
675    let imageref = if transport == "registry" {
676        Cow::Borrowed(imagename)
677    } else {
678        // But for non-registry we include the transport
679        Cow::Owned(format!("{transport}:{imagename}"))
680    };
681    let prefix = match slot {
682        Some(Slot::Staged) => "  Staged image".into(),
683        Some(Slot::Booted) => format!("{} Booted image", crate::glyph::Glyph::BlackCircle),
684        Some(Slot::Rollback) => "  Rollback image".into(),
685        _ => "   Other image".into(),
686    };
687    let prefix_len = prefix.chars().count();
688    writeln!(out, "{prefix}: {imageref}")?;
689
690    let arch = image.architecture.as_str();
691    write_row_name(&mut out, "Digest", prefix_len)?;
692    let digest = &image.image_digest;
693    writeln!(out, "{digest} ({arch})")?;
694
695    // Write the EROFS verity if present
696    if let Some(composefs) = &entry.composefs {
697        write_row_name(&mut out, "Verity", prefix_len)?;
698        writeln!(out, "{}", composefs.verity)?;
699    }
700
701    let timestamp = image.timestamp.as_ref().map(format_timestamp);
702    // If we have a version, combine with timestamp
703    if let Some(version) = image.version.as_deref() {
704        write_row_name(&mut out, "Version", prefix_len)?;
705        if let Some(timestamp) = timestamp {
706            writeln!(out, "{version} ({timestamp})")?;
707        } else {
708            writeln!(out, "{version}")?;
709        }
710    } else if let Some(timestamp) = timestamp {
711        // Otherwise just output timestamp
712        write_row_name(&mut out, "Timestamp", prefix_len)?;
713        writeln!(out, "{timestamp}")?;
714    }
715
716    if entry.pinned {
717        write_row_name(&mut out, "Pinned", prefix_len)?;
718        writeln!(out, "yes")?;
719    }
720
721    // Show cached update information when available (from a previous `bootc upgrade --check`)
722    if let Some(cached) = &entry.cached_update {
723        render_cached_update(&mut out, cached, image, prefix_len)?;
724    }
725
726    // Show /usr overlay status
727    write_usr_overlay(&mut out, slot, host_status, prefix_len)?;
728
729    if verbose {
730        // Show additional information in verbose mode similar to rpm-ostree
731        if let Some(ostree) = &entry.ostree {
732            render_verbose_ostree_info(&mut out, ostree, slot, prefix_len)?;
733
734            // Show the commit (equivalent to Base Commit in rpm-ostree)
735            write_row_name(&mut out, "Commit", prefix_len)?;
736            writeln!(out, "{}", ostree.checksum)?;
737        }
738
739        // Show signature information if available
740        if let Some(signature) = &image.image.signature {
741            write_row_name(&mut out, "Signature", prefix_len)?;
742            match signature {
743                crate::spec::ImageSignature::OstreeRemote(remote) => {
744                    writeln!(out, "ostree-remote:{remote}")?;
745                }
746                crate::spec::ImageSignature::ContainerPolicy => {
747                    writeln!(out, "container-policy")?;
748                }
749                crate::spec::ImageSignature::Insecure => {
750                    writeln!(out, "insecure")?;
751                }
752            }
753        }
754
755        // Show soft-reboot capability
756        write_soft_reboot(&mut out, entry, prefix_len)?;
757
758        write_fsverity_enforcement(&mut out, entry, prefix_len)?;
759
760        // Show download-only lock status
761        write_download_only(&mut out, slot, entry, prefix_len)?;
762    }
763
764    tracing::debug!("pinned={}", entry.pinned);
765
766    Ok(())
767}
768
769/// Helper function to render usr overlay status
770fn write_usr_overlay(
771    mut out: impl Write,
772    slot: Option<Slot>,
773    host_status: &crate::spec::HostStatus,
774    prefix_len: usize,
775) -> Result<()> {
776    // Only booted deployments can have /usr overlay status
777    if matches!(slot, Some(Slot::Booted)) {
778        // Only print row if overlay is present
779        if let Some(ref overlay) = host_status.usr_overlay {
780            write_row_name(&mut out, "/usr overlay", prefix_len)?;
781            writeln!(out, "{}", overlay)?;
782        }
783    }
784    Ok(())
785}
786
787/// Output a rendering of a non-container boot entry.
788fn human_render_slot_ostree(
789    mut out: impl Write,
790    slot: Option<Slot>,
791    entry: &crate::spec::BootEntry,
792    ostree_commit: &str,
793    host_status: &crate::spec::HostStatus,
794    verbose: bool,
795) -> Result<()> {
796    // TODO consider rendering more ostree stuff here like rpm-ostree status does
797    let prefix = match slot {
798        Some(Slot::Staged) => "  Staged ostree".into(),
799        Some(Slot::Booted) => format!("{} Booted ostree", crate::glyph::Glyph::BlackCircle),
800        Some(Slot::Rollback) => "  Rollback ostree".into(),
801        _ => " Other ostree".into(),
802    };
803    let prefix_len = prefix.len();
804    writeln!(out, "{prefix}")?;
805    write_row_name(&mut out, "Commit", prefix_len)?;
806    writeln!(out, "{ostree_commit}")?;
807
808    if entry.pinned {
809        write_row_name(&mut out, "Pinned", prefix_len)?;
810        writeln!(out, "yes")?;
811    }
812
813    // Show /usr overlay status
814    write_usr_overlay(&mut out, slot, host_status, prefix_len)?;
815
816    if verbose {
817        // Show additional information in verbose mode similar to rpm-ostree
818        if let Some(ostree) = &entry.ostree {
819            render_verbose_ostree_info(&mut out, ostree, slot, prefix_len)?;
820        }
821
822        // Show soft-reboot capability
823        write_soft_reboot(&mut out, entry, prefix_len)?;
824
825        // Show download-only lock status
826        write_download_only(&mut out, slot, entry, prefix_len)?;
827    }
828
829    tracing::debug!("pinned={}", entry.pinned);
830    Ok(())
831}
832
833/// Output a rendering of a non-container composefs boot entry.
834fn human_render_slot_composefs(
835    mut out: impl Write,
836    slot: Slot,
837    entry: &crate::spec::BootEntry,
838    erofs_verity: &str,
839) -> Result<()> {
840    // TODO consider rendering more ostree stuff here like rpm-ostree status does
841    let prefix = match slot {
842        Slot::Staged => "  Staged composefs".into(),
843        Slot::Booted => format!("{} Booted composefs", crate::glyph::Glyph::BlackCircle),
844        Slot::Rollback => "  Rollback composefs".into(),
845    };
846    let prefix_len = prefix.len();
847    writeln!(out, "{prefix}")?;
848    write_row_name(&mut out, "Commit", prefix_len)?;
849    writeln!(out, "{erofs_verity}")?;
850    tracing::debug!("pinned={}", entry.pinned);
851    Ok(())
852}
853
854fn human_readable_output_booted(mut out: impl Write, host: &Host, verbose: bool) -> Result<()> {
855    let mut first = true;
856    for (slot_name, status) in [
857        (Slot::Staged, &host.status.staged),
858        (Slot::Booted, &host.status.booted),
859        (Slot::Rollback, &host.status.rollback),
860    ] {
861        if let Some(host_status) = status {
862            if first {
863                first = false;
864            } else {
865                writeln!(out)?;
866            }
867
868            if let Some(image) = &host_status.image {
869                human_render_slot(
870                    &mut out,
871                    Some(slot_name),
872                    host_status,
873                    image,
874                    &host.status,
875                    verbose,
876                )?;
877            } else if let Some(ostree) = host_status.ostree.as_ref() {
878                human_render_slot_ostree(
879                    &mut out,
880                    Some(slot_name),
881                    host_status,
882                    &ostree.checksum,
883                    &host.status,
884                    verbose,
885                )?;
886            } else if let Some(composefs) = &host_status.composefs {
887                human_render_slot_composefs(&mut out, slot_name, host_status, &composefs.verity)?;
888            } else {
889                writeln!(out, "Current {slot_name} state is unknown")?;
890            }
891        }
892    }
893
894    if !host.status.other_deployments.is_empty() {
895        for entry in &host.status.other_deployments {
896            writeln!(out)?;
897
898            if let Some(image) = &entry.image {
899                human_render_slot(&mut out, None, entry, image, &host.status, verbose)?;
900            } else if let Some(ostree) = entry.ostree.as_ref() {
901                human_render_slot_ostree(
902                    &mut out,
903                    None,
904                    entry,
905                    &ostree.checksum,
906                    &host.status,
907                    verbose,
908                )?;
909            }
910        }
911    }
912
913    Ok(())
914}
915
916/// Implementation of rendering our host structure in a "human readable" way.
917fn human_readable_output(mut out: impl Write, host: &Host, verbose: bool) -> Result<()> {
918    if host.status.booted.is_some() {
919        human_readable_output_booted(out, host, verbose)?;
920    } else {
921        writeln!(out, "System is not deployed via bootc.")?;
922    }
923    Ok(())
924}
925
926/// Output container inspection in human-readable format
927fn container_inspect_print_human(
928    inspect: &crate::spec::ContainerInspect,
929    mut out: impl Write,
930) -> Result<()> {
931    // Collect rows to determine the max label width
932    let mut rows: Vec<(&str, String)> = Vec::new();
933
934    if let Some(kernel) = &inspect.kernel {
935        rows.push(("Kernel", kernel.version.clone()));
936        let kernel_type = if kernel.unified { "UKI" } else { "vmlinuz" };
937        rows.push(("Type", kernel_type.to_string()));
938    } else {
939        rows.push(("Kernel", "<none>".to_string()));
940    }
941
942    let kargs = if inspect.kargs.is_empty() {
943        "<none>".to_string()
944    } else {
945        inspect.kargs.join(" ")
946    };
947    rows.push(("Kargs", kargs));
948
949    // Find the max label width for right-alignment
950    let max_label_len = rows
951        .iter()
952        .map(|(label, _)| label.width())
953        .max()
954        .unwrap_or(0);
955
956    for (label, value) in rows {
957        write_row_name(&mut out, label, max_label_len)?;
958        writeln!(out, "{value}")?;
959    }
960
961    Ok(())
962}
963
964/// Inspect a container image and output information about it.
965pub(crate) fn container_inspect(
966    rootfs: &camino::Utf8Path,
967    json: bool,
968    format: Option<OutputFormat>,
969) -> Result<()> {
970    let root = cap_std_ext::cap_std::fs::Dir::open_ambient_dir(
971        rootfs,
972        cap_std_ext::cap_std::ambient_authority(),
973    )?;
974    let kargs = crate::bootc_kargs::get_kargs_in_root(&root, std::env::consts::ARCH)?;
975    let kargs: Vec<String> = kargs.iter_str().map(|s| s.to_owned()).collect();
976    let kernel = crate::kernel::find_kernel(&root)?.map(Into::into);
977    let inspect = crate::spec::ContainerInspect { kargs, kernel };
978
979    // Determine output format: explicit --format wins, then --json, then default to human-readable
980    let format = format.unwrap_or(if json {
981        OutputFormat::Json
982    } else {
983        OutputFormat::HumanReadable
984    });
985
986    let mut out = std::io::stdout().lock();
987    match format {
988        OutputFormat::Json => {
989            serde_json::to_writer_pretty(&mut out, &inspect)?;
990        }
991        OutputFormat::Yaml => {
992            serde_yaml::to_writer(&mut out, &inspect)?;
993        }
994        OutputFormat::HumanReadable => {
995            container_inspect_print_human(&inspect, &mut out)?;
996        }
997    }
998    Ok(())
999}
1000
1001#[cfg(test)]
1002mod tests {
1003    use super::*;
1004
1005    #[test]
1006    fn test_format_timestamp() {
1007        use chrono::TimeZone;
1008        let cases = [
1009            // Standard case
1010            (
1011                chrono::Utc.with_ymd_and_hms(2024, 8, 7, 12, 0, 0).unwrap(),
1012                "2024-08-07T12:00:00Z",
1013            ),
1014            // Midnight
1015            (
1016                chrono::Utc.with_ymd_and_hms(2023, 1, 1, 0, 0, 0).unwrap(),
1017                "2023-01-01T00:00:00Z",
1018            ),
1019            // End of day
1020            (
1021                chrono::Utc
1022                    .with_ymd_and_hms(2025, 12, 31, 23, 59, 59)
1023                    .unwrap(),
1024                "2025-12-31T23:59:59Z",
1025            ),
1026            // Subsecond precision should be dropped
1027            (
1028                chrono::Utc
1029                    .with_ymd_and_hms(2024, 6, 15, 10, 30, 45)
1030                    .unwrap()
1031                    + chrono::Duration::nanoseconds(123_456_789),
1032                "2024-06-15T10:30:45Z",
1033            ),
1034        ];
1035        for (input, expected) in cases {
1036            let result = format_timestamp(&input).to_string();
1037            assert_eq!(result, expected, "Failed for input {input:?}");
1038        }
1039    }
1040
1041    fn human_status_from_spec_fixture(spec_fixture: &str) -> Result<String> {
1042        let host: Host = serde_yaml::from_str(spec_fixture).unwrap();
1043        let mut w = Vec::new();
1044        human_readable_output(&mut w, &host, false).unwrap();
1045        let w = String::from_utf8(w).unwrap();
1046        Ok(w)
1047    }
1048
1049    /// Helper function to generate human-readable status output with verbose mode enabled
1050    /// from a YAML fixture string. Used for testing verbose output formatting.
1051    fn human_status_from_spec_fixture_verbose(spec_fixture: &str) -> Result<String> {
1052        let host: Host = serde_yaml::from_str(spec_fixture).unwrap();
1053        let mut w = Vec::new();
1054        human_readable_output(&mut w, &host, true).unwrap();
1055        let w = String::from_utf8(w).unwrap();
1056        Ok(w)
1057    }
1058
1059    #[test]
1060    fn test_human_readable_base_spec() {
1061        // Tests Staged and Booted, null Rollback
1062        let w = human_status_from_spec_fixture(include_str!("fixtures/spec-staged-booted.yaml"))
1063            .expect("No spec found");
1064        let expected = indoc::indoc! { r"
1065            Staged image: quay.io/example/someimage:latest
1066                  Digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566 (arm64)
1067                 Version: nightly (2023-10-14T19:22:15Z)
1068
1069          ● Booted image: quay.io/example/someimage:latest
1070                  Digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34 (arm64)
1071                 Version: nightly (2023-09-30T19:22:16Z)
1072        "};
1073        similar_asserts::assert_eq!(w, expected);
1074    }
1075
1076    #[test]
1077    fn test_human_readable_rfe_spec() {
1078        // Basic rhel for edge bootc install with nothing
1079        let w = human_status_from_spec_fixture(include_str!(
1080            "fixtures/spec-rfe-ostree-deployment.yaml"
1081        ))
1082        .expect("No spec found");
1083        let expected = indoc::indoc! { r"
1084            Staged ostree
1085                   Commit: 1c24260fdd1be20f72a4a97a75c582834ee3431fbb0fa8e4f482bb219d633a45
1086
1087          ● Booted ostree
1088                     Commit: f9fa3a553ceaaaf30cf85bfe7eed46a822f7b8fd7e14c1e3389cbc3f6d27f791
1089        "};
1090        similar_asserts::assert_eq!(w, expected);
1091    }
1092
1093    #[test]
1094    fn test_human_readable_staged_spec() {
1095        // staged image, no boot/rollback
1096        let w = human_status_from_spec_fixture(include_str!("fixtures/spec-ostree-to-bootc.yaml"))
1097            .expect("No spec found");
1098        let expected = indoc::indoc! { r"
1099            Staged image: quay.io/centos-bootc/centos-bootc:stream9
1100                  Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (s390x)
1101                 Version: stream9.20240807.0
1102
1103          ● Booted ostree
1104                     Commit: f9fa3a553ceaaaf30cf85bfe7eed46a822f7b8fd7e14c1e3389cbc3f6d27f791
1105        "};
1106        similar_asserts::assert_eq!(w, expected);
1107    }
1108
1109    #[test]
1110    fn test_human_readable_booted_spec() {
1111        // booted image, no staged/rollback
1112        let w = human_status_from_spec_fixture(include_str!("fixtures/spec-only-booted.yaml"))
1113            .expect("No spec found");
1114        let expected = indoc::indoc! { r"
1115          ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1116                  Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1117                 Version: stream9.20240807.0
1118        "};
1119        similar_asserts::assert_eq!(w, expected);
1120    }
1121
1122    #[test]
1123    fn test_human_readable_staged_rollback_spec() {
1124        // staged/rollback image, no booted
1125        let w = human_status_from_spec_fixture(include_str!("fixtures/spec-staged-rollback.yaml"))
1126            .expect("No spec found");
1127        let expected = "System is not deployed via bootc.\n";
1128        similar_asserts::assert_eq!(w, expected);
1129    }
1130
1131    #[test]
1132    fn test_via_oci() {
1133        let w = human_status_from_spec_fixture(include_str!("fixtures/spec-via-local-oci.yaml"))
1134            .unwrap();
1135        let expected = indoc::indoc! { r"
1136          ● Booted image: oci:/var/mnt/osupdate
1137                  Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (amd64)
1138                 Version: stream9.20240807.0
1139        "};
1140        similar_asserts::assert_eq!(w, expected);
1141    }
1142
1143    #[test]
1144    fn test_convert_signatures() {
1145        use std::str::FromStr;
1146        let ir_unverified = &OstreeImageReference::from_str(
1147            "ostree-unverified-registry:quay.io/someexample/foo:latest",
1148        )
1149        .unwrap();
1150        let ir_ostree = &OstreeImageReference::from_str(
1151            "ostree-remote-registry:fedora:quay.io/fedora/fedora-coreos:stable",
1152        )
1153        .unwrap();
1154
1155        let ir = ImageReference::from(ir_unverified.clone());
1156        assert_eq!(ir.image, "quay.io/someexample/foo:latest");
1157        assert_eq!(ir.signature, None);
1158
1159        let ir = ImageReference::from(ir_ostree.clone());
1160        assert_eq!(ir.image, "quay.io/fedora/fedora-coreos:stable");
1161        assert_eq!(
1162            ir.signature,
1163            Some(ImageSignature::OstreeRemote("fedora".into()))
1164        );
1165    }
1166
1167    #[test]
1168    fn test_human_readable_booted_pinned_spec() {
1169        // booted image, no staged/rollback
1170        let w = human_status_from_spec_fixture(include_str!("fixtures/spec-booted-pinned.yaml"))
1171            .expect("No spec found");
1172        let expected = indoc::indoc! { r"
1173          ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1174                  Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1175                 Version: stream9.20240807.0
1176                  Pinned: yes
1177
1178             Other image: quay.io/centos-bootc/centos-bootc:stream9
1179                  Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b37 (arm64)
1180                 Version: stream9.20240807.0
1181                  Pinned: yes
1182        "};
1183        similar_asserts::assert_eq!(w, expected);
1184    }
1185
1186    #[test]
1187    fn test_human_readable_verbose_spec() {
1188        // Test verbose output includes additional fields
1189        let w =
1190            human_status_from_spec_fixture_verbose(include_str!("fixtures/spec-only-booted.yaml"))
1191                .expect("No spec found");
1192
1193        // Verbose output should include StateRoot, Deploy serial, Staged, and Commit
1194        assert!(w.contains("StateRoot:"));
1195        assert!(w.contains("Deploy serial:"));
1196        assert!(w.contains("Staged:"));
1197        assert!(w.contains("Commit:"));
1198        assert!(w.contains("Soft-reboot:"));
1199    }
1200
1201    #[test]
1202    fn test_human_readable_staged_download_only() {
1203        // Test that download-only staged deployment shows the status in non-verbose mode
1204        // Download-only status is only shown in verbose mode per design
1205        let w =
1206            human_status_from_spec_fixture(include_str!("fixtures/spec-staged-download-only.yaml"))
1207                .expect("No spec found");
1208        let expected = indoc::indoc! { r"
1209            Staged image: quay.io/example/someimage:latest
1210                  Digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566 (arm64)
1211                 Version: nightly (2023-10-14T19:22:15Z)
1212
1213          ● Booted image: quay.io/example/someimage:latest
1214                  Digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34 (arm64)
1215                 Version: nightly (2023-09-30T19:22:16Z)
1216        "};
1217        similar_asserts::assert_eq!(w, expected);
1218    }
1219
1220    #[test]
1221    fn test_human_readable_staged_download_only_verbose() {
1222        // Test that download-only status is shown in verbose mode for staged deployments
1223        let w = human_status_from_spec_fixture_verbose(include_str!(
1224            "fixtures/spec-staged-download-only.yaml"
1225        ))
1226        .expect("No spec found");
1227
1228        // Verbose output should include download-only status
1229        assert!(w.contains("Download-only: yes"));
1230    }
1231
1232    #[test]
1233    fn test_human_readable_staged_not_download_only_verbose() {
1234        // Test that staged deployment not in download-only mode shows "Download-only: no" in verbose mode
1235        let w = human_status_from_spec_fixture_verbose(include_str!(
1236            "fixtures/spec-staged-booted.yaml"
1237        ))
1238        .expect("No spec found");
1239
1240        // Verbose output should include download-only status as "no" for normal staged deployments
1241        assert!(w.contains("Download-only: no"));
1242    }
1243
1244    #[test]
1245    fn test_container_inspect_human_readable() {
1246        let inspect = crate::spec::ContainerInspect {
1247            kargs: vec!["console=ttyS0".into(), "quiet".into()],
1248            kernel: Some(crate::kernel::Kernel {
1249                version: "6.12.0-100.fc41.x86_64".into(),
1250                unified: false,
1251            }),
1252        };
1253        let mut w = Vec::new();
1254        container_inspect_print_human(&inspect, &mut w).unwrap();
1255        let output = String::from_utf8(w).unwrap();
1256        let expected = indoc::indoc! { r"
1257            Kernel: 6.12.0-100.fc41.x86_64
1258              Type: vmlinuz
1259             Kargs: console=ttyS0 quiet
1260        "};
1261        similar_asserts::assert_eq!(output, expected);
1262    }
1263
1264    #[test]
1265    fn test_container_inspect_human_readable_uki() {
1266        let inspect = crate::spec::ContainerInspect {
1267            kargs: vec![],
1268            kernel: Some(crate::kernel::Kernel {
1269                version: "6.12.0-100.fc41.x86_64".into(),
1270                unified: true,
1271            }),
1272        };
1273        let mut w = Vec::new();
1274        container_inspect_print_human(&inspect, &mut w).unwrap();
1275        let output = String::from_utf8(w).unwrap();
1276        let expected = indoc::indoc! { r"
1277            Kernel: 6.12.0-100.fc41.x86_64
1278              Type: UKI
1279             Kargs: <none>
1280        "};
1281        similar_asserts::assert_eq!(output, expected);
1282    }
1283
1284    #[test]
1285    fn test_container_inspect_human_readable_no_kernel() {
1286        let inspect = crate::spec::ContainerInspect {
1287            kargs: vec!["console=ttyS0".into()],
1288            kernel: None,
1289        };
1290        let mut w = Vec::new();
1291        container_inspect_print_human(&inspect, &mut w).unwrap();
1292        let output = String::from_utf8(w).unwrap();
1293        let expected = indoc::indoc! { r"
1294            Kernel: <none>
1295             Kargs: console=ttyS0
1296        "};
1297        similar_asserts::assert_eq!(output, expected);
1298    }
1299
1300    #[test]
1301    fn test_human_readable_booted_usroverlay() {
1302        let w =
1303            human_status_from_spec_fixture(include_str!("fixtures/spec-booted-usroverlay.yaml"))
1304                .unwrap();
1305        let expected = indoc::indoc! { r"
1306          ● Booted image: quay.io/example/someimage:latest
1307                  Digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34 (arm64)
1308                 Version: nightly (2023-09-30T19:22:16Z)
1309            /usr overlay: transient, read/write
1310        "};
1311        similar_asserts::assert_eq!(w, expected);
1312    }
1313
1314    #[test]
1315    fn test_human_readable_booted_with_cached_update() {
1316        // When a cached update is present (from a previous `bootc upgrade --check`),
1317        // the human-readable output should show the available update info.
1318        let w =
1319            human_status_from_spec_fixture(include_str!("fixtures/spec-booted-with-update.yaml"))
1320                .expect("No spec found");
1321        let expected = indoc::indoc! { r"
1322          ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1323                  Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1324                 Version: stream9.20240807.0 (2024-08-07T12:00:00Z)
1325           UpdateVersion: stream9.20240901.0 (2024-09-01T12:00:00Z)
1326            UpdateDigest: sha256:a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0
1327        "};
1328        similar_asserts::assert_eq!(w, expected);
1329    }
1330
1331    #[test]
1332    fn test_human_readable_cached_update_same_digest_hidden() {
1333        // When the cached update has the same digest as the current image,
1334        // no update line should be shown.
1335        let w = human_status_from_spec_fixture(include_str!(
1336            "fixtures/spec-booted-update-same-digest.yaml"
1337        ))
1338        .expect("No spec found");
1339        assert!(
1340            !w.contains("UpdateVersion:"),
1341            "Should not show update version when digest matches current"
1342        );
1343        assert!(
1344            !w.contains("UpdateDigest:"),
1345            "Should not show update digest when digest matches current"
1346        );
1347    }
1348
1349    #[test]
1350    fn test_human_readable_cached_update_no_version() {
1351        // When the cached update has no version label, show "Available" as fallback.
1352        let w = human_status_from_spec_fixture(include_str!(
1353            "fixtures/spec-booted-with-update-no-version.yaml"
1354        ))
1355        .expect("No spec found");
1356        let expected = indoc::indoc! { r"
1357          ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1358                  Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1359                 Version: stream9.20240807.0
1360                  Update: Available
1361            UpdateDigest: sha256:b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1
1362        "};
1363        similar_asserts::assert_eq!(w, expected);
1364    }
1365}