Skip to content

Commit

Permalink
"variables" in the systemTests section now overwrite the "required-en…
Browse files Browse the repository at this point in the history
…vs" when deploying the service during a systemTest
  • Loading branch information
daniel-albuschat committed Nov 19, 2020
1 parent d9daf3b commit 64b0c84
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 23 deletions.
11 changes: 6 additions & 5 deletions kubedev/kubedev.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,15 @@ def _deploy(
tag: str,
release_name: str,
docker_network: str,
variable_overrides: dict,
shell_executor: object,
env_accessor: object,
file_accessor: object,
get_output: bool = False):
variables = KubedevConfig.get_global_variables(kubedev)
if docker_network is not None:
kubeContext = env_accessor.getenv('KUBEDEV_KUBECONTEXT')
envs = KubedevConfig.get_helm_set_env_args(kubedev, env_accessor, cmdline_as_list=True)
envs = KubedevConfig.get_helm_set_env_args(kubedev, env_accessor, variable_overrides=variable_overrides, cmdline_as_list=True)
command = [
'docker',
'run',
Expand All @@ -459,7 +460,7 @@ def _deploy(
['--set', f'KUBEDEV_TAG={tag}'] + \
envs['cmdline']
else:
envs = KubedevConfig.get_helm_set_env_args(kubedev, env_accessor, cmdline_as_list = False)
envs = KubedevConfig.get_helm_set_env_args(kubedev, env_accessor, variable_overrides=variable_overrides, cmdline_as_list = False)
command = [
'/bin/sh',
'-c',
Expand Down Expand Up @@ -497,7 +498,7 @@ def deploy_from_config(self, kubedev, shell_executor, env_accessor, file_accesso
release_name = KubedevConfig.get_helm_release_name(kubedev)
kubeconfig = KubedevConfig.get_kubeconfig_path(env_accessor, file_accessor)
tag = KubedevConfig.get_tag(env_accessor)
return self._deploy(kubedev, kubeconfig, tag, release_name, None, shell_executor, env_accessor, file_accessor)
return self._deploy(kubedev, kubeconfig, tag, release_name, None, dict(), shell_executor, env_accessor, file_accessor)

def _create_docker_config(self, file_accessor, env_accessor):
envCi = env_accessor.getenv('CI')
Expand Down Expand Up @@ -1021,7 +1022,7 @@ def system_test_from_config(self, kubedev, appName: str, file_accessor, env_acce
if not KubernetesTools.wait_for_deployment(tag, wslClusterConfig, "kube-system", "tiller-deploy", timeout, shell_executor, sleeper):
print(f'{colorama.Fore.RED}Tiller deployment did not become ready within timeout of {timeout} seconds :-(')
return False
if self._deploy(kubedev, clusterConfig, uuid, tag, tag, shell_executor, env_accessor, file_accessor) != 0:
if self._deploy(kubedev, clusterConfig, uuid, tag, tag, globalVariables, shell_executor, env_accessor, file_accessor) != 0:
return False

numSleepSeconds = 5
Expand All @@ -1046,7 +1047,7 @@ def system_test_from_config(self, kubedev, appName: str, file_accessor, env_acce
"--name", f"{appName}-system-tests-{uuid}",
"--interactive"] + \
Kubedev._build_cluster_config_mounts(clusterConfig, file_accessor=file_accessor, shell_executor=shell_executor) + \
functools.reduce(operator.concat, [["--env", f'{envName}="${{{attribs["targetName"]}}}"'] for envName, attribs in filteredRequiredEnvs.items()], []) + \
functools.reduce(operator.concat, [["--env", f'{envName}="{attribs["targetName"]}"'] for envName, attribs in filteredRequiredEnvs.items()], []) + \
functools.reduce(operator.concat, [["--env", f'{varName}="{varValue}"'] for varName, varValue in variables.items()], []) + \
[tag])]
if shell_executor.execute(cmdRunSystemTests, envVars=additionalEnvs, check=False) == 0:
Expand Down
19 changes: 12 additions & 7 deletions kubedev/utils/kubedev_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def is_base64(attribs: dict) -> bool:

def env_name(name: str, attribs: dict) -> str:
if 'transform' in attribs and attribs['transform'] == 'base64':
return f'{name}_AS_BASE64'
return f'${{{name}_AS_BASE64}}'
else:
return name
return f'${{{name}}}'

sortedEnvs = dict(sorted(envs.items()))
return (
Expand All @@ -58,22 +58,27 @@ def env_name(name: str, attribs: dict) -> str:
)

@staticmethod
def get_helm_set_env_args(kubedev: dict, env_accessor: object, cmdline_as_list: bool = False) -> dict:
def get_helm_set_env_args(kubedev: dict, env_accessor: object, variable_overrides: dict = dict(), cmdline_as_list: bool = False) -> dict:
'''
Returns shell parameters for helm commands in the form of ``--set <variable>="${<variable>}" ...''
from a kubedev config.
'''
(envs, extraEnvs) = KubedevConfig.prepare_envs(KubedevConfig.get_all_envs(kubedev, False, True), env_accessor=env_accessor)
for var, value in variable_overrides.items():
if var in envs:
envs[var] = {
'targetName': value
}

if len(envs) > 0:
if cmdline_as_list:
return {
'cmdline': [arg for arglist in [['--set', f'{name}="${{{attribs["targetName"]}}}"'] for name, attribs in envs.items()] for arg in arglist],
'cmdline': [arg for arglist in [['--set', f'{name}="{attribs["targetName"]}"'] for name, attribs in envs.items()] for arg in arglist],
'envs': extraEnvs
}
else:
return {
'cmdline': ' ' + ' '.join([f'--set {name}="${{{attribs["targetName"]}}}"' for name, attribs in envs.items()]),
'cmdline': ' ' + ' '.join([f'--set {name}="{attribs["targetName"]}"' for name, attribs in envs.items()]),
'envs': extraEnvs
}
else:
Expand Down Expand Up @@ -184,7 +189,7 @@ def get_docker_build_args(image: dict, env_accessor: object) -> tuple:
"""
(envs, extraEnvs) = KubedevConfig.prepare_envs(image['buildEnvs'], env_accessor=env_accessor)
return (
" ".join([f'--build-arg {name}="${{{attribs["targetName"]}}}"' for name, attribs in sorted(envs.items())]) + " ",
" ".join([f'--build-arg {name}="{attribs["targetName"]}"' for name, attribs in sorted(envs.items())]) + " ",
extraEnvs
)

Expand All @@ -197,7 +202,7 @@ def get_docker_run_envs(image: dict, env_accessor: object) -> tuple:
"""
(envs, extraEnvs) = KubedevConfig.prepare_envs(image['containerEnvs'], env_accessor)
return (
" ".join([f'--env {name}="${{{attribs["targetName"]}}}"' for name, attribs in sorted(envs.items())]) + " ",
" ".join([f'--env {name}="{attribs["targetName"]}"' for name, attribs in sorted(envs.items())]) + " ",
extraEnvs
)

Expand Down
8 changes: 4 additions & 4 deletions tests/deploy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def test_deploy_with_b64_transformed_variables(self):
'--set FOO_SERVICE_GLOBAL_ENV1="${FOO_SERVICE_GLOBAL_ENV1}" --set FOO_SERVICE_GLOBAL_ENV2="${FOO_SERVICE_GLOBAL_ENV2}"' +
''
], helmTemplateCommand)
self.assertIn('FOO_DEPLOY_BINARY_VALUE_AS_BASE64', helmTemplateEnv)
self.assertIn('FOO_DEPLOY_GLOBAL_BINARY_VALUE_AS_BASE64', helmTemplateEnv)
self.assertEqual(helmTemplateEnv['FOO_DEPLOY_BINARY_VALUE_AS_BASE64'], b64encode(binaryValue.encode('utf-8')))
self.assertEqual(helmTemplateEnv['FOO_DEPLOY_GLOBAL_BINARY_VALUE_AS_BASE64'], b64encode((binaryValue + binaryValue).encode('utf-8')))
self.assertIn('${FOO_DEPLOY_BINARY_VALUE_AS_BASE64}', helmTemplateEnv)
self.assertIn('${FOO_DEPLOY_GLOBAL_BINARY_VALUE_AS_BASE64}', helmTemplateEnv)
self.assertEqual(helmTemplateEnv['${FOO_DEPLOY_BINARY_VALUE_AS_BASE64}'], b64encode(binaryValue.encode('utf-8')))
self.assertEqual(helmTemplateEnv['${FOO_DEPLOY_GLOBAL_BINARY_VALUE_AS_BASE64}'], b64encode((binaryValue + binaryValue).encode('utf-8')))

def test_deploy_cronjob(self):
# ARRANGE
Expand Down
4 changes: 2 additions & 2 deletions tests/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def test_run_transforms_global_required_env_to_base64(self):
'FOO_SERVICE_GLOBAL_ENV1="${FOO_SERVICE_GLOBAL_ENV1_AS_BASE64}" ' +
'./foo-deploy/'
])
self.assertIn('FOO_SERVICE_GLOBAL_ENV1_AS_BASE64', calls[0]['env'])
self.assertIn('${FOO_SERVICE_GLOBAL_ENV1_AS_BASE64}', calls[0]['env'])
self.assertListEqual(calls[1]['cmd'], [
'/bin/sh',
'-c',
Expand All @@ -306,7 +306,7 @@ def test_run_transforms_global_required_env_to_base64(self):
'FOO_SERVICE_GLOBAL_ENV1="${FOO_SERVICE_GLOBAL_ENV1_AS_BASE64}" ' +
f'foo-registry/foo-service-foo-deploy:{mockTag}'
])
self.assertIn('FOO_SERVICE_GLOBAL_ENV1_AS_BASE64', calls[1]['env'])
self.assertIn('${FOO_SERVICE_GLOBAL_ENV1_AS_BASE64}', calls[1]['env'])

def test_run_cronjob(self):
envMock = EnvMock()
Expand Down
42 changes: 42 additions & 0 deletions tests/systemtest_cronjob_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,45 @@ def test_sytemtest_passes_args_to_services(self):
"arguments",
"${SHELLVAR}"
])], [call['cmd'] for call in shellMock._calls])

def test_sytemtest_overwrites_required_envs_with_variables_when_deploying(self):
fileMock = FileMock()
envMock = EnvMock()
shellMock = ShellExecutorMock(cmd_output=['x', 'y', '{"status": {"availableReplicas": 1}}'])
tagMock = TagGeneratorMock(['abcd'])
sleeper = SleepMock()

sut = Kubedev()
config = copy.deepcopy(testCronJobConfig)
config['cronjobs']['foo-job']['systemTest']['variables'] = {
'FOO_SERVICE_GLOBAL_ENV1': 'overwritten!',
'FOO_SERVICE_JOB_ENV2': '${FOO_SERVICE_JOB_ENV2_OVERWRITE}'
}
result = sut.system_test_from_config(config, 'foo-job', fileMock, envMock, shellMock, tagMock, sleeper)

self.assertEqual(result, 0)

self.assertIn([
'docker',
'run',
'-i',
'--rm',
'--network',
'local-foo-job-system-tests-abcd',
'--volume',
'/kubedev/systemtests/.kubedev/kind_config_foo-service-abcd:/tmp/kube_config',
'--volume',
'/kubedev/systemtests/helm-chart/:/app/helm-chart/',
'alpine/helm:2.16.9',
'upgrade',
'local-foo-job-system-tests-abcd',
'/app/helm-chart/',
'--install',
'--wait',
'--kubeconfig',
'/tmp/kube_config',
'--set', 'KUBEDEV_TAG=abcd',
'--set', 'FOO_SERVICE_GLOBAL_ENV1="overwritten!"',
'--set', 'FOO_SERVICE_JOB_ENV1="${FOO_SERVICE_JOB_ENV1}"',
'--set', 'FOO_SERVICE_JOB_ENV2="${FOO_SERVICE_JOB_ENV2_OVERWRITE}"'
], [call['cmd'] for call in shellMock._calls])
2 changes: 1 addition & 1 deletion tests/systemtest_deployment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def test_systemtest_transforms_deployment_required_env_to_base64(self):
]
self.assertIn(expectedCall, [call['cmd'] for call in shellMock._calls])
dockerRunCallEnvs = [call['env'] for call in shellMock._calls if call['cmd'] == expectedCall][0]
self.assertIn('FOO_SERVICE_GLOBAL_ENV1_AS_BASE64', dockerRunCallEnvs)
self.assertIn('${FOO_SERVICE_GLOBAL_ENV1_AS_BASE64}', dockerRunCallEnvs)

def test_systemtest_mounts_volumes_to_service_shorthand(self):
fileMock = FileMock()
Expand Down
8 changes: 4 additions & 4 deletions tests/template_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ def test_template_with_b64_transformed_variables(self):
'--set FOO_SERVICE_GLOBAL_ENV1="${FOO_SERVICE_GLOBAL_ENV1}" --set FOO_SERVICE_GLOBAL_ENV2="${FOO_SERVICE_GLOBAL_ENV2}"' +
''
], helmTemplateCommand)
self.assertIn('FOO_DEPLOY_BINARY_VALUE_AS_BASE64', helmTemplateEnv)
self.assertIn('FOO_DEPLOY_GLOBAL_BINARY_VALUE_AS_BASE64', helmTemplateEnv)
self.assertEqual(helmTemplateEnv['FOO_DEPLOY_BINARY_VALUE_AS_BASE64'], b64encode(binaryValue.encode('utf-8')))
self.assertEqual(helmTemplateEnv['FOO_DEPLOY_GLOBAL_BINARY_VALUE_AS_BASE64'], b64encode((binaryValue + binaryValue).encode('utf-8')))
self.assertIn('${FOO_DEPLOY_BINARY_VALUE_AS_BASE64}', helmTemplateEnv)
self.assertIn('${FOO_DEPLOY_GLOBAL_BINARY_VALUE_AS_BASE64}', helmTemplateEnv)
self.assertEqual(helmTemplateEnv['${FOO_DEPLOY_BINARY_VALUE_AS_BASE64}'], b64encode(binaryValue.encode('utf-8')))
self.assertEqual(helmTemplateEnv['${FOO_DEPLOY_GLOBAL_BINARY_VALUE_AS_BASE64}'], b64encode((binaryValue + binaryValue).encode('utf-8')))

def test_template_cronjob(self):
# ARRANGE
Expand Down

0 comments on commit 64b0c84

Please sign in to comment.