containerd-shim-wasm 0.5.0

Library for building containerd shims for wasm
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Testing utilities used across different modules

use std::collections::HashMap;
use std::fs::{self, create_dir, read, read_to_string, write, File};
use std::marker::PhantomData;
use std::ops::Add;
use std::time::Duration;

use anyhow::{bail, Result};
pub use containerd_shim_wasm_test_modules as modules;
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};

use crate::sandbox::{Instance, InstanceConfig};
use crate::sys::signals::SIGKILL;

pub const TEST_NAMESPACE: &str = "runwasi-test";

pub struct WasiTestBuilder<WasiInstance: Instance>
where
    WasiInstance::Engine: Default + Send + Sync + Clone,
{
    container_name: String,
    tempdir: tempfile::TempDir,
    _phantom: PhantomData<WasiInstance>,
}

pub struct WasiTest<WasiInstance: Instance>
where
    WasiInstance::Engine: Default + Send + Sync + Clone,
{
    instance: WasiInstance,
    tempdir: tempfile::TempDir,
}

impl<WasiInstance: Instance> WasiTestBuilder<WasiInstance>
where
    WasiInstance::Engine: Default + Send + Sync + Clone,
{
    pub fn new() -> Result<Self> {
        // start logging
        // to enable logging run `export RUST_LOG=trace` and append cargo command with
        // --show-output before running test
        let _ = env_logger::try_init();

        log::info!("creating new wasi test");

        let tempdir = tempfile::tempdir()?;
        let dir = tempdir.path();

        create_dir(dir.join("rootfs"))?;
        let rootdir = dir.join("runwasi");
        create_dir(&rootdir)?;
        let opts = HashMap::from([("root", rootdir)]);
        let opts_file = File::create(dir.join("options.json"))?;
        serde_json::to_writer(opts_file, &opts)?;

        write(dir.join("stdout"), "")?;
        write(dir.join("stderr"), "")?;

        let builder = Self {
            container_name: "test".to_string(),
            tempdir,
            _phantom: Default::default(),
        }
        .with_wasm([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00])?
        .with_start_fn("")?
        .with_stdin("")?;

        Ok(builder)
    }

    pub fn with_start_fn(self, start_fn: impl AsRef<str>) -> Result<Self> {
        let dir = self.tempdir.path();
        let start_fn = start_fn.as_ref();

        log::info!("setting wasi test start_fn to {start_fn:?}");

        let entrypoint = match start_fn {
            "" => "/hello.wasm".to_string(),
            s => "/hello.wasm#".to_string().add(s),
        };
        let spec = SpecBuilder::default()
            .root(RootBuilder::default().path("rootfs").build()?)
            .process(
                ProcessBuilder::default()
                    .cwd("/")
                    .args([entrypoint])
                    .build()?,
            )
            .build()?;

        spec.save(dir.join("config.json"))?;

        Ok(self)
    }

    pub fn with_wasm(self, wasmbytes: impl AsRef<[u8]>) -> Result<Self> {
        let dir = self.tempdir.path();

        log::info!(
            "setting wasi test wasm file [u8; {}]",
            wasmbytes.as_ref().len()
        );

        let wasm_path = dir.join("rootfs").join("hello.wasm");
        write(wasm_path, wasmbytes)?;

        Ok(self)
    }

    pub fn with_stdin(self, stdin: impl AsRef<[u8]>) -> Result<Self> {
        let dir = self.tempdir.path();

        log::info!("setting wasi test stdin to [u8; {}]", stdin.as_ref().len());

        write(dir.join("stdin"), stdin)?;

        Ok(self)
    }

    pub fn as_oci_image(
        mut self,
        image_name: Option<String>,
        container_name: Option<String>,
    ) -> Result<(Self, oci_helpers::OCICleanup)> {
        let image_name = image_name.unwrap_or("localhost/hello:latest".to_string());

        if !oci_helpers::image_exists(&image_name) {
            let wasm_path = self.tempdir.path().join("rootfs").join("hello.wasm");
            let bytes = read(&wasm_path)?;
            let wasm_content = oci_helpers::ImageContent {
                bytes,
                media_type: oci_tar_builder::WASM_LAYER_MEDIA_TYPE.to_string(),
            };
            oci_helpers::import_image(&image_name, &[&wasm_content])?;

            // remove the file from the rootfs so it doesn't get treated like a regular container
            fs::remove_file(&wasm_path)?;
        }

        let container_name = container_name.unwrap_or("test".to_string());
        oci_helpers::create_container(&container_name, &image_name)?;

        self.container_name = container_name.clone();
        Ok((
            self,
            oci_helpers::OCICleanup {
                image_name,
                container_name,
            },
        ))
    }

    pub fn build(self) -> Result<WasiTest<WasiInstance>> {
        let tempdir = self.tempdir;
        let dir = tempdir.path();

        log::info!("building wasi test");

        let mut cfg = InstanceConfig::new(
            WasiInstance::Engine::default(),
            TEST_NAMESPACE,
            "/run/containerd/containerd.sock",
        );
        cfg.set_bundle(dir)
            .set_stdout(dir.join("stdout"))
            .set_stderr(dir.join("stderr"))
            .set_stdin(dir.join("stdin"));

        let instance = WasiInstance::new(self.container_name, Some(&cfg))?;
        Ok(WasiTest { instance, tempdir })
    }
}

impl<WasiInstance: Instance> WasiTest<WasiInstance>
where
    WasiInstance::Engine: Default + Send + Sync + Clone,
{
    pub fn builder() -> Result<WasiTestBuilder<WasiInstance>> {
        WasiTestBuilder::new()
    }

    pub fn instance(&self) -> &WasiInstance {
        &self.instance
    }

    pub fn start(&self) -> Result<&Self> {
        log::info!("starting wasi test");
        self.instance.start()?;
        Ok(self)
    }

    pub fn delete(&self) -> Result<&Self> {
        log::info!("deleting wasi test");
        self.instance.delete()?;
        Ok(self)
    }

    pub fn wait(&self, timeout: Duration) -> Result<(u32, String, String)> {
        let dir = self.tempdir.path();

        log::info!("waiting wasi test");
        let (status, _) = match self.instance.wait_timeout(timeout) {
            Some(res) => res,
            None => {
                self.instance.kill(SIGKILL as u32)?;
                bail!("timeout while waiting for module to finish");
            }
        };

        let stdout = read_to_string(dir.join("stdout"))?;
        let stderr = read_to_string(dir.join("stderr"))?;

        self.instance.delete()?;

        log::info!("wasi test status is {status}");

        Ok((status, stdout, stderr))
    }
}

pub mod oci_helpers {
    use std::fs::{write, File};
    use std::process::{Command, Stdio};
    use std::time::{Duration, Instant};

    use anyhow::{bail, Result};
    use oci_spec::image::{self as spec, Arch};
    use oci_tar_builder::Builder;

    use super::TEST_NAMESPACE;

    pub struct OCICleanup {
        pub image_name: String,
        pub container_name: String,
    }

    impl Drop for OCICleanup {
        fn drop(&mut self) {
            log::debug!("dropping OCIGuard");
            clean_container(self.container_name.clone()).unwrap();
            clean_image(self.image_name.clone()).unwrap();
        }
    }

    pub fn clean_container(container_name: String) -> Result<()> {
        log::debug!("deleting container '{}'", container_name);
        let success = Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("c")
            .arg("rm")
            .arg(container_name)
            .spawn()?
            .wait()?
            .success();

        if !success {
            bail!("failed to clean container")
        }

        Ok(())
    }

    pub fn create_container(container_name: &str, image_name: &str) -> Result<(), anyhow::Error> {
        let success = Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("c")
            .arg("create")
            .arg(image_name)
            .arg(container_name)
            .spawn()?
            .wait()?
            .success();
        if !success {
            bail!(" failed to create container for image");
        }
        Ok(())
    }

    pub struct ImageContent {
        pub bytes: Vec<u8>,
        pub media_type: String,
    }

    pub fn import_image(
        image_name: &str,
        wasm_content: &[&ImageContent],
    ) -> Result<(), anyhow::Error> {
        let tempdir = tempfile::tempdir()?;
        let dir = tempdir.path();

        let mut builder = Builder::default();

        for (i, content) in wasm_content.iter().enumerate() {
            let path = tempdir.path().join(format!("{}.wasm", i));
            write(path.clone(), content.bytes.clone())?;
            builder.add_layer_with_media_type(&path, content.media_type.clone());
        }

        let config = spec::ConfigBuilder::default()
            .entrypoint(vec!["_start".to_string()])
            .build()
            .unwrap();
        let img = spec::ImageConfigurationBuilder::default()
            .config(config)
            .os("wasip1")
            .architecture(Arch::Wasm)
            .rootfs(
                spec::RootFsBuilder::default()
                    .diff_ids(vec![])
                    .build()
                    .unwrap(),
            )
            .build()?;
        builder.add_config(img, image_name.to_string());
        let img_path = dir.join("img.tar");
        let f = File::create(img_path.clone())?;
        builder.build(f)?;

        let success = Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("image")
            .arg("import")
            .arg("--all-platforms")
            .arg(img_path)
            .spawn()?
            .wait()?
            .success();
        if !success {
            // if the container still exists try cleaning it up
            bail!(" failed to import image");
        };
        Ok(())
    }

    pub fn clean_image(image_name: String) -> Result<()> {
        let image_sha = match get_image_sha(&image_name) {
            Ok(sha) => sha,
            Err(_) => return Ok(()), // doesn't exist
        };

        log::debug!("deleting image '{}'", image_name);
        let success = Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("i")
            .arg("rm")
            .arg(image_name)
            .spawn()?
            .wait()?
            .success();

        if !success {
            bail!("failed to clean image");
        }

        // the content is not removed immediately, so we need to wait for it to be removed
        // otherwise some tests will not behave as expected
        wait_for_content_removal(&image_sha)?;

        Ok(())
    }

    pub fn wait_for_content_removal(content_sha: &str) -> Result<(), anyhow::Error> {
        let start = Instant::now();
        let timeout = Duration::from_secs(60);
        log::info!("waiting for content to be removed: {}", &content_sha);
        loop {
            let output = Command::new("ctr")
                .arg("-n")
                .arg(TEST_NAMESPACE)
                .arg("content")
                .arg("get")
                .arg(content_sha)
                .output()?;

            if output.stdout.is_empty() {
                break;
            }

            if start.elapsed() > timeout {
                log::warn!("didn't clean content fully");
                break;
            }
        }
        Ok(())
    }

    fn get_image_sha(image_name: &str) -> Result<String> {
        log::info!("getting image sha for '{}'", image_name);
        let mut grep = Command::new("grep")
            .arg(image_name)
            .stdout(Stdio::piped())
            .stdin(Stdio::piped())
            .spawn()?;

        Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("i")
            .arg("ls")
            .stdout(grep.stdin.take().unwrap())
            .spawn()?;

        let output = grep.wait_with_output()?;
        let stdout = String::from_utf8(output.stdout)?;
        log::warn!("stdout: {}", stdout);

        let parts: Vec<&str> = stdout.trim().split(' ').collect();
        if parts.len() < 3 {
            bail!("failed to get image sha");
        }
        let sha = parts[2];
        log::warn!("sha: {}", sha);
        Ok(sha.to_string())
    }

    pub fn get_image_label() -> Result<(String, String)> {
        let mut grep = Command::new("grep")
            .arg("-ohE")
            .arg("runwasi.io/precompiled/.*")
            .stdout(Stdio::piped())
            .stdin(Stdio::piped())
            .spawn()?;

        Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("i")
            .arg("ls")
            .stdout(grep.stdin.take().unwrap())
            .spawn()?;

        let output = grep.wait_with_output()?;
        let stdout = String::from_utf8(output.stdout)?;
        log::debug!("stdout: {}", stdout);
        let label: Vec<&str> = stdout.split('=').collect();

        Ok((
            label.first().unwrap().trim().to_string(),
            label.last().unwrap().trim().to_string(),
        ))
    }

    pub fn image_exists(image_name: &str) -> bool {
        let output = Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("i")
            .arg("ls")
            .arg("--quiet")
            .output()
            .expect("failed to get output of image list");

        let stdout = String::from_utf8(output.stdout).expect("failed to parse stdout");
        stdout.contains(image_name)
    }

    pub fn get_content_label() -> Result<(String, String)> {
        let mut grep = Command::new("grep")
            .arg("-ohE")
            .arg("runwasi.io/precompiled/[[:alpha:]]*/[0-9]+=.*")
            .stdout(Stdio::piped())
            .stdin(Stdio::piped())
            .spawn()?;

        Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("content")
            .arg("ls")
            .stdout(grep.stdin.take().unwrap())
            .spawn()?;

        let output = grep.wait_with_output()?;

        let stdout = String::from_utf8(output.stdout)?;

        log::debug!("stdout: {}", stdout);

        let label: Vec<&str> = stdout.split('=').collect();

        Ok((
            label.first().unwrap().trim().to_string(),
            label.last().unwrap().trim().to_string(),
        ))
    }

    pub fn remove_content(digest: String) -> Result<()> {
        log::debug!("cleaning content '{}'", digest);
        let success = Command::new("ctr")
            .arg("-n")
            .arg(TEST_NAMESPACE)
            .arg("content")
            .arg("rm")
            .arg(digest)
            .spawn()?
            .wait()?
            .success();

        if !success {
            bail!("failed to remove content");
        }

        Ok(())
    }
}