Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deepu105 committed May 10, 2021
1 parent e603008 commit 45fc41d
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 9 deletions.
80 changes: 79 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ openssl = { version = "0.10.34", features = ["vendored"] }

[dev-dependencies]
spectral = "0.6.0"
faux = "0.1"

[dev-dependencies.cargo-husky]
version = "1"
Expand Down
2 changes: 1 addition & 1 deletion src/app/contexts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use kube::config::{Kubeconfig, NamedContext};

#[derive(Clone)]
#[derive(Clone, Default)]
pub struct KubeContext {
pub name: String,
pub cluster: String,
Expand Down
116 changes: 113 additions & 3 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ impl App {
self.dispatch(IoEvent::GetStatefulSets).await;
self.dispatch(IoEvent::GetReplicaSets).await;
self.dispatch(IoEvent::GetDeployments).await;

self.refresh = false;
}
// make network requests only in intervals to avoid hogging up the network
if self.tick_count == 0 || self.is_routing {
Expand Down Expand Up @@ -410,6 +412,7 @@ impl App {
}
ActiveBlock::Logs => {
if !self.is_streaming {
// do not tail to avoid duplicates
self.dispatch_stream(IoStreamEvent::GetPodLogs(false)).await;
}
}
Expand All @@ -429,9 +432,116 @@ impl App {
if self.tick_count > self.tick_until_poll {
self.tick_count = 0; // reset ticks
}
}
}

if self.refresh {
self.refresh = false;
}
#[cfg(test)]
mod tests {

use tokio::sync::mpsc;

use super::*;

#[tokio::test]
async fn test_on_tick_first_render() {
let (sync_io_tx, mut sync_io_rx) = mpsc::channel::<IoEvent>(500);
let (sync_io_cmd_tx, mut sync_io_cmd_rx) = mpsc::channel::<IoCmdEvent>(500);

let mut app = App {
tick_until_poll: 2,
io_tx: Some(sync_io_tx),
io_cmd_tx: Some(sync_io_cmd_tx),
..App::default()
};

assert_eq!(app.tick_count, 0);
// test first render
app.on_tick(true).await;
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetKubeConfig);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetPods);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetServices);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetConfigMaps);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetStatefulSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetReplicaSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetDeployments);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNamespaces);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNodes);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetPods);

assert_eq!(sync_io_cmd_rx.recv().await.unwrap(), IoCmdEvent::GetCliInfo);

assert!(!app.refresh);
assert!(!app.is_routing);
assert_eq!(app.tick_count, 1);
}
#[tokio::test]
async fn test_on_tick_refresh_tick_limit() {
let (sync_io_tx, mut sync_io_rx) = mpsc::channel::<IoEvent>(500);
let (sync_io_stream_tx, mut sync_io_stream_rx) = mpsc::channel::<IoStreamEvent>(500);
let (sync_io_cmd_tx, mut sync_io_cmd_rx) = mpsc::channel::<IoCmdEvent>(500);

let mut app = App {
tick_until_poll: 2,
tick_count: 2,
refresh: true,
io_tx: Some(sync_io_tx),
io_stream_tx: Some(sync_io_stream_tx),
io_cmd_tx: Some(sync_io_cmd_tx),
..App::default()
};

assert_eq!(app.tick_count, 2);
// test first render
app.on_tick(false).await;
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::RefreshClient);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetKubeConfig);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetPods);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetServices);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetConfigMaps);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetStatefulSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetReplicaSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetDeployments);

assert_eq!(
sync_io_stream_rx.recv().await.unwrap(),
IoStreamEvent::RefreshClient
);
assert_eq!(sync_io_cmd_rx.recv().await.unwrap(), IoCmdEvent::GetCliInfo);

assert!(!app.refresh);
assert!(!app.is_routing);
assert_eq!(app.tick_count, 0);
}
#[tokio::test]
async fn test_on_tick_routing() {
let (sync_io_tx, mut sync_io_rx) = mpsc::channel::<IoEvent>(500);
let (sync_io_stream_tx, mut sync_io_stream_rx) = mpsc::channel::<IoStreamEvent>(500);

let mut app = App {
tick_until_poll: 2,
tick_count: 2,
is_routing: true,
refresh: false,
io_tx: Some(sync_io_tx),
io_stream_tx: Some(sync_io_stream_tx),
..App::default()
};

app.push_navigation_stack(RouteId::Home, ActiveBlock::Logs);

assert_eq!(app.tick_count, 2);
// test first render
app.on_tick(false).await;
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNamespaces);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNodes);

assert_eq!(
sync_io_stream_rx.recv().await.unwrap(),
IoStreamEvent::GetPodLogs(false)
);

assert!(!app.refresh);
assert!(!app.is_routing);
assert_eq!(app.tick_count, 0);
}
}
2 changes: 1 addition & 1 deletion src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anyhow::anyhow;
use std::sync::Arc;
use tokio::sync::Mutex;

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum IoCmdEvent {
GetCliInfo,
GetDescribe {
Expand Down
19 changes: 18 additions & 1 deletion src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ fn copy_to_clipboard(content: String) {

#[cfg(test)]
mod tests {
use crate::app::pods::KubePod;
use crate::app::{contexts::KubeContext, pods::KubePod};

use super::*;

Expand Down Expand Up @@ -529,4 +529,21 @@ mod tests {
handle_scroll(&mut app, true, false).await;
assert_eq!(app.data.logs.state.selected(), Some(0));
}

#[tokio::test]
async fn test_context_switch() {
let mut app = App::default();
let ctx = KubeContext {
name: "test".into(),
..KubeContext::default()
};
app.data.contexts.set_items(vec![ctx]);

assert_eq!(app.data.selected.context, None);
app.route_contexts();
handle_route_events(Key::Enter, &mut app).await;

assert_eq!(app.data.selected.context, Some("test".into()));
assert!(app.refresh);
}
}
2 changes: 1 addition & 1 deletion src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use kube::Client;
use std::sync::Arc;
use tokio::sync::Mutex;

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum IoEvent {
GetKubeConfig,
GetNodes,
Expand Down
3 changes: 2 additions & 1 deletion src/network/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{sync::Arc, time::Duration};
use tokio::sync::Mutex;
use tokio_stream::StreamExt;

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum IoStreamEvent {
RefreshClient,
GetPodLogs(bool),
Expand Down Expand Up @@ -94,6 +94,7 @@ impl<'a> NetworkStream<'a> {
follow: true,
previous: false,
// timestamps: true,
// tail only on first call to avoid duplicates on disconnect
tail_lines: if tail { Some(10) } else { Some(0) },
..Default::default()
};
Expand Down

0 comments on commit 45fc41d

Please sign in to comment.