Skip to content

Commit

Permalink
Merge pull request #153 from ecordell/randdir
Browse files Browse the repository at this point in the history
Include a randomized container folder in the tarred bundle
  • Loading branch information
ecordell authored Aug 14, 2019
2 parents 7f675ea + 349a403 commit 1478097
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
5 changes: 2 additions & 3 deletions operatorcourier/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,13 @@ def build_verify_and_push(namespace, repository, revision, token,
"""
verified_manifest = build_and_verify(source_dir, yamls, repository=repository,
validation_output=validation_output)

if not verified_manifest.nested:
with TemporaryDirectory() as temp_dir:
with TemporaryDirectory(prefix=repository+"-") as temp_dir:
with open(os.path.join(temp_dir, 'bundle.yaml'), 'w') as outfile:
yaml.dump(verified_manifest.bundle, outfile, default_flow_style=False)
PushCmd().push(temp_dir, namespace, repository, revision, token)
else:
with TemporaryDirectory() as temp_dir:
with TemporaryDirectory(prefix=repository+"-") as temp_dir:
copy_tree(source_dir, temp_dir)
PushCmd().push(temp_dir, namespace, repository, revision, token)

Expand Down
2 changes: 1 addition & 1 deletion operatorcourier/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _create_base64_bundle(self, bundle_dir, repository):
with TemporaryDirectory() as temp_dir:
tarfile_name = os.path.join(temp_dir, "%s.tar.gz" % repository)
with tarfile.open(tarfile_name, "w:gz") as tar:
tar.add(bundle_dir, "")
tar.add(bundle_dir, os.path.basename(bundle_dir))
with open(tarfile_name, "rb") as tarball:
result = tarball.read()
result64 = base64.b64encode(result).decode("utf-8")
Expand Down
40 changes: 40 additions & 0 deletions tests/test_push.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import base64
import tarfile

import pytest
import os
from tempfile import TemporaryDirectory
from distutils.dir_util import copy_tree
from operatorcourier.push import PushCmd


@pytest.mark.parametrize('bundle_dir', [
"tests/test_files/bundles/api/etcd_valid_nested_bundle"
])
def test_create_base64_bundle(bundle_dir):
with TemporaryDirectory() as scratch:
directory_name = "directory-abcd"
# make a subdirectory with a known name so that the bundle is always the same
inpath = os.path.join(scratch, directory_name)
os.mkdir(inpath)

copy_tree(bundle_dir, inpath)
out = PushCmd()._create_base64_bundle(inpath, "repo")

outpath = os.path.join(scratch, "out")
os.mkdir(outpath)

# write the output tar
tardata = base64.b64decode(out)
tarfile_name = os.path.join(outpath, "bundle.tar.gz")
with open(tarfile_name, "wb") as bundle:
bundle.write(tardata)

# uncompress the bundle
with tarfile.open(tarfile_name) as bundle:
bundle.extractall(path=outpath)

outfiles = os.listdir(outpath)

# ensure the surrouding directory was packed into the tar archive
assert directory_name in outfiles

0 comments on commit 1478097

Please sign in to comment.