Author: Clint Byrum Bug: https://bugs.launchpad.net/heat-cfntools/+bug/1166323 diff --git a/heat_cfntools/cfntools/cfn_helper.py b/heat_cfntools/cfntools/cfn_helper.py index f733900..e2df794 100644 --- a/heat_cfntools/cfntools/cfn_helper.py +++ b/heat_cfntools/cfntools/cfn_helper.py @@ -36,6 +36,7 @@ except ImportError: rpmutils_present = False import re import subprocess +import tempfile # Override BOTO_CONFIG, which makes boto look only at the specified # config file, instead of the default locations @@ -582,16 +583,17 @@ class SourcesHandler(object): self._sources = sources def _url_to_tmp_filename(self, url): + tempdir = tempfile.mkdtemp() sp = url.split('/') if 'https://github.com' in url: if 'zipball' == sp[-2]: - return '/tmp/%s-%s.zip' % (sp[-3], sp[-1]) + return '%s/%s-%s.zip' % (tempdir, sp[-3], sp[-1]) elif 'tarball' == sp[-2]: - return '/tmp/%s-%s.tar.gz' % (sp[-3], sp[-1]) + return '%s/%s-%s.tar.gz' % (tempdir, sp[-3], sp[-1]) else: pass - return '/tmp/%s' % (sp[-1]) + return '%s/%s' % (tempdir, sp[-1]) def _decompress(self, archive, dest_dir): cmd_str = '' diff --git a/heat_cfntools/tests/test_cfn_helper.py b/heat_cfntools/tests/test_cfn_helper.py index 317103b..5f27e90 100644 --- a/heat_cfntools/tests/test_cfn_helper.py +++ b/heat_cfntools/tests/test_cfn_helper.py @@ -17,6 +17,7 @@ import boto.cloudformation as cfn import json import mox +import os import subprocess import tempfile import testtools @@ -637,3 +638,38 @@ class TestMetadataRetrieve(testtools.TestCase): md.retrieve(meta_str=md_data, last_path=last_file.name) md.cfn_init() self.assertThat(foo_file.name, ttm.FileContains('bar')) + + +class TestSourcesHandler(MockPopenTestCase): + def test_apply_sources_empty(self): + sh = cfn_helper.SourcesHandler({}) + sh.apply_sources() + + def _test_apply_sources(self, url, end_file): + dest = tempfile.mkdtemp() + self.addCleanup(os.rmdir, dest) + sources = {dest: url} + td = os.path.dirname(end_file) + self.m.StubOutWithMock(tempfile, 'mkdtemp') + tempfile.mkdtemp().AndReturn(td) + cmd = ['su', 'root', '-c', 'wget -O %s %s' % (end_file, url)] + self.mock_cmd_run(cmd).AndReturn(FakePOpen('Wget good')) + cmd = ['su', 'root', '-c', 'tar -C %s -xzf %s' % (dest, end_file)] + self.mock_cmd_run(cmd).AndReturn(FakePOpen('Tarball good')) + self.m.ReplayAll() + sh = cfn_helper.SourcesHandler(sources) + sh.apply_sources() + + def test_apply_sources_github(self): + url = "https://github.com/NoSuchProject/tarball/NoSuchTarball" + td = tempfile.mkdtemp() + self.addCleanup(os.rmdir, td) + end_file = '%s/NoSuchProject-NoSuchTarball.tar.gz' % td + self._test_apply_sources(url, end_file) + + def test_apply_sources_general(self): + url = "https://website.no.existe/a/b/c/file.tar.gz" + td = tempfile.mkdtemp() + self.addCleanup(os.rmdir, td) + end_file = '%s/file.tar.gz' % td + self._test_apply_sources(url, end_file)