blackjack/
error.rs

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
// Copyright 2024 Ole Kliemann
// SPDX-License-Identifier: Apache-2.0

use crate::test_spec::Expr;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("File error: {0}")]
    FileError(#[from] std::io::Error),

    #[error("String error")]
    StringError,

    #[error("Discovery error: {group}/{version} {kind}")]
    DiscoveryError {
        group: String,
        version: String,
        kind: String,
    },

    #[error("Watcher error: {0}")]
    WatcherError(#[from] kube::runtime::watcher::Error),

    #[error("Kube error: {0}")]
    KubeError(#[from] kube::Error),

    #[error("Serialization error: {0}")]
    SerializationYamlError(#[from] serde_yaml::Error),

    #[error("Serialization error: {0}")]
    SerializationJsonError(#[from] serde_json::Error),

    #[error("Multiple errors: {0:?}")]
    MultipleErrors(Vec<Error>),

    #[error("NamespaceExists")]
    NamespaceExists,

    #[error("Tests failed: {0:?}")]
    TestFailures(Vec<TestFailure>),

    #[error("Path encoding error")]
    PathEncodingError,

    #[error("Join error: {0:?}")]
    JoinError(tokio::task::JoinError),

    #[error("SIGINT")]
    SIGINT,

    #[error("Not executed")]
    NotExecuted,

    #[error("No tests found")]
    NoTestsFoundError,

    #[error("No UID?!")]
    NoUidError,

    #[error("Other error: {0}")]
    Other(String),
}

#[derive(Debug, thiserror::Error)]
pub enum TestFailure {
    #[error("Missed wait: {0}")]
    MissedWait(AssertDiagnostic),

    #[error("Failed assert: {0}")]
    FailedAssert(AssertDiagnostic),
}

pub struct FailedTest {
    pub test_name: String,
    pub step_name: String,
    pub failure: Error,
}

pub type SucceededTest = String;

pub type TestResult = std::result::Result<SucceededTest, FailedTest>;

#[derive(Clone, Debug)]
pub struct AssertDiagnostic {
    pub expr: Expr,
    pub input: Vec<serde_json::Value>,
}

impl std::fmt::Display for AssertDiagnostic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "🔴 **Assertion Failed**")?;
        writeln!(f, "Failed Expression: {}", self.expr)?;
        writeln!(f, "Input Data:")?;
        for (i, input) in self.input.iter().enumerate() {
            writeln!(f, "  {}. {}", i + 1, input)?;
        }
        Ok(())
    }
}

pub type Result<T> = std::result::Result<T, Error>;