Skip to content

Commit

Permalink
Fix test_limit and test_runner
Browse files Browse the repository at this point in the history
  • Loading branch information
derrix060 committed Jun 28, 2024
1 parent 2b26d5a commit a30e892
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 59 deletions.
2 changes: 1 addition & 1 deletion awslimitchecker/limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ class instance. Return True if usage is within thresholds, or false if
limit = u.get_maximum() or self.get_limit()
if limit is None or limit == 0:
continue
pct = (usage / (limit * 1.0)) * 100
pct = (usage / limit) * 100
if crit_int is not None and usage >= crit_int:
all_ok = False
elif pct >= crit_pct:
Expand Down
19 changes: 15 additions & 4 deletions awslimitchecker/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,22 @@ def show_usage(self):
service = svc
limit_name = lim
resource = usage.resource_id or '-'
limit = int(data.quotas_limit) if data.quotas_limit else "<unknown>"
limit = "<unknown>"
if data.quotas_limit:
limit = int(data.quotas_limit)
use = usage.value
use_percent = "{:.0f} %".format((use / limit) * 100) if isinstance(limit, (int, float)) else "-"
table.append([f"{service}/{limit_name}", resource, str(use), use_percent, str(limit)])
print(tabulate.tabulate(table, headers=headers, tablefmt="simple_outline"))
use_percent = "-"
if isinstance(limit, (int, float)):
use_percent = "{:.0f} %".format((use / limit) * 100)
table.append([
f"{service}/{limit_name}",
resource,
str(use),
use_percent,
str(limit),
])
print(tabulate.tabulate(
table, headers=headers, tablefmt="simple_outline"))

def check_thresholds(self, metrics=None):
have_warn = False
Expand Down
72 changes: 46 additions & 26 deletions awslimitchecker/tests/test_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,23 +602,33 @@ def test_int_warn(self):
assert mock_get_limit.mock_calls == [call(), call(), call()]

def test_int_warn_crit(self):
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
u1 = AwsLimitUsage(limit, 4, resource_id='foo4bar')
u2 = AwsLimitUsage(limit, 1, resource_id='foo3bar')
u3 = AwsLimitUsage(limit, 7, resource_id='foo2bar')
limit = AwsLimit(
name='limitname',
service=self.mock_svc,
default_limit=10,
def_warning_threshold=40,
def_critical_threshold=60,
)
u1 = AwsLimitUsage(
limit=limit,
value=4,
resource_id='foo4bar',
)
u2 = AwsLimitUsage(
limit=limit,
value=1,
resource_id='foo3bar',
)
u3 = AwsLimitUsage(
limit=limit,
value=7,
resource_id='foo2bar',
)
limit._current_usage = [u1, u2, u3]
with patch('awslimitchecker.limit.AwsLimit.'
'_get_thresholds') as mock_get_thresh:
with patch('awslimitchecker.limit.AwsLimit.get_'
'limit') as mock_get_limit:
mock_get_thresh.return_value = (4, 40, 6, 80)
mock_get_limit.return_value = 100
res = limit.check_thresholds()
res = limit.check_thresholds()
assert res is False
assert limit._warnings == [u1]
assert limit._criticals == [u3]
assert mock_get_thresh.mock_calls == [call()]
assert mock_get_limit.mock_calls == [call(), call(), call()]

def test_pct_crit(self):
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
Expand All @@ -640,23 +650,33 @@ def test_pct_crit(self):
assert mock_get_limit.mock_calls == [call(), call(), call()]

def test_int_crit(self):
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
u1 = AwsLimitUsage(limit, 9, resource_id='foo4bar')
u2 = AwsLimitUsage(limit, 3, resource_id='foo3bar')
u3 = AwsLimitUsage(limit, 95, resource_id='foo2bar')
limit = AwsLimit(
name='limitname',
service=self.mock_svc,
default_limit=10,
def_warning_threshold=60,
def_critical_threshold=80,
)
u1 = AwsLimitUsage(
limit=limit,
value=9,
resource_id='foo4bar',
)
u2 = AwsLimitUsage(
limit=limit,
value=3,
resource_id='foo3bar',
)
u3 = AwsLimitUsage(
limit=limit,
value=95,
resource_id='foo2bar',
)
limit._current_usage = [u1, u2, u3]
with patch('awslimitchecker.limit.AwsLimit.'
'_get_thresholds') as mock_get_thresh:
with patch('awslimitchecker.limit.AwsLimit.get_'
'limit') as mock_get_limit:
mock_get_thresh.return_value = (6, 40, 8, 80)
mock_get_limit.return_value = 100
res = limit.check_thresholds()
res = limit.check_thresholds()
assert res is False
assert limit._warnings == []
assert limit._criticals == [u1, u3]
assert mock_get_thresh.mock_calls == [call()]
assert mock_get_limit.mock_calls == [call(), call(), call()]

def test_pct_warn_crit(self):
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
Expand Down
49 changes: 21 additions & 28 deletions awslimitchecker/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,22 +769,18 @@ def test_default(self, capsys):
mock_checker = Mock(spec_set=AwsLimitChecker)
mock_checker.get_limits.return_value = limits
self.cls.checker = mock_checker
with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
mock_d2c.return_value = 'd2cval'
with patch('awslimitchecker.runner.tabulate') as mock_tabulate:
self.cls.show_usage()
out, err = capsys.readouterr()
assert out == 'd2cval\n'
assert mock_checker.mock_calls == [
call.find_usage(service=None, use_ta=True),
call.get_limits(service=None, use_ta=True)
]
assert mock_d2c.mock_calls == [
call({
'SvcBar/bar limit2': '22',
'SvcBar/barlimit1': '11',
'SvcFoo/foo limit3': '33',
})
]
assert mock_checker.mock_calls == [
call.find_usage(service=None, use_ta=True),
call.get_limits(service=None, use_ta=True)
]
assert len(mock_tabulate.method_calls) == 1
assert mock_tabulate.method_calls[0].args == ([
['SvcBar/bar limit2', '-', '22', '-', '<unknown>'],
['SvcBar/barlimit1', '-', '11', '-', '<unknown>'],
['SvcFoo/foo limit3', '-', '33', '-', '<unknown>']
],)

def test_one_service(self, capsys):
limits = {
Expand All @@ -796,20 +792,17 @@ def test_one_service(self, capsys):
self.cls.checker = mock_checker
self.cls.service_name = ['SvcFoo']
self.cls.skip_ta = True
with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
mock_d2c.return_value = 'd2cval'
with patch('awslimitchecker.runner.tabulate') as mock_tabulate:
self.cls.show_usage()
out, err = capsys.readouterr()
assert out == 'd2cval\n'
assert mock_checker.mock_calls == [
call.find_usage(service=['SvcFoo'], use_ta=False),
call.get_limits(service=['SvcFoo'], use_ta=False)
]
assert mock_d2c.mock_calls == [
call({
'SvcFoo/foo limit3': '33',
})
]
out, err = capsys.readouterr()
assert mock_checker.mock_calls == [
call.find_usage(service=['SvcFoo'], use_ta=False),
call.get_limits(service=['SvcFoo'], use_ta=False)
]
assert len(mock_tabulate.method_calls) == 1
assert mock_tabulate.method_calls[0].args == (
[['SvcFoo/foo limit3', '-', '33', '-', '<unknown>']],
)


class TestCheckThresholds(RunnerTester):
Expand Down

0 comments on commit a30e892

Please sign in to comment.