Skip to content

Cannot run simple unit test with default Rust template #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
radu-matei opened this issue Aug 4, 2022 · 3 comments · Fixed by #868
Closed

Cannot run simple unit test with default Rust template #672

radu-matei opened this issue Aug 4, 2022 · 3 comments · Fixed by #868
Labels

Comments

@radu-matei
Copy link
Member

The default Rust template for a Spin application sets the default build target in .cargo/config.toml as wasm32-wasi:

[build]
target = "wasm32-wasi"

This make sense in order to ensure cargo build creates a module that Spin can run, but this means that a simple unit test that purely wants to test some logic in the handler cannot be run:

  • with a regular cargo test, this fails because it would try to instantiate a Wasm binary and run the test:
.../target/wasm32-wasi/debug/deps/rust_api-a5d8eced053c53aa.wasm: cannot execute binary file
  • with cargo wasi test, this fails because of host imports that are not available in Wasmtime directly (but are in Spin):
wasm32-wasi/debug/deps/rust_api-a5d8eced053c53aa.wasm`
Error: failed to run main module `.../rust-api/target/wasm32-wasi/debug/deps/rust_api-a5d8eced053c53aa.wasm`

Caused by:
    0: failed to instantiate ".../target/wasm32-wasi/debug/deps/rust_api-a5d8eced053c53aa.wasm"
    1: unknown import: `wasi_experimental_http::req` has not been defined

The workaround for this is to remove the default target in .cargo/config.toml and run cargo test — assuming this only tests logic that does not have anything to do with Wasm or Spin functionality.

Because in the default Rust template we have a build command that sets the target to wasm32-wasi, do we want to remove the default target in the Cargo configuration file so testing with cargo test works for simple unit tests?

@radu-matei radu-matei added the sdk label Aug 4, 2022
@FrankYang0529
Copy link
Contributor

This is similar to what I encountered in spinframework/spin-fileserver#15. Probably, we can use cargo test --target=$$(rustc -vV | sed -n 's|host: ||p') as @lann mentioned in spinframework/spin-fileserver#15 (comment).

@radu-matei
Copy link
Member Author

Given that all our Rust templates explicitly build to --target wasm32-wasi, could we remove .cargo/config.toml from the templates?

@kate-goldenring kate-goldenring moved this to 🆕 Triage Needed in Spin Triage Aug 16, 2022
@kate-goldenring kate-goldenring moved this from 🆕 Triage Needed to 📋 Investigating in Spin Triage Aug 16, 2022
@vdice
Copy link
Contributor

vdice commented Oct 25, 2022

It sounds like we've converged on #672 (comment) as being the approved fix here? Shall we move into the Backlog?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants