pierresouchay / consul-rust

Rust client libray for Consul HTTP API
Apache License 2.0
92 stars 48 forks source link

Failed to run the use case #50

Closed Jzow closed 3 years ago

Jzow commented 3 years ago
extern crate consul;

use std::collections::HashMap;
use consul::{Client, Config, QueryMeta};
use consul::catalog::Catalog;

#[test]
fn main(){
    let config = Config::new().unwrap();
    let client = Client::new(config);
    let services: (HashMap<String, String>, QueryMeta) = client.services(None).unwrap();
    println!("{:?}", services);
}
C:/Users/Administrator/.cargo/bin/cargo.exe test --color=always --package cloudservice --test test main --no-fail-fast -- --format=json --exact -Z unstable-options --show-output
Testing started at 19:43 ...
    Finished test [unoptimized + debuginfo] target(s) in 0.49s
     Running target\debug\deps\test-ffdcd30ad7418a1c.exe

called `Result::unwrap()` on an `Err` value: Error(Msg("Failed to parse JSON response"), State { next_error: Some(Error(Json(Error("invalid type: sequence, expected a string", line: 2, column: 28)))), backtrace: InternalBacktrace { backtrace: Some(   0: backtrace::backtrace::trace_unsynchronized<closure-0>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\backtrace-0.3.56\src\backtrace\mod.rs:66
   1: backtrace::backtrace::trace<closure-0>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\backtrace-0.3.56\src\backtrace\mod.rs:53
   2: backtrace::capture::Backtrace::create
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\backtrace-0.3.56\src\capture.rs:176
   3: backtrace::capture::Backtrace::new_unresolved
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\backtrace-0.3.56\src\capture.rs:170
   4: error_chain::backtrace::imp::InternalBacktrace::new
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\error-chain-0.12.4\src\backtrace.rs:56
   5: core::ops::function::FnOnce::call_once<fn() -> error_chain::backtrace::imp::InternalBacktrace,tuple<>>
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
   6: core::option::Option<error_chain::backtrace::imp::InternalBacktrace>::unwrap_or_else<error_chain::backtrace::imp::InternalBacktrace,fn() -> error_chain::backtrace::imp::InternalBacktrace>
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\option.rs:427
   7: error_chain::State::new<consul::errors::Error>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\error-chain-0.12.4\src\lib.rs:696
   8: consul::errors::{{impl}}::chain_err::{{closure}}<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>,reqwest::error::Error,closure-0,str>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\error-chain-0.12.4\src\error_chain.rs:434
   9: core::result::Result<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>, reqwest::error::Error>::map_err<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::S
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:595
  10: consul::errors::{{impl}}::chain_err<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>,reqwest::error::Error,closure-0,str>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\error-chain-0.12.4\src\error_chain.rs:433
  11: consul::request::get::{{closure}}<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\consul-0.3.0\src\request.rs:112
  12: core::result::Result<reqwest::response::Response, consul::errors::Error>::and_then<reqwest::response::Response,consul::errors::Error,tuple<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::Rand
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:708
  13: consul::request::get<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\consul-0.3.0\src\request.rs:109
  14: consul::catalog::{{impl}}::services
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\consul-0.3.0\src\catalog.rs:143
  15: test::main
             at tests\test.rs:11
  16: test::main::{{closure}}
             at tests\test.rs:8
  17: core::ops::function::FnOnce::call_once<closure-0,tuple<>>
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
  18: core::ops::function::FnOnce::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\core\src\ops\function.rs:227
  19: test::__rust_begin_short_backtrace<fn()>
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\test\src\lib.rs:516
  20: alloc::boxed::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\alloc\src\boxed.rs:1328
  21: std::panic::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panic.rs:322
  22: std::panicking::try::do_call
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panicking.rs:379
  23: std::panicking::try
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panicking.rs:343
  24: std::panic::catch_unwind
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panic.rs:396
  25: test::run_test_in_process
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\test\src\lib.rs:538
  26: test::run_test::run_test_inner::{{closure}}
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\test\src\lib.rs:449
  27: std::sys_common::backtrace::__rust_begin_short_backtrace<closure-0,tuple<>>
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\sys_common\backtrace.rs:125
  28: std::sys_common::backtrace::__rust_begin_short_backtrace<closure-0,tuple<>>
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\sys_common\backtrace.rs:125
  29: std::thread::{{impl}}::spawn_unchecked::{{closure}}::{{closure}}
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\thread\mod.rs:474
  30: std::panic::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panic.rs:322
  31: std::panicking::try::do_call
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panicking.rs:379
  32: std::panicking::try
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panicking.rs:343
  33: std::panic::catch_unwind
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panic.rs:396
  34: std::thread::{{impl}}::spawn_unchecked::{{closure}}
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\thread\mod.rs:473
  35: core::ops::function::FnOnce::call_once<closure-0,tuple<>>
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\core\src\ops\function.rs:227
  36: alloc::boxed::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\alloc\src\boxed.rs:1328
  37: alloc::boxed::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\alloc\src\boxed.rs:1328
  38: std::sys::windows::thread::{{impl}}::new::thread_start
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\std\src\sys\windows\thread.rs:56
  39: BaseThreadInitThunk
  40: RtlUserThreadStart
) } })
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error(Msg("Failed to parse JSON response"), State { next_error: Some(Error(Json(Error("invalid type: sequence, expected a string", line: 2, column: 28)))), backtrace: InternalBacktrace { backtrace: Some(   0: backtrace::backtrace::trace_unsynchronized<closure-0>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\backtrace-0.3.56\src\backtrace\mod.rs:66
   1: backtrace::backtrace::trace<closure-0>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\backtrace-0.3.56\src\backtrace\mod.rs:53
   2: backtrace::capture::Backtrace::create
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\backtrace-0.3.56\src\capture.rs:176
   3: backtrace::capture::Backtrace::new_unresolved
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\backtrace-0.3.56\src\capture.rs:170
   4: error_chain::backtrace::imp::InternalBacktrace::new
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\error-chain-0.12.4\src\backtrace.rs:56
   5: core::ops::function::FnOnce::call_once<fn() -> error_chain::backtrace::imp::InternalBacktrace,tuple<>>
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
   6: core::option::Option<error_chain::backtrace::imp::InternalBacktrace>::unwrap_or_else<error_chain::backtrace::imp::InternalBacktrace,fn() -> error_chain::backtrace::imp::InternalBacktrace>
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\option.rs:427
   7: error_chain::State::new<consul::errors::Error>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\error-chain-0.12.4\src\lib.rs:696
   8: consul::errors::{{impl}}::chain_err::{{closure}}<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>,reqwest::error::Error,closure-0,str>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\error-chain-0.12.4\src\error_chain.rs:434
   9: core::result::Result<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>, reqwest::error::Error>::map_err<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::S
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:595
  10: consul::errors::{{impl}}::chain_err<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>,reqwest::error::Error,closure-0,str>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\error-chain-0.12.4\src\error_chain.rs:433
  11: consul::request::get::{{closure}}<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\consul-0.3.0\src\request.rs:112
  12: core::result::Result<reqwest::response::Response, consul::errors::Error>::and_then<reqwest::response::Response,consul::errors::Error,tuple<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::Rand
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:708
  13: consul::request::get<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>>
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\consul-0.3.0\src\request.rs:109
  14: consul::catalog::{{impl}}::services
             at C:\Users\Administrator\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\consul-0.3.0\src\catalog.rs:143
  15: test::main
             at tests\test.rs:11
  16: test::main::{{closure}}
             at tests\test.rs:8
  17: core::ops::function::FnOnce::call_once<closure-0,tuple<>>
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
  18: core::ops::function::FnOnce::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\core\src\ops\function.rs:227
  19: test::__rust_begin_short_backtrace<fn()>
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\test\src\lib.rs:516
  20: alloc::boxed::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\alloc\src\boxed.rs:1328
  21: std::panic::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panic.rs:322
  22: std::panicking::try::do_call
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panicking.rs:379
  23: std::panicking::try
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panicking.rs:343
  24: std::panic::catch_unwind
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panic.rs:396
  25: test::run_test_in_process
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\test\src\lib.rs:538
  26: test::run_test::run_test_inner::{{closure}}
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\test\src\lib.rs:449
  27: std::sys_common::backtrace::__rust_begin_short_backtrace<closure-0,tuple<>>
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\sys_common\backtrace.rs:125
  28: std::sys_common::backtrace::__rust_begin_short_backtrace<closure-0,tuple<>>
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\sys_common\backtrace.rs:125
  29: std::thread::{{impl}}::spawn_unchecked::{{closure}}::{{closure}}
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\thread\mod.rs:474
  30: std::panic::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panic.rs:322
  31: std::panicking::try::do_call
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panicking.rs:379
  32: std::panicking::try
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panicking.rs:343
  33: std::panic::catch_unwind
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\panic.rs:396
  34: std::thread::{{impl}}::spawn_unchecked::{{closure}}
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\std\src\thread\mod.rs:473
  35: core::ops::function::FnOnce::call_once<closure-0,tuple<>>
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\core\src\ops\function.rs:227
  36: alloc::boxed::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\alloc\src\boxed.rs:1328
  37: alloc::boxed::{{impl}}::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\alloc\src\boxed.rs:1328
  38: std::sys::windows::thread::{{impl}}::new::thread_start
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\std\src\sys\windows\thread.rs:56
  39: BaseThreadInitThunk
  40: RtlUserThreadStart
) } })', tests\test.rs:11:80
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\std\src\panicking.rs:493
   1: core::panicking::panic_fmt
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\core\src\panicking.rs:92
   2: core::option::expect_none_failed
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\core\src\option.rs:1268
   3: core::result::Result<tuple<std::collections::hash::map::HashMap<alloc::string::String, alloc::string::String, std::collections::hash::map::RandomState>, consul::QueryMeta>, consul::errors::Error>::unwrap<tuple<std::collections::hash::map::HashMap<alloc::s
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:973
   4: test::main
             at .\tests\test.rs:11
   5: test::main::{{closure}}
             at .\tests\test.rs:8
   6: core::ops::function::FnOnce::call_once<closure-0,tuple<>>
             at C:\Users\Administrator\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
   7: core::ops::function::FnOnce::call_once
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: test failed, to rerun pass '-p cloudservice --test test'

Process finished with exit code 101
pierresouchay commented 3 years ago

Hello @Jzow ,

Yes, this bug has been fixed a while ago and even contains a test-case: https://github.com/pierresouchay/consul-rust/blob/master/tests/catalog.rs#L14

Try using version 0.4.1 (available as crate, it should now work well).

Can you please close the issue if it fixes the issue for you?

Regards

Jzow commented 3 years ago

@pierresouchay Thank you very much for your prompt reply,I will try to write a test case,:)

Jzow commented 3 years ago

Hello @pierresouchay

I'm trying to register the service to the consumer through an Rust API, which is a bit similar to Java annotation registration of the consumer service, Do you have a demo or any good suggestions,I think the bug has been solved. You can add a question label. Thank you very much

extern crate consul;
use consul::{Client, Config};

#[test]
fn ds_test() {
    use consul::catalog::Catalog;
    let mut config = Config::new_from_env().unwrap();
    config.address = String::from("http://192.168.20.23:8500/");
    let client = Client::new(config);
    let r = client.datacenters().unwrap();
    assert_eq!(r.0, ["dc1"]);
}

#[test]
fn ds_services_test() {
    use consul::catalog::Catalog;
    let mut config = Config::new().unwrap();
    config.address = String::from("http://192.168.20.23:8500/");
    let client = Client::new(config);
    let r = client.services(Option::None).unwrap();
    assert_ne!(r.0.len(), 0);
    match r.0.get("consul") {
        None => panic!("Should have a Consul service"),
        Some(val) => assert_eq!(val.len(), 0), // consul has no tags
    }
}