Merge ~martin-kalcok/juju-lint:warn_on_missing_binding into juju-lint:master

Proposed by Martin Kalcok
Status: Merged
Approved by: Gabriel Cocenza
Approved revision: ce3847ea4d990884ee1d343d02e1e4c80b6fafc1
Merged at revision: e5d34a5b3d630954cfaeed061591ebc7f358d5d1
Proposed branch: ~martin-kalcok/juju-lint:warn_on_missing_binding
Merge into: juju-lint:master
Diff against target: 144 lines (+92/-5)
3 files modified
jujulint/check_spaces.py (+9/-1)
jujulint/lint.py (+13/-4)
tests/test_jujulint.py (+70/-0)
Reviewer Review Type Date Requested Status
Gabriel Cocenza Approve
Review via email: mp+424775@code.launchpad.net

Commit message

Add warnings if bundle does not specify explicit bindings

Description of the change

Added warnings if bundle does not contain explicit bindings for application endpoints.

3 types of warnings added:

* When there are relationships but no application specifies bindings
* When some application specify bindings but others don't
* when application specify bindings but do not include binding for default endpoint ("")

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Gabriel Cocenza (gabrielcocenza) wrote :

Hi Martin.
I've left some comments for you in the code.

review: Needs Fixing
Revision history for this message
Gabriel Cocenza (gabrielcocenza) wrote :

I left two comments about nit things. I recommend rebase the master branch that is already fixing test_check_spaces_detect_mismatches. Otherwise, LGTM.

review: Needs Fixing
Revision history for this message
Gabriel Cocenza (gabrielcocenza) wrote :

LGTM.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision e5d34a5b3d630954cfaeed061591ebc7f358d5d1

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/jujulint/check_spaces.py b/jujulint/check_spaces.py
2index 17b8d8c..337f9b2 100644
3--- a/jujulint/check_spaces.py
4+++ b/jujulint/check_spaces.py
5@@ -1,5 +1,8 @@
6 #!/usr/bin/python3
7 """Checks for space mismatches between relation endpoints."""
8+from logging import getLogger
9+
10+LOGGER = getLogger(__name__)
11
12
13 class Relation:
14@@ -116,8 +119,13 @@ def get_application_spaces(application_list, parsed_yaml):
15 """Return a dictionary with app.binding=space mappings."""
16 app_spaces = {}
17 for app in application_list:
18+ bindings = parsed_yaml["applications"][app].get('bindings', {})
19+ if not bindings:
20+ LOGGER.warning("Application %s is missing explicit bindings", app)
21+ continue
22+ if not bindings.get(""):
23+ LOGGER.warning("Application %s does not define explicit default binding", app)
24 app_spaces[app] = {}
25- bindings = parsed_yaml["applications"][app]["bindings"]
26 for name, value in bindings.items():
27 app_spaces[app][name] = value
28 return app_spaces
29diff --git a/jujulint/lint.py b/jujulint/lint.py
30index e908b7a..748fc8a 100755
31--- a/jujulint/lint.py
32+++ b/jujulint/lint.py
33@@ -1106,13 +1106,22 @@ class Linter:
34 if "relations" in parsed_yaml:
35 # "bindings" *should* be in exported bundles, *unless* no custom bindings exist,
36 # in which case "juju export-bundle" omits them. See LP#1949883.
37- if "bindings" in list(parsed_yaml[applications].values())[0]:
38+ bindings = any(
39+ "bindings" in app for app in parsed_yaml[applications].values()
40+ )
41+ if bindings:
42+ # try:
43 self.check_spaces(parsed_yaml)
44-
45+ # except Exception as e:
46+ # self._log_with_header(
47+ # "Encountered error while checking spaces: {}".format(e),
48+ # level=logging.WARN
49+ # )
50 else:
51 self._log_with_header(
52- "Relations detected but custom bindings not found; "
53- "skipping space binding checks."
54+ "Relations detected but explicit bindings not found; "
55+ "Not specifying explicit bindings may cause problems on models"
56+ " with multiple network spaces.", level=logging.WARNING
57 )
58 else:
59 self._log_with_header(
60diff --git a/tests/test_jujulint.py b/tests/test_jujulint.py
61index cff1d6b..11cc514 100644
62--- a/tests/test_jujulint.py
63+++ b/tests/test_jujulint.py
64@@ -6,6 +6,8 @@ from unittest import mock
65
66 import pytest
67
68+from jujulint import check_spaces
69+
70
71 class TestUtils:
72 """Test the jujulint utilities."""
73@@ -820,3 +822,71 @@ applications:
74 errors = linter.output_collector["errors"]
75 assert len(errors) == 0
76 assert mock_log.call_count == 0
77+
78+ def test_check_spaces_missing_explicit_bindings(self, linter, mocker):
79+ """Test that check_spaces shows warning if some application are missing bindings
80+
81+ This warning should be triggerred if some applications have bindings and some
82+ dont.
83+ """
84+ logger_mock = mocker.patch.object(check_spaces, "LOGGER")
85+
86+ app_without_binding = "prometheus-app"
87+ bundle = {
88+ "applications": {
89+ app_without_binding: {},
90+ "telegraf-app": {
91+ "bindings": {
92+ "": "alpha",
93+ "prometheus-client": "alpha",
94+ },
95+ },
96+ },
97+ "relations": [
98+ ["telegraf-app:prometheus-client", "prometheus-app:target"],
99+ ],
100+ }
101+
102+ expected_warning = "Application %s is missing explicit bindings"
103+
104+ linter.check_spaces(bundle)
105+
106+ logger_mock.warning.assert_called_once_with(expected_warning,
107+ app_without_binding)
108+
109+ def test_check_spaces_missing_default_endpoint_binding(self, linter, mocker):
110+ """Raise warning if application is missing explicit default binding.
111+
112+ Aside from specifying binding for each endpoint explicitly, bundle can also
113+ specify default space (represented by empty string ""). Any endpoint that's not
114+ mentioned explicitly will be bound to this default space.
115+ Juju lint should raise warning if bundles do not define default space.
116+ """
117+ logger_mock = mocker.patch.object(check_spaces, "LOGGER")
118+ app_without_default_space = "telegraf-app"
119+
120+ bundle = {
121+ "applications": {
122+ "prometheus-app": {
123+ "bindings": {
124+ "": "alpha",
125+ "target": "alpha",
126+ },
127+ },
128+ app_without_default_space: {
129+ "bindings": {
130+ "prometheus-client": "alpha",
131+ },
132+ },
133+ },
134+ "relations": [
135+ ["telegraf-app:prometheus-client", "prometheus-app:target"],
136+ ],
137+ }
138+
139+ expected_warning = "Application %s does not define explicit default binding"
140+
141+ linter.check_spaces(bundle)
142+
143+ logger_mock.warning.assert_called_once_with(expected_warning,
144+ app_without_default_space)

Subscribers

People subscribed via source and target branches