// Copyright (c) 2019-2022 Alibaba Cloud
// Copyright (c) 2019-2022 Ant Group
//
// SPDX-License-Identifier: Apache-2.0
//

//  TODO(burgerdev): These tests don't do much since they don't run .await.
//  Also, its not clear if running such network operations would belong in unit tests.
#[cfg(test)]
mod tests {
    use crate::network::{
        network_model::{
            l3_forwarding_model, tc_filter_model, L3_FORWARDING_NET_MODEL_STR,
            TC_FILTER_NET_MODEL_STR,
        },
        network_pair::NetworkPair,
    };
    use anyhow::Context;
    use rtnetlink::LinkVeth;
    use scopeguard::defer;
    #[actix_rt::test]
    async fn test_tc_redirect_network() {
        if let Ok((connection, handle, _)) = rtnetlink::new_connection().context("new connection") {
            let thread_handler = tokio::spawn(connection);
            defer!({
                thread_handler.abort();
            });

            let veth = LinkVeth::new("foo", "bar").build();

            handle.link().add(veth);

            if let Ok(net_pair) =
                NetworkPair::new(&handle, 1, "bar", TC_FILTER_NET_MODEL_STR, 2).await
            {
                if let Ok(index) = tc_filter_model::fetch_index(&handle, "bar").await {
                    assert!(net_pair.add_network_model().await.is_ok());
                    assert!(net_pair.del_network_model().await.is_ok());
                    assert!(handle.link().del(index).execute().await.is_ok());
                }
            }
        }
    }

    #[actix_rt::test]
    async fn test_l3_redirect_network() {
        if let Ok((connection, handle, _)) = rtnetlink::new_connection().context("new connection") {
            let thread_handler = tokio::spawn(connection);
            defer!({
                thread_handler.abort();
            });

            let veth = LinkVeth::new("foo", "bar").build();

            handle.link().add(veth);

            if let Ok(net_pair) =
                NetworkPair::new(&handle, 1, "bar", L3_FORWARDING_NET_MODEL_STR, 2).await
            {
                if let Ok(index) = l3_forwarding_model::fetch_index(&handle, "bar").await {
                    assert!(net_pair.add_network_model().await.is_ok());
                    assert!(net_pair.del_network_model().await.is_ok());
                    assert!(handle.link().del(index).execute().await.is_ok());
                }
            }
        }
    }
}
