Add wasm target smoke test

This commit is contained in:
Jesus Checa Hidalgo 2021-09-09 10:10:08 +02:00 committed by jistone
parent 02dbc18c83
commit 2513da263a
6 changed files with 175 additions and 0 deletions

View File

@ -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 <jcheca@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# 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 <jcheca@redhat.com>" > $(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)

View File

@ -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 <jcheca@redhat.com>

View File

@ -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
}

View File

@ -0,0 +1,15 @@
summary: Test that the rust wasm target is enabled and can compile correctly
description: ''
contact:
- Jesus Checa <jcheca@redhat.com>
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

View File

@ -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 <jcheca@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# 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

View File

@ -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);
}
);