From 2513da263ab323d49e2531969c2ef4c2cf32d720 Mon Sep 17 00:00:00 2001 From: Jesus Checa Hidalgo Date: Thu, 9 Sep 2021 10:10:08 +0200 Subject: [PATCH] Add wasm target smoke test --- tests/Sanity/rust-wasm-smoke-test/Makefile | 64 ++++++++++++++++++++ tests/Sanity/rust-wasm-smoke-test/PURPOSE | 3 + tests/Sanity/rust-wasm-smoke-test/lib.rs | 12 ++++ tests/Sanity/rust-wasm-smoke-test/main.fmf | 15 +++++ tests/Sanity/rust-wasm-smoke-test/runtest.sh | 53 ++++++++++++++++ tests/Sanity/rust-wasm-smoke-test/test.js | 28 +++++++++ 6 files changed, 175 insertions(+) create mode 100644 tests/Sanity/rust-wasm-smoke-test/Makefile create mode 100644 tests/Sanity/rust-wasm-smoke-test/PURPOSE create mode 100644 tests/Sanity/rust-wasm-smoke-test/lib.rs create mode 100644 tests/Sanity/rust-wasm-smoke-test/main.fmf create mode 100755 tests/Sanity/rust-wasm-smoke-test/runtest.sh create mode 100644 tests/Sanity/rust-wasm-smoke-test/test.js diff --git a/tests/Sanity/rust-wasm-smoke-test/Makefile b/tests/Sanity/rust-wasm-smoke-test/Makefile new file mode 100644 index 0000000..437da6b --- /dev/null +++ b/tests/Sanity/rust-wasm-smoke-test/Makefile @@ -0,0 +1,64 @@ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Makefile of /tools/rust/Sanity/rust-wasm-smoke-test +# Description: Test that the rust wasm target is enabled and can compile correctly +# Author: Jesus Checa +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Copyright (c) 2021 Red Hat, Inc. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation, either version 2 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +# PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +export TEST=/tools/rust/Sanity/rust-wasm-smoke-test +export TESTVERSION=1.0 + +BUILT_FILES= + +FILES=$(METADATA) runtest.sh Makefile PURPOSE lib.rs test.js + +.PHONY: all install download clean + +run: $(FILES) build + ./runtest.sh + +build: $(BUILT_FILES) + test -x runtest.sh || chmod a+x runtest.sh + +clean: + rm -f *~ $(BUILT_FILES) + + +include /usr/share/rhts/lib/rhts-make.include + +$(METADATA): Makefile + @echo "Owner: Jesus Checa " > $(METADATA) + @echo "Name: $(TEST)" >> $(METADATA) + @echo "TestVersion: $(TESTVERSION)" >> $(METADATA) + @echo "Path: $(TEST_DIR)" >> $(METADATA) + @echo "Description: Test that the rust wasm target is enabled and can compile correctly" >> $(METADATA) + @echo "Type: Sanity" >> $(METADATA) + @echo "TestTime: 5m" >> $(METADATA) + @echo "RunFor: rust" >> $(METADATA) + @echo "Requires: rust rust-std-static-wasm32-unknown-unknown nodejs" >> $(METADATA) + @echo "Priority: Normal" >> $(METADATA) + @echo "License: GPLv2+" >> $(METADATA) + @echo "Confidential: no" >> $(METADATA) + @echo "Destructive: no" >> $(METADATA) + @echo "Releases: -RHEL4 -RHELClient5 -RHELServer5 -RHEL7" >> $(METADATA) + @echo "Architectures: aarch64 ppc64le x86_64" >> $(METADATA) + + rhts-lint $(METADATA) diff --git a/tests/Sanity/rust-wasm-smoke-test/PURPOSE b/tests/Sanity/rust-wasm-smoke-test/PURPOSE new file mode 100644 index 0000000..e21d668 --- /dev/null +++ b/tests/Sanity/rust-wasm-smoke-test/PURPOSE @@ -0,0 +1,3 @@ +PURPOSE of /tools/rust/Sanity/rust-wasm-smoke-test +Description: Test that the rust wasm target is enabled and can compile correctly +Author: Jesus Checa diff --git a/tests/Sanity/rust-wasm-smoke-test/lib.rs b/tests/Sanity/rust-wasm-smoke-test/lib.rs new file mode 100644 index 0000000..36e7457 --- /dev/null +++ b/tests/Sanity/rust-wasm-smoke-test/lib.rs @@ -0,0 +1,12 @@ +#[no_mangle] +pub fn fib(index: u32) -> u32 { + let mut nminus2; + let mut nminus1 = 1; + let mut n = 0; + for _ in 0..index { + nminus2 = nminus1; + nminus1 = n; + n = nminus2 + nminus1; + } + n +} diff --git a/tests/Sanity/rust-wasm-smoke-test/main.fmf b/tests/Sanity/rust-wasm-smoke-test/main.fmf new file mode 100644 index 0000000..0fe807c --- /dev/null +++ b/tests/Sanity/rust-wasm-smoke-test/main.fmf @@ -0,0 +1,15 @@ +summary: Test that the rust wasm target is enabled and can compile correctly +description: '' +contact: + - Jesus Checa +component: + - rust +test: ./runtest.sh +framework: beakerlib +recommend: + - rust + - rust-std-static-wasm32-unknown-unknown + - nodejs +duration: 5m +extra-summary: /tools/rust/Sanity/rust-wasm-smoke-test +extra-task: /tools/rust/Sanity/rust-wasm-smoke-test diff --git a/tests/Sanity/rust-wasm-smoke-test/runtest.sh b/tests/Sanity/rust-wasm-smoke-test/runtest.sh new file mode 100755 index 0000000..bee4890 --- /dev/null +++ b/tests/Sanity/rust-wasm-smoke-test/runtest.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# runtest.sh of /tools/rust/Sanity/rust-wasm-smoke-test +# Description: Test that the rust wasm target is enabled and can compile correctly +# Author: Jesus Checa +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Copyright (c) 2021 Red Hat, Inc. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation, either version 2 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +# PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# Include Beaker environment +. /usr/share/beakerlib/beakerlib.sh || exit 1 + +PACKAGES="$(rpm -qf $(which rustc)) rust-std-static-wasm32-unknown-unknown" + +rlJournalStart + rlPhaseStartSetup + rlAssertRpm --all + rlRun "TmpDir=\$(mktemp -d)" 0 "Creating tmp directory" + rlRun "cp lib.rs $TmpDir" + rlRun "cp test.js $TmpDir" + rlRun "pushd $TmpDir" + rlPhaseEnd + + rlPhaseStartTest + rlRun "rustc --target wasm32-unknown-unknown --crate-type=cdylib lib.rs -o fib.wasm" 0 "Building WASM binary" + rlRun "node test.js" 0 "Testing WASM binary" + rlPhaseEnd + + rlPhaseStartCleanup + rlRun "popd" + rlRun "rm -r $TmpDir" 0 "Removing tmp directory" + rlPhaseEnd +rlJournalPrintText +rlJournalEnd + diff --git a/tests/Sanity/rust-wasm-smoke-test/test.js b/tests/Sanity/rust-wasm-smoke-test/test.js new file mode 100644 index 0000000..921df38 --- /dev/null +++ b/tests/Sanity/rust-wasm-smoke-test/test.js @@ -0,0 +1,28 @@ +function js_fibonacci(index) { + let nminus2 = 0; + let nminus1 = 1; + let n = 0; + for(let i = 0; i < index; ++i) { + nminus2 = nminus1; + nminus1 = n; + n = nminus1 + nminus2; + } + return n; +} + +const fs = require('fs'); +const buf = fs.readFileSync('./fib.wasm'); +const lib = WebAssembly.instantiate(new Uint8Array(buf)). + then(res => { + var fib = res.instance.exports.fib; + for (var i=1; i<=10; i++) { + if(fib(i) != js_fibonacci(i)){ + console.log("Mismatch between wasm and JS functions"); + process.exit(1); + } + } + }).catch(e => { + console.log(e); + process.exit(1); + } +);