libcrio 2.1.0

A wrapper around the crictl cli to return serde_json objects
Documentation
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
use log::debug;
use serde::Serialize;
use serde_json::Value;
use std::io::prelude::*;
use std::process::Command;
use std::process::Stdio;
use std::str::FromStr;

/// A CLI wrapper object
#[derive(Debug, Serialize, PartialEq, Clone)]
pub struct Cli {
    /// The bin_path to find the crio_cli required as the host process may not have this preconfigured.
    /// Usually set to "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/home/kubernetes/bin"
    /// If you are deploying crictl on the host you may want to append that location as well.
    pub bin_path: String,
    /// The location of the crictl.yaml
    pub config_path: Option<String>,
    /// The command for listing images. If not supplied it will default to 'img'
    pub image_command: ImageCommand,
}

/// A switch to indicate which image command to run
#[derive(Debug, Serialize, PartialEq, Clone)]
pub enum ImageCommand {
    Img,
    Images,
}

impl fmt::Display for ImageCommand {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(LowercaseFormatter(formatter), "{:?}", self)
    }
}

impl FromStr for ImageCommand {
    type Err = ();

    fn from_str(input: &str) -> Result<ImageCommand, Self::Err> {
        match input.to_lowercase().as_str() {
            "img" => Ok(ImageCommand::Img),
            "images" => Ok(ImageCommand::Images),
            _ => Err(()),
        }
    }
}

use std::fmt::{self, Write};

struct LowercaseFormatter<'a, 'b>(pub &'a mut fmt::Formatter<'b>);

impl<'a, 'b> fmt::Write for LowercaseFormatter<'a, 'b> {
    fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
        for ch in s.chars() {
            self.0.write_fmt(format_args!("{}", ch.to_lowercase()))?;
        }

        Ok(())
    }
}

/// Returns a defauilt instance of `Cli` with
/// bin_path`: "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/home/kubernetes/bin"
/// config_path`: None,
/// image_command` `ImageCommand::Img`
impl Default for Cli {
    fn default() -> Cli {
        Cli {
            bin_path: "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/home/kubernetes/bin"
                .to_string(),
            config_path: None,
            image_command: ImageCommand::Img,
        }
    }
}

impl Cli {
    /// Returns a JSON value containing the pod information
    ///
    /// # Arguments
    ///
    /// * `hostname` - The hostname of the pod
    ///
    /// # Examples
    ///
    /// ```
    /// use libcrio::Cli;
    /// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
    /// let cli = Cli {
    ///     bin_path,
    ///     ..Default::default()
    /// };
    /// let val = cli.pod("tests").unwrap();
    /// ```
    pub fn pod(&self, hostname: &str) -> Result<Value, String> {
        let pod_output_args = match &self.config_path {
            Some(s) => {
                vec!["-c", s.as_str(), "pods", "--name", hostname, "-o", "json"]
            }
            None => {
                vec!["pods", "--name", hostname, "-o", "json"]
            }
        };

        let pod_list = run_command(pod_output_args, &self.bin_path)?;
        let pod = match pod_list["items"].get(0) {
            Some(s) => s,
            None => {
                return Err("failed to create pod at index 0".to_string());
            }
        };
        Ok(pod.clone())
    }

    /// Returns a JSON value containing the pod inpection output
    ///
    /// # Arguments
    ///
    /// * `pod_id` - The id of the pod
    ///
    /// # Examples
    ///
    /// ```
    /// use libcrio::Cli;
    /// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
    /// let cli = Cli {
    ///     bin_path,
    ///     ..Default::default()
    /// };
    /// let val = cli.inspect_pod("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6").unwrap();
    /// ```
    pub fn inspect_pod(&self, pod_id: &str) -> Result<Value, String> {
        let inspect_output_args = match &self.config_path {
            Some(s) => vec!["-c", s.as_str(), "inspectp", pod_id],
            None => vec!["inspectp", pod_id],
        };
        run_command(inspect_output_args, &self.bin_path)
    }

    /// Returns a JSON value containing the containers related to a pod
    ///
    /// # Arguments
    ///
    /// * `pod_id` - The id of the pod
    ///
    /// # Examples
    ///
    /// ```
    /// use libcrio::Cli;
    /// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
    /// let cli = Cli {
    ///     bin_path,
    ///     ..Default::default()
    /// };
    /// let val = cli.pod_containers("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6").unwrap();
    /// ```
    pub fn pod_containers(&self, pod_id: &str) -> Result<Value, String> {
        let ps_output_args = match &self.config_path {
            Some(s) => vec!["-c", s.as_str(), "ps", "-o", "json", "-p", pod_id],
            None => vec!["ps", "-o", "json", "-p", pod_id],
        };
        run_command(ps_output_args, &self.bin_path)
    }

    /// Returns a JSON value containing the container inpection output
    ///
    /// # Arguments
    ///
    /// * `container_id` - The id of the container
    ///
    /// # Examples
    ///
    /// ```
    /// use libcrio::Cli;
    /// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
    /// let cli = Cli {
    ///     bin_path,
    ///     ..Default::default()
    /// };
    /// let val = cli.inspect_container("765312810c818bca4836c3598e21471bfd96be8ca84ca952290a9900b7c055a7").unwrap();
    /// ```
    pub fn inspect_container(&self, container_id: &str) -> Result<Value, String> {
        let inspect_output_args = match &self.config_path {
            Some(s) => vec!["-c", s.as_str(), "inspect", container_id],
            None => vec!["inspect", container_id],
        };
        run_command(inspect_output_args, &self.bin_path)
    }

    /// Returns a JSON value containing the images related to a container
    ///
    /// # Arguments
    ///
    /// * `image_ref` - The image reference related to one of the containers obtained from `pod_containers`
    ///
    /// # Examples
    ///
    /// ```
    /// use libcrio::Cli;
    /// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
    /// let cli = Cli {
    ///     bin_path,
    ///     ..Default::default()
    /// };
    /// let val = cli.image("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa").unwrap();
    /// ```
    pub fn image(&self, image_ref: &str) -> Result<Value, String> {
        let img_cmd_string = format!("{}", &self.image_command);
        let img_cmd = img_cmd_string.as_str();

        let image_output_args = match &self.config_path {
            Some(s) => vec!["-c", s.as_str(), img_cmd, "-o", "json"],
            None => vec![img_cmd, "-o", "json"],
        };
        let log_args = image_output_args.clone();
        let image_list = run_command(image_output_args, &self.bin_path)?;
        match image_list["images"].as_array() {
            Some(img_lines) => {
                debug!("Found {} images", img_lines.len());
                for line in img_lines {
                    let line_obj: Value = serde_json::to_value(line).unwrap();
                    let line_obj_id = line_obj["id"].as_str().unwrap_or_default();

                    debug!("Matching {} using {}", line_obj_id, image_ref);
                    if line_obj_id == image_ref {
                        debug!("MATCHED {} using {}", line_obj_id, image_ref);
                        return Ok(line_obj.clone());
                    } else if let Some(arr) = line_obj["repoDigests"].as_array() {
                        debug!("Matching inspecting repoDigests \n{:?}", arr);
                        for digest in arr {
                            let digest_str = digest.as_str().unwrap_or_default();
                            debug!("Matching repoDigests {} to {}", digest_str, image_ref);
                            if digest_str == image_ref {
                                debug!("MATCHED {} to {}", line_obj_id, image_ref);
                                return Ok(line_obj.clone());
                            }
                        }
                    }
                }
                Err(format!("no images matched in crictl img {:?}", log_args))
            }
            None => Err(format!("no images found in crictl img {:?}", log_args)),
        }
    }

    /// Returns a text value containing the logs related to a container
    ///
    /// # Arguments
    ///
    /// * `container_id` - The container_id related to one of the containers obtained from `pod_containers`
    ///
    /// # Examples
    ///
    /// ```
    /// use libcrio::Cli;
    /// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
    /// let cli = Cli {
    ///     bin_path,
    ///     ..Default::default()
    /// };
    /// #[allow(deprecated)]
    /// let val = cli.logs("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa").unwrap();
    /// ```
    #[deprecated]
    pub fn logs(&self, container_id: &str) -> Result<String, String> {
        let log_output_args = match &self.config_path {
            Some(s) => vec!["-c", s.as_str(), "logs", container_id],
            None => vec!["logs", container_id],
        };
        run_command_text(log_output_args, &self.bin_path)
    }

    /// Returns a text value containing the logs related to a container
    ///
    /// # Arguments
    ///
    /// * `container_id` - The container_id related to one of the containers obtained from `pod_containers`
    ///
    /// * `line_count` - The number of lines to take from the end of the log.
    ///
    /// # Examples
    ///
    /// ```
    /// use libcrio::Cli;
    /// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
    /// let cli = Cli {
    ///     bin_path,
    ///     ..Default::default()
    /// };
    /// let val = cli.tail_logs("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa", 500).unwrap();
    /// ```
    pub fn tail_logs(&self, container_id: &str, line_count: u32) -> Result<String, String> {
        let tailoption = format!("--tail={}", line_count);
        let log_output_args = match &self.config_path {
            Some(s) => vec!["-c", s.as_str(), "logs", tailoption.as_str(), container_id],
            None => vec!["logs", tailoption.as_str(), container_id],
        };
        run_command_text(log_output_args, &self.bin_path)
    }

    /// # Arguments
    ///
    /// * `path` - The additional path to append to bin_path,
    ///
    /// # Examples
    ///
    /// ```
    /// use libcrio::Cli;
    /// let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
    /// let mut cli = Cli {
    ///     bin_path,
    ///     ..Default::default()
    /// };
    /// cli.append_bin_path("/my/new/location".to_string());
    /// ```
    pub fn append_bin_path(&mut self, path: String) {
        let internal = if !path.starts_with(':') {
            format!(":{}", path)
        } else {
            path
        };
        self.bin_path.push_str(internal.as_str());
    }
}

fn slice_to_value(slice: &[u8], args: Vec<&str>) -> Result<Value, String> {
    match serde_json::from_slice(slice) {
        Ok(v) => Ok(v),
        Err(e) => Err(format!(
            "failed to create output from slice for {:?} {}",
            args, e
        )),
    }
}

fn run_command_text(args: Vec<&str>, bin_path: &str) -> Result<String, String> {
    debug!("running {:?} {:?}", args, bin_path);
    let cmd = match Command::new("crictl")
        .env("PATH", bin_path)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .args(&args)
        .spawn()
    {
        Ok(v) => v,
        Err(e) => {
            return Err(format!("failed to execute crictl {:?} {}", args, e));
        }
    };
    let waiter = match cmd.wait_with_output() {
        Ok(v) => v,
        Err(e) => {
            return Err(format!("failed to execute crictl {:?} {}", args, e));
        }
    };

    let mut err_str = String::new();
    match waiter.stderr.as_slice().read_to_string(&mut err_str) {
        Err(e) => {
            return Err(format!(
                "stderr read error - failed to execute crictl {:?} {}",
                args, e
            ));
        }
        Ok(_) => {
            if !err_str.is_empty() {
                return Err(format!(
                    "stderr not empty - failed to execute crictl {:?} {}",
                    args, err_str
                ));
            }
        }
    }

    // if !waiter.success() {
    //     return Err(format!(
    //         "crictl status is unsuccessful {:?}, {}",
    //         args, waiter
    //     ));
    // }
    let mut ok_str = String::new();
    match waiter.stdout.as_slice().read_to_string(&mut ok_str) {
        Err(e) => Err(format!(
            "stdout error - failed to execute crictl {:?} {}",
            args, e
        )),
        Ok(_) => Ok(ok_str),
    }
}

fn run_command(args: Vec<&str>, bin_path: &str) -> Result<Value, String> {
    let l_args = args.clone();
    let str_ok = run_command_text(args, bin_path)?;
    slice_to_value(str_ok.as_bytes(), l_args)
}

#[cfg(test)]
mod tests {
    use crate::{Cli, ImageCommand};
    use std::str::FromStr;

    pub fn get_clis() -> Vec<Cli> {
        let mut test_cases: Vec<Cli> = vec![];
        let bin_path = format!("{}/mock/iks", env!("CARGO_MANIFEST_DIR"));
        test_cases.push(Cli {
            bin_path,
            config_path: None,
            image_command: ImageCommand::Img,
        });
        test_cases
    }

    pub fn get_big_data_cli() -> Cli {
        let bin_path = format!("{}/mock/big_data", env!("CARGO_MANIFEST_DIR"));
        Cli {
            bin_path,
            config_path: None,
            image_command: ImageCommand::Img,
        }
    }

    pub fn get_only_errors_cli() -> Cli {
        let bin_path = format!("{}/mock/only_errors", env!("CARGO_MANIFEST_DIR"));
        Cli {
            bin_path,
            config_path: None,
            image_command: ImageCommand::Img,
        }
    }

    pub fn get_long_logs_cli() -> Cli {
        let bin_path = format!("{}/mock/long_logs:/usr/bin", env!("CARGO_MANIFEST_DIR"));
        Cli {
            bin_path,
            config_path: None,
            image_command: ImageCommand::Img,
        }
    }

    pub fn get_mixed_errors_cli() -> Cli {
        let bin_path = format!("{}/mock/mixed_errors", env!("CARGO_MANIFEST_DIR"));
        Cli {
            bin_path,
            config_path: None,
            image_command: ImageCommand::Img,
        }
    }
    pub fn get_bad_json_cli() -> Cli {
        let bin_path = format!("{}/mock/bad_json", env!("CARGO_MANIFEST_DIR"));
        Cli {
            bin_path,
            config_path: None,
            image_command: ImageCommand::Img,
        }
    }
    pub fn get_openshift_cli() -> Cli {
        let bin_path = format!("{}/mock/openshift", env!("CARGO_MANIFEST_DIR"));
        Cli {
            bin_path,
            config_path: None,
            image_command: ImageCommand::Img,
        }
    }

    #[test]
    fn test_append_bin_path() {
        let mut cli = Cli::default();
        let path = "/my/path".to_string();
        cli.append_bin_path(path);
        assert_eq!(
            cli.bin_path,
            "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/home/kubernetes/bin:/my/path"
                .to_string(),
        );

        let path2 = ":/my/path2".to_string();
        cli.append_bin_path(path2);
        assert_eq!(
            cli.bin_path,
            "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/home/kubernetes/bin:/my/path:/my/path2"
                .to_string(),
        );
    }

    /*************************************************************************
     * pod Tests
     **************************************************************************/
    #[test]
    fn test_pod_returns_a_pod_openshift() {
        let cli = get_openshift_cli();
        let val = cli.pod("tests").unwrap();
        assert_eq!(
            val["id"].as_str().unwrap(),
            "134b58ab2e0cfd7432a9db818b1b4ec52fdc747333f0ba2c9342860dc2ea7c50"
        );
    }

    #[test]
    fn test_pod_returns_a_pod() {
        for cli in get_clis() {
            let val = cli.pod("tests").unwrap();
            assert_eq!(
                val["id"].as_str().unwrap(),
                "51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6"
            );
        }
    }
    #[test]
    fn test_pod_returns_a_pod_only_errors_cli() {
        let cli = get_only_errors_cli();
        let val = cli.pod("tests");
        let expected = Err(String::from(
            "failed to create output from slice for [\"pods\", \"--name\", \"tests\", \"-o\", \"json\"] EOF while parsing a value at line 2 column 0",
        ));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_pod_returns_a_pod_mixed_errors_cli() {
        let cli = get_mixed_errors_cli();
        let val = cli.pod("tests");
        let expected = Err(String::from("stderr not empty - failed to execute crictl [\"pods\", \"--name\", \"tests\", \"-o\", \"json\"] An error message\n"));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_pod_returns_a_pod_bad_json_cli() {
        let cli = get_bad_json_cli();
        let val = cli.pod("tests");
        let expected = Err(String::from("failed to create output from slice for [\"pods\", \"--name\", \"tests\", \"-o\", \"json\"] EOF while parsing a value at line 2 column 0"));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_get_big_data() {
        let cli = get_big_data_cli();
        let val = cli.tail_logs("", 0).unwrap();
        let mut expected = String::from("");
        for _f in 0..65536 {
            expected.push('a');
        }
        expected.push('\n');
        assert_eq!(expected, val);
    }
    /*************************************************************************
     * inspect tests
     **************************************************************************/
    #[test]
    fn test_inspect_pod() {
        for cli in get_clis() {
            let val = cli
                .inspect_pod("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6")
                .unwrap();
            assert_eq!(val["info"]["pid"].as_i64().unwrap(), 14017)
        }
    }
    #[test]
    fn test_inspect_pod_openshift() {
        let cli = get_openshift_cli();
        let val = cli
            .inspect_pod("134b58ab2e0cfd7432a9db818b1b4ec52fdc747333f0ba2c9342860dc2ea7c50")
            .unwrap();
        assert_eq!(val["info"]["pid"].as_i64().unwrap(), 38091)
    }
    #[test]
    fn test_inspect_returns_a_pod_mixed_errors_cli() {
        let cli = get_mixed_errors_cli();
        let val = cli.inspect_pod("tests");
        let expected = Err(String::from(
            "stderr not empty - failed to execute crictl [\"inspectp\", \"tests\"] An error message\n",
        ));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_inspect_pod_only_errors_cli() {
        let cli = get_only_errors_cli();
        let val =
            cli.inspect_pod("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
        let expected = Err(String::from("failed to create output from slice for [\"inspectp\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] EOF while parsing a value at line 2 column 0"));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_inspect_pod_bad_json_cli() {
        let cli = get_bad_json_cli();
        let val =
            cli.inspect_pod("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
        let expected = Err(String::from("failed to create output from slice for [\"inspectp\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] EOF while parsing a value at line 2 column 0"));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_inspect_container() {
        for cli in get_clis() {
            let val = cli
                .inspect_container(
                    "765312810c818bca4836c3598e21471bfd96be8ca84ca952290a9900b7c055a7",
                )
                .unwrap();
            assert_eq!(val["info"]["pid"].as_i64().unwrap(), 254405)
        }
    }
    #[test]
    fn test_inspect_returns_a_container_mixed_errors_cli() {
        let cli = get_mixed_errors_cli();
        let val = cli.inspect_container("tests");
        let expected = Err(String::from(
            "stderr not empty - failed to execute crictl [\"inspect\", \"tests\"] An error message\n",
        ));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_inspect_container_only_errors_cli() {
        let cli = get_only_errors_cli();
        let val = cli
            .inspect_container("765312810c818bca4836c3598e21471bfd96be8ca84ca952290a9900b7c055a7");
        let expected = Err(String::from("failed to create output from slice for [\"inspect\", \"765312810c818bca4836c3598e21471bfd96be8ca84ca952290a9900b7c055a7\"] EOF while parsing a value at line 2 column 0"));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_inspect_container_bad_json_cli() {
        let cli = get_bad_json_cli();
        let val = cli
            .inspect_container("765312810c818bca4836c3598e21471bfd96be8ca84ca952290a9900b7c055a7");
        let expected = Err(String::from("failed to create output from slice for [\"inspect\", \"765312810c818bca4836c3598e21471bfd96be8ca84ca952290a9900b7c055a7\"] EOF while parsing a value at line 2 column 0"));
        assert_eq!(expected, val);
    }

    /*************************************************************************
     * pod containers tests
     **************************************************************************/
    #[test]
    fn test_pod_containers() {
        for cli in get_clis() {
            let val = cli
                .pod_containers("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6")
                .unwrap();
            assert_eq!(
                val["containers"][0]["id"].as_str().unwrap(),
                "4bd48d7c6a03cd94a0e95e97011ed5d2ca72045723a5ed55da06fd54eff32b0a"
            )
        }
    }
    #[test]
    fn test_pod_containers_openshift() {
        let cli = get_openshift_cli();
        let val = cli
            .pod_containers("134b58ab2e0cfd7432a9db818b1b4ec52fdc747333f0ba2c9342860dc2ea7c50")
            .unwrap();
        assert_eq!(
            val["containers"][0]["id"].as_str().unwrap(),
            "0e04af54d9273f5bb37eddbe8ace750275d7939612dd4864c792168cce2cff82"
        )
    }
    #[test]
    fn test_pod_containers_only_errors_cli() {
        let cli = get_only_errors_cli();
        let val =
            cli.pod_containers("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
        let expected = Err(String::from("failed to create output from slice for [\"ps\", \"-o\", \"json\", \"-p\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] EOF while parsing a value at line 2 column 0"));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_pod_containers_bad_json_cli() {
        let cli = get_bad_json_cli();
        let val =
            cli.pod_containers("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
        let expected = Err(String::from("failed to create output from slice for [\"ps\", \"-o\", \"json\", \"-p\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] EOF while parsing a value at line 2 column 0"));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_pod_containers_mixed_errors_cli() {
        let cli = get_mixed_errors_cli();
        let val =
            cli.pod_containers("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
        let expected = Err(String::from(
            "stderr not empty - failed to execute crictl [\"ps\", \"-o\", \"json\", \"-p\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] An error message\n",
        ));
        assert_eq!(expected, val);
    }

    /*************************************************************************
     * image tests
     **************************************************************************/
    #[test]
    fn test_image() {
        for cli in get_clis() {
            let val = cli
                .image("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa")
                .unwrap();
            assert_eq!(val["size"].as_str().unwrap(), "338054458")
        }
    }
    #[test]
    fn test_image_openshift() {
        let cli = get_openshift_cli();
        let val = cli
            .image("quay.io/icdh/segfaulter@sha256:0630afbcfebb45059794b9a9f160f57f50062d28351c49bb568a3f7e206855bd")
            .unwrap();
        assert_eq!(val["size"].as_str().unwrap(), "10229047")
    }
    #[test]
    fn test_images_only_errors_cli() {
        let cli = get_only_errors_cli();
        let val =
            cli.image("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa");
        let expected = Err(String::from(
            "failed to create output from slice for [\"img\", \"-o\", \"json\"] EOF while parsing a value at line 2 column 0",
        ));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_json_errors_cli() {
        let cli = get_bad_json_cli();
        let val =
            cli.image("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa");
        let expected = Err(String::from("failed to create output from slice for [\"img\", \"-o\", \"json\"] EOF while parsing a value at line 2 column 0"));
        assert_eq!(expected, val);
    }

    #[test]
    fn test_image_mixed_errors_cli() {
        let cli = get_mixed_errors_cli();
        let val =
            cli.image("sha256:3b8adc6c30f4e7e4afb57daef9d1c8af783a4a647a4670780e9df085c0525efa");
        let expected = Err(String::from(
            "stderr not empty - failed to execute crictl [\"img\", \"-o\", \"json\"] An error message\n",
        ));
        assert_eq!(expected, val);
    }
    /*************************************************************************
     * log tests
     **************************************************************************/
    #[allow(deprecated)]
    #[test]
    fn test_logs() {
        for cli in get_clis() {
            let val = cli
                .logs("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6")
                .unwrap();
            assert_eq!(val, "A LOG\n".to_string())
        }
    }
    #[allow(deprecated)]
    #[test]
    fn test_logs_mixed_errors_cli() {
        let cli = get_mixed_errors_cli();
        let val = cli.logs("51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6");
        let expected = Err(String::from(
             "stderr not empty - failed to execute crictl [\"logs\", \"51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6\"] An error message\n",
         ));
        assert_eq!(expected, val);
    }
    #[test]
    fn test_tail_logs() {
        let cli = get_long_logs_cli();
        let val = cli
            .tail_logs(
                "51cd8bdaa13a65518e790d307359d33f9288fc82664879c609029b1a83862db6",
                500,
            )
            .unwrap();
        assert_eq!(val.lines().count(), 500);
        assert!(val.ends_with("logging 500\n"));
        assert!(!val.contains("logging 501"));
    }

    #[test]
    fn test_image_cmd_from_str() {
        assert_eq!(
            ImageCommand::Images,
            ImageCommand::from_str("IMAGES").unwrap()
        );
        assert_eq!(ImageCommand::Img, ImageCommand::from_str("imG").unwrap());

        let actual_error_kind = ImageCommand::from_str("ADSF").unwrap_err();
        assert_eq!((), actual_error_kind);

        let cl = ImageCommand::Img;
        assert_eq!(cl.clone(), ImageCommand::Img);
    }
}