Browse Source

CI: add test launching for `near-client`

develop
Silvestr Predko 3 years ago
parent
commit
bd2e353512
3 changed files with 57 additions and 53 deletions
  1. 22
    8
      .github/workflows/ci.yml
  2. 1
    0
      near-client/Cargo.toml
  3. 34
    45
      near-client/tests/rpc.rs

+ 22
- 8
.github/workflows/ci.yml View File

50
         uses: actions-rs/toolchain@v1
50
         uses: actions-rs/toolchain@v1
51
         with:
51
         with:
52
           toolchain: stable
52
           toolchain: stable
53
-      - name: Install wasm32-unknown-unknown
54
-        run: rustup target add wasm32-unknown-unknown
53
+          target: wasm32-unknown-unknown
55
       - name: cargo build [web-client, near-client, common-api] for wasm
54
       - name: cargo build [web-client, near-client, common-api] for wasm
56
         run: cargo build -p web-client -p near-client -p common-api --target wasm32-unknown-unknown --release
55
         run: cargo build -p web-client -p near-client -p common-api --target wasm32-unknown-unknown --release
57
       - name: cargo build [near-client, common-api]
56
       - name: cargo build [near-client, common-api]
58
         run: cargo build -p near-client -p common-api --tests --release
57
         run: cargo build -p near-client -p common-api --tests --release
59
-  tests:
60
-    name: tests
58
+  tests-common-api:
59
+    name: tests-common-api
61
     needs: [clippy, fmt]
60
     needs: [clippy, fmt]
62
     runs-on: ubuntu-latest
61
     runs-on: ubuntu-latest
63
     steps:
62
     steps:
66
         uses: actions-rs/toolchain@v1
65
         uses: actions-rs/toolchain@v1
67
         with:
66
         with:
68
           toolchain: stable
67
           toolchain: stable
69
-      - name: Run unit-tests for [common-api]
70
-        run: cargo test -p common-api crypto
71
-      - name: Run integration-tests for [common-api]
72
-        run: cargo test -p common-api --test crypto
68
+      - name: Run tests for [common-api]
69
+        run: cargo test -p common-api --tests
70
+  tests-near:
71
+    name: tests-near
72
+    needs: [clippy, fmt]
73
+    runs-on: ubuntu-latest
74
+    steps:
75
+      - uses: actions/checkout@v3
76
+      - name: Install Rust Toolchain
77
+        uses: actions-rs/toolchain@v1
78
+        with:
79
+          toolchain: stable
80
+          target: wasm32-unknown-unknown
81
+      - name: Set up ssh access 
82
+        uses: webfactory/ssh-agent@v0.5.4
83
+        with:
84
+          ssh-private-key: ${{ secrets.SMARTCONTRACTS_SSH_PRIVATE_KEY }}
85
+      - name: Run tests for [near-client]
86
+        run: cargo test -p near-client --tests

+ 1
- 0
near-client/Cargo.toml View File

27
 reqwest = { version = "0.11", features = ["json"] }
27
 reqwest = { version = "0.11", features = ["json"] }
28
 rand = "0.8.5"
28
 rand = "0.8.5"
29
 rand_chacha = "0.3"
29
 rand_chacha = "0.3"
30
+tempfile = "3"
30
 tokio = { version = "1.2.1", features = ["full"] }
31
 tokio = { version = "1.2.1", features = ["full"] }
31
 workspaces = { git = "https://github.com/near/workspaces-rs.git", features = ["unstable"] }
32
 workspaces = { git = "https://github.com/near/workspaces-rs.git", features = ["unstable"] }

+ 34
- 45
near-client/tests/rpc.rs View File

8
 use rand_chacha::ChaChaRng;
8
 use rand_chacha::ChaChaRng;
9
 use reqwest::Url;
9
 use reqwest::Url;
10
 use serde_json::json;
10
 use serde_json::json;
11
-use std::{
12
-    fs::{create_dir, read, write},
13
-    path::Path,
14
-    str::FromStr,
15
-};
11
+use std::{fs::write, path::Path, str::FromStr};
16
 use workspaces::{network::Sandbox, types::SecretKey, AccountId, Worker};
12
 use workspaces::{network::Sandbox, types::SecretKey, AccountId, Worker};
17
 
13
 
18
 // auxiliary structs and methods
14
 // auxiliary structs and methods
50
 
46
 
51
 async fn download_contract() -> Vec<u8> {
47
 async fn download_contract() -> Vec<u8> {
52
     let target = "https://github.com/near-examples/FT/raw/master/res/fungible_token.wasm";
48
     let target = "https://github.com/near-examples/FT/raw/master/res/fungible_token.wasm";
53
-    let target_path = "../target/tmp-contracts";
49
+    let target_path = temp_dir().into_path();
54
     let fname = "contract.wasm";
50
     let fname = "contract.wasm";
55
-    let full_dest = format!("{}/{}", target_path, fname);
56
-
57
-    if !Path::new(&full_dest).exists() {
58
-        if !Path::new(&target_path).exists() {
59
-            create_dir(target_path).unwrap();
60
-        };
51
+    let full_dest = format!("{}/{}", target_path.to_string_lossy(), fname);
61
 
52
 
62
-        let contract_bytes = reqwest::get(target).await.unwrap().bytes().await.unwrap();
63
-        write(full_dest, &contract_bytes).unwrap();
64
-        contract_bytes.to_vec()
65
-    } else {
66
-        read(&full_dest).unwrap()
67
-    }
53
+    let contract_bytes = reqwest::get(target).await.unwrap().bytes().await.unwrap();
54
+    write(full_dest, &contract_bytes).unwrap();
55
+    contract_bytes.to_vec()
68
 }
56
 }
69
 
57
 
70
 async fn clone_and_compile_wasm() -> Vec<u8> {
58
 async fn clone_and_compile_wasm() -> Vec<u8> {
71
-    let tmp_path = "../target/tmp-contracts";
72
-    let repo_path = format!("{}/near-smartcontracts", tmp_path);
59
+    let tmp_path = temp_dir().into_path();
60
+    let repo_path = format!("{}/near-smartcontracts", tmp_path.to_string_lossy());
73
     let target_path = format!("{}/test-contract", repo_path);
61
     let target_path = format!("{}/test-contract", repo_path);
74
     let repo_url = "git@github.com:Relayz-io/near-smartcontracts.git";
62
     let repo_url = "git@github.com:Relayz-io/near-smartcontracts.git";
75
 
63
 
76
-    if !Path::new(&target_path).exists() {
77
-        if !Path::new(&tmp_path).exists() {
78
-            create_dir(tmp_path).unwrap();
79
-        }
64
+    // Prepare callbacks.
65
+    let mut callbacks = RemoteCallbacks::new();
66
+    callbacks.credentials(|_, username_from_url, _| {
67
+        let username = username_from_url
68
+            .ok_or_else(|| git2::Error::from_str("Parsing the given URL is failed"))
69
+            .unwrap();
70
+        Cred::ssh_key_from_agent(username)
71
+    });
80
 
72
 
81
-        // Prepare callbacks.
82
-        let mut callbacks = RemoteCallbacks::new();
83
-        callbacks.credentials(|_, username_from_url, _| {
84
-            let username = username_from_url
85
-                .ok_or_else(|| git2::Error::from_str("Parsing the given URL is failed"))
86
-                .unwrap();
87
-            Cred::ssh_key_from_agent(username)
88
-        });
89
-
90
-        // Prepare fetch options.
91
-        let mut fo = git2::FetchOptions::new();
92
-        fo.remote_callbacks(callbacks);
93
-
94
-        // Prepare builder.
95
-        let mut builder = git2::build::RepoBuilder::new();
96
-        builder.fetch_options(fo);
97
-        builder.branch("main");
98
-
99
-        // Clone the project.
100
-        builder.clone(repo_url, Path::new(&repo_path)).unwrap();
101
-    }
73
+    // Prepare fetch options.
74
+    let mut fo = git2::FetchOptions::new();
75
+    fo.remote_callbacks(callbacks);
76
+
77
+    // Prepare builder.
78
+    let mut builder = git2::build::RepoBuilder::new();
79
+    builder.fetch_options(fo);
80
+    builder.branch("main");
81
+
82
+    // Clone the project.
83
+    builder.clone(repo_url, Path::new(&repo_path)).unwrap();
102
 
84
 
103
     workspaces::compile_project(&target_path).await.unwrap()
85
     workspaces::compile_project(&target_path).await.unwrap()
104
 }
86
 }
440
         }
422
         }
441
     ));
423
     ));
442
 }
424
 }
425
+
426
+fn temp_dir() -> tempfile::TempDir {
427
+    tempfile::Builder::new()
428
+        .prefix("near-client-test-")
429
+        .tempdir()
430
+        .unwrap()
431
+}

Loading…
Cancel
Save