yoshidan / google-cloud-rust

Google Cloud Client Libraries for Rust.
MIT License
222 stars 80 forks source link

Automatic authentication fails inside test module #142

Closed LenWilliamson closed 1 year ago

LenWilliamson commented 1 year ago

I have a config module that returns a Client:

mod config {
    use google_cloud_default::WithAuthExt;
    use google_cloud_storage::client::{Client, ClientConfig};

    pub async fn get_google_cloud_client() -> Client {
        let config = ClientConfig::default().with_auth().await.unwrap();
        Client::new(config)
    }
}

When I make API calls to Cloud Storage from my test module I get the following error: panicked at 'This is dummy token source provider. you can use 'google_cloud_default'. I initialised the client by calling the above getter.

 #[tokio::test]
 async fn test_call_to_gcs() {
    let client = config::get_google_cloud_client().await
    // Make API calls with this client...

    // ... then: panicked at 'This is dummy token source provider. you can use 'google_cloud_default
}

This problem occurred, when I was migrating to the latest version. However, I don't face this problem when I make API calls to Cloud Storage inside my main function, when running my program. In my main function I also created a client by initialising it to let client = config::get_google_cloud_client().await Currently I don't know how to fix this problem: How can I use the automatic authentication inside unit tests in test modules?

yoshidan commented 1 year ago

The following code works. please check the version of crate. It may be because google-cloud-default was not 0.2.0 as of yesterday.

mod config {
    use google_cloud_default::WithAuthExt;
    use google_cloud_storage::client::{Client, ClientConfig};

    pub async fn get_google_cloud_client() -> Client {
        let config = ClientConfig::default().with_auth().await.unwrap();
        Client::new(config)
    }
}

#[cfg(test)]
mod tests {
    use google_cloud_storage::http::buckets::get::GetBucketRequest;
    use crate::config;

    #[tokio::test]
    async fn test_call_to_gcs() {
        let client = config::get_google_cloud_client().await;
        let bucket = client.get_bucket(&GetBucketRequest {
            bucket: "rust-bucket-test".to_string(),
            ..Default::default()
        }).await;
        assert_eq!(bucket.name.as_str(), "rust-bucket-test");
    }
}

Cargo.toml

google-cloud-spanner = { version="0.18.1", features=["trace"]}
google-cloud-pubsub = { version="0.14.1", features=["trace", "bytes"]}
google-cloud-storage = { version="0.11.0", default-features=false, features=["trace", "rustls-tls"]}
google-cloud-gax = { version = "0.14.1" }
google-cloud-auth = { version = "0.9.2", default-features=false, features=["rustls-tls"]}
google-cloud-token = { version="0.1.1" }
google-cloud-googleapis= { version="0.8.0" }
google-cloud-default = { version="0.2.0", default-features=false, features=["spanner", "storage", "pubsub", "rustls-tls"]}
google-cloud-spanner-derive = { version="0.1.0" }