Skip to content

Commit

Permalink
Adding start and end date to builds on the Zuul source (#529)
Browse files Browse the repository at this point in the history
* Adding 'start_time' and 'end_time' to build model.

* Formatting dates as expected by Cibyl.

* Fixing tests.

* Small change

* Using an external library to parser the ISO date.

* Adding times to json output
  • Loading branch information
jsanemet authored Oct 21, 2022
1 parent 9feb8b0 commit c7e80eb
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 10 deletions.
16 changes: 16 additions & 0 deletions cibyl/models/ci/zuul/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Data:
"""Result of the build."""
duration: float
"""Time, in seconds, the build took to complete."""
start_time: str
"""Date at which the build begun, in 'YYYY-MM-DD HH:MM:SS' format."""
end_time: str
"""Date at which the build ended, in 'YYYY-MM-DD HH:MM:SS' format."""

API = {
'build_id': {
Expand Down Expand Up @@ -72,6 +76,14 @@ class Data:
'attr_type': float,
'arguments': [],
},
'start_time': {
'attr_type': str,
'arguments': []
},
'end_time': {
'attr_type': str,
'arguments': []
},
'suites': {
'attr_type': TestSuite,
'attribute_value_class': AttributeListValue,
Expand Down Expand Up @@ -101,6 +113,8 @@ def __init__(self, data, suites=None):
'pipeline': data.pipeline,
'status': data.result,
'duration': data.duration,
'start_time': data.start_time,
'end_time': data.end_time,
'suites': suites
}
)
Expand All @@ -119,6 +133,8 @@ def __eq__(self, other):
self.pipeline == other.pipeline and \
self.status == other.status and \
self.duration == other.duration and \
self.start_time == other.start_time and \
self.end_time == other.end_time and \
self.suites == other.suites

def add_suite(self, suite: TestSuite) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ def print_build(self, build: Build) -> str:
unit="s")
result.add(build_duration, 1)

if build.start_time.value:
result.add(self.palette.blue('Start Time: '), 1)
result[-1].append(build.start_time.value)

if build.end_time.value:
result.add(self.palette.blue('End Time: '), 1)
result[-1].append(build.end_time.value)

if self.query >= QueryType.TESTS:
result.add(self.palette.blue('Test Suites: '), 1)

Expand Down
2 changes: 2 additions & 0 deletions cibyl/outputs/cli/ci/system/impls/zuul/serialized.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def print_build(self, build: Build) -> str:
'pipeline': build.pipeline.value,
'status': build.status.value,
'duration': build.duration.value,
'start_time': build.start_time.value,
'end_time': build.end_time.value,
'test_suites': []
}

Expand Down
17 changes: 17 additions & 0 deletions cibyl/sources/zuul/apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ def duration(self):
"""
return self._build['duration']

@property
def start_time(self):
"""
:return: Date, following ISO 8601, at which the build started running.
:rtype: str
"""
return self._build['start_time']

@property
def end_time(self):
"""
:return: Date, following ISO 8601, at which the build finished
running. 'None' if it still is.
:rtype: str or None
"""
return self._build['end_time']

@property
def artifacts(self):
"""
Expand Down
10 changes: 9 additions & 1 deletion cibyl/sources/zuul/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from dataclasses import dataclass, field
from typing import Dict, Optional

import dateparser

from cibyl.models.ci.zuul.build import Build
from cibyl.models.ci.zuul.job import Job
from cibyl.models.ci.zuul.pipeline import Pipeline
Expand Down Expand Up @@ -196,6 +198,10 @@ def with_build(self, build):
:return: Model for this build.
:rtype: :class:`Build`
"""

def format_iso(date):
return str(dateparser.parse(date))

# Register this build's job
job = self.with_job(build.job)

Expand All @@ -208,7 +214,9 @@ def with_build(self, build):
pipeline=build.data['pipeline'],
uuid=build.data['uuid'],
result=build.data['result'],
duration=build.data['duration']
duration=build.data['duration'],
start_time=format_iso(build.data['start_time']),
end_time=format_iso(build.data['end_time'])
)
)
)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ xsdata~=22.7
StrEnum~=0.4.8
pbr>=2.0.0
anytree~=2.8.0
dateparser~=1.1.2
24 changes: 20 additions & 4 deletions tests/cibyl/intr/sources/zuul/queries/composition/test_quick.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ def test_builds_query(self):
'pipeline': 'pipeline',
'uuid': '1234',
'result': 'success',
'duration': 10
'duration': 10,
'start_time': '1970-01-01T00:00:00',
'end_time': '1970-01-01T00:00:00'
}

api = Mock()
Expand Down Expand Up @@ -401,6 +403,10 @@ def test_builds_query(self):
result = source.get_builds(**kwargs)

models = result.value

expected_start = build.raw['start_time'].replace('T', ' ')
expected_end = build.raw['end_time'].replace('T', ' ')

expected = {
tenant.name: Tenant(
name=tenant.name,
Expand All @@ -415,7 +421,9 @@ def test_builds_query(self):
pipeline=build.raw['pipeline'],
uuid=build.raw['uuid'],
result=build.raw['result'],
duration=build.raw['duration']
duration=build.raw['duration'],
start_time=expected_start,
end_time=expected_end
)
)
}
Expand Down Expand Up @@ -454,7 +462,9 @@ def test_tests_query(self):
'pipeline': 'pipeline',
'uuid': '1234',
'result': 'success',
'duration': 10
'duration': 10,
'start_time': '1970-01-01T00:00:00',
'end_time': '1970-01-01T00:00:00'
}
build.tests = Mock()
build.tests.return_value = [suite]
Expand Down Expand Up @@ -502,6 +512,10 @@ def test_tests_query(self):
result = source.get_tests(**kwargs)

models = result.value

expected_start = build.raw['start_time'].replace('T', ' ')
expected_end = build.raw['end_time'].replace('T', ' ')

expected = {
tenant.name: Tenant(
name=tenant.name,
Expand All @@ -516,7 +530,9 @@ def test_tests_query(self):
pipeline=build.raw['pipeline'],
uuid=build.raw['uuid'],
result=build.raw['result'],
duration=build.raw['duration']
duration=build.raw['duration'],
start_time=expected_start,
end_time=expected_end
),
suites=[
TestSuite(
Expand Down
46 changes: 42 additions & 4 deletions tests/cibyl/unit/models/ci/zuul/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,19 @@ def test_attributes(self):
uuid = 'uuid'
status = 'STATUS'
duration = 1
start_time = '1970-01-01T00:00:00'
end_time = '1970-01-01T00:00:00'

data = Build.Data(
uuid,
project,
pipeline,
status,
duration,
start_time,
end_time
)

data = Build.Data(uuid, project, pipeline, status, duration)
build = Build(data, suites)

self.assertEqual(uuid, build.build_id.value)
Expand All @@ -46,7 +57,16 @@ def test_attributes(self):
def test_equality_by_type(self):
"""Checks that a build is not equal to something not of its type.
"""
data = Build.Data('uuid', 'project', 'pipeline', 'status', 0)
data = Build.Data(
'uuid',
'project',
'pipeline',
'status',
0,
'1970-01-01T00:00:00',
'1970-01-01T00:00:00'
)

build = Build(data)
other = Mock()

Expand All @@ -55,15 +75,33 @@ def test_equality_by_type(self):
def test_equality_by_reference(self):
"""Checks that a build is equal to itself.
"""
data = Build.Data('uuid', 'project', 'pipeline', 'status', 0)
data = Build.Data(
'uuid',
'project',
'pipeline',
'status',
0,
'1970-01-01T00:00:00',
'1970-01-01T00:00:00'
)

build = Build(data)

self.assertEqual(build, build)

def test_equality_by_contents(self):
"""Checks that a build equals another whose contents are the same.
"""
data = Build.Data('uuid', 'project', 'pipeline', 'status', 0)
data = Build.Data(
'uuid',
'project',
'pipeline',
'status',
0,
'1970-01-01T00:00:00',
'1970-01-01T00:00:00'
)

build1 = Build(data)
build2 = Build(data)

Expand Down
14 changes: 13 additions & 1 deletion tests/cibyl/unit/sources/zuul/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ def test_with_build_of_unknown_job(self):
'result': 'SUCCESS',
'project': 'project',
'pipeline': 'pipeline',
'duration': 0
'duration': 0,
'start_time': '1970-01-01T00:00:00',
'end_time': '1970-01-01T00:00:00'
}

builder = QueryOutputBuilder()
Expand All @@ -169,3 +171,13 @@ def test_with_build_of_unknown_job(self):
self.assertEqual(build.data['uuid'], result_build.build_id.value)
self.assertEqual(build.data['result'], result_build.status.value)
self.assertEqual(build.data['duration'], result_build.duration.value)

self.assertEqual(
build.data['start_time'].replace('T', ' '),
result_build.start_time.value
)

self.assertEqual(
build.data['end_time'].replace('T', ' '),
result_build.end_time.value
)

0 comments on commit c7e80eb

Please sign in to comment.