Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat(adapter.rs):
Lars Wirzenius committed 2 years ago
commit 742824f07dc093dc8b7289b5e55d29dc43f84e29
parent 730d6abcb7086156e174f483e37edbe426832c4e
1 file changed +48 -14
modified src/adapter.rs
@@ -90,7 +90,9 @@ impl Adapter {
        debug!("send request to sub-process stdin");
        {
            let stdin = child.stdin.take().ok_or(AdapterError::StdinHandle)?;
-
            trigger.to_writer(stdin)?;
+
            trigger
+
                .to_writer(stdin)
+
                .map_err(AdapterError::RequestWrite)?;
        }

        // Get the child's stdout into a BufReader so that we can loop
@@ -101,7 +103,7 @@ impl Adapter {

        if let Some(line) = lines.next() {
            let line = line.map_err(AdapterError::ReadLine)?;
-
            let resp = Response::from_str(&line)?;
+
            let resp = Response::from_str(&line).map_err(AdapterError::ParseResponse)?;
            debug!("first response {resp:#?}");
            match resp {
                Response::Triggered { run_id } => {
@@ -117,7 +119,7 @@ impl Adapter {

        if let Some(line) = lines.next() {
            let line = line.map_err(AdapterError::ReadLine)?;
-
            let resp = Response::from_str(&line)?;
+
            let resp = Response::from_str(&line).map_err(AdapterError::ParseResponse)?;
            debug!("second response {resp:#?}");
            match resp {
                Response::Finished { result } => {
@@ -132,7 +134,7 @@ impl Adapter {

        if let Some(line) = lines.next() {
            let line = line.map_err(AdapterError::ReadLine)?;
-
            let resp = Response::from_str(&line)?;
+
            let resp = Response::from_str(&line).map_err(AdapterError::ParseResponse)?;
            debug!("third response is too many {resp:#?}");
            return Err(AdapterError::TooMany(resp));
        } else {
@@ -164,9 +166,13 @@ impl Adapter {

#[derive(Debug, thiserror::Error)]
pub enum AdapterError {
-
    /// A message related error.
-
    #[error(transparent)]
-
    Message(#[from] MessageError),
+
    /// Error creating Response from a string.
+
    #[error("failed to create a Response message from adapter output")]
+
    ParseResponse(#[source] MessageError),
+

+
    /// Request writing failure.
+
    #[error("failed to write request to adapter stdin")]
+
    RequestWrite(#[source] MessageError),

    /// Error from spawning a sub-process.
    #[error("failed to spawn a CI adapter sub-process: {0}")]
@@ -254,6 +260,7 @@ mod test {
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
echo '{"response":"finished","result":"success"}'
"#;
@@ -275,6 +282,7 @@ echo '{"response":"finished","result":"success"}'
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
echo '{"response":"finished","result":"failure"}'
"#;
@@ -285,8 +293,13 @@ echo '{"response":"finished","result":"failure"}'

        let mut run = run();
        let mut status = status_page();
-
        Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status)?;
-
        assert_eq!(run.result(), Some(&RunResult::Failure));
+
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+

+
        match x {
+
            Ok(_) => (),
+
            Err(AdapterError::RequestWrite(_)) => (),
+
            _ => panic!("unexpected result: {x:#?}"),
+
        }

        Ok(())
    }
@@ -296,6 +309,7 @@ echo '{"response":"finished","result":"failure"}'
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
echo '{"response":"finished","result":"failure"}'
echo woe be me 1>&2
@@ -309,6 +323,7 @@ exit 1
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+
        eprintln!("{x:#?}");
        assert!(x.is_err());
        assert_eq!(run.result(), Some(&RunResult::Failure));

@@ -330,7 +345,11 @@ kill -9 $BASHPID
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
-
        assert!(matches!(x, Err(AdapterError::Failed(_))));
+
        eprintln!("{x:#?}");
+
        assert!(matches!(
+
            x,
+
            Err(AdapterError::Failed(_)) | Err(AdapterError::RequestWrite(_))
+
        ));

        Ok(())
    }
@@ -340,6 +359,7 @@ kill -9 $BASHPID
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
kill -9 $BASHPID
"#;
@@ -351,6 +371,7 @@ kill -9 $BASHPID
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+
        eprintln!("{x:#?}");
        assert!(matches!(x, Err(AdapterError::Failed(_))));

        Ok(())
@@ -361,6 +382,7 @@ kill -9 $BASHPID
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
echo '{"response":"finished","result":"success"}'
kill -9 $BASHPID
@@ -373,6 +395,7 @@ kill -9 $BASHPID
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+
        eprintln!("{x:#?}");
        assert!(matches!(x, Err(AdapterError::Failed(_))));

        Ok(())
@@ -383,6 +406,7 @@ kill -9 $BASHPID
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
echo '{"response":"finished","result":"success","bad":"field"}'
"#;
@@ -394,10 +418,11 @@ echo '{"response":"finished","result":"success","bad":"field"}'
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
-
        assert!(matches!(
-
            x,
-
            Err(AdapterError::Message(MessageError::DeserializeResponse(_)))
-
        ));
+

+
        match x {
+
            Err(AdapterError::ParseResponse(MessageError::DeserializeResponse(_))) => (),
+
            _ => panic!("unexpected result: {x:#?}"),
+
        }

        Ok(())
    }
@@ -407,6 +432,7 @@ echo '{"response":"finished","result":"success","bad":"field"}'
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"finished","result":"success"}'
"#;

@@ -417,6 +443,7 @@ echo '{"response":"finished","result":"success"}'
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+
        eprintln!("{x:#?}");
        assert!(matches!(
            x,
            Err(AdapterError::NotTriggered(Response::Finished {
@@ -432,6 +459,7 @@ echo '{"response":"finished","result":"success"}'
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
echo '{"response":"finished","result":"success"}'
echo '{"response":"finished","result":"success"}'
@@ -444,6 +472,7 @@ echo '{"response":"finished","result":"success"}'
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+
        eprintln!("{x:#?}");
        assert!(matches!(
            x,
            Err(AdapterError::TooMany(Response::Finished {
@@ -464,6 +493,7 @@ echo '{"response":"finished","result":"success"}'
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+
        eprintln!("{x:#?}");
        match x {
            Err(AdapterError::SpawnAdapter(filename, e)) => {
                assert_eq!(bin, filename);
@@ -480,6 +510,7 @@ echo '{"response":"finished","result":"success"}'
        log_in_tests();

        const ADAPTER: &str = r#"#!/bin/bash
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
echo '{"response":"finished","result":"success"}'
"#;
@@ -491,6 +522,7 @@ echo '{"response":"finished","result":"success"}'
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+
        eprintln!("{x:#?}");
        match x {
            Err(AdapterError::SpawnAdapter(filename, e)) => {
                assert_eq!(bin, filename);
@@ -511,6 +543,7 @@ echo '{"response":"finished","result":"success"}'
        // loaded due to missing dynamic linker or library or such.

        const ADAPTER: &str = r#"#!/bin/does-not-exist
+
read
echo '{"response":"triggered","run_id":{"id":"xyzzy"}}'
echo '{"response":"finished","result":"success"}'
"#;
@@ -522,6 +555,7 @@ echo '{"response":"finished","result":"success"}'
        let mut run = run();
        let mut status = status_page();
        let x = Adapter::new(&bin).run(&trigger_request()?, &mut run, &mut status);
+
        eprintln!("{x:#?}");
        match x {
            Err(AdapterError::SpawnAdapter(filename, e)) => {
                assert_eq!(bin, filename);