Skip to content

Commit

Permalink
Merge pull request #362 from rollbrettler/change-permission
Browse files Browse the repository at this point in the history
Use config user/group for consul_definition instead of hard coded string
  • Loading branch information
johnbellone committed Nov 21, 2016
2 parents 742a56a + 56e77ed commit dec4039
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions libraries/consul_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class ConsulConfig < Chef::Resource
attribute(:path, kind_of: String, name_attribute: true)
# @!attribute owner
# @return [String]
attribute(:owner, kind_of: String, default: 'consul')
attribute(:owner, kind_of: String, default: lazy { node['consul']['service_user'] })
# @!attribute group
# @return [String]
attribute(:group, kind_of: String, default: 'consul')
attribute(:group, kind_of: String, default: lazy { node['consul']['service_group'] })
# @!attribute config_dir
# @return [String]
attribute(:config_dir, kind_of: String, default: lazy { node['consul']['service']['config_dir'] })
Expand Down
4 changes: 2 additions & 2 deletions libraries/consul_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class ConsulDefinition < Chef::Resource

# @!attribute user
# @return [String]
attribute(:user, kind_of: String, default: 'consul')
attribute(:user, kind_of: String, default: lazy { node['consul']['service_user'] })

# @!attribute group
# @return [String]
attribute(:group, kind_of: String, default: 'consul')
attribute(:group, kind_of: String, default: lazy { node['consul']['service_group'] })

# @!attribute type
# @return [String]
Expand Down
2 changes: 2 additions & 0 deletions test/spec/libraries/consul_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
recipe = double('Chef::Recipe')
allow_any_instance_of(Chef::RunContext).to receive(:include_recipe).and_return([recipe])
default_attributes['consul'] = {
'service_user' => 'consul',
'service_group' => 'consul',
'service' => {
'config_dir' => '/etc/consul/conf.d'
}
Expand Down
36 changes: 36 additions & 0 deletions test/spec/libraries/consul_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let(:chefspec_options) { {platform: 'ubuntu', version: '14.04'} }
before do
default_attributes['consul'] = {
'service_user' => 'consul',
'service_group' => 'consul',
'service' => {
'config_dir' => '/etc/consul/conf.d'
}
Expand Down Expand Up @@ -62,6 +64,40 @@
end
end

context 'service definition with owner and group from node config' do
before do
default_attributes['consul'] = {
'service_user' => 'root',
'service_group' => 'root',
'service' => {
'config_dir' => '/etc/consul/conf.d'
}
}
end

recipe do
consul_definition 'redis' do
type 'service'
parameters(name: 'myredis', tags: %w{master}, address: '127.0.0.1', port: 6379, interval: '10s')
end
end

it { is_expected.to create_directory('/etc/consul/conf.d') }
it do
is_expected.to create_file('/etc/consul/conf.d/redis.json')
.with(user: 'root', group: 'root', mode: '0640')
.with(content: JSON.pretty_generate(
service: {
name: 'myredis',
tags: ['master'],
address: '127.0.0.1',
port: 6379,
interval: '10s'
}
))
end
end

context 'check definition' do
recipe do
consul_definition 'web-api' do
Expand Down

0 comments on commit dec4039

Please sign in to comment.