public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer 6/6] tui: install progress: add tests for UI^2 stdio protocol
Date: Wed,  6 Dec 2023 12:34:55 +0100	[thread overview]
Message-ID: <20231206113456.411898-7-c.heiss@proxmox.com> (raw)
In-Reply-To: <20231206113456.411898-1-c.heiss@proxmox.com>

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
 .../src/views/install_progress.rs             | 94 +++++++++++++++++++
 1 file changed, 94 insertions(+)

diff --git a/proxmox-tui-installer/src/views/install_progress.rs b/proxmox-tui-installer/src/views/install_progress.rs
index 741529f..4626fe4 100644
--- a/proxmox-tui-installer/src/views/install_progress.rs
+++ b/proxmox-tui-installer/src/views/install_progress.rs
@@ -246,3 +246,97 @@ enum UiMessage {
         text: String,
     },
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use std::env;
+
+    #[test]
+    fn run_low_level_installer_test_session() {
+        env::set_current_dir("..").expect("failed to change working directory");
+        let mut child = spawn_low_level_installer(true)
+            .expect("failed to run low-level installer test session");
+
+        let mut reader = child
+            .stdout
+            .take()
+            .map(BufReader::new)
+            .expect("failed to get stdin reader");
+
+        let mut writer = child.stdin.take().expect("failed to get stdin writer");
+
+        serde_json::to_writer(&mut writer, &serde_json::json!({ "autoreboot": false }))
+            .expect("failed to serialize install config");
+
+        writeln!(writer).expect("failed to write install config: {err}");
+
+        let mut next_msg = || {
+            let mut line = String::new();
+            reader.read_line(&mut line).expect("a line");
+
+            match serde_json::from_str::<UiMessage>(&line) {
+                Ok(msg) => Some(msg),
+                Err(err) => panic!("unexpected error: '{err}'"),
+            }
+        };
+
+        assert_eq!(
+            next_msg(),
+            Some(UiMessage::Prompt {
+                query: "Reply anything?".to_owned()
+            }),
+        );
+
+        serde_json::to_writer(
+            &mut writer,
+            &serde_json::json!({"type": "prompt-answer", "answer": "ok"}),
+        )
+        .expect("failed to write prompt answer");
+        writeln!(writer).expect("failed to write prompt answer");
+
+        assert_eq!(
+            next_msg(),
+            Some(UiMessage::Info {
+                message: "Test Message - got ok".to_owned()
+            }),
+        );
+
+        for i in (1..=1000).step_by(3) {
+            assert_eq!(
+                next_msg(),
+                Some(UiMessage::Progress {
+                    ratio: (i as f32) / 1000.,
+                    text: format!("foo {i}"),
+                }),
+            );
+        }
+
+        assert_eq!(
+            next_msg(),
+            Some(UiMessage::Finished {
+                state: "ok".to_owned(),
+                message: "Installation finished - reboot now?".to_owned(),
+            }),
+        );
+
+        // Should be nothing left to read now
+        let mut line = String::new();
+        assert_eq!(reader.read_line(&mut line).expect("success"), 0);
+
+        // Give the low-level installer some time to exit properly
+        std::thread::sleep(Duration::new(1, 0));
+
+        match child.try_wait() {
+            Ok(Some(status)) => assert!(
+                status.success(),
+                "low-level installer did not exit successfully"
+            ),
+            Ok(None) => {
+                child.kill().expect("could not kill low-level installer");
+                panic!("low-level install was not successful");
+            }
+            Err(err) => panic!("failed to wait for low-level installer: {err}"),
+        }
+    }
+}
-- 
2.42.0





  parent reply	other threads:[~2023-12-06 11:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-06 11:34 [pve-devel] [PATCH installer 0/6] switch low-level installer protocol to json Christoph Heiss
2023-12-06 11:34 ` [pve-devel] [PATCH installer 1/6] low-level: align wording of finish message Christoph Heiss
2023-12-06 11:34 ` [pve-devel] [PATCH installer 2/6] ui: stdio: log error if display_html() is called on stdio backend Christoph Heiss
2023-12-06 11:34 ` [pve-devel] [PATCH installer 3/6] tui, ui: switch over to JSON-based protocol Christoph Heiss
2024-02-24 16:55   ` Thomas Lamprecht
2023-12-06 11:34 ` [pve-devel] [PATCH installer 4/6] test: add tests for UI^2 stdio protocol Christoph Heiss
2023-12-06 11:34 ` [pve-devel] [PATCH installer 5/6] buildsys: setup proper test environment for testsuite Christoph Heiss
2023-12-06 11:34 ` Christoph Heiss [this message]
2024-01-24  9:54 ` [pve-devel] [PATCH installer 0/6] switch low-level installer protocol to json Christoph Heiss
2024-02-26 14:07 ` [pve-devel] applied-series: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231206113456.411898-7-c.heiss@proxmox.com \
    --to=c.heiss@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal