Skip to content

Commit

Permalink
providers/tests: address review comments and cleanup tests setup
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Bai <[email protected]>
  • Loading branch information
Allen Bai committed Oct 10, 2019
1 parent 0fe9934 commit cfffae4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 126 deletions.
99 changes: 34 additions & 65 deletions src/providers/aws/mock_tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use crate::errors::*;
use crate::providers::aws;
use mockito;
use std::collections::HashMap;
use crate::providers::MetadataProvider;

#[cfg(not(feature = "cl-legacy"))]
static ENV_PREFIX: &str = "AWS";
#[cfg(feature = "cl-legacy")]
static ENV_PREFIX: &str = "EC2";

#[test]
fn test_aws_basic() {
let ep = "/meta-data/public-keys";
Expand All @@ -35,40 +29,46 @@ fn test_aws_basic() {

#[test]
fn test_aws_attributes() {
let ep_instance_id = "/meta-data/instance-id";
let instance_id = "test-instance-id";

let ep_instance_type = "/meta-data/instance-type";
let instance_type = "test-instance-type";

let ep_ipv4_local = "/meta-data/local-ipv4";
let ipv4_local = "test-ipv4-local";

let ep_ipv4_public = "/meta-data/public-ipv4";
let ipv4_public = "test-ipv4-public";

let ep_availability_zone = "/meta-data/placement/availability-zone";
let availability_zone = "test-availability-zone";

let ep_hostname = "/meta-data/hostname";
let hostname = "test-hostname";

let ep_public_hostname = "/meta-data/public-hostname";
let public_hostname = "test-public-hostname";

let ep_region = "/dynamic/instance-identity/document";
let instance_id_doc = "{\"region\": \"test-region\"}";
let instance_id_doc = r#"{"region": "test-region"}"#;
let region = "test-region";

let mut attributes:HashMap<String, String> = HashMap::new();
attributes.insert(format!("{}_INSTANCE_ID", ENV_PREFIX), String::from(instance_id));
attributes.insert(format!("{}_INSTANCE_TYPE", ENV_PREFIX), String::from(instance_type));
attributes.insert(format!("{}_IPV4_LOCAL", ENV_PREFIX), String::from(ipv4_local));
attributes.insert(format!("{}_IPV4_PUBLIC", ENV_PREFIX), String::from(ipv4_public));
attributes.insert(format!("{}_AVAILABILITY_ZONE", ENV_PREFIX), String::from(availability_zone));
attributes.insert(format!("{}_HOSTNAME", ENV_PREFIX), String::from(hostname));
attributes.insert(format!("{}_PUBLIC_HOSTNAME", ENV_PREFIX), String::from(public_hostname));
attributes.insert(format!("{}_REGION", ENV_PREFIX), String::from(region));
let endpoints = maplit::btreemap! {
"/meta-data/instance-id" => instance_id,
"/meta-data/instance-type" => instance_type,
"/meta-data/local-ipv4" => ipv4_local,
"/meta-data/public-ipv4" => ipv4_public,
"/meta-data/placement/availability-zone" => availability_zone,
"/meta-data/hostname" => hostname,
"/meta-data/public-hostname" => public_hostname,
"/dynamic/instance-identity/document" => instance_id_doc,
};

let mut mocks = Vec::with_capacity(endpoints.len());
for (endpoint, body) in endpoints {
let m = mockito::mock("GET", endpoint)
.with_status(200)
.with_body(body)
.create();
mocks.push(m);
}

let attributes = maplit::hashmap! {
format!("{}_INSTANCE_ID", aws::ENV_PREFIX) => String::from(instance_id),
format!("{}_INSTANCE_TYPE", aws::ENV_PREFIX) => String::from(instance_type),
format!("{}_IPV4_LOCAL", aws::ENV_PREFIX) => String::from(ipv4_local),
format!("{}_IPV4_PUBLIC", aws::ENV_PREFIX) => String::from(ipv4_public),
format!("{}_AVAILABILITY_ZONE", aws::ENV_PREFIX) => String::from(availability_zone),
format!("{}_HOSTNAME", aws::ENV_PREFIX) => String::from(hostname),
format!("{}_PUBLIC_HOSTNAME", aws::ENV_PREFIX) => String::from(public_hostname),
format!("{}_REGION", aws::ENV_PREFIX) => String::from(region),
};

let client = crate::retry::Client::try_new()
.chain_err(|| "failed to create http client")
Expand All @@ -77,40 +77,9 @@ fn test_aws_attributes() {
.return_on_404(true);
let provider = aws::AwsProvider { client };

let _m = mockito::mock("GET", ep_instance_id)
.with_status(200)
.with_body(instance_id)
.create();
let _m = mockito::mock("GET", ep_instance_type)
.with_status(200)
.with_body(instance_type)
.create();
let _m = mockito::mock("GET", ep_ipv4_local)
.with_status(200)
.with_body(ipv4_local)
.create();
let _m = mockito::mock("GET", ep_ipv4_public)
.with_status(200)
.with_body(ipv4_public)
.create();
let _m = mockito::mock("GET", ep_availability_zone)
.with_status(200)
.with_body(availability_zone)
.create();
let _m = mockito::mock("GET", ep_hostname)
.with_status(200)
.with_body(hostname)
.create();
let _m = mockito::mock("GET", ep_public_hostname)
.with_status(200)
.with_body(public_hostname)
.create();
let _m = mockito::mock("GET", ep_region)
.with_status(200)
.with_body(instance_id_doc)
.create();

let v = provider.attributes().unwrap();

assert_eq!(v, attributes);

mockito::reset();
provider.attributes().unwrap_err();
}
4 changes: 2 additions & 2 deletions src/providers/azure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ mod crypto;

use std::collections::HashMap;
use std::net::IpAddr;
#[cfg(not(test))]
use std::net::SocketAddr;

use openssh_keys::PublicKey;
use reqwest::header::{HeaderName, HeaderValue};
Expand Down Expand Up @@ -367,6 +365,8 @@ impl Azure {

#[cfg(not(test))]
fn get_attributes(&self) -> Result<Attributes> {
use std::net::SocketAddr;

let endpoint = &self.goal_state.container.role_instance_list.role_instances[0]
.configuration
.shared_config;
Expand Down
69 changes: 30 additions & 39 deletions src/providers/gcp/mock_tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use crate::providers::gcp;
use crate::providers::MetadataProvider;
use std::collections::HashMap;
use mockito;

#[cfg(not(feature = "cl-legacy"))]
static ENV_PREFIX: &str = "GCP";
#[cfg(feature = "cl-legacy")]
static ENV_PREFIX: &str = "GCE";

#[test]
fn basic_hostname() {
let ep = "/instance/hostname";
Expand Down Expand Up @@ -36,45 +30,42 @@ fn basic_hostname() {

#[test]
fn basic_attributes() {
let ep_hostname = "/instance/hostname";
let hostname = "test-hostname";

let ep_ip_external = "/instance/network-interfaces/0/access-configs/0/external-ip";
let ip_external = "test-ip-external";

let ep_ip_local = "/instance/network-interfaces/0/ip";
let ip_local = "test-ip-local";

let ep_machine_type = "/instance/machine-type";
let machine_type = "test-machine-type";

let mut attributes:HashMap<String, String> = HashMap::new();
attributes.insert(format!("{}_HOSTNAME", ENV_PREFIX), String::from(hostname));
attributes.insert(format!("{}_IP_EXTERNAL_0", ENV_PREFIX), String::from(ip_external));
attributes.insert(format!("{}_IP_LOCAL_0", ENV_PREFIX), String::from(ip_local));
attributes.insert(format!("{}_MACHINE_TYPE", ENV_PREFIX), String::from(machine_type));

let mut provider = gcp::GcpProvider::try_new().unwrap();
provider.client = provider.client.max_attempts(1);

let _m = mockito::mock("GET", ep_hostname)
.with_status(200)
.with_body(hostname)
.create();
let _m = mockito::mock("GET", ep_ip_external)
.with_status(200)
.with_body(ip_external)
.create();
let _m = mockito::mock("GET", ep_ip_local)
.with_status(200)
.with_body(ip_local)
.create();
let _m = mockito::mock("GET", ep_machine_type)
.with_status(200)
.with_body(machine_type)
.create();
let endpoints = maplit::btreemap! {
"/instance/hostname" => hostname,
"/instance/network-interfaces/0/access-configs/0/external-ip" => ip_external,
"/instance/network-interfaces/0/ip" => ip_local,
"/instance/machine-type" => machine_type,
};
let mut mocks = Vec::with_capacity(endpoints.len());
for (endpoint, body) in endpoints {
let m = mockito::mock("GET", endpoint)
.with_status(200)
.with_body(body)
.create();
mocks.push(m);
}

let attributes = maplit::hashmap! {
format!("{}_HOSTNAME", gcp::ENV_PREFIX) => String::from(hostname),
format!("{}_IP_EXTERNAL_0", gcp::ENV_PREFIX) => String::from(ip_external),
format!("{}_IP_LOCAL_0", gcp::ENV_PREFIX) => String::from(ip_local),
format!("{}_MACHINE_TYPE", gcp::ENV_PREFIX) => String::from(machine_type),
};

let client = crate::retry::Client::try_new()
.unwrap()
.max_attempts(1)
.return_on_404(true);
let provider = gcp::GcpProvider { client };

let v = provider.attributes().unwrap();

assert_eq!(v, attributes);

mockito::reset();
provider.attributes().unwrap_err();
}
40 changes: 20 additions & 20 deletions src/providers/packet/mock_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::providers::{packet, MetadataProvider};
use mockito::{self, Matcher};
use std::collections::HashMap;

#[test]
fn test_boot_checkin() {
Expand Down Expand Up @@ -38,30 +37,31 @@ fn test_boot_checkin() {

#[test]
fn test_packet_attributes() {
let metadata = "{
\"id\": \"test-id\",
\"hostname\": \"test-hostname\",
\"iqn\": \"test-iqn\",
\"plan\": \"test-plan\",
\"facility\": \"test-facility\",
\"tags\": [],
\"ssh_keys\": [],
\"network\": {
\"interfaces\": [],
\"addresses\": [],
\"bonding\": { \"mode\": 0 }
let metadata = r#"{
"id": "test-id",
"hostname": "test-hostname",
"iqn": "test-iqn",
"plan": "test-plan",
"facility": "test-facility",
"tags": [],
"ssh_keys": [],
"network": {
"interfaces": [],
"addresses": [],
"bonding": { "mode": 0 }
},
\"phone_home_url\": \"test-url\"
}";
"phone_home_url": "test-url"
}"#;

let hostname = "test-hostname";
let phone_home_url = "test-url";
let plan = "test-plan";

let mut attributes:HashMap<String, String> = HashMap::new();
attributes.insert(format!("PACKET_HOSTNAME"), String::from(hostname));
attributes.insert(format!("PACKET_PHONE_HOME_URL"), String::from(phone_home_url));
attributes.insert(format!("PACKET_PLAN"), String::from(plan));
let attributes = maplit::hashmap! {
format!("PACKET_HOSTNAME") => String::from(hostname),
format!("PACKET_PHONE_HOME_URL") => String::from(phone_home_url),
format!("PACKET_PLAN") => String::from(plan),
};

let _m = mockito::mock("GET", "/metadata")
.with_status(200)
Expand All @@ -72,4 +72,4 @@ fn test_packet_attributes() {
let v = provider.attributes().unwrap();

assert_eq!(v, attributes);
}
}

0 comments on commit cfffae4

Please sign in to comment.