Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_cekit(cwd, args=['build', '--dry-run', 'docker']):
with Chdir(cwd):
return CliRunner().invoke(cli, args, catch_exceptions=False)
def run_cekit(image_dir, args=None, descriptor=None):
if args is None:
args = ['build', '--dry-run', 'docker']
if descriptor is None:
descriptor = image_descriptor
with Chdir(image_dir):
with open('image.yaml', 'w') as fd:
yaml.dump(descriptor, fd, default_flow_style=False)
result = CliRunner().invoke(cli, args, catch_exceptions=False)
assert result.exit_code == 0
return result
def run_cekit(image_dir, args=None, env=None):
if args is None:
args = ['build', '--dry-run', 'docker']
if env is None:
env = {}
with Chdir(image_dir):
result = CliRunner(env=env).invoke(cli, args, catch_exceptions=False)
assert result.exit_code == 0
return result
def run_cekit_exception(cwd,
parameters=['-v', 'build', '--dry-run', 'podman'],
exit_code=1,
exception=SystemExit,
message=None):
with Chdir(cwd):
result = CliRunner().invoke(cli, parameters, catch_exceptions=False)
assert isinstance(result.exception, exception)
assert result.exit_code == exit_code
if message:
assert message in result.output
def test_args_command(mocker, args, clazz, params):
cekit_class = mocker.patch('cekit.cli.Cekit')
cekit_object = mocker.Mock()
cekit_class.return_value = cekit_object
CliRunner().invoke(cli, args, catch_exceptions=False)
cls = _get_class_by_name(clazz)
cekit_class.assert_called_once_with(params)
cekit_object.run.assert_called_once_with(cls)
help_template = os.path.join(str(tmpdir), "help.jinja")
if path_type == 'relative':
help_template = "help.jinja"
my_image_descriptor = image_descriptor.copy()
my_image_descriptor['help'] = {'template': help_template, 'add': True}
with Chdir(str(tmpdir)):
with open('help.jinja', "w") as fd:
fd.write(template_teststr)
with open('image.yaml', 'w') as fd:
yaml.dump(my_image_descriptor, fd, default_flow_style=False)
CliRunner().invoke(cli, ['-v', 'build', '--dry-run', 'docker'], catch_exceptions=False)
with open("target/image/help.md", "r") as fd:
contents = fd.read()
assert contents.find(template_teststr) >= 0
def test_args_not_valid_command():
result = CliRunner().invoke(cli, ['explode'], catch_exceptions=False)
assert isinstance(result.exception, SystemExit)
assert "No such command 'explode'" in result.output
assert result.exit_code == 2
def generate(image_dir, command, descriptor=None, exit_code=0):
desc = basic_config.copy()
if descriptor:
desc.update(descriptor)
tmp_image_file = os.path.join(image_dir, 'image.yaml')
with open(tmp_image_file, 'w') as outfile:
yaml.dump(desc, outfile, default_flow_style=False)
with Chdir(image_dir):
result = CliRunner().invoke(cli, command, catch_exceptions=False)
assert result.exit_code == exit_code
if exit_code != 0:
return
with open(os.path.join(image_dir, 'target', 'image.yaml'), 'r') as desc:
return yaml.safe_load(desc)
"""
overrides = '{"name": "different/image"}'
test_image_dir = build_image(overrides)
features_dir = os.path.join(test_image_dir, 'tests', 'features')
os.makedirs(features_dir)
with open(os.path.join(features_dir, 'basic.feature'), 'w') as fd:
fd.write(feature)
with Chdir(test_image_dir):
result = CliRunner().invoke(
cli, ["-v", "test", "--overrides", overrides, "behave"], catch_exceptions=False)
print(result.output)
assert result.exit_code == 0
assert "1 feature passed, 0 failed, 0 skipped" in result.output
assert "1 scenario passed, 0 failed, 0 skipped" in result.output
assert "3 steps passed, 0 failed, 0 skipped, 0 undefined" in result.output
shutil.rmtree(os.path.join(test_image_dir, 'target'), ignore_errors=True)
def run_cekit_return_dockerfile(mocker, workdir, argv, getservbyport=getservbyport_works):
# Do not try to validate dependencies while running tests, these are not neccessary
mocker.patch('cekit.generator.docker.DockerGenerator.dependencies').return_value({})
"""utility function to invoke cekit and return the generated Dockerfile"""
mocker.patch.object(socket, 'getservbyport', getservbyport)
with Chdir(str(workdir)):
if os.path.exists('target'):
shutil.rmtree('target')
CliRunner().invoke(cli, argv, catch_exceptions=False)
with open("target/image/Dockerfile", "r") as fd:
return fd.read()