astrs-cli 0.1.0

The astrs command line: run, build, monitor, migrate and bridge robotic dataflows.
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
//! `astrs ros2 doctor` (blueprint §17): the discovery probe.
//!
//! `astrs doctor` answers "can this machine run AstRS?"; this answers the
//! neighbouring question, "can this machine *see* a ROS 2 domain, and what
//! is on it?" — the one a user asks when a `ros2:` bridge (§10.5) is not
//! receiving anything and they need to know whether the fault is discovery,
//! QoS or the graph.
//!
//! Five checks, in a fixed order, each of which reports rather than aborts —
//! the same convention `astrs doctor` and `astrs validate` follow:
//!
//! | Check | Says |
//! |---|---|
//! | `ros2:domain` | which domain was probed, and where it came from |
//! | `ros2:multicast` | whether the SPDP group could be joined |
//! | `ros2:participants` | how many participants answered, and how many are AstRS |
//! | `ros2:graph` | how many ROS nodes announced `ros_discovery_info` |
//! | `ros2:endpoints` | how many topics, services and actions were seen |
//!
//! # Why "no participants" is a warning and not an error
//!
//! An empty domain is a perfectly legal state — nothing is running yet — and
//! a diagnostic that exits non-zero for it would be unusable in a script
//! that starts the stack and then checks. A *refused* multicast join with no
//! participants at all is the shape that usually means "this will not work",
//! and that pair is what the warning text names.

use std::io::Write;

use serde::Serialize;

use crate::command::doctor::{CheckStatus, DoctorCheck};
use crate::command::ros2::{ProbeArgs, ProbeReport, probe};
use crate::error::CliError;

/// The complete `astrs ros2 doctor` report.
#[derive(Debug, Clone, Serialize)]
pub struct Ros2DoctorReport {
    /// Every check, in a fixed, deterministic order.
    pub checks: Vec<DoctorCheck>,
    /// What the probe actually heard, so `--json` consumers do not have to
    /// re-parse the check text.
    pub probe: ProbeReport,
}

impl Ros2DoctorReport {
    /// `1` if any check is [`CheckStatus::Error`], else `0`.
    ///
    /// A warning does not fail the verb: see the module docs on why an empty
    /// domain is a legal state.
    #[must_use]
    pub fn exit_code(&self) -> i32 {
        i32::from(
            self.checks
                .iter()
                .any(|check| check.status == CheckStatus::Error),
        )
    }
}

/// Probe the domain and print the report.
///
/// # Errors
///
/// [`CliError::Ros2`] when the probe participant cannot be created at all —
/// the one failure that is not a *finding*, because there is nothing to
/// report on. [`CliError::Io`] when writing to `out` fails.
pub fn run(out: &mut dyn Write, args: &ProbeArgs) -> Result<Ros2DoctorReport, CliError> {
    let probe = probe(args)?;
    let checks = checks_for(&probe);
    let report = Ros2DoctorReport { checks, probe };

    if args.json {
        let rendered = serde_json::to_string_pretty(&report)
            .map_err(|error| CliError::Ros2(format!("the report will not serialize: {error}")))?;
        writeln!(out, "{rendered}").map_err(io_error)?;
    } else {
        render(out, &report)?;
    }
    Ok(report)
}

/// The five checks a probe report produces.
#[must_use]
pub fn checks_for(probe: &ProbeReport) -> Vec<DoctorCheck> {
    vec![
        check_domain(probe),
        check_multicast(probe),
        check_participants(probe),
        check_graph(probe),
        check_endpoints(probe),
    ]
}

/// Which domain was probed.
fn check_domain(probe: &ProbeReport) -> DoctorCheck {
    DoctorCheck {
        name: "ros2:domain".to_owned(),
        status: CheckStatus::Info,
        detail: format!(
            "probed DDS domain {} for {} ms from {}",
            probe.domain_id, probe.listened_ms, probe.locator
        ),
    }
}

/// Whether the SPDP multicast group could be joined.
///
/// A refusal is a *warning*, never an error: §10.2's addendum makes unicast
/// initial peers a first-class discovery path, so a bridge with
/// `ASTRS_ROS2_INITIAL_PEERS` set works perfectly with multicast refused.
fn check_multicast(probe: &ProbeReport) -> DoctorCheck {
    let (status, detail) = if probe.multicast_joined {
        (
            CheckStatus::Ok,
            format!("SPDP multicast {}", probe.multicast),
        )
    } else {
        (
            CheckStatus::Warning,
            format!(
                "SPDP multicast {} — discovery needs unicast initial peers \
                 (`ASTRS_ROS2_INITIAL_PEERS` on a bridge node)",
                probe.multicast
            ),
        )
    };
    DoctorCheck {
        name: "ros2:multicast".to_owned(),
        status,
        detail,
    }
}

/// How many participants answered.
fn check_participants(probe: &ProbeReport) -> DoctorCheck {
    let total = probe.participants.len();
    let astrs = probe
        .participants
        .iter()
        .filter(|participant| participant.is_astrs)
        .count();
    let (status, detail) = if total == 0 {
        (
            CheckStatus::Warning,
            if probe.multicast_joined {
                "no participants answered — nothing is running on this domain".to_owned()
            } else {
                "no participants answered, and the multicast join was refused — \
                 this domain is very likely unreachable from here"
                    .to_owned()
            },
        )
    } else {
        (
            CheckStatus::Ok,
            format!("{total} participant(s) answered, {astrs} of them AstRS (vendor id 41 53)"),
        )
    };
    DoctorCheck {
        name: "ros2:participants".to_owned(),
        status,
        detail,
    }
}

/// How many ROS nodes announced themselves.
fn check_graph(probe: &ProbeReport) -> DoctorCheck {
    let (status, detail) = if probe.nodes.is_empty() && !probe.participants.is_empty() {
        (
            CheckStatus::Warning,
            "participants answered but none announced `ros_discovery_info` — \
             they are DDS peers rather than ROS 2 nodes, or their graph \
             sample has not arrived yet"
                .to_owned(),
        )
    } else if probe.nodes.is_empty() {
        (CheckStatus::Info, "no ROS 2 nodes on the graph".to_owned())
    } else {
        (
            CheckStatus::Ok,
            format!(
                "{} ROS 2 node(s): {}",
                probe.nodes.len(),
                probe.nodes.join(", ")
            ),
        )
    };
    DoctorCheck {
        name: "ros2:graph".to_owned(),
        status,
        detail,
    }
}

/// How many endpoints were seen.
fn check_endpoints(probe: &ProbeReport) -> DoctorCheck {
    let mut detail = format!(
        "{} topic(s), {} service(s), {} action(s), {} endpoint(s) total",
        probe.topics.len(),
        probe.services.len(),
        probe.actions.len(),
        probe.endpoint_count()
    );
    if probe.unclaimed_endpoints > 0 {
        detail.push_str(&format!(
            "; {} endpoint(s) claimed by no node",
            probe.unclaimed_endpoints
        ));
    }
    DoctorCheck {
        name: "ros2:endpoints".to_owned(),
        status: if probe.endpoint_count() == 0 {
            CheckStatus::Info
        } else {
            CheckStatus::Ok
        },
        detail,
    }
}

/// Print the report as a table, matching `astrs doctor`'s layout.
fn render(out: &mut dyn Write, report: &Ros2DoctorReport) -> Result<(), CliError> {
    let width = report
        .checks
        .iter()
        .map(|check| check.name.len())
        .max()
        .unwrap_or(0);
    for check in &report.checks {
        writeln!(
            out,
            "{:<width$}  {:<7}  {}",
            check.name,
            check.status.to_string(),
            check.detail,
            width = width
        )
        .map_err(io_error)?;
    }
    Ok(())
}

/// Wrap a write failure the way every other verb does.
fn io_error(source: std::io::Error) -> CliError {
    CliError::Io {
        path: "<stdout>".to_owned(),
        source,
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

    use crate::command::ros2::{ParticipantSummary, TopicSummary};

    use super::*;

    fn report(
        multicast_joined: bool,
        participants: Vec<ParticipantSummary>,
        nodes: Vec<String>,
        topics: Vec<TopicSummary>,
    ) -> ProbeReport {
        ProbeReport {
            domain_id: 0,
            listened_ms: 12,
            multicast: if multicast_joined {
                "joined 239.255.0.1".to_owned()
            } else {
                "refused 239.255.0.1 (operation not permitted)".to_owned()
            },
            multicast_joined,
            locator: "127.0.0.1:54321".to_owned(),
            participants,
            nodes,
            topics,
            services: Vec::new(),
            actions: Vec::new(),
            unclaimed_endpoints: 0,
        }
    }

    fn participant(is_astrs: bool, nodes: &[&str]) -> ParticipantSummary {
        ParticipantSummary {
            guid: "4153000000000001".to_owned(),
            is_astrs,
            nodes: nodes.iter().map(|name| (*name).to_owned()).collect(),
        }
    }

    fn topic(name: &str, publishers: usize, subscribers: usize) -> TopicSummary {
        TopicSummary {
            name: name.to_owned(),
            types: vec!["std_msgs/msg/String".to_owned()],
            publishers,
            subscribers,
            publisher_nodes: Vec::new(),
            subscriber_nodes: Vec::new(),
        }
    }

    #[test]
    fn a_healthy_domain_passes_every_check() {
        let probe = report(
            true,
            vec![participant(true, &["/talker"])],
            vec!["/talker".to_owned()],
            vec![topic("/chatter", 1, 1)],
        );
        let checks = checks_for(&probe);
        assert_eq!(checks.len(), 5);
        assert_eq!(checks[1].status, CheckStatus::Ok, "multicast");
        assert_eq!(checks[2].status, CheckStatus::Ok, "participants");
        assert_eq!(checks[3].status, CheckStatus::Ok, "graph");
        assert_eq!(checks[4].status, CheckStatus::Ok, "endpoints");

        let doctor = Ros2DoctorReport { checks, probe };
        assert_eq!(doctor.exit_code(), 0);
    }

    #[test]
    fn a_refused_multicast_join_is_a_warning_not_an_error() {
        let probe = report(
            false,
            vec![participant(false, &["/talker"])],
            vec!["/talker".to_owned()],
            Vec::new(),
        );
        let checks = checks_for(&probe);
        assert_eq!(checks[1].status, CheckStatus::Warning);
        assert!(
            checks[1].detail.contains("ASTRS_ROS2_INITIAL_PEERS"),
            "{}",
            checks[1].detail
        );
        let doctor = Ros2DoctorReport { checks, probe };
        assert_eq!(doctor.exit_code(), 0, "a warning does not fail the verb");
    }

    #[test]
    fn an_empty_domain_with_multicast_refused_says_so_plainly() {
        let probe = report(false, Vec::new(), Vec::new(), Vec::new());
        let checks = checks_for(&probe);
        assert_eq!(checks[2].status, CheckStatus::Warning);
        assert!(
            checks[2].detail.contains("unreachable"),
            "{}",
            checks[2].detail
        );
    }

    #[test]
    fn an_empty_domain_with_multicast_working_is_merely_empty() {
        let probe = report(true, Vec::new(), Vec::new(), Vec::new());
        let checks = checks_for(&probe);
        assert!(
            checks[2].detail.contains("nothing is running"),
            "{}",
            checks[2].detail
        );
        assert_eq!(checks[3].status, CheckStatus::Info);
        assert_eq!(checks[4].status, CheckStatus::Info);
    }

    #[test]
    fn a_dds_peer_that_is_not_a_ros_node_is_flagged() {
        let probe = report(true, vec![participant(false, &[])], Vec::new(), Vec::new());
        let checks = checks_for(&probe);
        assert_eq!(checks[3].status, CheckStatus::Warning);
        assert!(
            checks[3].detail.contains("ros_discovery_info"),
            "{}",
            checks[3].detail
        );
    }

    #[test]
    fn unclaimed_endpoints_are_counted_in_the_detail() {
        let mut probe = report(
            true,
            vec![participant(true, &["/n"])],
            vec!["/n".to_owned()],
            vec![topic("/t", 1, 0)],
        );
        probe.unclaimed_endpoints = 2;
        let checks = checks_for(&probe);
        assert!(
            checks[4].detail.contains("claimed by no node"),
            "{}",
            checks[4].detail
        );
    }

    #[test]
    fn the_table_prints_one_line_per_check() {
        let probe = report(
            true,
            vec![participant(true, &["/n"])],
            vec!["/n".to_owned()],
            Vec::new(),
        );
        let checks = checks_for(&probe);
        let doctor = Ros2DoctorReport { checks, probe };
        let mut out = Vec::new();
        render(&mut out, &doctor).unwrap();
        let text = String::from_utf8(out).unwrap();
        assert_eq!(text.lines().count(), 5);
        assert!(text.contains("ros2:domain"), "{text}");
        assert!(text.contains("ros2:multicast"), "{text}");
    }

    #[test]
    fn the_json_form_carries_the_probe_beside_the_checks() {
        let probe = report(
            true,
            vec![participant(true, &["/n"])],
            vec!["/n".to_owned()],
            Vec::new(),
        );
        let checks = checks_for(&probe);
        let doctor = Ros2DoctorReport { checks, probe };
        let rendered = serde_json::to_string(&doctor).unwrap();
        assert!(rendered.contains("\"checks\""), "{rendered}");
        assert!(rendered.contains("\"probe\""), "{rendered}");
        assert!(rendered.contains("\"domain_id\":0"), "{rendered}");
    }
}