Merge "fix(chartbuilder): Fix ChartBuilder failing on get_files()"

This commit is contained in:
Anthony Lin 2018-01-25 02:40:47 -05:00 committed by Gerrit Code Review
commit 1a9d2e747a
2 changed files with 38 additions and 28 deletions

View File

@ -22,11 +22,11 @@ from hapi.chart.metadata_pb2 import Metadata
from hapi.chart.template_pb2 import Template
from supermutes.dot import dotify
from armada.exceptions import chartbuilder_exceptions
from oslo_config import cfg
from oslo_log import log as logging
from armada.exceptions import chartbuilder_exceptions
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
@ -131,9 +131,9 @@ class ChartBuilder(object):
'''
Return (non-template) files in this chart.
Non-template files include all files **not** under templates and
charts subfolders, except: Chart.yaml, values.yaml, values.toml and
charts/.prov
Non-template files include all files *except* Chart.yaml, values.yaml,
values.toml, and any file nested under charts/ or templates/. The only
exception to this rule is charts/.prov
The class :class:`google.protobuf.any_pb2.Any` is wrapped around
each file as that is what Helm uses.
@ -148,23 +148,28 @@ class ChartBuilder(object):
files_to_ignore = ['Chart.yaml', 'values.yaml', 'values.toml']
non_template_files = []
def _append_file_to_result(root, file):
def _append_file_to_result(root, rel_folder_path, file):
abspath = os.path.abspath(os.path.join(root, file))
relpath = os.path.join(rel_folder_path, file)
with open(abspath, 'r') as f:
file_contents = f.read().encode('utf-8')
non_template_files.append(
Any(type_url=abspath,
Any(type_url=relpath,
value=file_contents))
for root, dirs, files in os.walk(self.source_directory):
relfolder = os.path.split(root)[-1]
if relfolder not in ['charts', 'templates']:
rel_folder_path = os.path.relpath(root, self.source_directory)
if not any(root.startswith(os.path.join(self.source_directory, x))
for x in ['templates', 'charts']):
for file in files:
if (file not in files_to_ignore and
file not in non_template_files):
_append_file_to_result(root, file)
_append_file_to_result(root, rel_folder_path, file)
elif relfolder == 'charts' and '.prov' in files:
_append_file_to_result(root, '.prov')
_append_file_to_result(root, rel_folder_path, '.prov')
return non_template_files

View File

@ -28,7 +28,6 @@ from armada.handlers.chartbuilder import ChartBuilder
class ChartBuilderTestCase(testtools.TestCase):
chart_yaml = """
apiVersion: v1
description: A sample Helm chart for Kubernetes
@ -159,10 +158,8 @@ class ChartBuilderTestCase(testtools.TestCase):
mock_chart = mock.Mock(source_dir=[chart_dir.path, ''])
chartbuilder = ChartBuilder(mock_chart)
expected_files = ('[type_url: "%s"\n, type_url: "%s"\n]' % (
os.path.join(chart_dir.path, 'bar'),
os.path.join(chart_dir.path, 'foo')))
expected_files = ('[type_url: "%s"\n, type_url: "%s"\n]' % ('./bar',
'./foo'))
# Validate that only 'foo' and 'bar' are returned.
actual_files = sorted(chartbuilder.get_files(),
key=lambda x: x.type_url)
@ -247,10 +244,8 @@ class ChartBuilderTestCase(testtools.TestCase):
expected_files = ('[type_url: "%s"\nvalue: "bazqux"\n, '
'type_url: "%s"\nvalue: "foobar"\n, '
'type_url: "%s"\nvalue: "random"\n]' % (
os.path.join(chart_dir.path, 'bar'),
os.path.join(chart_dir.path, 'foo'),
os.path.join(nested_dir, 'nested0')))
'type_url: "%s"\nvalue: "random"\n]' %
('./bar', './foo', 'nested/nested0'))
self.assertIsInstance(helm_chart, Chart)
self.assertTrue(hasattr(helm_chart, 'metadata'))
@ -267,6 +262,10 @@ class ChartBuilderTestCase(testtools.TestCase):
chart_dir.path, 'templates')
charts_subdir = self._make_temporary_subdirectory(
chart_dir.path, 'charts')
templates_nested_subdir = self._make_temporary_subdirectory(
templates_subdir, 'bin')
charts_nested_subdir = self._make_temporary_subdirectory(
charts_subdir, 'extra')
self._write_temporary_file_contents(chart_dir.path, 'Chart.yaml',
self.chart_yaml)
@ -277,14 +276,22 @@ class ChartBuilderTestCase(testtools.TestCase):
files_to_ignore = ['Chart.yaml', 'values.yaml', 'values.toml']
for file in files_to_ignore:
self._write_temporary_file_contents(chart_dir.path, file, "")
file_to_ignore = 'file_to_ignore'
# Files to ignore within templates/ subdirectory.
for filename in ['template%d' % x for x in range(3)]:
self._write_temporary_file_contents(templates_subdir, filename, "")
self._write_temporary_file_contents(
templates_subdir, file_to_ignore, "")
# Files to ignore within charts/ subdirectory.
for filename in ['chart%d' % x for x in range(3)]:
self._write_temporary_file_contents(charts_subdir, filename, "")
self._write_temporary_file_contents(
charts_subdir, file_to_ignore, "")
# Files to ignore within templates/bin subdirectory.
self._write_temporary_file_contents(
templates_nested_subdir, file_to_ignore, "")
# Files to ignore within charts/extra subdirectory.
self._write_temporary_file_contents(
charts_nested_subdir, file_to_ignore, "")
# Files to **include** within charts/ subdirectory.
self._write_temporary_file_contents(charts_subdir, '.prov', "xyzzy")
self._write_temporary_file_contents(
charts_subdir, '.prov', "xyzzy")
ch = yaml.safe_load(self.chart_stream)['chart']
ch['source_dir'] = (chart_dir.path, '')
@ -295,10 +302,8 @@ class ChartBuilderTestCase(testtools.TestCase):
expected_files = ('[type_url: "%s"\nvalue: "bazqux"\n, '
'type_url: "%s"\nvalue: "foobar"\n, '
'type_url: "%s"\nvalue: "xyzzy"\n]' % (
os.path.join(chart_dir.path, 'bar'),
os.path.join(chart_dir.path, 'foo'),
os.path.join(charts_subdir, '.prov')))
'type_url: "%s"\nvalue: "xyzzy"\n]' %
('./bar', './foo', 'charts/.prov'))
# Validate that only relevant files are included, that the ignored
# files are present.