From 609733c1165e047c905cc6b854814c89327fda09 Mon Sep 17 00:00:00 2001 From: Paul Belanger Date: Thu, 1 Oct 2015 13:26:47 -0400 Subject: [PATCH] Add the ability to parse directories Change-Id: I0ff6c803608d91e04b8e40750265157d3f4f7530 Signed-off-by: Paul Belanger --- grafana_dashboards/builder.py | 14 +++++++- tests/cmd/test_validate.py | 34 +++++++++++++++++++ .../test0001/good-dashboard-0001.yaml | 1 + 3 files changed, 48 insertions(+), 1 deletion(-) create mode 120000 tests/fixtures/cmd/validate/test0001/good-dashboard-0001.yaml diff --git a/grafana_dashboards/builder.py b/grafana_dashboards/builder.py index 9f284c6..b56ed68 100644 --- a/grafana_dashboards/builder.py +++ b/grafana_dashboards/builder.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import os + from oslo_config import cfg from oslo_log import log as logging @@ -46,7 +48,17 @@ class Builder(object): self.parser = YamlParser() def load_files(self, path): - self.parser.parse(path) + files_to_process = [] + if os.path.isdir(path): + files_to_process.extend([os.path.join(path, f) + for f in os.listdir(path) + if (f.endswith('.yaml') + or f.endswith('.yml'))]) + else: + files_to_process.append(path) + + for fn in files_to_process: + self.parser.parse(fn) def update_dashboard(self, path): self.load_files(path) diff --git a/tests/cmd/test_validate.py b/tests/cmd/test_validate.py index a15e13a..1d5f133 100644 --- a/tests/cmd/test_validate.py +++ b/tests/cmd/test_validate.py @@ -58,6 +58,40 @@ class TestCaseValidateScenarios(TestWithScenarios, TestCase): class TestCaseValidate(TestCase): + def test_validate_directory_success(self): + path = os.path.join( + os.path.dirname(__file__), '../fixtures/cmd/validate/test0001') + required = [ + 'SUCCESS!', + ] + stdout, stderr = self.shell( + 'validate %s' % path, exitcodes=[0]) + for r in required: + self.assertThat( + (stdout + stderr), + matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) + + def test_validate_directory_invalid(self): + path = os.path.join( + os.path.dirname(__file__), '../fixtures/cmd/validate/__invalid__') + self._validate_invalid_file_or_directory(path) + + def test_validate_file_invalid(self): + path = os.path.join( + os.path.dirname(__file__), '../fixtures/cmd/validate/invalid.yaml') + self._validate_invalid_file_or_directory(path) + + def _validate_invalid_file_or_directory(self, path): + required = [ + '%s: ERROR: \[Errno 2\] No such file or directory:' % path, + ] + stdout, stderr = self.shell( + 'validate %s' % path, exitcodes=[1]) + for r in required: + self.assertThat( + (stdout + stderr), + matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) + def test_validate_without_path(self): required = [ '.*?^usage: grafana-dashboards validate \[-h\] path', diff --git a/tests/fixtures/cmd/validate/test0001/good-dashboard-0001.yaml b/tests/fixtures/cmd/validate/test0001/good-dashboard-0001.yaml new file mode 120000 index 0000000..593cf4d --- /dev/null +++ b/tests/fixtures/cmd/validate/test0001/good-dashboard-0001.yaml @@ -0,0 +1 @@ +../good-dashboard-0001.yaml \ No newline at end of file