Skip to content

Commit 7df155e

Browse files
committed
removing humanize.naturaldelta() for task output duration, to ensure long-running tasks no longer output "an hour" when they actually run longer then an hour. This should prevent requests to increase timeouts past 1hr, which has already been done.
Signed-off-by: Adam D. Cornett <adc@redhat.com>
1 parent c4f792c commit 7df155e

4 files changed

Lines changed: 63 additions & 5 deletions

File tree

operatorcert/tekton.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import json
55
from typing import Any, Dict
66

7-
import humanize
87
from dateutil.parser import isoparse
98

109

@@ -50,14 +49,20 @@ def completion_time(self) -> datetime.datetime:
5049
return isoparse(self.obj["status"]["completionTime"])
5150

5251
@property
53-
def duration(self) -> Any:
52+
def duration(self) -> str:
5453
"""
5554
Get the duration of the TaskRun.
5655
5756
Returns:
58-
Any: A human readable duration of the TaskRun
59-
"""
60-
return humanize.naturaldelta(self.completion_time - self.start_time)
57+
str: A human readable duration of the TaskRun.
58+
"""
59+
# Avoid humanize.naturaldelta() which rounds e.g. 67 min to "an hour"
60+
delta = self.completion_time - self.start_time
61+
total_seconds = int(delta.total_seconds())
62+
if total_seconds >= 60:
63+
minutes = round(total_seconds / 60)
64+
return f"{minutes} minute{'s' if minutes != 1 else ''}"
65+
return f"{total_seconds} second{'s' if total_seconds != 1 else ''}"
6166

6267
@property
6368
def status(self) -> str:

tests/data/pipelinerun_long.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"apiVersion": "tekton.dev/v1",
3+
"kind": "PipelineRun",
4+
"metadata": {
5+
"name": "long-running-pipeline-run",
6+
"labels": {
7+
"tekton.dev/pipeline": "test-pipeline"
8+
}
9+
},
10+
"status": {
11+
"startTime": "2021-11-10T18:00:00Z",
12+
"pipelineSpec": {
13+
"tasks": [
14+
{"name": "build-fbc-index-images"}
15+
],
16+
"finally": []
17+
}
18+
}
19+
}

tests/data/taskruns_long.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"apiVersion": "tekton.dev/v1",
4+
"kind": "TaskRun",
5+
"metadata": {
6+
"labels": {
7+
"tekton.dev/pipelineTask": "build-fbc-index-images"
8+
}
9+
},
10+
"status": {
11+
"startTime": "2021-11-10T18:00:00Z",
12+
"completionTime": "2021-11-10T19:07:12Z",
13+
"conditions": [
14+
{
15+
"type": "Succeeded",
16+
"reason": "Succeeded"
17+
}
18+
]
19+
}
20+
}
21+
]

tests/test_tekton.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
PARENT_DIR = pathlib.Path(__file__).parent.resolve()
77
PIPELINERUN_PATH = PARENT_DIR.joinpath("data/pipelinerun.json")
88
TASKRUNS_PATH = PARENT_DIR.joinpath("data/taskruns.json")
9+
PIPELINERUN_LONG_PATH = PARENT_DIR.joinpath("data/pipelinerun_long.json")
10+
TASKRUNS_LONG_PATH = PARENT_DIR.joinpath("data/taskruns_long.json")
911

1012

1113
def test_taskrun() -> None:
@@ -32,6 +34,17 @@ def test_taskrun() -> None:
3234
assert tr.status == TaskRun.FAILED
3335

3436

37+
def test_taskrun_long_duration() -> None:
38+
pr = PipelineRun.from_files(str(PIPELINERUN_LONG_PATH), str(TASKRUNS_LONG_PATH))
39+
40+
tr = pr.taskruns[0]
41+
assert tr.pipelinetask == "build-fbc-index-images"
42+
assert str(tr.start_time) == "2021-11-10 18:00:00+00:00"
43+
assert str(tr.completion_time) == "2021-11-10 19:07:12+00:00"
44+
assert tr.duration == "67 minutes"
45+
assert tr.status == TaskRun.SUCCEEDED
46+
47+
3548
def test_pipelinerun() -> None:
3649
pr = PipelineRun.from_files(str(PIPELINERUN_PATH), str(TASKRUNS_PATH))
3750

0 commit comments

Comments
 (0)