Skip to content

Commit

Permalink
feat: Add edge test s3_read_on_wasm (#3802)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored Dec 22, 2023
1 parent 8d23c15 commit 64b5e47
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 5 deletions.
53 changes: 49 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions core/edge/s3_read_on_wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pkg/
node_modules/
dist/
35 changes: 35 additions & 0 deletions core/edge/s3_read_on_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
edition = "2021"
name = "edge_test_s3_read_on_wasm"
publish = false
version = "0.0.0"

license.workspace = true

[lib]
crate-type = ["cdylib"]

[dependencies]
opendal = { path = "../../", default-features = false, features = ["services-s3"] }
wasm-bindgen = "0.2.89"
wasm-bindgen-futures = "0.4.39"

[dev-dependencies]
wasm-bindgen-test = "0.3.0"
42 changes: 42 additions & 0 deletions core/edge/s3_read_on_wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# S3 Read on WASM

This test is used to ensure opendal can run on WASM target.

## Setup

Start the S3 server at `http://127.0.0.1:9900`

We are using the following config values, please feel free to change based on your own needs.

```rust
let mut cfg = S3::default();
cfg.endpoint("http://127.0.0.1:9900");
cfg.access_key_id("minioadmin");
cfg.secret_access_key("minioadmin");
cfg.bucket("opendal");
cfg.region("us-east-1");
```

## Install

```shell
cargo install wasm-pack
```

## Build

```shell
wasm-pack build
```

## Test

NOTE:

- You need to have Chrome installed.
- We can't run on node.js yet.
- We can't run on headless chrome yet.

```shell
wasm-pack test --chrome
```
57 changes: 57 additions & 0 deletions core/edge/s3_read_on_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use opendal::services::S3;
use opendal::Operator;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub async fn hello_world() -> String {
let mut cfg = S3::default();
cfg.endpoint("http://127.0.0.1:9900");
cfg.access_key_id("minioadmin");
cfg.secret_access_key("minioadmin");
cfg.bucket("opendal");
cfg.region("us-east-1");

let op = Operator::new(cfg).unwrap().finish();
op.write(
"test",
"Hello, WASM! We are from OpenDAL at rust side!"
.as_bytes()
.to_vec(),
)
.await
.unwrap();
let bs = op.read("test").await.unwrap();
String::from_utf8_lossy(&bs).to_string()
}

#[cfg(test)]
mod tests {
use super::*;
use wasm_bindgen_test::wasm_bindgen_test;
use wasm_bindgen_test::wasm_bindgen_test_configure;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
async fn test_hello_world() {
let s = hello_world().await;
assert_eq!(s, "Hello, WASM! We are from OpenDAL at rust side!")
}
}
15 changes: 15 additions & 0 deletions core/edge/s3_read_on_wasm/webdriver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"moz:firefoxOptions": {
"prefs": {
"media.navigator.streams.fake": true,
"media.navigator.permission.disabled": true
},
"args": []
},
"goog:chromeOptions": {
"args": [
"--use-fake-device-for-media-stream",
"--use-fake-ui-for-media-stream"
]
}
}
2 changes: 1 addition & 1 deletion core/src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,8 @@ impl Builder for S3Builder {
#[cfg(not(target_arch = "wasm32"))]
{
cfg = cfg.from_profile();
cfg = cfg.from_env();
}
cfg = cfg.from_env();
}

if let Some(v) = self.config.region.take() {
Expand Down

0 comments on commit 64b5e47

Please sign in to comment.