Skip to content

Commit

Permalink
Merge pull request #29 from fussybeaver/ND-public-connect-with
Browse files Browse the repository at this point in the history
Change mock unit test connect method to public
  • Loading branch information
Niel Drummond authored Aug 16, 2019
2 parents 48e5ad2 + 301a529 commit 5caa67d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 75 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bollard"
description = "An asynchronous Docker daemon API"
version = "0.3.2"
version = "0.3.3"
authors = ["Graham Lee <[email protected]>",
"Toby Lawrence <[email protected]>",
"Eric Kidd <[email protected]>",
Expand Down Expand Up @@ -50,13 +50,13 @@ tokio-io = "0.1.11"
tokio-reactor = "0.1.9"
tokio-timer = "0.2.10"
url = "1.7.2"
yup-hyper-mock = "3.15.0"

[dev-dependencies]
tokio-executor = "0.1.7"
tokio-threadpool = "0.1.14"
flate2 = "1.0"
tar = "0.4.20"
yup-hyper-mock = "3.15.0"

[target.'cfg(unix)'.dependencies]
hyperlocal = "0.6.0"
Expand Down
133 changes: 63 additions & 70 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use http::request::Builder;
use hyper::client::HttpConnector;
use hyper::rt::Future;
use hyper::{self, Body, Chunk, Client, Method, Request, Response, StatusCode};
use hyper_mock::HostToReplyConnector;
#[cfg(feature = "openssl")]
use hyper_openssl::HttpsConnector;
#[cfg(feature = "tls")]
Expand Down Expand Up @@ -56,9 +57,6 @@ use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use serde_json;

#[cfg(test)]
use hyper_mock::HostToReplyConnector;

/// The default `DOCKER_SOCKET` address that we will try to connect to.
#[cfg(unix)]
pub const DEFAULT_SOCKET: &'static str = "unix:///var/run/docker.sock";
Expand Down Expand Up @@ -136,7 +134,6 @@ pub(crate) enum Transport {
NamedPipe {
client: Client<NamedPipeConnector>,
},
#[cfg(test)]
HostToReply {
client: Client<HostToReplyConnector>,
},
Expand All @@ -154,7 +151,6 @@ impl fmt::Debug for Transport {
Transport::Unix { .. } => write!(f, "Unix"),
#[cfg(windows)]
Transport::NamedPipe { .. } => write!(f, "NamedPipe"),
#[cfg(test)]
Transport::HostToReply { .. } => write!(f, "HostToReply"),
}
}
Expand Down Expand Up @@ -838,70 +834,6 @@ impl Docker {
}
}

#[cfg(test)]
impl Docker {
/// Connect using the `HostToReplyConnector`.
///
/// This connector is used to test the Docker client api.
///
/// # Arguments
///
/// - `connector`: the HostToReplyConnector.
/// - `client_addr`: location to connect to.
/// - `timeout`: the read/write timeout (seconds) to use for every hyper connection
/// - `client_version`: the client version to communicate with the server.
///
/// # Examples
///
/// ```rust,no_run
/// # extern crate bollard;
/// # extern crate futures;
/// # extern crate hyper_mock;
/// # fn main () {
/// use bollard::Docker;
///
/// use futures::future::Future;
///
/// # use hyper_mock::HostToReplyConnector;
/// let mut connector = HostToReplyConnector::default();
/// connector.content.push(
/// "HTTP/1.1 200 OK\r\nServer: mock1\r\nContent-Type: application/json\r\nContent-Length: 0\r\n\r\n".to_string()
/// );
/// let connection = Docker::connect_with(connector, String::new(), 5).unwrap();
/// connection.ping()
/// .and_then(|_| Ok(println!("Connected!")));
/// # }
/// ```
pub(crate) fn connect_with_host_to_reply(
connector: HostToReplyConnector,
client_addr: String,
timeout: u64,
client_version: &ClientVersion,
) -> Result<Docker, Error> {
let client_builder = Client::builder();
let client = client_builder.build(connector);

#[cfg(unix)]
let client_type = ClientType::Unix;
#[cfg(windows)]
let client_type = ClientType::NamedPipe;
let transport = Transport::HostToReply { client };

let docker = Docker {
transport: Arc::new(transport),
client_type: client_type,
client_addr,
client_timeout: timeout,
version: Arc::new((
AtomicUsize::new(client_version.major_version),
AtomicUsize::new(client_version.minor_version),
)),
};

Ok(docker)
}
}

#[derive(Debug)]
/// ---
/// # DockerChain
Expand Down Expand Up @@ -1196,7 +1128,6 @@ impl Docker {
Transport::Unix { ref client } => client.request(request),
#[cfg(windows)]
Transport::NamedPipe { ref client } => client.request(request),
#[cfg(test)]
Transport::HostToReply { ref client } => client.request(request),
};
Timeout::new_at(request, now + Duration::from_secs(timeout)).from_err()
Expand Down Expand Up @@ -1261,4 +1192,66 @@ impl Docker {
})
})
}

/// Connect using the `HostToReplyConnector`.
///
/// This connector is used to test the Docker client api.
///
/// # Arguments
///
/// - `connector`: the HostToReplyConnector.
/// - `client_addr`: location to connect to.
/// - `timeout`: the read/write timeout (seconds) to use for every hyper connection
/// - `client_version`: the client version to communicate with the server.
///
/// # Examples
///
/// ```rust,no_run
/// # extern crate bollard;
/// # extern crate futures;
/// # extern crate yup_hyper_mock;
/// # fn main () {
/// use bollard::{API_DEFAULT_VERSION, Docker};
///
/// use futures::future::Future;
///
/// # use yup_hyper_mock::HostToReplyConnector;
/// let mut connector = HostToReplyConnector::default();
/// connector.m.insert(
/// format!("{}://5f", if cfg!(windows) { "net.pipe" } else { "unix" }),
/// "HTTP/1.1 200 OK\r\nServer: mock1\r\nContent-Type: application/json\r\nContent-Length: 0\r\n\r\n".to_string()
/// );
/// let connection = Docker::connect_with_host_to_reply(connector, String::new(), 5, API_DEFAULT_VERSION).unwrap();
/// connection.ping()
/// .and_then(|_| Ok(println!("Connected!")));
/// # }
/// ```
pub fn connect_with_host_to_reply(
connector: HostToReplyConnector,
client_addr: String,
timeout: u64,
client_version: &ClientVersion,
) -> Result<Docker, Error> {
let client_builder = Client::builder();
let client = client_builder.build(connector);

#[cfg(unix)]
let client_type = ClientType::Unix;
#[cfg(windows)]
let client_type = ClientType::NamedPipe;
let transport = Transport::HostToReply { client };

let docker = Docker {
transport: Arc::new(transport),
client_type: client_type,
client_addr,
client_timeout: timeout,
version: Arc::new((
AtomicUsize::new(client_version.major_version),
AtomicUsize::new(client_version.minor_version),
)),
};

Ok(docker)
}
}
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ extern crate tokio_timer;
extern crate url;
#[cfg(windows)]
extern crate winapi;
extern crate yup_hyper_mock as hyper_mock;

// declare modules
pub mod auth;
Expand All @@ -423,8 +424,5 @@ mod read;
pub mod system;
mod uri;

#[cfg(test)]
extern crate yup_hyper_mock as hyper_mock;

// publicly re-export
pub use docker::{ClientVersion, Docker, DockerChain, API_DEFAULT_VERSION};

0 comments on commit 5caa67d

Please sign in to comment.