Skip to content

Commit e67930a

Browse files
committed
tests: Refactor runner tests extract test utils into recipe.rs
1 parent e484fa6 commit e67930a

File tree

3 files changed

+138
-183
lines changed

3 files changed

+138
-183
lines changed

tests/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ pub mod helpers;
55
#[macro_use]
66
pub mod macros;
77
pub mod subgraph;
8+
pub mod recipe;
89

910
pub use config::{Config, DbConfig, EthConfig, CONFIG};

tests/src/recipe.rs

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
use crate::{
2+
fixture::{stores, Stores, TestInfo},
3+
helpers::run_cmd,
4+
};
5+
use graph::ipfs;
6+
use graph::prelude::{DeploymentHash, SubgraphName};
7+
use std::process::Command;
8+
pub struct RunnerTestRecipe {
9+
pub stores: Stores,
10+
pub test_info: TestInfo,
11+
}
12+
13+
impl RunnerTestRecipe {
14+
pub async fn new(test_name: &str, subgraph_name: &str) -> Self {
15+
let subgraph_name = SubgraphName::new(subgraph_name).unwrap();
16+
let test_dir = format!("./runner-tests/{}", subgraph_name);
17+
18+
let (stores, hash) = tokio::join!(
19+
stores(test_name, "./runner-tests/config.simple.toml"),
20+
build_subgraph(&test_dir, None)
21+
);
22+
23+
Self {
24+
stores,
25+
test_info: TestInfo {
26+
test_dir,
27+
test_name: test_name.to_string(),
28+
subgraph_name,
29+
hash,
30+
},
31+
}
32+
}
33+
34+
/// Builds a new test subgraph with a custom deploy command.
35+
pub async fn new_with_custom_cmd(name: &str, subgraph_name: &str, deploy_cmd: &str) -> Self {
36+
let subgraph_name = SubgraphName::new(subgraph_name).unwrap();
37+
let test_dir = format!("./runner-tests/{}", subgraph_name);
38+
39+
let (stores, hash) = tokio::join!(
40+
stores(name, "./runner-tests/config.simple.toml"),
41+
build_subgraph(&test_dir, Some(deploy_cmd))
42+
);
43+
44+
Self {
45+
stores,
46+
test_info: TestInfo {
47+
test_dir,
48+
test_name: name.to_string(),
49+
subgraph_name,
50+
hash,
51+
},
52+
}
53+
}
54+
55+
pub async fn new_with_file_link_resolver(
56+
name: &str,
57+
subgraph_name: &str,
58+
manifest: &str,
59+
) -> Self {
60+
let subgraph_name = SubgraphName::new(subgraph_name).unwrap();
61+
let test_dir = format!("./runner-tests/{}", subgraph_name);
62+
63+
let stores = stores(name, "./runner-tests/config.simple.toml").await;
64+
build_subgraph(&test_dir, None).await;
65+
let hash = DeploymentHash::new(manifest).unwrap();
66+
Self {
67+
stores,
68+
test_info: TestInfo {
69+
test_dir,
70+
test_name: name.to_string(),
71+
subgraph_name,
72+
hash,
73+
},
74+
}
75+
}
76+
}
77+
78+
/// deploy_cmd is the command to run to deploy the subgraph. If it is None, the
79+
/// default `yarn deploy:test` is used.
80+
async fn build_subgraph(dir: &str, deploy_cmd: Option<&str>) -> DeploymentHash {
81+
build_subgraph_with_yarn_cmd(dir, deploy_cmd.unwrap_or("deploy:test")).await
82+
}
83+
84+
async fn build_subgraph_with_yarn_cmd(dir: &str, yarn_cmd: &str) -> DeploymentHash {
85+
build_subgraph_with_yarn_cmd_and_arg(dir, yarn_cmd, None).await
86+
}
87+
88+
pub async fn build_subgraph_with_yarn_cmd_and_arg(
89+
dir: &str,
90+
yarn_cmd: &str,
91+
arg: Option<&str>,
92+
) -> DeploymentHash {
93+
// Test that IPFS is up.
94+
ipfs::IpfsRpcClient::new(ipfs::ServerAddress::local_rpc_api(), &graph::log::discard())
95+
.await
96+
.expect("Could not connect to IPFS, make sure it's running at port 5001");
97+
98+
// Make sure dependencies are present.
99+
100+
run_cmd(
101+
Command::new("yarn")
102+
.arg("install")
103+
.arg("--mutex")
104+
.arg("file:.yarn-mutex")
105+
.current_dir("./runner-tests/"),
106+
);
107+
108+
// Run codegen.
109+
run_cmd(Command::new("yarn").arg("codegen").current_dir(dir));
110+
111+
let mut args = vec![yarn_cmd];
112+
args.extend(arg);
113+
114+
// Run `deploy` for the side effect of uploading to IPFS, the graph node url
115+
// is fake and the actual deploy call is meant to fail.
116+
let deploy_output = run_cmd(
117+
Command::new("yarn")
118+
.args(&args)
119+
.env("IPFS_URI", "http://127.0.0.1:5001")
120+
.env("GRAPH_NODE_ADMIN_URI", "http://localhost:0")
121+
.current_dir(dir),
122+
);
123+
124+
// Hack to extract deployment id from `graph deploy` output.
125+
const ID_PREFIX: &str = "Build completed: ";
126+
let Some(mut line) = deploy_output.lines().find(|line| line.contains(ID_PREFIX)) else {
127+
panic!("No deployment id found, graph deploy probably had an error")
128+
};
129+
if !line.starts_with(ID_PREFIX) {
130+
line = &line[5..line.len() - 5]; // workaround for colored output
131+
}
132+
DeploymentHash::new(line.trim_start_matches(ID_PREFIX)).unwrap()
133+
}

tests/tests/runner_tests.rs

+4-183
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::marker::PhantomData;
2-
use std::process::Command;
32
use std::str::FromStr;
43
use std::sync::atomic::{self, AtomicBool};
54
use std::sync::Arc;
@@ -13,93 +12,24 @@ use graph::data::subgraph::schema::{SubgraphError, SubgraphHealth};
1312
use graph::data::value::Word;
1413
use graph::data_source::CausalityRegion;
1514
use graph::env::{EnvVars, TEST_WITH_NO_REORG};
16-
use graph::ipfs;
1715
use graph::ipfs::test_utils::add_files_to_local_ipfs_node_for_testing;
1816
use graph::object;
1917
use graph::prelude::ethabi::ethereum_types::H256;
2018
use graph::prelude::web3::types::Address;
21-
use graph::prelude::{
22-
hex, CheapClone, DeploymentHash, SubgraphAssignmentProvider, SubgraphName, SubgraphStore,
23-
};
19+
use graph::prelude::{hex, CheapClone, SubgraphAssignmentProvider, SubgraphName, SubgraphStore};
2420
use graph_tests::fixture::ethereum::{
2521
chain, empty_block, generate_empty_blocks_for_range, genesis, push_test_command, push_test_log,
2622
push_test_polling_trigger,
2723
};
2824

2925
use graph_tests::fixture::substreams::chain as substreams_chain;
3026
use graph_tests::fixture::{
31-
self, stores, test_ptr, test_ptr_reorged, MockAdapterSelector, NoopAdapterSelector, Stores,
32-
TestChainTrait, TestContext, TestInfo,
27+
self, test_ptr, test_ptr_reorged, MockAdapterSelector, NoopAdapterSelector, TestChainTrait,
28+
TestContext, TestInfo,
3329
};
34-
use graph_tests::helpers::run_cmd;
30+
use graph_tests::recipe::{build_subgraph_with_yarn_cmd_and_arg, RunnerTestRecipe};
3531
use slog::{o, Discard, Logger};
3632

37-
struct RunnerTestRecipe {
38-
pub stores: Stores,
39-
pub test_info: TestInfo,
40-
}
41-
42-
impl RunnerTestRecipe {
43-
async fn new(test_name: &str, subgraph_name: &str) -> Self {
44-
let subgraph_name = SubgraphName::new(subgraph_name).unwrap();
45-
let test_dir = format!("./runner-tests/{}", subgraph_name);
46-
47-
let (stores, hash) = tokio::join!(
48-
stores(test_name, "./runner-tests/config.simple.toml"),
49-
build_subgraph(&test_dir, None)
50-
);
51-
52-
Self {
53-
stores,
54-
test_info: TestInfo {
55-
test_dir,
56-
test_name: test_name.to_string(),
57-
subgraph_name,
58-
hash,
59-
},
60-
}
61-
}
62-
63-
/// Builds a new test subgraph with a custom deploy command.
64-
async fn new_with_custom_cmd(name: &str, subgraph_name: &str, deploy_cmd: &str) -> Self {
65-
let subgraph_name = SubgraphName::new(subgraph_name).unwrap();
66-
let test_dir = format!("./runner-tests/{}", subgraph_name);
67-
68-
let (stores, hash) = tokio::join!(
69-
stores(name, "./runner-tests/config.simple.toml"),
70-
build_subgraph(&test_dir, Some(deploy_cmd))
71-
);
72-
73-
Self {
74-
stores,
75-
test_info: TestInfo {
76-
test_dir,
77-
test_name: name.to_string(),
78-
subgraph_name,
79-
hash,
80-
},
81-
}
82-
}
83-
84-
async fn new_with_file_link_resolver(name: &str, subgraph_name: &str, manifest: &str) -> Self {
85-
let subgraph_name = SubgraphName::new(subgraph_name).unwrap();
86-
let test_dir = format!("./runner-tests/{}", subgraph_name);
87-
88-
let stores = stores(name, "./runner-tests/config.simple.toml").await;
89-
build_subgraph(&test_dir, None).await;
90-
let hash = DeploymentHash::new(manifest).unwrap();
91-
Self {
92-
stores,
93-
test_info: TestInfo {
94-
test_dir,
95-
test_name: name.to_string(),
96-
subgraph_name,
97-
hash,
98-
},
99-
}
100-
}
101-
}
102-
10333
fn assert_eq_ignore_backtrace(err: &SubgraphError, expected: &SubgraphError) {
10434
let equal = {
10535
if err.subgraph_id != expected.subgraph_id
@@ -1170,58 +1100,6 @@ async fn retry_create_ds() {
11701100
assert_eq!(runner.context().hosts_len(), 2);
11711101
}
11721102

1173-
#[tokio::test]
1174-
async fn file_link_resolver() -> anyhow::Result<()> {
1175-
let RunnerTestRecipe { stores, test_info } = RunnerTestRecipe::new_with_file_link_resolver(
1176-
"file_link_resolver",
1177-
"file-link-resolver",
1178-
"subgraph.yaml",
1179-
)
1180-
.await;
1181-
1182-
let blocks = {
1183-
let block_0 = genesis();
1184-
let block_1 = empty_block(block_0.ptr(), test_ptr(1));
1185-
let block_2 = empty_block(block_1.ptr(), test_ptr(2));
1186-
let block_3 = empty_block(block_2.ptr(), test_ptr(3));
1187-
1188-
vec![block_0, block_1, block_2, block_3]
1189-
};
1190-
1191-
let chain = chain(&test_info.test_name, blocks, &stores, None).await;
1192-
1193-
let ctx = fixture::setup_with_file_link_resolver(&test_info, &stores, &chain, None, None).await;
1194-
ctx.start_and_sync_to(test_ptr(3)).await;
1195-
let query = r#"{ blocks(first: 4, orderBy: number) { id, hash } }"#;
1196-
let query_res = ctx.query(query).await.unwrap();
1197-
1198-
assert_eq!(
1199-
query_res,
1200-
Some(object! {
1201-
blocks: vec![
1202-
object! {
1203-
id: test_ptr(0).number.to_string(),
1204-
hash: format!("0x{}", test_ptr(0).hash_hex()),
1205-
},
1206-
object! {
1207-
id: test_ptr(1).number.to_string(),
1208-
hash: format!("0x{}", test_ptr(1).hash_hex()),
1209-
},
1210-
object! {
1211-
id: test_ptr(2).number.to_string(),
1212-
hash: format!("0x{}", test_ptr(2).hash_hex()),
1213-
},
1214-
object! {
1215-
id: test_ptr(3).number.to_string(),
1216-
hash: format!("0x{}", test_ptr(3).hash_hex()),
1217-
},
1218-
]
1219-
})
1220-
);
1221-
1222-
Ok(())
1223-
}
1224-
12251103
#[tokio::test]
12261104
async fn fatal_error() -> anyhow::Result<()> {
12271105
let RunnerTestRecipe { stores, test_info } =
@@ -1348,60 +1226,3 @@ async fn arweave_file_data_sources() {
13481226
Some(object! { file: object!{ id: id, content: content.clone() } })
13491227
);
13501228
}
1351-
1352-
/// deploy_cmd is the command to run to deploy the subgraph. If it is None, the
1353-
/// default `yarn deploy:test` is used.
1354-
async fn build_subgraph(dir: &str, deploy_cmd: Option<&str>) -> DeploymentHash {
1355-
build_subgraph_with_yarn_cmd(dir, deploy_cmd.unwrap_or("deploy:test")).await
1356-
}
1357-
1358-
async fn build_subgraph_with_yarn_cmd(dir: &str, yarn_cmd: &str) -> DeploymentHash {
1359-
build_subgraph_with_yarn_cmd_and_arg(dir, yarn_cmd, None).await
1360-
}
1361-
1362-
async fn build_subgraph_with_yarn_cmd_and_arg(
1363-
dir: &str,
1364-
yarn_cmd: &str,
1365-
arg: Option<&str>,
1366-
) -> DeploymentHash {
1367-
// Test that IPFS is up.
1368-
ipfs::IpfsRpcClient::new(ipfs::ServerAddress::local_rpc_api(), &graph::log::discard())
1369-
.await
1370-
.expect("Could not connect to IPFS, make sure it's running at port 5001");
1371-
1372-
// Make sure dependencies are present.
1373-
1374-
run_cmd(
1375-
Command::new("yarn")
1376-
.arg("install")
1377-
.arg("--mutex")
1378-
.arg("file:.yarn-mutex")
1379-
.current_dir("./runner-tests/"),
1380-
);
1381-
1382-
// Run codegen.
1383-
run_cmd(Command::new("yarn").arg("codegen").current_dir(dir));
1384-
1385-
let mut args = vec![yarn_cmd];
1386-
args.extend(arg);
1387-
1388-
// Run `deploy` for the side effect of uploading to IPFS, the graph node url
1389-
// is fake and the actual deploy call is meant to fail.
1390-
let deploy_output = run_cmd(
1391-
Command::new("yarn")
1392-
.args(&args)
1393-
.env("IPFS_URI", "http://127.0.0.1:5001")
1394-
.env("GRAPH_NODE_ADMIN_URI", "http://localhost:0")
1395-
.current_dir(dir),
1396-
);
1397-
1398-
// Hack to extract deployment id from `graph deploy` output.
1399-
const ID_PREFIX: &str = "Build completed: ";
1400-
let Some(mut line) = deploy_output.lines().find(|line| line.contains(ID_PREFIX)) else {
1401-
panic!("No deployment id found, graph deploy probably had an error")
1402-
};
1403-
if !line.starts_with(ID_PREFIX) {
1404-
line = &line[5..line.len() - 5]; // workaround for colored output
1405-
}
1406-
DeploymentHash::new(line.trim_start_matches(ID_PREFIX)).unwrap()
1407-
}

0 commit comments

Comments
 (0)