Skip to content

Commit

Permalink
Merge branch 'tests/init' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackwills committed Jan 16, 2024
2 parents d0b2721 + 53543a1 commit 4bcf77d
Show file tree
Hide file tree
Showing 12 changed files with 3,442 additions and 232 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ see <a href="https://forums.raspberrypi.com/viewtopic.php?t=203128" target='_bla

## Tests

As of yet untested, needs work
~~As of yet untested, needs work~~
The work has been done

```shell
cargo test -- --test-threads=1
Expand Down
6 changes: 3 additions & 3 deletions create_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,19 @@ check_typos() {
}

# Make sure the unused lint isn't used
check_allow_unsued() {
check_allow_unused() {
matches_any=$(find . -type d \( -name .git -o -name target \) -prune -o -type f -exec grep -lE '^#!\[allow\(unused\)\]$' {} +)
matches_cargo=$(grep "^unused = \"allow\"" ./Cargo.toml)
if [ -n "$matches_any" ]; then
error_close "\"#[allow(unused)]\" in ${matches_any}"
elif [ -n "$matches_cargo" ]; then
error_close "\"unsed = \"allow\"\" in Cargo.toml"
error_close "\"unused = \"allow\"\" in Cargo.toml"
fi
}

# Full flow to create a new release
release_flow() {
check_allow_unsued
check_allow_unused
check_typos

check_git
Expand Down
14 changes: 7 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ networks:
oxker-example-net:
name: oxker-examaple-net
services:
postgres:
image: postgres:alpine3.19
container_name: postgres
postgres_but_with_a_longer_container_name:
container_name: postgres_but_with_a_longer_container_name
build:
dockerfile: ./postgres.Dockerfile
context: "."
environment:
- POSTGRES_PASSWORD=never_use_this_password_in_production
- POSTGRES_PASSWORD=never_use_this_password_in_production
ipc: private
restart: always
shm_size: 256MB
networks:
- oxker-example-net
- oxker-example-net
deploy:
resources:
limits:
Expand All @@ -39,5 +41,3 @@ services:
resources:
limits:
memory: 512M


1 change: 1 addition & 0 deletions postgres.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM postgres:16-alpine3.19
114 changes: 108 additions & 6 deletions src/app_data/container_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ macro_rules! unit_struct {
}
}

#[cfg(test)]
impl From<&str> for $name {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}

impl$name {
pub fn get(&self) -> &str {
self.0.as_str()
Expand Down Expand Up @@ -93,7 +100,7 @@ macro_rules! unit_struct {
unit_struct!(ContainerName);
unit_struct!(ContainerImage);

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatefulList<T> {
pub state: ListState,
pub items: Vec<T>,
Expand Down Expand Up @@ -154,7 +161,7 @@ impl<T> StatefulList<T> {
.state
.selected()
.map_or(0, |value| if len > 0 { value + 1 } else { value });
format!("{c}/{}", self.items.len())
format!(" {c}/{}", self.items.len())
}
}
}
Expand Down Expand Up @@ -234,7 +241,7 @@ impl fmt::Display for State {
}

/// Items for the container control list
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DockerControls {
Pause,
Restart,
Expand Down Expand Up @@ -416,7 +423,7 @@ impl fmt::Display for LogsTz {
/// Store the logs alongside a HashSet, each log *should* generate a unique timestamp,
/// so if we store the timestamp separately in a HashSet, we can then check if we should insert a log line into the
/// stateful list dependent on whethere the timestamp is in the HashSet or not
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Logs {
logs: StatefulList<ListItem<'static>>,
tz: HashSet<LogsTz>,
Expand Down Expand Up @@ -475,7 +482,7 @@ impl Logs {
}

/// Info for each container
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContainerItem {
pub created: u64,
pub cpu_stats: VecDeque<CpuStats>,
Expand Down Expand Up @@ -594,7 +601,7 @@ impl ContainerItem {
}

/// Container information panel headings + widths, for nice pretty formatting
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Columns {
pub name: (Header, u8),
pub state: (Header, u8),
Expand Down Expand Up @@ -623,3 +630,98 @@ impl Columns {
}
}
}

#[cfg(test)]
mod tests {
use ratatui::widgets::ListItem;

use crate::{
app_data::{ContainerImage, Logs},
ui::log_sanitizer,
};

use super::{ByteStats, ContainerName, CpuStats, LogsTz};

#[test]
// Display CpuStats as a string
fn test_container_state_cpustats_to_string() {
let test = |f: f64, s: &str| {
assert_eq!(CpuStats::new(f).to_string(), s);
};

test(0.0, "00.00%");
test(1.5, "01.50%");
test(15.15, "15.15%");
test(150.15, "150.15%");
}

#[test]
// Display bytestats as a string, convert into correct data unit (Kb, MB, GB)
fn test_container_state_bytestats_to_string() {
let test = |u: u64, s: &str| {
assert_eq!(ByteStats::new(u).to_string(), s);
};

test(0, "0.00 kB");
test(150, "0.15 kB");
test(1500, "1.50 kB");
test(150_000, "150.00 kB");
test(1_500_000, "1.50 MB");
test(15_000_000, "15.00 MB");
test(150_000_000, "150.00 MB");
test(1_500_000_000, "1.50 GB");
test(15_000_000_000, "15.00 GB");
test(150_000_000_000, "150.00 GB");
}

#[test]
/// ContainerName as string truncated correctly
fn test_container_state_container_name_to_string() {
let result = ContainerName::from("name_01");
assert_eq!(result.to_string(), "name_01");

let result = ContainerName::from("name_01_name_01_name_01_name_01_");
assert_eq!(result.to_string(), "name_01_name_01_name_01_name_…");

let result = result.get();
assert_eq!(result, "name_01_name_01_name_01_name_01_");
}

#[test]
/// ContainerImage as string truncated correctly
fn test_container_state_container_image() {
let result = ContainerImage::from("name_01");
assert_eq!(result.to_string(), "name_01");

let result = ContainerImage::from("name_01_name_01_name_01_name_01_");
assert_eq!(result.to_string(), "name_01_name_01_name_01_name_…");

let result = result.get();
assert_eq!(result, "name_01_name_01_name_01_name_01_");
}

#[test]
/// Logs can only contain 1 entry per LogzTz
fn test_container_state_logz() {
let input = "2023-01-14T19:13:30.783138328Z Lorem ipsum dolor sit amet";
let tz = LogsTz::from(input);
let mut logs = Logs::default();
let line = log_sanitizer::remove_ansi(input);

logs.insert(ListItem::new(line.clone()), tz.clone());
logs.insert(ListItem::new(line.clone()), tz.clone());
logs.insert(ListItem::new(line), tz);

assert_eq!(logs.logs.items.len(), 1);

let input = "2023-01-15T19:13:30.783138328Z Lorem ipsum dolor sit amet";
let tz = LogsTz::from(input);
let line = log_sanitizer::remove_ansi(input);

logs.insert(ListItem::new(line.clone()), tz.clone());
logs.insert(ListItem::new(line.clone()), tz.clone());
logs.insert(ListItem::new(line), tz);

assert_eq!(logs.logs.items.len(), 2);
}
}
Loading

0 comments on commit 4bcf77d

Please sign in to comment.