Skip to content

Commit

Permalink
Added some network_service unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Mar 22, 2024
1 parent 0a4634e commit 5a25072
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions rust/agama-server/tests/network_service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
pub mod common;

use crate::common::DBusServer;
use agama_lib::error::ServiceError;
use agama_lib::network::types::DeviceType;
use agama_server::network::web::network_service;
use agama_server::network::{
self,
model::{self, GeneralState, Ipv4Method, Ipv6Method, StateConfig},
Adapter, NetworkAdapterError, NetworkService, NetworkState,
};
use agama_server::web::{generate_token, MainServiceBuilder, ServiceConfig};

use async_trait::async_trait;
use axum::http::header;
use axum::{
body::Body,
http::{Method, Request, StatusCode},
response::Response,
routing::{get, put},
Json, Router,
};
use common::body_to_string;
use serde_json::to_string;
use std::{error::Error, path::PathBuf};
use tokio::{sync::broadcast::channel, test};
use tower::ServiceExt;

fn public_dir() -> PathBuf {
std::env::current_dir().unwrap().join("public")
}
async fn build_state() -> NetworkState {
let general_state = GeneralState::default();
let device = model::Device {
name: String::from("eth0"),
type_: DeviceType::Ethernet,
};
let eth0 = model::Connection::new("eth0".to_string(), DeviceType::Ethernet);

NetworkState::new(general_state, vec![], vec![device], vec![eth0])
}

async fn build_service(state: NetworkState) -> Result<Router, ServiceError> {
let dbus = DBusServer::new().start().await?.connection();

let adapter = NetworkTestAdapter(state);
Ok(network_service(dbus, adapter).await?)
}

#[derive(Default)]
pub struct NetworkTestAdapter(network::NetworkState);

#[async_trait]
impl Adapter for NetworkTestAdapter {
async fn read(
&self,
_: Vec<NetworkStateItems>,
) -> Result<network::NetworkState, NetworkAdapterError> {
Ok(self.0.clone())
}

async fn write(&self, _network: &network::NetworkState) -> Result<(), NetworkAdapterError> {
unimplemented!("Not used in tests");
}
}

#[test]
async fn test_network_state() -> Result<(), Box<dyn Error>> {
let state = build_state().await;
let network_service = build_service(state).await?;

let request = Request::builder()
.uri("/state")
.method(Method::GET)
.body(Body::empty())
.unwrap();

let response = network_service.oneshot(request).await?;
assert_eq!(response.status(), StatusCode::OK);
let body = body_to_string(response.into_body()).await;
assert!(body.contains(r#""wireless_enabled":false"#));
Ok(())
}

#[test]
async fn test_change_network_state() -> Result<(), Box<dyn Error>> {
let mut state = build_state().await;
let network_service = build_service(state.clone()).await?;
state.general_state.wireless_enabled = true;

let request = Request::builder()
.uri("/state")
.method(Method::PUT)
.header(header::CONTENT_TYPE, "application/json")
.body(to_string(&state.general_state)?)
.unwrap();

let response = network_service.oneshot(request).await?;
assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body();
let body = body_to_string(body).await;
assert_eq!(body, to_string(&state.general_state)?);
Ok(())
}

#[test]
async fn test_network_connections() -> Result<(), Box<dyn Error>> {
let state = build_state().await;
let network_service = build_service(state.clone()).await?;

let request = Request::builder()
.uri("/connections")
.method(Method::GET)
.body(Body::empty())
.unwrap();

let response = network_service.oneshot(request).await?;
assert_eq!(response.status(), StatusCode::OK);
let body = body_to_string(response.into_body()).await;
assert!(body.contains(r#""id":"eth0""#));
Ok(())
}

#[test]
async fn test_network_devices() -> Result<(), Box<dyn Error>> {
let state = build_state().await;
let network_service = build_service(state.clone()).await?;

let request = Request::builder()
.uri("/devices")
.method(Method::GET)
.body(Body::empty())
.unwrap();

let response = network_service.oneshot(request).await?;
assert_eq!(response.status(), StatusCode::OK);
let body = body_to_string(response.into_body()).await;
assert!(body.contains(r#""name":"eth0""#));
Ok(())
}

0 comments on commit 5a25072

Please sign in to comment.