|
| 1 | +use integration_test_commons::test::prelude::*; |
| 2 | + |
| 3 | +struct ExitService<'a> { |
| 4 | + client: &'a TestKubeClient, |
| 5 | + pod: TemporaryResource<'a, Pod>, |
| 6 | +} |
| 7 | + |
| 8 | +impl<'a> ExitService<'a> { |
| 9 | + pub fn new(client: &'a TestKubeClient, exit_code: i32) -> Self { |
| 10 | + setup_repository(&client); |
| 11 | + |
| 12 | + let pod = TemporaryResource::new( |
| 13 | + &client, |
| 14 | + &with_unique_name(&format!( |
| 15 | + " |
| 16 | + apiVersion: v1 |
| 17 | + kind: Pod |
| 18 | + metadata: |
| 19 | + name: agent-service-integration-test-job |
| 20 | + spec: |
| 21 | + containers: |
| 22 | + - name: exit-service |
| 23 | + image: exit-service:1.0.0 |
| 24 | + command: |
| 25 | + - exit-service-1.0.0/start.sh |
| 26 | + env: |
| 27 | + - name: EXIT_CODE |
| 28 | + value: {exit_code} |
| 29 | + restartPolicy: Never |
| 30 | + tolerations: |
| 31 | + - key: kubernetes.io/arch |
| 32 | + operator: Equal |
| 33 | + value: stackable-linux |
| 34 | + ", |
| 35 | + exit_code = exit_code |
| 36 | + )), |
| 37 | + ); |
| 38 | + |
| 39 | + ExitService { client, pod } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn verify_terminated(&mut self) { |
| 43 | + self.client.verify_status::<Pod, _>(&self.pod, |pod| { |
| 44 | + let phase = ExitService::phase_from(pod); |
| 45 | + let container_terminated = ExitService::terminated_container_state_from(pod).is_some(); |
| 46 | + (phase == "Succeeded" || phase == "Failed") && container_terminated |
| 47 | + }); |
| 48 | + |
| 49 | + self.pod.update(); |
| 50 | + } |
| 51 | + |
| 52 | + pub fn phase(&self) -> String { |
| 53 | + ExitService::phase_from(&self.pod) |
| 54 | + } |
| 55 | + |
| 56 | + pub fn terminated_container_state(&self) -> Option<ContainerStateTerminated> { |
| 57 | + ExitService::terminated_container_state_from(&self.pod) |
| 58 | + } |
| 59 | + |
| 60 | + fn phase_from(pod: &Pod) -> String { |
| 61 | + pod.status |
| 62 | + .as_ref() |
| 63 | + .and_then(|status| status.phase.clone()) |
| 64 | + .unwrap_or_else(|| String::from("Unknown")) |
| 65 | + } |
| 66 | + |
| 67 | + fn terminated_container_state_from(pod: &Pod) -> Option<ContainerStateTerminated> { |
| 68 | + pod.status |
| 69 | + .as_ref() |
| 70 | + .and_then(|status| status.container_statuses.as_ref()) |
| 71 | + .and_then(|container_statuses| container_statuses.first().to_owned()) |
| 72 | + .and_then(|container_status| container_status.state.as_ref()) |
| 73 | + .and_then(|state| state.terminated.to_owned()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn successful_job_should_have_phase_succeeded_and_error_code_0() { |
| 79 | + let client = TestKubeClient::new(); |
| 80 | + |
| 81 | + let exit_code = 0; |
| 82 | + let mut exit_service = ExitService::new(&client, exit_code); |
| 83 | + |
| 84 | + exit_service.verify_terminated(); |
| 85 | + |
| 86 | + asserting("phase") |
| 87 | + .that(&exit_service.phase()) |
| 88 | + .is_equal_to(String::from("Succeeded")); |
| 89 | + |
| 90 | + let container_state = exit_service |
| 91 | + .terminated_container_state() |
| 92 | + .expect("Terminated container state expected"); |
| 93 | + asserting("exit code") |
| 94 | + .that(&container_state.exit_code) |
| 95 | + .is_equal_to(0); |
| 96 | + asserting("message") |
| 97 | + .that(&container_state.message) |
| 98 | + .is_equal_to(Some(String::from("Completed"))); |
| 99 | + asserting("reason") |
| 100 | + .that(&container_state.message) |
| 101 | + .is_equal_to(Some(String::from("Completed"))); |
| 102 | +} |
| 103 | + |
| 104 | +#[test] |
| 105 | +fn failed_job_should_have_phase_failed_and_error_code_1() { |
| 106 | + let client = TestKubeClient::new(); |
| 107 | + |
| 108 | + // All non-zero exit codes are mapped by the agent to 1. |
| 109 | + let exit_code = 42; |
| 110 | + let mut exit_service = ExitService::new(&client, exit_code); |
| 111 | + |
| 112 | + exit_service.verify_terminated(); |
| 113 | + |
| 114 | + asserting("phase") |
| 115 | + .that(&exit_service.phase()) |
| 116 | + .is_equal_to(String::from("Failed")); |
| 117 | + |
| 118 | + let container_state = exit_service |
| 119 | + .terminated_container_state() |
| 120 | + .expect("Terminated container state expected"); |
| 121 | + asserting("exit code") |
| 122 | + .that(&container_state.exit_code) |
| 123 | + .is_equal_to(1); |
| 124 | + asserting("message") |
| 125 | + .that(&container_state.message) |
| 126 | + .is_equal_to(Some(String::from("Error"))); |
| 127 | + asserting("reason") |
| 128 | + .that(&container_state.message) |
| 129 | + .is_equal_to(Some(String::from("Error"))); |
| 130 | +} |
0 commit comments