-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogout.rs
More file actions
401 lines (338 loc) · 10.9 KB
/
logout.rs
File metadata and controls
401 lines (338 loc) · 10.9 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
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
//! Docker logout command implementation
//!
//! This module provides functionality to log out from Docker registries.
//! It supports logging out from specific registries or using the daemon default.
use super::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
use std::fmt;
/// Command for logging out from Docker registries
///
/// The `LogoutCommand` provides a builder pattern for constructing Docker logout commands
/// to remove stored authentication credentials for Docker registries.
///
/// # Examples
///
/// ```rust
/// use docker_wrapper::LogoutCommand;
///
/// // Logout from default registry (daemon-defined)
/// let logout = LogoutCommand::new();
///
/// // Logout from specific registry
/// let logout = LogoutCommand::new()
/// .server("my-registry.com");
/// ```
#[derive(Debug, Clone)]
pub struct LogoutCommand {
/// Registry server URL (None for daemon default)
server: Option<String>,
/// Command executor for running the command
pub executor: CommandExecutor,
}
/// Output from a logout command execution
///
/// Contains the raw output from the Docker logout command and provides
/// convenience methods for checking logout status.
#[derive(Debug, Clone)]
pub struct LogoutOutput {
/// Raw output from the Docker command
pub output: CommandOutput,
}
impl LogoutCommand {
/// Creates a new logout command
///
/// By default, logs out from the daemon-defined default registry
///
/// # Examples
///
/// ```rust
/// use docker_wrapper::LogoutCommand;
///
/// let logout = LogoutCommand::new();
/// ```
#[must_use]
pub fn new() -> Self {
Self {
server: None,
executor: CommandExecutor::default(),
}
}
/// Sets the registry server to logout from
///
/// If not specified, uses the daemon-defined default registry
///
/// # Arguments
///
/// * `server` - The registry server URL
///
/// # Examples
///
/// ```rust
/// use docker_wrapper::LogoutCommand;
///
/// let logout = LogoutCommand::new()
/// .server("gcr.io");
/// ```
#[must_use]
pub fn server(mut self, server: impl Into<String>) -> Self {
self.server = Some(server.into());
self
}
/// Sets a custom command executor
///
/// # Arguments
///
/// * `executor` - Custom command executor
#[must_use]
pub fn executor(mut self, executor: CommandExecutor) -> Self {
self.executor = executor;
self
}
/// Gets the server (if set)
#[must_use]
pub fn get_server(&self) -> Option<&str> {
self.server.as_deref()
}
/// Get a reference to the command executor
#[must_use]
pub fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
/// Get a mutable reference to the command executor
#[must_use]
pub fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
}
impl Default for LogoutCommand {
fn default() -> Self {
Self::new()
}
}
impl LogoutOutput {
/// Returns true if the logout was successful
#[must_use]
pub fn success(&self) -> bool {
self.output.success
}
/// Returns true if the output indicates successful logout
#[must_use]
pub fn is_logged_out(&self) -> bool {
self.success()
&& (self.output.stdout.contains("Removing login credentials")
|| self.output.stdout.contains("Not logged in")
|| self.output.stdout.is_empty() && self.output.stderr.is_empty())
}
/// Gets any warning messages from the logout output
#[must_use]
pub fn warnings(&self) -> Vec<&str> {
self.output
.stderr
.lines()
.filter(|line| line.to_lowercase().contains("warning"))
.collect()
}
/// Gets any info messages from the logout output
#[must_use]
pub fn info_messages(&self) -> Vec<&str> {
self.output
.stdout
.lines()
.filter(|line| !line.trim().is_empty())
.collect()
}
}
#[async_trait]
impl DockerCommand for LogoutCommand {
type Output = LogoutOutput;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["logout".to_string()];
// Add server if specified
if let Some(ref server) = self.server {
args.push(server.clone());
}
// Add raw args from executor
args.extend(self.executor.raw_args.clone());
args
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_command_args();
let output = self.execute_command(args).await?;
Ok(LogoutOutput { output })
}
}
impl fmt::Display for LogoutCommand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "docker logout")?;
if let Some(ref server) = self.server {
write!(f, " {server}")?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_logout_command_basic() {
let logout = LogoutCommand::new();
assert_eq!(logout.get_server(), None);
let args = logout.build_command_args();
assert_eq!(args, vec!["logout"]);
}
#[test]
fn test_logout_command_with_server() {
let logout = LogoutCommand::new().server("gcr.io");
assert_eq!(logout.get_server(), Some("gcr.io"));
let args = logout.build_command_args();
assert_eq!(args, vec!["logout", "gcr.io"]);
}
#[test]
fn test_logout_command_with_private_registry() {
let logout = LogoutCommand::new().server("my-registry.example.com:5000");
let args = logout.build_command_args();
assert_eq!(args, vec!["logout", "my-registry.example.com:5000"]);
}
#[test]
fn test_logout_command_daemon_default() {
let logout = LogoutCommand::new();
// No server specified should use daemon default
assert_eq!(logout.get_server(), None);
let args = logout.build_command_args();
assert_eq!(args, vec!["logout"]);
}
#[test]
fn test_logout_command_display() {
let logout = LogoutCommand::new().server("example.com");
let display = format!("{logout}");
assert_eq!(display, "docker logout example.com");
}
#[test]
fn test_logout_command_display_no_server() {
let logout = LogoutCommand::new();
let display = format!("{logout}");
assert_eq!(display, "docker logout");
}
#[test]
fn test_logout_command_default() {
let logout = LogoutCommand::default();
assert_eq!(logout.get_server(), None);
let args = logout.build_command_args();
assert_eq!(args, vec!["logout"]);
}
#[test]
fn test_logout_output_success_with_credentials_removal() {
let output = CommandOutput {
stdout: "Removing login credentials for https://index.docker.io/v1/".to_string(),
stderr: String::new(),
exit_code: 0,
success: true,
};
let logout_output = LogoutOutput { output };
assert!(logout_output.success());
assert!(logout_output.is_logged_out());
}
#[test]
fn test_logout_output_success_not_logged_in() {
let output = CommandOutput {
stdout: "Not logged in to https://index.docker.io/v1/".to_string(),
stderr: String::new(),
exit_code: 0,
success: true,
};
let logout_output = LogoutOutput { output };
assert!(logout_output.success());
assert!(logout_output.is_logged_out());
}
#[test]
fn test_logout_output_success_empty() {
let output = CommandOutput {
stdout: String::new(),
stderr: String::new(),
exit_code: 0,
success: true,
};
let logout_output = LogoutOutput { output };
assert!(logout_output.success());
assert!(logout_output.is_logged_out());
}
#[test]
fn test_logout_output_warnings() {
let output = CommandOutput {
stdout: "Removing login credentials for registry".to_string(),
stderr: "WARNING: credentials may still be cached\ninfo: using default registry"
.to_string(),
exit_code: 0,
success: true,
};
let logout_output = LogoutOutput { output };
let warnings = logout_output.warnings();
assert_eq!(warnings.len(), 1);
assert!(warnings[0].contains("WARNING"));
}
#[test]
fn test_logout_output_info_messages() {
let output = CommandOutput {
stdout: "Removing login credentials for https://registry.example.com\nLogout completed"
.to_string(),
stderr: String::new(),
exit_code: 0,
success: true,
};
let logout_output = LogoutOutput { output };
let info = logout_output.info_messages();
assert_eq!(info.len(), 2);
assert!(info[0].contains("Removing login credentials"));
assert!(info[1].contains("Logout completed"));
}
#[test]
fn test_logout_output_failure() {
let output = CommandOutput {
stdout: String::new(),
stderr: "Error: unable to logout".to_string(),
exit_code: 1,
success: false,
};
let logout_output = LogoutOutput { output };
assert!(!logout_output.success());
assert!(!logout_output.is_logged_out());
}
#[test]
fn test_logout_multiple_servers_concept() {
// Test that we can create logout commands for different servers
let daemon_default_logout = LogoutCommand::new();
let gcr_logout = LogoutCommand::new().server("gcr.io");
let private_logout = LogoutCommand::new().server("my-registry.com");
assert_eq!(daemon_default_logout.get_server(), None);
assert_eq!(gcr_logout.get_server(), Some("gcr.io"));
assert_eq!(private_logout.get_server(), Some("my-registry.com"));
}
#[test]
fn test_logout_builder_pattern() {
let logout = LogoutCommand::new().server("registry.example.com");
assert_eq!(logout.get_server(), Some("registry.example.com"));
}
#[test]
fn test_logout_various_server_formats() {
let test_cases = vec![
"gcr.io",
"registry-1.docker.io",
"localhost:5000",
"my-registry.com:443",
"registry.example.com/path",
];
for server in test_cases {
let logout = LogoutCommand::new().server(server);
assert_eq!(logout.get_server(), Some(server));
let args = logout.build_command_args();
assert!(args.contains(&server.to_string()));
}
}
}