Skip to content

Commit

Permalink
feat: add pebble.CheckInfo.change_id field (canonical#1197)
Browse files Browse the repository at this point in the history
This is new with health checks being implemented as changes and tasks
(canonical/pebble#409).

Ops/charms will likely not use this new field. Even once we have the new
`change-updated` event, that has a `.get_change()` method to get the
change directly. But we're adding it to the Pebble client/types for
completeness.
  • Loading branch information
benhoyt authored Apr 24, 2024
1 parent fe9c563 commit be7f8ac
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/framework-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python 3
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install tox
Expand All @@ -24,7 +24,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python 3
uses: actions/setup-python@v2
uses: actions/setup-python@v5

- name: Install tox
run: pip install tox~=4.2
Expand All @@ -39,11 +39,13 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ["3.8", "3.10"]
exclude:
- {python-version: "3.8", os: "macos-latest"} # macos-14 is arm64, and there's no Python 3.8 build for arm64

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -64,7 +66,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand Down Expand Up @@ -99,11 +101,13 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
exclude:
- {python-version: "3.8", os: "macos-latest"} # macos-14 is arm64, and there's no Python 3.8 build for arm64

steps:
- uses: actions/checkout@v3
- name: Set up Python 3
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/hello-charm-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

steps:
- name: Set up Python 3.8
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: "3.8"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v1
uses: actions/setup-python@v5
- name: Install build dependencies
run: pip install wheel build
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install wheel
Expand Down
18 changes: 15 additions & 3 deletions ops/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ def __enter__(self) -> typing.IO[typing.AnyStr]: ...
"level": NotRequired[Optional[Union['CheckLevel', str]]],
"status": Union['CheckStatus', str],
"failures": NotRequired[int],
"threshold": int})
"threshold": int,
"change-id": NotRequired[str]})
_FileInfoDict = TypedDict('_FileInfoDict',
{"path": str,
"name": str,
Expand Down Expand Up @@ -1268,19 +1269,28 @@ class CheckInfo:
This is how many consecutive failures for the check to be considered "down".
"""

change_id: Optional[ChangeID]
"""Change ID of ``perform-check`` or ``recover-check`` change driving this check.
This will be None on older versions of Pebble, which did not use changes
to drive health checks.
"""

def __init__(
self,
name: str,
level: Optional[Union[CheckLevel, str]],
status: Union[CheckStatus, str],
failures: int = 0,
threshold: int = 0,
change_id: Optional[ChangeID] = None,
):
self.name = name
self.level = level
self.status = status
self.failures = failures
self.threshold = threshold
self.change_id = change_id

@classmethod
def from_dict(cls, d: '_CheckInfoDict') -> 'CheckInfo':
Expand All @@ -1299,15 +1309,17 @@ def from_dict(cls, d: '_CheckInfoDict') -> 'CheckInfo':
status=status,
failures=d.get('failures', 0),
threshold=d['threshold'],
change_id=ChangeID(d['change-id']) if 'change-id' in d else None,
)

def __repr__(self):
return ('CheckInfo('
f'name={self.name!r}, '
f'level={self.level!r}, '
f'level={self.level}, '
f'status={self.status}, '
f'failures={self.failures}, '
f'threshold={self.threshold!r})'
f'threshold={self.threshold!r}, '
f'change_id={self.change_id!r})'
)


Expand Down
6 changes: 6 additions & 0 deletions test/test_pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,19 +1289,22 @@ def test_check_info(self):
assert check.status == pebble.CheckStatus.UP
assert check.failures == 0
assert check.threshold == 3
assert check.change_id is None

check = pebble.CheckInfo(
name='chk2',
level=pebble.CheckLevel.ALIVE,
status=pebble.CheckStatus.DOWN,
failures=5,
threshold=3,
change_id=pebble.ChangeID('10'),
)
assert check.name == 'chk2'
assert check.level == pebble.CheckLevel.ALIVE
assert check.status == pebble.CheckStatus.DOWN
assert check.failures == 5
assert check.threshold == 3
assert check.change_id == pebble.ChangeID('10')

d: pebble._CheckInfoDict = {
'name': 'chk3',
Expand All @@ -1314,19 +1317,22 @@ def test_check_info(self):
assert check.status == pebble.CheckStatus.UP
assert check.failures == 0
assert check.threshold == 3
assert check.change_id is None

check = pebble.CheckInfo.from_dict({
'name': 'chk4',
'level': pebble.CheckLevel.UNSET,
'status': pebble.CheckStatus.DOWN,
'failures': 3,
'threshold': 3,
'change-id': '42',
})
assert check.name == 'chk4'
assert check.level == pebble.CheckLevel.UNSET
assert check.status == pebble.CheckStatus.DOWN
assert check.failures == 3
assert check.threshold == 3
assert check.change_id == pebble.ChangeID('42')


_bytes_generator = typing.Generator[bytes, typing.Any, typing.Any]
Expand Down

0 comments on commit be7f8ac

Please sign in to comment.