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