forked from krustlet/krustlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.rs
More file actions
171 lines (150 loc) · 4.21 KB
/
assert.rs
File metadata and controls
171 lines (150 loc) · 4.21 KB
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
use futures::TryStreamExt;
use k8s_openapi::api::core::v1::Pod;
use kube::api::{Api, LogParams};
pub async fn pod_log_equals(
pods: &Api<Pod>,
pod_name: &str,
expected_log: &str,
) -> anyhow::Result<()> {
let mut logs = pods.log_stream(pod_name, &LogParams::default()).await?;
while let Some(chunk) = logs.try_next().await? {
assert_eq!(expected_log, String::from_utf8_lossy(&chunk));
}
Ok(())
}
pub async fn pod_log_contains(
pods: &Api<Pod>,
pod_name: &str,
expected_log: &str,
) -> anyhow::Result<()> {
let logs = pods.logs(pod_name, &LogParams::default()).await?;
assert!(
logs.contains(expected_log),
"Expected log containing {} but got {}",
expected_log,
logs,
);
Ok(())
}
pub async fn pod_container_log_contains(
pods: &Api<Pod>,
pod_name: &str,
container_name: &str,
expected_log: &str,
) -> anyhow::Result<()> {
let log_params = LogParams {
container: Some(container_name.to_owned()),
..Default::default()
};
let logs = pods.logs(pod_name, &log_params).await?;
assert!(
logs.contains(expected_log),
"Expected log containing {} but got {}",
expected_log,
logs
);
Ok(())
}
pub async fn pod_exited_successfully(pods: &Api<Pod>, pod_name: &str) -> anyhow::Result<()> {
let pod = pods.get(pod_name).await?;
let state = (|| {
let _ = &pod;
pod.status?.container_statuses?[0]
.state
.as_ref()?
.terminated
.clone()
})()
.expect("Could not fetch terminated states");
if state.exit_code != 0 {
try_dump_pod_logs(pods, pod_name).await;
assert_eq!(state.exit_code, 0);
}
Ok(())
}
pub async fn pod_exited_with_failure(pods: &Api<Pod>, pod_name: &str) -> anyhow::Result<()> {
let pod = pods.get(pod_name).await?;
let phase = (|| {
let _ = &pod;
pod.status?.phase
})()
.expect("Could not get pod phase");
assert_eq!(phase, "Failed");
Ok(())
}
pub async fn pod_reason_contains(
pods: &Api<Pod>,
pod_name: &str,
expected_message: &str,
) -> anyhow::Result<()> {
let pod = pods.get(pod_name).await?;
let message = (|| {
let _ = &pod;
pod.status?.reason
})()
.expect("Could not get pod message.");
assert!(
message.contains(expected_message),
"Expected pod message containing {} but got {}",
expected_message,
message,
);
Ok(())
}
pub async fn main_container_exited_with_failure(
pods: &Api<Pod>,
pod_name: &str,
) -> anyhow::Result<()> {
let pod = pods.get(pod_name).await?;
let state = (|| {
let _ = &pod;
pod.status?.container_statuses?[0]
.state
.as_ref()?
.terminated
.clone()
})()
.expect("Could not fetch terminated states");
assert_eq!(state.exit_code, 1);
Ok(())
}
pub async fn container_file_contains(
pod_name: &str,
pod_namespace: &str,
container_file_path: &str,
expected_content: &str,
file_error: &str,
) -> anyhow::Result<()> {
let pod_dir_name = format!("{}-{}", pod_name, pod_namespace);
let file_path_base = data_dir().join("volumes").join(pod_dir_name);
let container_file_bytes = tokio::fs::read(file_path_base.join(container_file_path))
.await
.expect(file_error);
assert_eq!(
expected_content.to_owned().into_bytes(),
container_file_bytes
);
Ok(())
}
fn data_dir() -> std::path::PathBuf {
match std::env::var("KRUSTLET_DATA_DIR") {
Ok(data_dir) => std::path::PathBuf::from(data_dir),
_ => {
let home_dir = dirs::home_dir().expect("Can't get home dir");
home_dir.join(".krustlet")
}
}
}
pub async fn try_dump_pod_logs(pods: &Api<Pod>, pod_name: &str) {
let logs = pods.logs(pod_name, &LogParams::default()).await;
match logs {
Err(e) => {
println!("Unable to dump logs for {}: {}", pod_name, e);
}
Ok(content) => {
println!("--- BEGIN LOGS for pod {}", pod_name);
println!("{}", content);
println!("--- END LOGS for pod {}", pod_name);
}
}
}