From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 25904B8BBF for ; Wed, 6 Dec 2023 12:35:14 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0A058655 for ; Wed, 6 Dec 2023 12:35:14 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 6 Dec 2023 12:35:13 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 2C41441036 for ; Wed, 6 Dec 2023 12:35:13 +0100 (CET) From: Christoph Heiss To: pve-devel@lists.proxmox.com Date: Wed, 6 Dec 2023 12:34:55 +0100 Message-ID: <20231206113456.411898-7-c.heiss@proxmox.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231206113456.411898-1-c.heiss@proxmox.com> References: <20231206113456.411898-1-c.heiss@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.003 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH installer 6/6] tui: install progress: add tests for UI^2 stdio protocol X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Dec 2023 11:35:14 -0000 Signed-off-by: Christoph Heiss --- .../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::(&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