summaryrefslogtreecommitdiff
path: root/tests/package_installer.rs
blob: 74bf9aa5cf8132297c825ff94b63e0b464c98a43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::path::PathBuf;

use alt_controller_image::hasher::{CommandRunner, HasherInstaller, Invocation};
use alt_controller_image::package_installer::{AptConfig, PackageInstaller, PackageRequest};
use alt_controller_image::{BuildPlan, ImageSpec};

#[derive(Default)]
struct RecordingRunner {
    invocations: std::cell::RefCell<Vec<Invocation>>,
}

impl CommandRunner for RecordingRunner {
    fn run(&self, invocation: Invocation) -> anyhow::Result<()> {
        self.invocations.borrow_mut().push(invocation);
        Ok(())
    }
}

#[test]
fn hasher_installer_passes_typed_request_to_initroot_and_install_commands() {
    let runner = RecordingRunner::default();
    let installer = HasherInstaller::new(runner);
    let request = PackageRequest::new(
        "/build/work",
        AptConfig::new("profiles/apt.conf").expect("valid apt config path"),
        ["basesystem", "make-initrd"],
        ["^kernel-image-rt$"],
    )
    .expect("valid package request");

    installer.install(&request).expect("installation succeeds");
    let invocations = &installer.runner().invocations.borrow();

    assert_eq!(
        invocations.as_slice(),
        [
            Invocation::new(
                "hsh",
                [
                    "--mountpoints=/proc",
                    "--initroot-only",
                    "--apt-config",
                    "profiles/apt.conf",
                    "--workdir",
                    "/build/work",
                ],
            ),
            Invocation::new(
                "hsh-install",
                [
                    "--mountpoints=/proc",
                    "--workdir",
                    "/build/work",
                    "^kernel-image-rt$",
                    "basesystem",
                    "make-initrd",
                ],
            ),
        ]
    );
}

#[test]
fn package_request_rejects_empty_packages_and_unsafe_apt_config_paths() {
    assert!(AptConfig::new("").is_err());
    assert!(
        PackageRequest::new(
            PathBuf::from("work"),
            AptConfig::new("profiles/apt.conf").expect("valid apt config"),
            ["basesystem", ""],
            std::iter::empty::<&str>(),
        )
        .is_err()
    );
}

#[test]
fn build_plan_compiles_its_typed_package_request() {
    let spec = ImageSpec::load(std::path::Path::new("profiles/alt-controller.toml"))
        .expect("load controller spec");
    let plan = BuildPlan::compile(spec).expect("compile controller plan");

    let request = plan
        .package_request("work/alt-controller", "profiles/apt.conf")
        .expect("compile package request");

    assert_eq!(
        request.workdir(),
        std::path::Path::new("work/alt-controller")
    );
    assert_eq!(
        request.apt_config().as_path(),
        std::path::Path::new("profiles/apt.conf")
    );
    assert_eq!(request.packages()[0], "anacron");
    assert!(request.packages().contains(&"apt".to_owned()));
    assert!(request.packages().contains(&"libiec61850".to_owned()));
    assert!(
        request
            .packages()
            .contains(&"make-initrd-multipath".to_owned())
    );
    assert_eq!(request.selectors(), ["^kernel-(image|modules-())-(rt)$"]);
}

#[test]
fn sudo_user_runner_executes_hasher_commands_as_the_configured_user() {
    let runner = alt_controller_image::hasher::SudoUserRunner::new("hermes");
    let invocation = runner.wrap(Invocation::new("hsh", ["--version"]));

    assert_eq!(
        invocation,
        Invocation::new("sudo", ["-n", "-u", "hermes", "hsh", "--version"])
    );
    let preparation = runner.prepare_workdir(std::path::Path::new("/build/work"));
    assert_eq!(
        preparation,
        Invocation::new(
            "sudo",
            [
                "-n",
                "install",
                "-d",
                "-o",
                "hermes",
                "-g",
                "hermes",
                "/build/work"
            ]
        )
    );
}