Skip to content

Commit

Permalink
[scaling] feat: builder service (shuttle-hq#999) - only test refactor
Browse files Browse the repository at this point in the history
* feat: builder init

* feat: build route

* feat: builder service

* feat: binary and docker image

* refactor: clippy suggestions

* refactor: move test helpers to common crate

* refactor: trim deployer-alpha

* refactor: get name from nbuild

* refactor: .crate to .tar.gz

* refactor: Cmd to Entrypoint
  • Loading branch information
chesedo committed Jul 12, 2023
1 parent 9b738d9 commit 196c1c1
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 81 deletions.
51 changes: 28 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"cargo-shuttle",
"codegen",
"common",
"common-tests",
"deployer",
"gateway",
"proto",
Expand All @@ -31,6 +32,7 @@ repository = "https://github.com/shuttle-hq/shuttle"
[workspace.dependencies]
shuttle-codegen = { path = "codegen", version = "0.21.0" }
shuttle-common = { path = "common", version = "0.21.0" }
shuttle-common-tests = { path = "common-tests", version = "0.21.0" }
shuttle-proto = { path = "proto", version = "0.21.0" }
shuttle-service = { path = "service", version = "0.21.0" }

Expand Down
12 changes: 12 additions & 0 deletions common-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "shuttle-common-tests"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
publish = false

[dependencies]
hyper = { workspace = true }
shuttle-common = { workspace = true }
tower = { workspace = true }
57 changes: 57 additions & 0 deletions common-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use shuttle_common::claims::{Claim, Scope};

/// Layer to set JwtScopes on a request.
/// For use in other tests
#[derive(Clone)]
pub struct JwtScopesLayer {
/// Thes scopes to set
scopes: Vec<Scope>,
}

impl JwtScopesLayer {
/// Create a new layer to set scopes on requests
pub fn new(scopes: Vec<Scope>) -> Self {
Self { scopes }
}
}

impl<S> tower::Layer<S> for JwtScopesLayer {
type Service = JwtScopes<S>;

fn layer(&self, inner: S) -> Self::Service {
JwtScopes {
inner,
scopes: self.scopes.clone(),
}
}
}

/// Middleware to set scopes on a request
#[derive(Clone)]
pub struct JwtScopes<S> {
inner: S,
scopes: Vec<Scope>,
}

impl<S> tower::Service<hyper::Request<hyper::Body>> for JwtScopes<S>
where
S: tower::Service<hyper::Request<hyper::Body>> + Send + Clone + 'static,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;

fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, mut req: hyper::Request<hyper::Body>) -> Self::Future {
req.extensions_mut()
.insert(Claim::new("test".to_string(), self.scopes.clone()));
self.inner.call(req)
}
}
3 changes: 1 addition & 2 deletions resource-recorder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ features = ["backend", "tonic"]
workspace = true

[dev-dependencies]
hyper = { workspace = true }
portpicker = { workspace = true }
pretty_assertions = { workspace = true }
serde_json = { workspace = true }
tower = { workspace = true }
shuttle-common-tests = { workspace = true }
58 changes: 2 additions & 56 deletions resource-recorder/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::net::{Ipv4Addr, SocketAddr};
use portpicker::pick_unused_port;
use pretty_assertions::{assert_eq, assert_ne};
use serde_json::json;
use shuttle_common::claims::{Claim, Scope};
use shuttle_common::claims::Scope;
use shuttle_common_tests::JwtScopesLayer;
use shuttle_proto::resource_recorder::{
record_request, resource_recorder_client::ResourceRecorderClient,
resource_recorder_server::ResourceRecorderServer, ProjectResourcesRequest, RecordRequest,
Expand Down Expand Up @@ -252,58 +253,3 @@ async fn manage_resources() {
_ = test_future => {},
}
}

/// Layer to set JwtScopes on a request
#[derive(Clone)]
pub struct JwtScopesLayer {
/// Thes scopes to set
scopes: Vec<Scope>,
}

impl JwtScopesLayer {
/// Create a new layer to set scopes on requests
pub fn new(scopes: Vec<Scope>) -> Self {
Self { scopes }
}
}

impl<S> tower::Layer<S> for JwtScopesLayer {
type Service = JwtScopes<S>;

fn layer(&self, inner: S) -> Self::Service {
JwtScopes {
inner,
scopes: self.scopes.clone(),
}
}
}

/// Middleware to set scopes on a request
#[derive(Clone)]
pub struct JwtScopes<S> {
inner: S,
scopes: Vec<Scope>,
}

impl<S> tower::Service<hyper::Request<hyper::Body>> for JwtScopes<S>
where
S: tower::Service<hyper::Request<hyper::Body>> + Send + Clone + 'static,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;

fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, mut req: hyper::Request<hyper::Body>) -> Self::Future {
req.extensions_mut()
.insert(Claim::new("test".to_string(), self.scopes.clone()));
self.inner.call(req)
}
}

0 comments on commit 196c1c1

Please sign in to comment.