Skip to content

Commit 20ac3d7

Browse files
authored
Merge branch 'master' into issue/426
2 parents 3955b37 + 2157a1c commit 20ac3d7

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

juju/application.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818

1919
from . import model, tag
20+
from .status import derive_status
2021
from .annotationhelper import _get_annotations, _set_annotations
2122
from .client import client
2223
from .errors import JujuError
@@ -76,7 +77,14 @@ def status(self):
7677
"""Get the application status, as set by the charm's leader.
7778
7879
"""
79-
return self.safe_data['status']['current']
80+
status = self.safe_data['status']['current']
81+
if status == 'unset':
82+
unit_status = []
83+
for unit in self.units:
84+
unit_status.append(unit.workload_status)
85+
return derive_status(unit_status)
86+
87+
return status
8088

8189
@property
8290
def status_message(self):

juju/status.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
""" derive_status is used to determine the application status from a set of unit
2+
status values.
3+
4+
:param statues: list of known unit workload statues
5+
6+
"""
7+
8+
9+
def derive_status(statues):
10+
current = 'unknown'
11+
for status in statues:
12+
if status in serverities and serverities[status] > serverities[current]:
13+
current = status
14+
return current
15+
16+
17+
""" serverities holds status values with a severity measure.
18+
Status values with higher severity are used in preference to others.
19+
"""
20+
serverities = {
21+
'error': 100,
22+
'blocked': 90,
23+
'waiting': 80,
24+
'maintenance': 70,
25+
'active': 60,
26+
'terminated': 50,
27+
'unknown': 40
28+
}

tests/integration/test_application.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ async def test_action(event_loop):
4545
assert 'backup' in actions.keys()
4646

4747

48+
@base.bootstrapped
49+
@pytest.mark.asyncio
50+
async def test_status(event_loop):
51+
52+
async with base.CleanModel() as model:
53+
app = await model.deploy(
54+
'ubuntu-0',
55+
application_name='ubuntu',
56+
series='trusty',
57+
channel='stable',
58+
)
59+
60+
assert app.status != 'unset'
61+
62+
4863
@base.bootstrapped
4964
@pytest.mark.asyncio
5065
async def test_add_units(event_loop):

tests/unit/test_status.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
3+
4+
from juju.status import derive_status
5+
from random import sample
6+
7+
8+
class TestStatus(unittest.TestCase):
9+
def test_derive_status_with_empty_list(self):
10+
result = derive_status([])
11+
self.assertEqual(result, 'unknown')
12+
13+
def test_derive_status_with_unknown(self):
14+
result = derive_status(['unknown'])
15+
self.assertEqual(result, 'unknown')
16+
17+
def test_derive_status_with_invalid(self):
18+
result = derive_status(['boom'])
19+
self.assertEqual(result, 'unknown')
20+
21+
def test_derive_status_with_highest_value(self):
22+
result = derive_status(sample(['error', 'active', 'terminated'], 3))
23+
self.assertEqual(result, 'error')

0 commit comments

Comments
 (0)