Skip to content

Commit

Permalink
[run] Add exclusive environment and exclusive secrets flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w committed Sep 29, 2019
1 parent bc1f941 commit fbb8410
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 5 deletions.
8 changes: 5 additions & 3 deletions ecs_deploy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ def scale(cluster, service, desired_count, access_key_id, secret_access_key, reg
@click.option('--secret-access-key', help='AWS secret access key')
@click.option('--profile', help='AWS configuration profile name')
@click.option('--diff/--no-diff', default=True, help='Print what values were changed in the task definition')
def run(cluster, task, count, command, env, secret, launchtype, subnet, securitygroup, public_ip, region, access_key_id, secret_access_key, profile, diff):
@click.option('--exclusive-env', is_flag=True, default=False, help='Set the given environment variables exclusively and remove all other pre-existing env variables from all containers')
@click.option('--exclusive-secrets', is_flag=True, default=False, help='Set the given secrets exclusively and remove all other pre-existing secrets from all containers')
def run(cluster, task, count, command, env, secret, launchtype, subnet, securitygroup, public_ip, region, access_key_id, secret_access_key, profile, diff, exclusive_env, exclusive_secrets):
"""
Run a one-off task.
Expand All @@ -323,8 +325,8 @@ def run(cluster, task, count, command, env, secret, launchtype, subnet, security

td = action.get_task_definition(task)
td.set_commands(**{key: value for (key, value) in command})
td.set_environment(env)
td.set_secrets(secret)
td.set_environment(env, exclusive_env)
td.set_secrets(secret, exclusive_secrets)

if diff:
print_diff(td, 'Using task definition: %s' % task)
Expand Down
155 changes: 153 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,12 @@ def test_run_task_with_command(get_client, runner):


@patch('ecs_deploy.cli.get_client')
def test_run_task_with_environment_var(get_client, runner):
def test_run_one_new_environment_variable(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'application', 'foo', 'bar'))

assert not result.exception
assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" in result.output
assert u'Changed environment "foo" of container "application" to: "bar"' in result.output
Expand All @@ -657,6 +657,157 @@ def test_run_task_with_environment_var(get_client, runner):
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_change_environment_variable_empty_string(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'application', 'foo', ''))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" in result.output
assert u'Changed environment "foo" of container "application" to: ""' in result.output
assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_new_empty_environment_variable(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'application', 'new', ''))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" in result.output
assert u'Changed environment "new" of container "application" to: ""' in result.output
assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_empty_environment_variable_again(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'webserver', 'empty', ''))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" not in result.output
assert u'Changed environment' not in result.output
assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_previously_empty_environment_variable_with_value(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'webserver', 'empty', 'not-empty'))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" in result.output
assert u'Changed environment "empty" of container "webserver" to: "not-empty"' in result.output
assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_exclusive_environment(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'webserver', 'new-env', 'new-value', '--exclusive-env'))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" in result.output
assert u'Changed environment "new-env" of container "webserver" to: "new-value"' in result.output

assert u'Removed environment "foo" of container "webserver"' in result.output
assert u'Removed environment "lorem" of container "webserver"' in result.output

assert u'Removed secret' not in result.output

assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_exclusive_secret(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-s', 'webserver', 'new-secret', 'new-place', '--exclusive-secrets'))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" in result.output
assert u'Changed secret "new-secret" of container "webserver" to: "new-place"' in result.output

assert u'Removed secret "baz" of container "webserver"' in result.output
assert u'Removed secret "dolor" of container "webserver"' in result.output

assert u'Removed environment' not in result.output

assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_one_new_secret_variable(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2',
'-s', 'application', 'baz', 'qux',
'-s', 'webserver', 'baz', 'quux'))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" in result.output
assert u'Changed secret "baz" of container "application" to: "qux"' in result.output
assert u'Changed secret "baz" of container "webserver" to: "quux"' in result.output
assert u'Changed secret "dolor" of container "webserver" to: "sit"' not in result.output
assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_without_changing_environment_value(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-e', 'webserver', 'foo', 'bar'))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" not in result.output
assert u'Changed environment' not in result.output
assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_without_changing_secrets_value(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
result = runner.invoke(cli.run, (CLUSTER_NAME, 'test-task', '2', '-s', 'webserver', 'baz', 'qux'))

assert result.exit_code == 0
assert not result.exception

assert u"Using task definition: test-task" not in result.output
assert u'Changed secrets' not in result.output
assert u"Successfully started 2 instances of task: test-task:2" in result.output
assert u"- arn:foo:bar" in result.output
assert u"- arn:lorem:ipsum" in result.output


@patch('ecs_deploy.cli.get_client')
def test_run_task_without_diff(get_client, runner):
get_client.return_value = EcsTestClient('acces_key', 'secret_key')
Expand Down

0 comments on commit fbb8410

Please sign in to comment.