Skip to content

Commit

Permalink
tests/net: adapt to async/await changes
Browse files Browse the repository at this point in the history
  • Loading branch information
steveej committed Jan 29, 2020
1 parent 4410140 commit b9cfe09
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 120 deletions.
2 changes: 1 addition & 1 deletion tests/net/docker_io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate dkregistry;
extern crate tokio;

use self::tokio::runtime::current_thread::Runtime;
use self::tokio::runtime::Runtime;

static REGISTRY: &'static str = "registry-1.docker.io";

Expand Down
12 changes: 7 additions & 5 deletions tests/net/gcr_io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ extern crate dkregistry;
extern crate futures;
extern crate tokio;

use self::futures::stream::Stream;
use self::tokio::runtime::current_thread::Runtime;
use self::futures::stream::StreamExt;
use self::tokio::runtime::Runtime;

static REGISTRY: &'static str = "gcr.io";

Expand Down Expand Up @@ -78,8 +78,8 @@ fn test_gcrio_get_tags() {

let image = "google_containers/mounttest";
let fut_tags = dclient.get_tags(image, None);
let tags = runtime.block_on(fut_tags.collect()).unwrap();
let has_version = tags.iter().any(|t| t == "0.2");
let tags = runtime.block_on(fut_tags.collect::<Vec<_>>());
let has_version = tags.iter().map(|t| t.as_ref().unwrap()).any(|t| t == "0.2");

assert_eq!(has_version, true);
}
Expand All @@ -98,7 +98,9 @@ fn test_gcrio_has_manifest() {
let image = "google_containers/mounttest";
let tag = "0.2";
let manifest_type = dkregistry::mediatypes::MediaTypes::ManifestV2S1Signed.to_string();
let fut = dclient.has_manifest(image, tag, Some(vec![manifest_type.as_str()].as_slice()));
let manifest_type_str = manifest_type.as_str();
let manifest_type_vec = vec![manifest_type_str];
let fut = dclient.has_manifest(image, tag, Some(manifest_type_vec.as_slice()));
let has_manifest = runtime.block_on(fut).unwrap();

assert_eq!(
Expand Down
215 changes: 101 additions & 114 deletions tests/net/quay_io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
extern crate dkregistry;
extern crate futures;
extern crate serde_json;
extern crate tokio;

use self::dkregistry::mediatypes::MediaTypes;
use self::dkregistry::v2::manifest::ManifestSchema1Signed;
use self::futures::future;
use self::futures::prelude::*;
use self::tokio::runtime::current_thread::Runtime;
use dkregistry::mediatypes::MediaTypes;
use dkregistry::v2::authenticate_client;
use futures::future::FutureExt;
use futures::future::TryFutureExt;
use futures::prelude::*;
use futures::stream::StreamExt;
use tokio::runtime::Runtime;

static REGISTRY: &'static str = "quay.io";

Expand All @@ -20,6 +17,36 @@ fn get_env() -> Option<(String, String)> {
}
}

fn common_init(
login_scope: Option<&str>,
) -> Option<(tokio::runtime::Runtime, dkregistry::v2::Client)> {
let mut runtime = Runtime::new().unwrap();

let (user, password, login_scope) = if let Some(login_scope) = login_scope {
match get_env() {
Some((user, password)) => (Some(user), Some(password), login_scope.to_string()),
None => return None,
}
} else {
(None, None, "".to_string())
};

let dclient = runtime
.block_on(authenticate_client(
dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(user)
.password(password)
.build()
.unwrap(),
login_scope,
))
.unwrap();

Some((runtime, dclient))
}

#[test]
fn test_quayio_getenv() {
if get_env().is_none() {
Expand Down Expand Up @@ -73,26 +100,13 @@ fn test_quayio_insecure() {
#[test]
fn test_quayio_auth_login() {
let login_scope = "";
let (user, password) = match get_env() {
Some(t) => t,
None => return,
let (mut runtime, dclient) = if let Some(x) = common_init(Some(&login_scope)) {
x
} else {
return;
};

let mut runtime = Runtime::new().unwrap();
let dclient = dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(Some(user))
.password(Some(password))
.build()
.unwrap();

let futlogin = futures::future::ok(dclient).and_then(|dclient| {
dclient
.login(&[&login_scope])
.and_then(move |token| dclient.is_auth(Some(token.token())))
});

let futlogin = dclient.is_auth(None);
let res = runtime.block_on(futlogin).unwrap();
assert_eq!(res, true);
}
Expand All @@ -110,8 +124,11 @@ fn test_quayio_get_tags_simple() {

let image = "coreos/alpine-sh";
let fut_tags = dclient.get_tags(image, None);
let tags = runtime.block_on(fut_tags.collect()).unwrap();
let has_version = tags.iter().any(|t| t == "latest");
let tags = runtime.block_on(fut_tags.collect::<Vec<_>>());
let has_version = tags
.iter()
.map(|t| t.as_ref().unwrap())
.any(|t| t == "latest");

assert_eq!(has_version, true);
}
Expand All @@ -129,8 +146,11 @@ fn test_quayio_get_tags_limit() {

let image = "coreos/alpine-sh";
let fut_tags = dclient.get_tags(image, Some(10));
let tags = runtime.block_on(fut_tags.collect()).unwrap();
let has_version = tags.iter().any(|t| t == "latest");
let tags = runtime.block_on(fut_tags.collect::<Vec<_>>());
let has_version = tags
.iter()
.map(|t| t.as_ref().unwrap())
.any(|t| t == "latest");

assert_eq!(has_version, true);
}
Expand All @@ -148,8 +168,11 @@ fn test_quayio_get_tags_pagination() {

let image = "coreos/flannel";
let fut_tags = dclient.get_tags(image, Some(20));
let tags = runtime.block_on(fut_tags.collect()).unwrap();
let has_version = tags.iter().any(|t| t == "v0.10.0");
let tags = runtime.block_on(fut_tags.collect::<Vec<_>>());
let has_version = tags
.iter()
.map(|t| t.as_ref().unwrap())
.any(|t| t == "v0.10.0");

assert_eq!(has_version, true);
}
Expand All @@ -158,38 +181,19 @@ fn test_quayio_get_tags_pagination() {
fn test_quayio_auth_tags() {
let image = "steveej/cincinnati-test";
let login_scope = format!("repository:{}:pull", image);
let (user, password) = match get_env() {
Some(t) => t,
None => return,
};

let mut runtime = Runtime::new().unwrap();
let dclient = dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(Some(user))
.password(Some(password))
.build()
.unwrap();
let (mut runtime, dclient) = if let Some(x) = common_init(Some(&login_scope)) {
x
} else {
return;
};

let fut_tags = future::ok(dclient).and_then(|dclient| {
dclient
.login(&[&login_scope])
.and_then(move |auth| {
let token = auth.token().to_string();
dclient
.is_auth(Some(&token))
.map(move |ok| (dclient, token, ok))
})
.and_then(|(mut dclient, token, ok)| {
ensure!(ok, "authentication failed");
dclient.set_token(Some(&token));
Ok(dclient)
})
.and_then(|dclient| dclient.get_tags(image, None).collect())
});
let tags = runtime
.block_on(dclient.get_tags(image, None).collect::<Vec<_>>())
.into_iter()
.map(Result::unwrap)
.collect::<Vec<_>>();

let tags = runtime.block_on(fut_tags).unwrap();
let has_version = tags.iter().any(|t| t == "0.0.1");
assert_eq!(has_version, true);
}
Expand Down Expand Up @@ -224,30 +228,21 @@ fn test_quayio_auth_manifest() {
};

let mut runtime = Runtime::new().unwrap();
let dclient = dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(Some(user))
.password(Some(password))
.build()

let dclient = runtime
.block_on(authenticate_client(
dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(Some(user))
.password(Some(password))
.build()
.unwrap(),
login_scope,
))
.unwrap();

let fut_has_manifest = future::ok(dclient).and_then(|dclient| {
dclient
.login(&[&login_scope])
.and_then(move |auth| {
let token = auth.token().to_string();
dclient
.is_auth(Some(&token))
.map(move |ok| (dclient, token, ok))
})
.and_then(|(mut dclient, token, ok)| {
ensure!(ok, "authentication failed");
dclient.set_token(Some(&token));
Ok(dclient)
})
.and_then(|dclient| dclient.has_manifest(image, reference, None))
});
let fut_has_manifest = dclient.has_manifest(image, reference, None);

let has_manifest = runtime.block_on(fut_has_manifest).unwrap();
assert_eq!(has_manifest, Some(MediaTypes::ManifestV2S1Signed));
Expand Down Expand Up @@ -286,43 +281,35 @@ fn test_quayio_auth_layer_blob() {
};

let mut runtime = Runtime::new().unwrap();
let dclient = dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(Some(user))
.password(Some(password))
.build()
let dclient = runtime
.block_on(authenticate_client(
dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(Some(user))
.password(Some(password))
.build()
.unwrap(),
login_scope,
))
.unwrap();

let fut_layer0_blob = future::ok(dclient).and_then(|dclient| {
dclient
.login(&[&login_scope])
.and_then(move |auth| {
let token = auth.token().to_string();
dclient
.is_auth(Some(&token))
.map(move |ok| (dclient, token, ok))
})
.and_then(|(mut dclient, token, ok)| {
ensure!(ok, "authentication failed");
dclient.set_token(Some(&token));
Ok(dclient)
})
.and_then(|dclient| {
dclient
.get_manifest(image, reference)
.map(|manifest| (dclient, manifest))
})
.and_then(|(dclient, manifest)| {
let fut_layer0_blob = async {
let digest = dclient
.get_manifest(image, reference)
.await
.and_then(|manifest| {
let layers: Vec<String> = manifest.layers_digests(None)?;
let num_layers = layers.len();
ensure!(num_layers == 1, "layers length: {}", num_layers);
let digest = layers[0].clone();
ensure!(digest == layer0_sha, "layer0 digest: {}", digest);
Ok((dclient, digest))
Ok(digest)
})
.and_then(|(dclient, digest)| dclient.get_blob(&image, &digest))
});
.unwrap();

dclient.get_blob(&image, &digest).await
};

let layer0_blob = runtime.block_on(fut_layer0_blob).unwrap();
assert_eq!(layer0_blob.len(), layer0_len);
Expand Down

0 comments on commit b9cfe09

Please sign in to comment.