fix: Fix influxdb_iox_client integration tests

pull/24376/head
Raphael Taylor-Davies 2021-02-08 18:02:39 +00:00
parent e5da2ab589
commit 18ff486138
4 changed files with 61 additions and 24 deletions

View File

@ -3,7 +3,8 @@ use std::num::NonZeroU32;
use data_types::database_rules::DatabaseRules; use data_types::database_rules::DatabaseRules;
use reqwest::{Method, Url}; use reqwest::{Method, Url};
use crate::errors::{CreateDatabaseError, Error, ServerErrorResponse}; use crate::errors::{ClientError, CreateDatabaseError, Error, ServerErrorResponse};
use data_types::DatabaseName;
// TODO: move DatabaseRules / WriterId to the API client // TODO: move DatabaseRules / WriterId to the API client
@ -83,12 +84,7 @@ impl Client {
name: impl AsRef<str>, name: impl AsRef<str>,
rules: &DatabaseRules, rules: &DatabaseRules,
) -> Result<(), CreateDatabaseError> { ) -> Result<(), CreateDatabaseError> {
const DB_PATH: &str = "iox/api/v1/databases/"; let url = self.db_url(name.as_ref())?;
let url = self
.url_for(DB_PATH)
.join(name.as_ref())
.map_err(|_| CreateDatabaseError::InvalidName)?;
let r = self let r = self
.http .http
@ -147,6 +143,18 @@ impl Client {
.join(path) .join(path)
.expect("failed to construct request URL") .expect("failed to construct request URL")
} }
fn db_url(&self, database: &str) -> Result<Url, ClientError> {
const DB_PATH: &str = "iox/api/v1/databases/";
// Perform validation in the client as URL parser silently drops invalid
// characters
let name = DatabaseName::new(database).map_err(|_| ClientError::InvalidDatabaseName)?;
self.url_for(DB_PATH)
.join(name.as_ref())
.map_err(|_| ClientError::InvalidDatabaseName)
}
} }
#[cfg(test)] #[cfg(test)]
@ -202,13 +210,11 @@ mod tests {
let endpoint = maybe_skip_integration!(); let endpoint = maybe_skip_integration!();
let c = ClientBuilder::default().build(endpoint).unwrap(); let c = ClientBuilder::default().build(endpoint).unwrap();
let rand_name: String = thread_rng() c.set_writer_id(NonZeroU32::new(42).unwrap())
.sample_iter(&Alphanumeric) .await
.take(10) .expect("set ID failed");
.map(char::from)
.collect();
c.create_database(rand_name, &DatabaseRules::default()) c.create_database(rand_name(), &DatabaseRules::default())
.await .await
.expect("create database failed"); .expect("create database failed");
} }
@ -218,18 +224,18 @@ mod tests {
let endpoint = maybe_skip_integration!(); let endpoint = maybe_skip_integration!();
let c = ClientBuilder::default().build(endpoint).unwrap(); let c = ClientBuilder::default().build(endpoint).unwrap();
let rand_name: String = thread_rng() c.set_writer_id(NonZeroU32::new(42).unwrap())
.sample_iter(&Alphanumeric) .await
.take(10) .expect("set ID failed");
.map(char::from)
.collect();
c.create_database(rand_name.clone(), &DatabaseRules::default()) let db_name = rand_name();
c.create_database(db_name.clone(), &DatabaseRules::default())
.await .await
.expect("create database failed"); .expect("create database failed");
let err = c let err = c
.create_database(rand_name, &DatabaseRules::default()) .create_database(db_name, &DatabaseRules::default())
.await .await
.expect_err("create database failed"); .expect_err("create database failed");
@ -241,12 +247,19 @@ mod tests {
let endpoint = maybe_skip_integration!(); let endpoint = maybe_skip_integration!();
let c = ClientBuilder::default().build(endpoint).unwrap(); let c = ClientBuilder::default().build(endpoint).unwrap();
c.set_writer_id(NonZeroU32::new(42).unwrap())
.await
.expect("set ID failed");
let err = c let err = c
.create_database("bananas!", &DatabaseRules::default()) .create_database("my_example\ndb", &DatabaseRules::default())
.await .await
.expect_err("expected request to fail"); .expect_err("expected request to fail");
assert!(matches!(dbg!(err), CreateDatabaseError::InvalidName)) assert!(matches!(
dbg!(err),
CreateDatabaseError::ClientError(ClientError::InvalidDatabaseName)
));
} }
#[test] #[test]
@ -277,4 +290,12 @@ mod tests {
c.url_for("/bananas"); c.url_for("/bananas");
} }
fn rand_name() -> String {
thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect()
}
} }

View File

@ -0,0 +1,9 @@
use thiserror::Error;
/// Error responses when creating a new IOx database.
#[derive(Debug, Error)]
pub enum ClientError {
/// The database name contains an invalid character.
#[error("the database name contains an invalid character")]
InvalidDatabaseName,
}

View File

@ -1,6 +1,6 @@
use thiserror::Error; use thiserror::Error;
use super::{ApiErrorCode, HttpError, ServerErrorResponse}; use super::{ApiErrorCode, ClientError, HttpError, ServerErrorResponse};
/// Error responses when creating a new IOx database. /// Error responses when creating a new IOx database.
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -13,7 +13,7 @@ pub enum CreateDatabaseError {
#[error("a database with the requested name already exists")] #[error("a database with the requested name already exists")]
AlreadyExists, AlreadyExists,
/// An unknown server error occured. /// An unknown server error occurred.
/// ///
/// The error string contains the error string returned by the server. /// The error string contains the error string returned by the server.
#[error(transparent)] #[error(transparent)]
@ -22,6 +22,10 @@ pub enum CreateDatabaseError {
/// A non-application HTTP request/response error occurred. /// A non-application HTTP request/response error occurred.
#[error(transparent)] #[error(transparent)]
HttpError(#[from] HttpError), HttpError(#[from] HttpError),
/// An error occurred in the client.
#[error(transparent)]
ClientError(#[from] ClientError),
} }
/// Convert a [`ServerErrorResponse`] into a [`CreateDatabaseError`]. /// Convert a [`ServerErrorResponse`] into a [`CreateDatabaseError`].

View File

@ -23,6 +23,9 @@ use thiserror::Error;
mod http_error; mod http_error;
pub use http_error::*; pub use http_error::*;
mod client_error;
pub use client_error::*;
mod server_error_response; mod server_error_response;
pub use server_error_response::*; pub use server_error_response::*;