Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 4 additions & 88 deletions tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ use std::fs;
use std::io;
use std::path::Path;

use mockito::{self, server_url, Mock};
use mockito::{self, Mock};
use trycmd::TestCases;

use test_utils::env;
use test_utils::{mock_endpoint, MockEndpointBuilder};
use test_utils::{
mock_common_upload_endpoints, mock_endpoint, ChunkOptions, MockEndpointBuilder, ServerBehavior,
};

pub const UTC_DATE_FORMAT: &str = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6,9}Z";
const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -75,92 +77,6 @@ pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>)
Ok(())
}

pub enum ServerBehavior {
Legacy,
Modern,
ModernV2,
}

#[derive(Debug)]
pub struct ChunkOptions {
chunk_size: usize,
missing_chunks: Vec<String>,
}

impl Default for ChunkOptions {
fn default() -> Self {
Self {
chunk_size: 8388608,
missing_chunks: vec![],
}
}
}

// Endpoints need to be bound, as they need to live long enough for test to finish
pub fn mock_common_upload_endpoints(
behavior: ServerBehavior,
chunk_options: ChunkOptions,
) -> Vec<Mock> {
let ChunkOptions {
chunk_size,
missing_chunks,
} = chunk_options;
let (accept, release_request_count, assemble_endpoint) = match behavior {
ServerBehavior::Legacy => (
"\"release_files\"",
2,
"/api/0/organizations/wat-org/releases/wat-release/assemble/",
),
ServerBehavior::Modern => (
"\"release_files\", \"artifact_bundles\"",
0,
"/api/0/organizations/wat-org/artifactbundle/assemble/",
),
ServerBehavior::ModernV2 => (
"\"release_files\", \"artifact_bundles_v2\"",
0,
"/api/0/organizations/wat-org/artifactbundle/assemble/",
),
};
let chunk_upload_response = format!(
"{{
\"url\": \"{}/api/0/organizations/wat-org/chunk-upload/\",
\"chunkSize\": {chunk_size},
\"chunksPerRequest\": 64,
\"maxRequestSize\": 33554432,
\"concurrency\": 8,
\"hashAlgorithm\": \"sha1\",
\"accept\": [{}]
}}",
server_url(),
accept,
);

vec![
mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/projects/wat-org/wat-project/releases/", 208)
.with_response_file("releases/get-release.json"),
)
.expect_at_least(release_request_count)
.expect_at_most(release_request_count),
mock_endpoint(
MockEndpointBuilder::new("GET", "/api/0/organizations/wat-org/chunk-upload/", 200)
.with_response_body(chunk_upload_response),
),
mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/organizations/wat-org/chunk-upload/", 200)
.with_response_body("[]"),
),
mock_endpoint(
MockEndpointBuilder::new("POST", assemble_endpoint, 200).with_response_body(format!(
r#"{{"state":"created","missingChunks":{}}}"#,
serde_json::to_string(&missing_chunks).unwrap()
)),
)
.expect_at_least(1),
]
}

pub fn assert_endpoints(mocks: &[Mock]) {
for mock in mocks {
mock.assert();
Expand Down
88 changes: 88 additions & 0 deletions tests/integration/test_utils/mock_common_endpoints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::integration::test_utils::{self, MockEndpointBuilder};
use mockito::Mock;

// Endpoints need to be bound, as they need to live long enough for test to finish
pub fn mock_common_upload_endpoints(
behavior: ServerBehavior,
chunk_options: ChunkOptions,
) -> Vec<Mock> {
let ChunkOptions {
chunk_size,
missing_chunks,
} = chunk_options;
let (accept, release_request_count, assemble_endpoint) = match behavior {
ServerBehavior::Legacy => (
"\"release_files\"",
2,
"/api/0/organizations/wat-org/releases/wat-release/assemble/",
),
ServerBehavior::Modern => (
"\"release_files\", \"artifact_bundles\"",
0,
"/api/0/organizations/wat-org/artifactbundle/assemble/",
),
ServerBehavior::ModernV2 => (
"\"release_files\", \"artifact_bundles_v2\"",
0,
"/api/0/organizations/wat-org/artifactbundle/assemble/",
),
};
let chunk_upload_response = format!(
"{{
\"url\": \"{}/api/0/organizations/wat-org/chunk-upload/\",
\"chunkSize\": {chunk_size},
\"chunksPerRequest\": 64,
\"maxRequestSize\": 33554432,
\"concurrency\": 8,
\"hashAlgorithm\": \"sha1\",
\"accept\": [{}]
}}",
mockito::server_url(),
accept,
);

vec![
test_utils::mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/projects/wat-org/wat-project/releases/", 208)
.with_response_file("releases/get-release.json"),
)
.expect_at_least(release_request_count)
.expect_at_most(release_request_count),
test_utils::mock_endpoint(
MockEndpointBuilder::new("GET", "/api/0/organizations/wat-org/chunk-upload/", 200)
.with_response_body(chunk_upload_response),
),
test_utils::mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/organizations/wat-org/chunk-upload/", 200)
.with_response_body("[]"),
),
test_utils::mock_endpoint(
MockEndpointBuilder::new("POST", assemble_endpoint, 200).with_response_body(format!(
r#"{{"state":"created","missingChunks":{}}}"#,
serde_json::to_string(&missing_chunks).unwrap()
)),
)
.expect_at_least(1),
]
}

pub enum ServerBehavior {
Legacy,
Modern,
ModernV2,
}

#[derive(Debug)]
pub struct ChunkOptions {
pub chunk_size: usize,
pub missing_chunks: Vec<String>,
}

impl Default for ChunkOptions {
fn default() -> Self {
Self {
chunk_size: 8388608,
missing_chunks: vec![],
}
}
}
2 changes: 2 additions & 0 deletions tests/integration/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! A collection of utilities for integration tests.

pub mod env;
mod mock_common_endpoints;
mod mock_endpoint_builder;

pub use mock_common_endpoints::{mock_common_upload_endpoints, ChunkOptions, ServerBehavior};
pub use mock_endpoint_builder::{mock_endpoint, MockEndpointBuilder};
Loading