From 89b28f1dfeb892be9a268d91b306c4a7be1579f4 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Tue, 20 Jun 2023 15:53:30 +0200 Subject: [PATCH] feat(tests): introducing tests framework Signed-off-by: Vincenzo Palazzo --- Cargo.lock | 649 ++++++++++++++++++++++++-- Cargo.toml | 1 + Makefile | 7 +- coffee_core/Cargo.toml | 2 +- coffee_core/src/coffee.rs | 9 +- coffee_lib/src/errors.rs | 14 + coffee_lib/src/macros.rs | 9 +- coffee_lib/src/types/mod.rs | 16 +- coffee_plugin/Cargo.toml | 2 +- coffee_testing/Cargo.toml | 16 + coffee_testing/src/btc.rs | 103 ++++ coffee_testing/src/cln.rs | 106 +++++ coffee_testing/src/lib.rs | 106 +++++ tests/Cargo.toml | 14 + tests/src/coffee_integration_tests.rs | 90 ++++ tests/src/lib.rs | 4 + tests/src/logger.rs | 63 +++ 17 files changed, 1153 insertions(+), 58 deletions(-) create mode 100644 coffee_testing/Cargo.toml create mode 100644 coffee_testing/src/btc.rs create mode 100644 coffee_testing/src/cln.rs create mode 100644 coffee_testing/src/lib.rs create mode 100644 tests/Cargo.toml create mode 100644 tests/src/coffee_integration_tests.rs create mode 100644 tests/src/lib.rs create mode 100644 tests/src/logger.rs diff --git a/Cargo.lock b/Cargo.lock index 04f402ca..38f678c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,7 +30,7 @@ dependencies = [ "actix-service", "actix-utils", "ahash 0.8.3", - "base64", + "base64 0.21.0", "bitflags 1.3.2", "brotli", "bytes", @@ -49,7 +49,7 @@ dependencies = [ "mime", "percent-encoding", "pin-project-lite", - "rand", + "rand 0.8.5", "sha1", "smallvec", "tokio", @@ -244,9 +244,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.70" +version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" +checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" [[package]] name = "async-trait" @@ -276,12 +276,152 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "base16ct" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" + +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "base64" version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" +[[package]] +name = "base64-compat" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a8d4d2746f89841e49230dd26917df1876050f95abafafbe34f47cb534b88d7" +dependencies = [ + "byteorder", +] + +[[package]] +name = "base64ct" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" + +[[package]] +name = "bech32" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9ff0bbfd639f15c74af777d81383cf53efb7c93613f6cab67c6c11e05bbf8b" + +[[package]] +name = "bech32" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" + +[[package]] +name = "bitcoin" +version = "0.28.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d30fb43d287492017964a1fd7d3f82e8cc760818471c6ef2d44111e317d5c3" +dependencies = [ + "bech32 0.8.1", + "bitcoin_hashes 0.10.0", + "secp256k1 0.22.2", + "serde", +] + +[[package]] +name = "bitcoin" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b36f4c848f6bd9ff208128f08751135846cc23ae57d66ab10a22efff1c675f3c" +dependencies = [ + "bech32 0.9.1", + "bitcoin-private", + "bitcoin_hashes 0.12.0", + "hex_lit", + "secp256k1 0.27.0", + "serde", +] + +[[package]] +name = "bitcoin-private" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73290177011694f38ec25e165d0387ab7ea749a4b81cd4c80dae5988229f7a57" + +[[package]] +name = "bitcoin_hashes" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "006cc91e1a1d99819bc5b8214be3555c1f0611b169f527a1fdc54ed1f2b745b0" +dependencies = [ + "serde", +] + +[[package]] +name = "bitcoin_hashes" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d7066118b13d4b20b23645932dfb3a81ce7e29f95726c2036fa33cd7b092501" +dependencies = [ + "bitcoin-private", + "serde", +] + +[[package]] +name = "bitcoincore-rpc" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0e67dbf7a9971e7f4276f6089e9e814ce0f624a03216b7d92d00351ae7fb3e" +dependencies = [ + "bitcoincore-rpc-json 0.15.0", + "jsonrpc 0.12.1", + "log", + "serde", + "serde_json", +] + +[[package]] +name = "bitcoincore-rpc" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d6c0ee9354e3dac217db4cb1dd31941073a87fe53c86bcf3eb2b8bc97f00a08" +dependencies = [ + "bitcoin-private", + "bitcoincore-rpc-json 0.17.0", + "jsonrpc 0.14.1", + "log", + "serde", + "serde_json", +] + +[[package]] +name = "bitcoincore-rpc-json" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e2ae16202721ba8c3409045681fac790a5ddc791f05731a2df22c0c6bffc0f1" +dependencies = [ + "bitcoin 0.28.2", + "serde", + "serde_json", +] + +[[package]] +name = "bitcoincore-rpc-json" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d30ce6f40fb0a2e8d98522796219282504b7a4b14e2b4c26139a7bea6aec6586" +dependencies = [ + "bitcoin 0.30.0", + "bitcoin-private", + "serde", + "serde_json", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -324,6 +464,12 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "bytes" version = "1.4.0" @@ -391,11 +537,41 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "clightningrpc" +version = "0.3.0-beta.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78fb539a9ff076240519d1c6693d822791c697910718c22cf19e90b82ed191b5" +dependencies = [ + "clightningrpc-common 0.3.0-beta.4 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", +] + +[[package]] +name = "clightningrpc" +version = "0.3.0-beta.6" +source = "git+https://github.com/laanwj/cln4rust.git?branch=macros/test-framework#dd65567e170537bb1c86ed233c77d61ff20d0bde" +dependencies = [ + "clightningrpc-common 0.3.0-beta.4 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", +] + [[package]] name = "clightningrpc-common" -version = "0.3.0-beta.3" +version = "0.3.0-beta.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04d9821ddb8b5eee8f922fea6871f8684cf3eb74a1cb54f6a6782df70fe28b37" +checksum = "6dc4030d1ff7bf3c8705b48c8fd73319a2c7cfccae086399c6b3a6c07026cf5a" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "clightningrpc-common" +version = "0.3.0-beta.4" +source = "git+https://github.com/laanwj/cln4rust.git?branch=macros/test-framework#dd65567e170537bb1c86ed233c77d61ff20d0bde" dependencies = [ "serde", "serde_json", @@ -413,22 +589,72 @@ dependencies = [ [[package]] name = "clightningrpc-plugin" -version = "0.3.0-beta.7" +version = "0.3.0-beta.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "653c5c4427d241cf13d5aba6d2f30e4a594167a925391c2394050956f30789c2" +checksum = "9807dd1f895fadfc9fe8e9f355cabbccf994d4c7b0e3a921726679faa3e2713d" dependencies = [ - "clightningrpc-common", + "clightningrpc-common 0.3.0-beta.4 (registry+https://github.com/rust-lang/crates.io-index)", "serde", "serde_json", ] +[[package]] +name = "cln-btc-test" +version = "0.1.0" +source = "git+https://github.com/laanwj/cln4rust.git?branch=macros/test-framework#dd65567e170537bb1c86ed233c77d61ff20d0bde" +dependencies = [ + "bitcoin 0.28.2", + "bitcoin_hashes 0.10.0", + "bitcoincore-rpc 0.15.0", + "bitcoincore-rpc-json 0.15.0", + "env_logger", + "futures", + "log", + "nix", + "p256", + "port-selector", + "rand_core 0.6.4", + "serde", + "serde_json", + "tempdir", + "thiserror", + "tokio", +] + +[[package]] +name = "cln-test" +version = "0.1.0" +source = "git+https://github.com/laanwj/cln4rust.git?branch=macros/test-framework#dd65567e170537bb1c86ed233c77d61ff20d0bde" +dependencies = [ + "anyhow", + "bitcoin 0.28.2", + "bitcoin_hashes 0.10.0", + "bitcoincore-rpc 0.15.0", + "bitcoincore-rpc-json 0.15.0", + "clightningrpc 0.3.0-beta.6 (git+https://github.com/laanwj/cln4rust.git?branch=macros/test-framework)", + "clightningrpc-common 0.3.0-beta.4 (git+https://github.com/laanwj/cln4rust.git?branch=macros/test-framework)", + "cln-btc-test", + "env_logger", + "futures", + "log", + "nix", + "p256", + "port-selector", + "rand_core 0.6.4", + "serde", + "serde_json", + "tempdir", + "thiserror", + "tokio", +] + [[package]] name = "coffee" version = "0.0.1-alpha.1" dependencies = [ "async-trait", "clap", - "clightningrpc-common", + "clightningrpc-common 0.3.0-beta.4 (registry+https://github.com/rust-lang/crates.io-index)", "clightningrpc-conf", "coffee_core", "coffee_lib", @@ -446,7 +672,7 @@ name = "coffee_core" version = "0.0.1-alpha.1" dependencies = [ "async-trait", - "clightningrpc-common", + "clightningrpc-common 0.3.0-beta.4 (registry+https://github.com/rust-lang/crates.io-index)", "clightningrpc-conf", "coffee_github", "coffee_lib", @@ -505,7 +731,7 @@ dependencies = [ name = "coffee_plugin" version = "0.1.0" dependencies = [ - "clightningrpc-common", + "clightningrpc-common 0.3.0-beta.4 (registry+https://github.com/rust-lang/crates.io-index)", "clightningrpc-plugin", "coffee_core", "serde_json", @@ -523,6 +749,22 @@ dependencies = [ "tokio", ] +[[package]] +name = "coffee_testing" +version = "0.1.0" +dependencies = [ + "anyhow", + "bitcoincore-rpc 0.17.0", + "clightningrpc 0.3.0-beta.6 (registry+https://github.com/rust-lang/crates.io-index)", + "cln-btc-test", + "cln-test", + "coffee_core", + "log", + "port-selector", + "tempfile", + "tokio", +] + [[package]] name = "concolor" version = "0.0.12" @@ -532,6 +774,12 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "const-oid" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520fbf3c07483f94e3e3ca9d0cfd913d7718ef2483d2cfd91c0d9e91474ab913" + [[package]] name = "convert_case" version = "0.4.0" @@ -620,7 +868,7 @@ dependencies = [ "autocfg", "cfg-if", "crossbeam-utils", - "memoffset", + "memoffset 0.8.0", "scopeguard", ] @@ -668,6 +916,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "crypto-bigint" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef2b4b23cddf68b89b8f8069890e8c270d54e2d5fe1b143820234805e4cb17ef" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "subtle", + "zeroize", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -678,6 +938,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "der" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1a467a65c5e759bce6e65eaf91cc29f466cdc57cb65777bd646872a8a1fd4de" +dependencies = [ + "const-oid", + "zeroize", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -693,12 +963,13 @@ dependencies = [ [[package]] name = "digest" -version = "0.10.6" +version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -707,12 +978,44 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68b0cf012f1230e43cd00ebb729c6bb58707ecfa8ad08b52ef3a4ccd2697fc30" +[[package]] +name = "ecdsa" +version = "0.14.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413301934810f597c1d19ca71c8710e99a3f1ba28a0d2ebc01551a2daeea3c5c" +dependencies = [ + "der", + "elliptic-curve", + "rfc6979", + "signature", +] + [[package]] name = "either" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "elliptic-curve" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7bb888ab5300a19b8e5bceef25ac745ad065f3c9f7efc6de1b91958110891d3" +dependencies = [ + "base16ct", + "crypto-bigint", + "der", + "digest", + "ff", + "generic-array", + "group", + "pkcs8", + "rand_core 0.6.4", + "sec1", + "subtle", + "zeroize", +] + [[package]] name = "encoding_rs" version = "0.8.32" @@ -776,6 +1079,16 @@ dependencies = [ "instant", ] +[[package]] +name = "ff" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d013fc25338cc558c5c2cfbad646908fb23591e2404481826742b651c9af7160" +dependencies = [ + "rand_core 0.6.4", + "subtle", +] + [[package]] name = "flate2" version = "1.0.25" @@ -801,6 +1114,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "futures" version = "0.3.27" @@ -926,6 +1245,17 @@ dependencies = [ "url", ] +[[package]] +name = "group" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfbfb3a6cfbd390d5c9564ab283a0349b9b9fcd46a706c1eb10e0db70bfbac7" +dependencies = [ + "ff", + "rand_core 0.6.4", + "subtle", +] + [[package]] name = "h2" version = "0.3.17" @@ -981,6 +1311,21 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" +[[package]] +name = "hex_lit" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "http" version = "0.2.9" @@ -1058,13 +1403,13 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ "hermit-abi 0.3.1", "libc", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -1103,6 +1448,29 @@ dependencies = [ "libc", ] +[[package]] +name = "jsonrpc" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f8423b78fc94d12ef1a4a9d13c348c9a78766dda0cc18817adf0faf77e670c8" +dependencies = [ + "base64-compat", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "jsonrpc" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8128f36b47411cd3f044be8c1f5cc0c9e24d1d1bfdc45f0a57897b32513053f2" +dependencies = [ + "base64 0.13.1", + "serde", + "serde_json", +] + [[package]] name = "language-tags" version = "0.3.2" @@ -1117,9 +1485,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.140" +version = "0.2.146" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" +checksum = "f92be4933c13fd498862a9e02a3055f8a8d9c039ce33db97306fd5a6caa7f29b" [[package]] name = "libgit2-sys" @@ -1169,9 +1537,9 @@ checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" [[package]] name = "linux-raw-sys" -version = "0.3.1" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" +checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" [[package]] name = "local-channel" @@ -1203,12 +1571,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.17" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "memchr" @@ -1216,6 +1581,15 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + [[package]] name = "memoffset" version = "0.8.0" @@ -1280,6 +1654,18 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "nix" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "libc", + "memoffset 0.6.5", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -1327,6 +1713,17 @@ version = "6.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" +[[package]] +name = "p256" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51f44edd08f51e2ade572f141051021c5af22677e42b7dd28a88155151c33594" +dependencies = [ + "ecdsa", + "elliptic-curve", + "sha2", +] + [[package]] name = "paperclip" version = "0.8.0" @@ -1447,12 +1844,31 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkcs8" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eca2c590a5f85da82668fa685c09ce2888b9430e83299debf1f34b65fd4a4ba" +dependencies = [ + "der", + "spki", +] + [[package]] name = "pkg-config" version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" +[[package]] +name = "port-selector" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd119ef551a50cd8939f0ff93bd062891f7b0dbb771b4a05df8a9c13aebaff68" +dependencies = [ + "rand 0.8.5", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1516,6 +1932,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + [[package]] name = "rand" version = "0.8.5" @@ -1524,7 +1953,7 @@ checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", - "rand_core", + "rand_core 0.6.4", ] [[package]] @@ -1534,9 +1963,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.6.4" @@ -1546,6 +1990,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -1590,6 +2043,26 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "rfc6979" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7743f17af12fa0b03b803ba12cd6a8d9483a587e89c69445e3909655c0b9fabb" +dependencies = [ + "crypto-bigint", + "hmac", + "zeroize", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -1615,16 +2088,16 @@ dependencies = [ [[package]] name = "rustix" -version = "0.37.7" +version = "0.37.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2aae838e49b3d63e9274e1c01833cc8139d3fec468c3b84688c628f44b1ae11d" +checksum = "b96e891d04aa506a6d1f318d2771bcb1c7dfda84e126660ace067c9b474bb2c0" dependencies = [ "bitflags 1.3.2", "errno 0.3.1", "io-lifetimes", "libc", - "linux-raw-sys 0.3.1", - "windows-sys 0.45.0", + "linux-raw-sys 0.3.8", + "windows-sys 0.48.0", ] [[package]] @@ -1654,6 +2127,60 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "sec1" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3be24c1842290c45df0a7bf069e0c268a747ad05a192f2fd7dcfdbc1cba40928" +dependencies = [ + "base16ct", + "der", + "generic-array", + "pkcs8", + "subtle", + "zeroize", +] + +[[package]] +name = "secp256k1" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "295642060261c80709ac034f52fca8e5a9fa2c7d341ded5cdb164b7c33768b2a" +dependencies = [ + "secp256k1-sys 0.5.2", + "serde", +] + +[[package]] +name = "secp256k1" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" +dependencies = [ + "bitcoin_hashes 0.12.0", + "rand 0.8.5", + "secp256k1-sys 0.8.1", + "serde", +] + +[[package]] +name = "secp256k1-sys" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "152e20a0fd0519390fc43ab404663af8a0b794273d2a91d60ad4a39f13ffe110" +dependencies = [ + "cc", +] + +[[package]] +name = "secp256k1-sys" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e" +dependencies = [ + "cc", +] + [[package]] name = "semver" version = "1.0.17" @@ -1727,6 +2254,17 @@ dependencies = [ "digest", ] +[[package]] +name = "sha2" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "signal-hook" version = "0.3.15" @@ -1757,6 +2295,16 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "1.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" +dependencies = [ + "digest", + "rand_core 0.6.4", +] + [[package]] name = "slab" version = "0.4.8" @@ -1782,6 +2330,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "spki" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67cf02bbac7a337dc36e4f5a693db6c21e7863f45070f7064577eb4367a3212b" +dependencies = [ + "base64ct", + "der", +] + [[package]] name = "strsim" version = "0.10.0" @@ -1807,6 +2365,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "syn" version = "1.0.109" @@ -1829,17 +2393,28 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +dependencies = [ + "rand 0.4.6", + "remove_dir_all", +] + [[package]] name = "tempfile" -version = "3.5.0" +version = "3.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" +checksum = "31c0432476357e58790aaa47a8efb0c5138f137343f3b5f23bd36a27e3b0a6d6" dependencies = [ + "autocfg", "cfg-if", "fastrand", "redox_syscall 0.3.5", - "rustix 0.37.7", - "windows-sys 0.45.0", + "rustix 0.37.20", + "windows-sys 0.48.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ed79f6e4..7b3fa041 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ members = [ "coffee_cmd", "coffee_httpd", "coffee_plugin", + "coffee_testing", ] resolver = "2" diff --git a/Makefile b/Makefile index ca7ce2cc..29ddb662 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=cargo FMT=fmt -OPTIONS= +ARGS="--all" default: fmt $(CC) build @@ -14,7 +14,7 @@ fmt: $(CC) fmt --all check: - $(CC) test --all + $(CC) test $(ARGS) example: @echo "No example for the moment" @@ -30,3 +30,6 @@ dev-book: install: $(CC) install --locked --path ./coffee_cmd + +integration: + cd tests; $(CC) test $(ARGS) diff --git a/coffee_core/Cargo.toml b/coffee_core/Cargo.toml index 7ebb5312..af477514 100644 --- a/coffee_core/Cargo.toml +++ b/coffee_core/Cargo.toml @@ -15,4 +15,4 @@ coffee_storage = { path = "../coffee_storage" } serde = { version = "1.0", features = ["derive"] } serde_json = "1" clightningrpc-conf = "0.0.3" -clightningrpc-common = "0.3.0-beta.3" +clightningrpc-common = "0.3.0-beta.4" diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index abeef01f..f4711935 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -218,6 +218,7 @@ impl PluginManager for CoffeeManager { if let Some(mut plugin) = repo.get_plugin_by_name(plugin) { log::trace!("{:#?}", plugin); let result = plugin.configure(verbose).await; + log::debug!("result from plugin configure: {:?}", result); match result { Ok(path) => { log::debug!("runnable plugin path {path}"); @@ -238,11 +239,9 @@ impl PluginManager for CoffeeManager { } } } - let err = CoffeeError::new( - 1, - &format!("plugin `{plugin}` are not present inside the repositories"), - ); - Err(err) + Err(error!( + "plugin `{plugin}` are not present inside the repositories" + )) } async fn remove(&mut self, plugin: &str) -> Result { diff --git a/coffee_lib/src/errors.rs b/coffee_lib/src/errors.rs index 1b9f4c0b..0a214a23 100644 --- a/coffee_lib/src/errors.rs +++ b/coffee_lib/src/errors.rs @@ -20,6 +20,20 @@ impl CoffeeError { } } +impl std::error::Error for CoffeeError { + fn cause(&self) -> Option<&dyn std::error::Error> { + Some(self) + } + + fn description(&self) -> &str { + &self.msg + } + + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(self) + } +} + impl fmt::Display for CoffeeError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "code: {}, msg: {}", self.code, self.msg) diff --git a/coffee_lib/src/macros.rs b/coffee_lib/src/macros.rs index 83d230ba..8825ff86 100644 --- a/coffee_lib/src/macros.rs +++ b/coffee_lib/src/macros.rs @@ -34,10 +34,11 @@ macro_rules! sh { }; if !command.status.success() { - return Err(CoffeeError::new( - 2, - &String::from_utf8(command.stderr).unwrap(), - )); + let mut content = String::from_utf8(command.stderr).unwrap(); + if content.trim().is_empty() { + content = String::from_utf8(command.stdout).unwrap(); + } + return Err(CoffeeError::new(2, &content)); } } }; diff --git a/coffee_lib/src/types/mod.rs b/coffee_lib/src/types/mod.rs index 75f437d3..a851b722 100644 --- a/coffee_lib/src/types/mod.rs +++ b/coffee_lib/src/types/mod.rs @@ -3,46 +3,46 @@ use serde::{Deserialize, Serialize}; use crate::plugin::Plugin; -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct CoffeeRemove { pub plugin: Plugin, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct CoffeeList { pub plugins: Vec, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct CoffeeRemote { pub remotes: Option>, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct CoffeeListRemote { pub local_name: String, pub url: String, pub plugins: Vec, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub enum NurseStatus { Corrupted, Sane, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct CoffeeNurse { pub status: NurseStatus, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub enum UpgradeStatus { UpToDate, Updated, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct CoffeeUpgrade { pub repo: String, pub status: UpgradeStatus, diff --git a/coffee_plugin/Cargo.toml b/coffee_plugin/Cargo.toml index c3c3610a..6d01d77f 100644 --- a/coffee_plugin/Cargo.toml +++ b/coffee_plugin/Cargo.toml @@ -6,6 +6,6 @@ edition = "2021" [dependencies] tokio = { version = "1.22.0", features = ["rt"] } clightningrpc-common = "0.3.0-beta.3" -clightningrpc-plugin = "0.3.0-beta.7" +clightningrpc-plugin = "0.3.0-beta.8" coffee_core = { path = "../coffee_core" } serde_json = "1" diff --git a/coffee_testing/Cargo.toml b/coffee_testing/Cargo.toml new file mode 100644 index 00000000..6d5b7861 --- /dev/null +++ b/coffee_testing/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "coffee_testing" +version = "0.1.0" +edition = "2021" + +[dependencies] +clightningrpc = "0.3.0-beta.6" +bitcoincore-rpc = "0.17.0" +log = "0.4.19" +cln-test = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/test-framework" } +cln-btc-test = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/test-framework" } +coffee_core = { path = "../coffee_core" } +tempfile = "3.6.0" +port-selector = "0.1.6" +anyhow = "1.0.71" +tokio = { version = "1.22.0", features = ["process", "time"] } diff --git a/coffee_testing/src/btc.rs b/coffee_testing/src/btc.rs new file mode 100644 index 00000000..bcc81205 --- /dev/null +++ b/coffee_testing/src/btc.rs @@ -0,0 +1,103 @@ +//! Bitcoin Testing framework. +use bitcoincore_rpc::{Auth, Client, RpcApi}; +use port::Port; +use port_selector as port; +use tempfile::TempDir; + +pub mod macros { + #[macro_export] + macro_rules! bitcoind { + ($dir:expr, $port:expr, $opt_args:expr) => { + async { + use std::process::Stdio; + + use log; + use tokio::process::Command; + + let opt_args = format!($opt_args); + let args = opt_args.trim(); + let args_tok: Vec<&str> = args.split(" ").collect(); + log::debug!("additional args: {:?}", args_tok); + let mut command = Command::new("bitcoind"); + command + .args(&args_tok) + .arg(format!("-port={}", $port + 1)) + .arg(format!("-rpcport={}", $port)) + .arg(format!("-datadir={}", $dir.path().to_str().unwrap())) + .stdout(Stdio::null()) + .spawn() + } + .await + }; + ($dir:expr, $port:expr) => { + $crate::bitcoind!($dir, $port, "") + }; + } + + pub use bitcoind; +} + +pub struct BtcNode { + inner: Client, + pub user: String, + pub pass: String, + pub port: Port, + root_path: TempDir, + process: Vec, +} + +impl Drop for BtcNode { + fn drop(&mut self) { + for process in self.process.iter() { + let child = process.id().unwrap(); + let mut kill = std::process::Command::new("kill") + .args(["-s", "SIGKILL", &child.to_string()]) + .spawn() + .unwrap(); + kill.wait().unwrap(); + } + std::fs::remove_dir_all(self.root_path.path()).unwrap(); + } +} + +impl BtcNode { + pub async fn tmp() -> anyhow::Result { + let dir = tempfile::tempdir()?; + let user = "crab".to_owned(); + let pass = "crab".to_owned(); + let port = port::random_free_port().unwrap(); + let process = macros::bitcoind!( + dir, + port, + "-server -regtest -rpcuser={user} -rpcpassword={pass}" + )?; + let rpc = Client::new( + &format!("http://localhost:{port}"), + Auth::UserPass(user.clone(), pass.clone()), + )?; + let bg_process = vec![process]; + Ok(Self { + inner: rpc, + root_path: dir, + user, + pass, + port, + process: bg_process, + }) + } + + pub fn rpc(&self) -> &Client { + &self.inner + } + + pub async fn stop(&mut self) -> anyhow::Result<()> { + log::info!("stop bitcoin node"); + self.inner.stop()?; + for process in self.process.iter_mut() { + process.kill().await?; + let _ = process.wait().await?; + log::debug!("process killed"); + } + Ok(()) + } +} diff --git a/coffee_testing/src/cln.rs b/coffee_testing/src/cln.rs new file mode 100644 index 00000000..81818819 --- /dev/null +++ b/coffee_testing/src/cln.rs @@ -0,0 +1,106 @@ +//! Integration testing library for core lightning + +use clightningrpc::LightningRPC; +use port_selector as port; +use tempfile::TempDir; + +use crate::btc::BtcNode; +use crate::prelude::*; + +pub mod macros { + #[macro_export] + macro_rules! lightningd { + ($dir:expr, $port:expr, $($opt_args:tt)*) => { + async { + use std::process::Stdio; + + use tokio::process::Command; + + let opt_args = format!($($opt_args)*); + let args = opt_args.trim(); + let args_tok: Vec<&str> = args.split(" ").collect(); + + let mut command = Command::new("lightningd"); + command + .args(&args_tok) + .arg(format!("--addr=127.0.0.1:{}", $port)) + .arg(format!("--bind-addr=127.0.0.1:{}", $port)) + .arg(format!("--lightning-dir={}", $dir.path().to_str().unwrap())) + .arg("--dev-fast-gossip") + .arg("--funding-confirms=1") + .stdout(Stdio::null()) + .spawn() + }.await + }; + ($dir:expr, $port:expr) => { + $crate::lightningd!($dir, $port, "") + }; + } + + pub use lightningd; +} + +pub struct Node { + inner: LightningRPC, + root_path: TempDir, + bitcoin: BtcNode, + process: Vec, +} + +impl Drop for Node { + fn drop(&mut self) { + for process in self.process.iter() { + let child = process.id().unwrap(); + let mut kill = std::process::Command::new("kill") + .args(["-s", "SIGKILL", &child.to_string()]) + .spawn() + .unwrap(); + kill.wait().unwrap(); + } + + std::fs::remove_dir_all(self.root_path.path()).unwrap(); + } +} + +impl Node { + pub async fn tmp() -> anyhow::Result { + let btc = BtcNode::tmp().await?; + + let dir = tempfile::tempdir()?; + let process = macros::lightningd!( + dir, + port::random_free_port().unwrap(), + "--network=regtest --bitcoin-rpcuser={} --bitcoin-rpcpassword={} --bitcoin-rpcport={}", + btc.user, + btc.pass, + btc.port, + )?; + + let rpc = LightningRPC::new(dir.path().join("regtest").join("lightning-rpc")); + + wait_for!(async { rpc.getinfo() }); + + Ok(Self { + inner: rpc, + root_path: dir, + bitcoin: btc, + process: vec![process], + }) + } + + pub fn rpc(&self) -> &LightningRPC { + &self.inner + } + + pub async fn stop(&mut self) -> anyhow::Result<()> { + log::info!("stop lightning node"); + self.inner.stop()?; + for process in self.process.iter_mut() { + process.kill().await?; + let _ = process.wait().await?; + log::debug!("killing process"); + } + self.bitcoin.stop().await?; + Ok(()) + } +} diff --git a/coffee_testing/src/lib.rs b/coffee_testing/src/lib.rs new file mode 100644 index 00000000..5d1a10ef --- /dev/null +++ b/coffee_testing/src/lib.rs @@ -0,0 +1,106 @@ +//! Coffee testing implementation! +pub mod btc; +pub mod cln; + +pub mod prelude { + pub use cln_btc_test; + pub use cln_test; + + pub use crate::macros::*; +} + +use tempfile::TempDir; + +use coffee_core::coffee::CoffeeManager; + +static DEFAULT_TIMEOUT: u64 = 100; + +pub mod macros { + #[macro_export] + macro_rules! wait_for { + ($callback:expr, $timeout:expr) => { + use log; + use tokio::time::{sleep, Duration}; + + for wait in 0..$timeout { + if let Err(err) = $callback.await { + log::debug!("callback return {:?}", err); + sleep(Duration::from_millis(wait)).await; + continue; + } + log::info!("callback completed in {wait} milliseconds"); + break; + } + }; + ($callback:expr) => { + use crate::DEFAULT_TIMEOUT; + + $crate::wait_for!($callback, DEFAULT_TIMEOUT); + }; + } + + pub use wait_for; +} + +pub struct CoffeeTestingArgs { + pub conf: Option, + pub network: String, + pub data_dir: String, +} + +impl coffee_core::CoffeeArgs for CoffeeTestingArgs { + fn command(&self) -> coffee_core::CoffeeOperation { + unimplemented!() + } + + fn conf(&self) -> Option { + self.conf.clone() + } + + fn data_dir(&self) -> Option { + Some(self.data_dir.clone()) + } + + fn network(&self) -> Option { + Some(self.network.clone()) + } +} + +/// Coffee testing manager +/// that contains all the information that +/// we need to perform integration testing for coffee. +pub struct CoffeeTesting { + inner: CoffeeManager, + root_path: TempDir, +} + +impl Drop for CoffeeTesting { + fn drop(&mut self) { + use std::fs; + + fs::remove_dir_all(self.root_path.path()).unwrap(); + } +} + +impl CoffeeTesting { + // init coffee in a tmp directory. + pub async fn tmp() -> anyhow::Result { + let dir = tempfile::tempdir()?; + let args = CoffeeTestingArgs { + data_dir: dir.path().to_str().unwrap().to_owned(), + network: "regtest".to_owned(), + conf: None, + }; + let coffee = CoffeeManager::new(&args) + .await + .map_err(|err| anyhow::anyhow!("{err}"))?; + Ok(Self { + inner: coffee, + root_path: dir, + }) + } + + pub fn coffee(&mut self) -> &mut CoffeeManager { + &mut self.inner + } +} diff --git a/tests/Cargo.toml b/tests/Cargo.toml new file mode 100644 index 00000000..badcfb84 --- /dev/null +++ b/tests/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "tests" +version = "0.1.0" +edition = "2021" + +[dependencies] +coffee_lib = { path = "../coffee_lib" } +coffee_testing = { path = "../coffee_testing" } +anyhow = "1.0.71" +log = { version = "0.4", features = ["std"] } +colored = "1.9" +chrono = { version = "0.4", features = ["std"], default-features = false } +tokio = { version = "1.22.0", features = ["rt"] } +ntest = "0.9.0" diff --git a/tests/src/coffee_integration_tests.rs b/tests/src/coffee_integration_tests.rs new file mode 100644 index 00000000..a133869b --- /dev/null +++ b/tests/src/coffee_integration_tests.rs @@ -0,0 +1,90 @@ +use std::collections::HashMap; +use std::sync::Once; + +use coffee_lib::plugin_manager::PluginManager; +use coffee_testing::cln::Node; +use coffee_testing::CoffeeTesting; + +#[cfg(test)] +static INIT: Once = Once::new(); + +#[cfg(test)] +fn init() { + use crate::logger; + // ignore error + INIT.call_once(|| { + logger::init(log::Level::Debug).expect("initializing logger for the first time"); + }); +} + +#[tokio::test] +pub async fn init_coffee_test() -> anyhow::Result<()> { + init(); + + let mut manager = CoffeeTesting::tmp().await?; + let result = manager.coffee().list().await?; + assert!( + result.plugins.is_empty(), + "list of plugin not empty: {:?}", + result + ); + Ok(()) +} + +#[tokio::test] +pub async fn init_coffee_test_with_cln() -> anyhow::Result<()> { + init(); + let cln = Node::tmp().await?; + + let mut manager = CoffeeTesting::tmp().await?; + let result = manager.coffee().list().await?; + assert!( + result.plugins.is_empty(), + "list of plugin not empty: {:?}", + result + ); + let lightning_dir = cln.rpc().getinfo()?.ligthning_dir; + let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap(); + log::info!("lightning path: {lightning_dir}"); + + manager.coffee().setup(&lightning_dir).await?; + + Ok(()) +} + +#[tokio::test] +#[ntest::timeout(60000)] +pub async fn init_coffee_test_add_remote() { + init(); + let mut cln = Node::tmp().await.unwrap(); + + let mut manager = CoffeeTesting::tmp().await.unwrap(); + let result = manager.coffee().list().await.unwrap(); + assert!( + result.plugins.is_empty(), + "list of plugin not empty: {:?}", + result + ); + let lightning_dir = cln.rpc().getinfo().unwrap().ligthning_dir; + let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap(); + log::info!("lightning path: {lightning_dir}"); + + manager.coffee().setup(&lightning_dir).await.unwrap(); + + manager + .coffee() + .add_remote("lightningd", "https://github.com/lightningd/plugins.git") + .await + .unwrap(); + manager + .coffee() + .install("summary", true, true) + .await + .unwrap(); + + cln.rpc() + .call::<_, HashMap>("summary", HashMap::::new()) + .unwrap(); + + cln.stop().await.unwrap(); +} diff --git a/tests/src/lib.rs b/tests/src/lib.rs new file mode 100644 index 00000000..fccc2a2b --- /dev/null +++ b/tests/src/lib.rs @@ -0,0 +1,4 @@ +#[cfg(test)] +mod coffee_integration_tests; +#[cfg(test)] +pub(crate) mod logger; diff --git a/tests/src/logger.rs b/tests/src/logger.rs new file mode 100644 index 00000000..a7e14195 --- /dev/null +++ b/tests/src/logger.rs @@ -0,0 +1,63 @@ +//! Logging module. +/// +/// Credit to https://github.com/vincenzopalazzo/nakamoto/blob/master/node/src/logger.rs +use std::{io, time::SystemTime}; + +use chrono::prelude::*; +use colored::*; +pub use log::{Level, Log, Metadata, Record, SetLoggerError}; + +struct Logger { + level: Level, +} + +impl Log for Logger { + fn enabled(&self, metadata: &Metadata) -> bool { + metadata.level() <= self.level + } + + fn log(&self, record: &Record) { + if self.enabled(record.metadata()) { + let target = record.target(); + + if record.level() == Level::Error { + write(record, target, io::stderr()); + } else { + write(record, target, io::stdout()); + } + + fn write(record: &log::Record, target: &str, mut stream: impl io::Write) { + let message = format!("{} {} {}", record.level(), target.bold(), record.args()); + let message = match record.level() { + Level::Error => message.red(), + Level::Warn => message.yellow(), + Level::Info => message.normal(), + Level::Debug => message.dimmed(), + Level::Trace => message.white().dimmed(), + }; + + writeln!( + stream, + "{} {}", + DateTime::from(SystemTime::now()) + .to_rfc3339_opts(SecondsFormat::Millis, true) + .white(), + message, + ) + .expect("write shouldn't fail"); + } + } + } + + fn flush(&self) {} +} + +/// Initialize a new logger. +pub fn init(level: Level) -> Result<(), SetLoggerError> { + let logger = Logger { level }; + + log::set_boxed_logger(Box::new(logger))?; + log::set_max_level(level.to_level_filter()); + + Ok(()) +}