Skip to content

Commit

Permalink
Merge pull request #5 from myii/test/add-helper-state-to-run-service-…
Browse files Browse the repository at this point in the history
…in-containers

test(service): add helper state to run service in containers
  • Loading branch information
javierbertoli authored Jun 23, 2021
2 parents 8a54bef + 4fb69a3 commit bffe66a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
5 changes: 5 additions & 0 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,16 @@ verifier:
suites:
- name: default
provisioner:
dependencies:
- name: states
path: ./test/salt
state_top:
base:
'*':
- rng-tools._mapdata
- rng-tools
'G@init:systemd and ( G@os:Fedora or G@os:Gentoo or G@os:SUSE )':
- states.custom_systemd_service
pillars:
top.sls:
base:
Expand Down
11 changes: 2 additions & 9 deletions rng-tools/service/running.sls
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@
include:
- {{ sls_config_file }}
{%- set service_state = 'running' %}
{#- Do not attempt to run the service in a container where the service is configured with #}
{#- `ConditionVirtualization=!container` or similar (e.g. via. `kitchen-salt`) #}
{%- if grains.os_family in ['Suse'] and
salt['grains.get']('virtual_subtype', '') in ['Docker', 'LXC', 'kubernetes', 'libpod'] %}
{%- set service_state = 'dead' %}
{%- endif %}
rng-tools/service/{{ service_state }}:
service.{{ service_state }}:
rng-tools/service/running:
service.running:
- name: {{ rng_tools.service.name }}
- enable: true
- watch:
Expand Down
8 changes: 1 addition & 7 deletions test/integration/default/controls/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
describe service(service) do
it { should be_installed }
it { should be_enabled }
if %w[suse].include?(platform[:family])
# Unable to run the service in a container due to its
# `ConditionVirtualization` setting
it { should_not be_running }
else
it { should be_running }
end
it { should be_running }
end
end
29 changes: 29 additions & 0 deletions test/salt/states/custom_systemd_service.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This state is used to prepare environment for formula testing

# In distros that have a systemd unit, make sure the service is
# started even if running in a containerized environment

{%- set sls_config_file = 'rng-tools.config.file' %}
{%- set service_file = {
'Fedora': '/usr/lib/systemd/system/rngd.service',
'Gentoo': '/lib/systemd/system/rngd.service',
'SUSE': '/usr/lib/systemd/system/rng-tools.service',
}.get(grains.os, '') %}

test-salt-states-custom-systemd-service-file-replace:
file.replace:
- name: {{ service_file }}
- pattern: 'ConditionVirtualization=!container'
- repl: ''
- show_changes: True
- require:
- sls: {{ sls_config_file }}

test-salt-states-custom-systemd-service-cmd-wait:
cmd.wait:
- name: systemctl daemon-reload
- runas: root
- watch:
- file: test-salt-states-custom-systemd-service-file-replace
- require_in:
- service: rng-tools/service/running

0 comments on commit bffe66a

Please sign in to comment.