|
| 1 | +// Copyright 2025-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::{env, process::Command}; |
| 5 | + |
| 6 | +use anyhow::{anyhow, Context, Result}; |
| 7 | +use regex::Regex; |
| 8 | + |
| 9 | +/// Minimal wrapper around the `indexer-cli` Docker service used in integration tests. |
| 10 | +/// |
| 11 | +/// This struct is intentionally tiny; we only need the docker container name and |
| 12 | +/// the target network (e.g., "hardhat"). |
| 13 | +/// this abstraction is planned to be used in our integration tests |
| 14 | +/// to close allocations programmatically along with getting allocations statuses and information |
| 15 | +/// this is still a WIP, we found some issues with the indexer-cli container, that needs more |
| 16 | +/// investigation(the indexer-cli package itsefl seems to have a bug) |
| 17 | +pub struct IndexerCli { |
| 18 | + container: String, |
| 19 | + network: String, |
| 20 | +} |
| 21 | + |
| 22 | +impl IndexerCli { |
| 23 | + /// Create a new wrapper. |
| 24 | + /// |
| 25 | + /// - `network` is the Graph network argument passed to `graph indexer ... --network {network}` |
| 26 | + /// - Container name defaults to "indexer-cli" and can be overridden by env `INDEXER_CLI_CONTAINER`. |
| 27 | + pub fn new<S: Into<String>>(network: S) -> Self { |
| 28 | + // Prefer explicit override via INDEXER_CLI_CONTAINER, fall back to CONTAINER_NAME |
| 29 | + // (as mentioned in contrib/indexer-cli/README.md), then to the default "indexer-cli". |
| 30 | + let container = env::var("CONTAINER_NAME").unwrap_or_else(|_| "indexer-cli".to_string()); |
| 31 | + Self { |
| 32 | + container, |
| 33 | + network: network.into(), |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// List allocation IDs by invoking: |
| 38 | + /// docker exec {container} graph indexer allocations get --network {network} |
| 39 | + /// |
| 40 | + /// Returns a list of 0x-prefixed addresses extracted from stdout in order of appearance (deduplicated). |
| 41 | + pub fn list_allocations(&self) -> Result<Vec<String>> { |
| 42 | + let stdout = self.exec([ |
| 43 | + "graph", |
| 44 | + "indexer", |
| 45 | + "allocations", |
| 46 | + "get", |
| 47 | + "--network", |
| 48 | + &self.network, |
| 49 | + ])?; |
| 50 | + Ok(parse_eth_addresses(&stdout)) |
| 51 | + } |
| 52 | + |
| 53 | + /// Close a specific allocation by invoking: |
| 54 | + /// docker exec {container} graph indexer allocations close {allocation} <poi> --network {network} --force |
| 55 | + /// |
| 56 | + /// For integration testing convenience, we use a zero POI by default as in the README example. |
| 57 | + pub fn close_allocation(&self, allocation: &str) -> Result<()> { |
| 58 | + let re = Regex::new(r"^0x[a-fA-F0-9]{40}$").unwrap(); |
| 59 | + if !re.is_match(allocation) { |
| 60 | + return Err(anyhow!("Invalid allocation ID: {allocation}")); |
| 61 | + } |
| 62 | + // Zero POI (32 bytes of zeros) |
| 63 | + const ZERO_POI: &str = "0x0000000000000000000000000000000000000000000000000000000000000000"; |
| 64 | + let _stdout = self.exec([ |
| 65 | + "graph", |
| 66 | + "indexer", |
| 67 | + "allocations", |
| 68 | + "close", |
| 69 | + allocation, |
| 70 | + ZERO_POI, |
| 71 | + "--network", |
| 72 | + &self.network, |
| 73 | + "--force", |
| 74 | + ])?; |
| 75 | + Ok(()) |
| 76 | + } |
| 77 | + |
| 78 | + /// Helper to run `docker exec {container} ...` and capture stdout/stderr. |
| 79 | + fn exec<I, S>(&self, args: I) -> Result<String> |
| 80 | + where |
| 81 | + I: IntoIterator<Item = S>, |
| 82 | + S: AsRef<str>, |
| 83 | + { |
| 84 | + let mut cmd = Command::new("docker"); |
| 85 | + cmd.arg("exec").arg(&self.container); |
| 86 | + for a in args { |
| 87 | + cmd.arg(a.as_ref()); |
| 88 | + } |
| 89 | + let output = cmd.output().with_context(|| { |
| 90 | + format!( |
| 91 | + "failed to spawn docker exec for container {}", |
| 92 | + self.container |
| 93 | + ) |
| 94 | + })?; |
| 95 | + if !output.status.success() { |
| 96 | + return Err(anyhow!( |
| 97 | + "docker exec exited with {}: {}", |
| 98 | + output.status.code().unwrap_or(-1), |
| 99 | + String::from_utf8_lossy(&output.stderr) |
| 100 | + )); |
| 101 | + } |
| 102 | + Ok(String::from_utf8_lossy(&output.stdout).to_string()) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// Extract all 0x-addresses (40 hex chars) from an arbitrary output string, preserving order and de-duplicating. |
| 107 | +fn parse_eth_addresses(input: &str) -> Vec<String> { |
| 108 | + let re = Regex::new(r"0x[a-fA-F0-9]{40}").unwrap(); |
| 109 | + let mut seen = std::collections::HashSet::new(); |
| 110 | + let mut out = Vec::new(); |
| 111 | + for m in re.find_iter(input) { |
| 112 | + let addr = m.as_str().to_string(); |
| 113 | + if seen.insert(addr.clone()) { |
| 114 | + out.push(addr); |
| 115 | + } |
| 116 | + } |
| 117 | + out |
| 118 | +} |
| 119 | + |
| 120 | +#[cfg(test)] |
| 121 | +mod tests { |
| 122 | + use super::parse_eth_addresses; |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn parse_addresses_mixed_content() { |
| 126 | + let s = "some text 0xAbCdEf0123456789aBCdEf0123456789abCDef01 and again 0xabcdef0123456789abcdef0123456789abcdef01 and dup 0xabcdef0123456789abcdef0123456789abcdef01"; |
| 127 | + let addrs = parse_eth_addresses(s); |
| 128 | + assert_eq!(addrs.len(), 2); |
| 129 | + assert_eq!(addrs[0], "0xAbCdEf0123456789aBCdEf0123456789abCDef01"); |
| 130 | + assert_eq!(addrs[1], "0xabcdef0123456789abcdef0123456789abcdef01"); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn parse_addresses_none() { |
| 135 | + let s = "no addresses here"; |
| 136 | + let addrs = parse_eth_addresses(s); |
| 137 | + assert!(addrs.is_empty()); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn parse_addresses_case_insensitive() { |
| 142 | + let s = |
| 143 | + "0xABCDEF0123456789ABCDEF0123456789ABCDEF01 0xabcdef0123456789abcdef0123456789abcdef02"; |
| 144 | + let addrs = parse_eth_addresses(s); |
| 145 | + assert_eq!(addrs.len(), 2); |
| 146 | + } |
| 147 | +} |
0 commit comments