Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Test logs with t2 #12

Merged
merged 4 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:

- name: Test (step 3) - run tests
run: |
./stackable.sh testdriver-1 -i ./.cluster/key "cd agent-integration-tests/ && cargo test"
./stackable.sh testdriver-1 -i ./.cluster/key "cd agent-integration-tests/ && cargo test -- --test-threads=1"

- name: Terminate cluster via T2 API
if: always()
Expand Down
10 changes: 10 additions & 0 deletions 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 @@ -19,3 +19,4 @@ serde_json = "1.0"
serde_yaml = "0.8"
spectral = "0.6"
tokio = { version = "1.5", features = ["rt-multi-thread"] }
uuid = { version = "0.8", features = ["v4"] }
111 changes: 111 additions & 0 deletions tests/logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
mod test;
use test::prelude::*;

struct EchoService<'a> {
client: &'a TestKubeClient,
pod: TemporaryResource<'a, Pod>,
}

impl<'a> EchoService<'a> {
pub fn new(client: &'a TestKubeClient, log_output: &[&str]) -> Self {
setup_repository(&client);

/// Newline character for LOG_OUTPUT
///
/// Source code: \\\\\\\\n
/// Pod spec: \\\\n
/// Systemd unit file: \\n
/// echo-service: \n
/// Journal: separate entries
const NEWLINE: &str = "\\\\\\\\n";

let pod = TemporaryResource::new(
&client,
&formatdoc! {r#"
apiVersion: v1
kind: Pod
metadata:
name: agent-logs-integration-test-{id}
spec:
containers:
- name: echo-service
image: echo-service:1.0.0
command:
- echo-service-1.0.0/start.sh
env:
- name: LOG_OUTPUT
value: "{log_output}"
tolerations:
- key: kubernetes.io/arch
operator: Equal
value: stackable-linux
"#,
id = Uuid::new_v4(),
log_output = log_output.join(NEWLINE)
},
);

client.verify_pod_condition(&pod, "Ready");

EchoService { client, pod }
}

pub fn get_logs(&self, params: &LogParams) -> Vec<String> {
self.client.get_logs(&self.pod, params)
}
}

#[test]
fn all_logs_should_be_retrievable() {
let client = TestKubeClient::new();

let log_output = vec!["line 1", "line 2", "line 3"];
let echo_service = EchoService::new(&client, &log_output);

let logs = echo_service.get_logs(&LogParams::default());
assert_equals(&["line 1", "line 2", "line 3"], &logs);
}

#[test]
fn the_tail_of_logs_should_be_retrievable() {
let client = TestKubeClient::new();

let log_output = vec!["line 1", "line 2", "line 3"];
let echo_service = EchoService::new(&client, &log_output);

let with_tail_lines = |tail_lines| LogParams {
tail_lines: Some(tail_lines),
..Default::default()
};

let logs = echo_service.get_logs(&with_tail_lines(0));
assert_that(&logs).is_empty();

let logs = echo_service.get_logs(&with_tail_lines(1));
assert_equals(&["line 3"], &logs);

let logs = echo_service.get_logs(&with_tail_lines(2));
assert_equals(&["line 2", "line 3"], &logs);

let logs = echo_service.get_logs(&with_tail_lines(3));
assert_equals(&["line 1", "line 2", "line 3"], &logs);

let logs = echo_service.get_logs(&with_tail_lines(4));
assert_equals(&["line 1", "line 2", "line 3"], &logs);
}

#[test]
fn non_ascii_characters_should_be_handled_correctly() {
let client = TestKubeClient::new();

let log_output = vec!["Spade: ♠", "Heart: ♥", "Diamond: ♦", "Club: ♣"];
let echo_service = EchoService::new(&client, &log_output);

let logs = echo_service.get_logs(&LogParams::default());
assert_equals(&["Spade: ♠", "Heart: ♥", "Diamond: ♦", "Club: ♣"], &logs);
}

fn assert_equals(expected: &[&str], actual: &[String]) {
assert_that(&actual.iter().map(String::as_ref).collect::<Vec<_>>())
.equals_iterator(&expected.iter());
}
62 changes: 21 additions & 41 deletions tests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,27 @@ use test::prelude::*;
fn service_should_be_started_successfully() {
let client = TestKubeClient::new();

create_repository(&client);

// Remove pod if it still exists from a previous test run.
if let Some(pod) = client.find::<Pod>("agent-integration-test") {
client.delete(pod);
};

let pod = client.create(indoc! {r#"
apiVersion: v1
kind: Pod
metadata:
name: agent-integration-test
spec:
containers:
- name: test-service
image: test-service:0.1.0
command:
- test-service-0.1.0/start.sh
tolerations:
- key: kubernetes.io/arch
operator: Equal
value: stackable-linux
"#});
setup_repository(&client);

let pod = TemporaryResource::new(
&client,
indoc! {"
apiVersion: v1
kind: Pod
metadata:
name: agent-service-integration-test
spec:
containers:
- name: noop-service
image: noop-service:1.0.0
command:
- noop-service-1.0.0/start.sh
tolerations:
- key: kubernetes.io/arch
operator: Equal
value: stackable-linux
"},
);

client.verify_pod_condition(&pod, "Ready");

client.delete(pod);
}

fn create_repository(client: &TestKubeClient) {
client.apply_crd(&Repository::crd());

client.apply::<Repository>(indoc!("
apiVersion: stable.stackable.de/v1
kind: Repository
metadata:
name: integration-test-repository
namespace: default
spec:
repo_type: StackableRepo
properties:
url: https://raw.githubusercontent.com/stackabletech/integration-test-repo/6d784f1fb433123cb3b1d5cd7364a4553246d749/
"));
}
Loading