Skip to content

Commit 2130c5f

Browse files
authored
Apply labels to the docker container as well as the marathon app when using the default template (#130)
* Apply labels to the docker container as well as the marathon app when using the default template * Remove leftover print
1 parent 06afb67 commit 2130c5f

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

shpkpr/resources/templates/chronos/default/job.json.tmpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
"type": "DOCKER",
3737
{# The name/tag of the Docker image to use. #}
3838
"image": "{{DOCKER_REPOTAG}}",
39+
"parameters": [
40+
{% block docker_labels %}
41+
{% for key, value in _all_env|filter_items("LABEL_", strip_prefix=True) %}
42+
{"key": "label", "value": "{{key|slugify}}={{value}}"}{% if not loop.last %},{% endif %}
43+
{% endfor %}
44+
{% endblock %}
45+
],
3946
"network": "BRIDGE"
4047
},
4148
{# The number of CPU shares this job needs. #}

shpkpr/resources/templates/marathon/default/standard.json.tmpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
],
125125
{% endif %}
126126
{% endblock %}
127+
"parameters": [
128+
{% block docker_labels %}
129+
{% for key, value in _all_env|filter_items("LABEL_", strip_prefix=True) %}
130+
{"key": "label", "value": "{{key|slugify}}={{value}}"}{% if not loop.last %},{% endif %}
131+
{% endfor %}
132+
{% endblock %}
133+
],
127134
"network": "BRIDGE"
128135
}
129136
},

tests/commands/test_cmd_apps_deploy.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,44 @@ def test_strategy_bluegreen(runner):
150150
result = runner(['apps', 'deploy', '--dry-run', '--strategy', 'bluegreen', '--template', _tmpl_path], env=env)
151151

152152
assert result.exit_code == 0
153+
154+
155+
@responses.activate
156+
@mock.patch('shpkpr.marathon.MarathonDeployment.wait')
157+
def test_default_template(mock_deployment_wait, runner, json_fixture):
158+
responses.add(responses.PUT,
159+
'http://marathon.somedomain.com:8080/v2/apps/test-app',
160+
status=201,
161+
json=json_fixture("deployment"),
162+
match_querystring=True)
163+
mock_deployment_wait.return_value = True
164+
165+
env = {
166+
'SHPKPR_MARATHON_URL': "http://marathon.somedomain.com:8080",
167+
'SHPKPR_MARATHON_APP_ID': 'test-app',
168+
'SHPKPR_DOCKER_REPOTAG': 'goexample/outyet:latest',
169+
}
170+
result = runner(['apps', 'deploy'], env=env)
171+
172+
assert result.exit_code == 0
173+
174+
175+
@responses.activate
176+
@mock.patch('shpkpr.marathon.MarathonDeployment.wait')
177+
def test_default_template_labels(mock_deployment_wait, runner, json_fixture):
178+
responses.add(responses.PUT,
179+
'http://marathon.somedomain.com:8080/v2/apps/test-app',
180+
status=201,
181+
json=json_fixture("deployment"),
182+
match_querystring=True)
183+
mock_deployment_wait.return_value = True
184+
185+
env = {
186+
'SHPKPR_MARATHON_URL': "http://marathon.somedomain.com:8080",
187+
'SHPKPR_MARATHON_APP_ID': 'test-app',
188+
'SHPKPR_DOCKER_REPOTAG': 'goexample/outyet:latest',
189+
'SHPKPR_LABEL_SOME_LABEL': 'somevalue',
190+
}
191+
result = runner(['apps', 'deploy'], env=env)
192+
193+
assert result.exit_code == 0

tests/commands/test_cmd_cron.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,43 @@ def test_set(mock_chronos_add, mock_chronos_list, runner, json_fixture):
5959
assert result.exit_code == 0
6060

6161

62+
@mock.patch("shpkpr.cli.options.ChronosClient.list")
63+
@mock.patch("shpkpr.cli.options.ChronosClient.add")
64+
def test_set_default_template(mock_chronos_add, mock_chronos_list, runner, json_fixture):
65+
mock_chronos_list.return_value = []
66+
mock_chronos_add.return_value = True
67+
68+
result = runner(['cron', 'set'], env={
69+
'SHPKPR_CHRONOS_URL': "chronos.somedomain.com:4400",
70+
'SHPKPR_CHRONOS_JOB_NAME': 'shpkpr-test-job',
71+
'SHPKPR_CHRONOS_OWNER': 'shpkpr-test@example.com',
72+
'SHPKPR_CHRONOS_CMD': 'someprogram --run',
73+
'SHPKPR_DOCKER_REPOTAG': 'goexample/outyet:latest',
74+
})
75+
76+
assert mock_chronos_add.called
77+
assert result.exit_code == 0
78+
79+
80+
@mock.patch("shpkpr.cli.options.ChronosClient.list")
81+
@mock.patch("shpkpr.cli.options.ChronosClient.add")
82+
def test_set_default_template_labels(mock_chronos_add, mock_chronos_list, runner, json_fixture):
83+
mock_chronos_list.return_value = []
84+
mock_chronos_add.return_value = True
85+
86+
result = runner(['cron', 'set'], env={
87+
'SHPKPR_CHRONOS_URL': "chronos.somedomain.com:4400",
88+
'SHPKPR_CHRONOS_JOB_NAME': 'shpkpr-test-job',
89+
'SHPKPR_CHRONOS_OWNER': 'shpkpr-test@example.com',
90+
'SHPKPR_CHRONOS_CMD': 'someprogram --run',
91+
'SHPKPR_LABEL_SOME_LABEL': 'some-value',
92+
'SHPKPR_DOCKER_REPOTAG': 'goexample/outyet:latest',
93+
})
94+
95+
assert mock_chronos_add.called
96+
assert result.exit_code == 0
97+
98+
6299
@mock.patch("shpkpr.cli.options.ChronosClient.list")
63100
@mock.patch("shpkpr.cli.options.ChronosClient.add")
64101
def test_set_multiple(mock_chronos_add, mock_chronos_list, runner):

0 commit comments

Comments
 (0)