From 8235de26533c570b2eb69bc29b31bdf6ab082f2f Mon Sep 17 00:00:00 2001 From: Tim Petter Date: Wed, 5 Oct 2016 09:58:48 +0200 Subject: [PATCH 1/2] Use config user/group for consul_definition instead of hard coded string --- libraries/consul_definition.rb | 4 +- test/spec/libraries/consul_definition_spec.rb | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/libraries/consul_definition.rb b/libraries/consul_definition.rb index 3bd69063..0eae214e 100644 --- a/libraries/consul_definition.rb +++ b/libraries/consul_definition.rb @@ -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']['config']['owner'] }) # @!attribute group # @return [String] - attribute(:group, kind_of: String, default: 'consul') + attribute(:group, kind_of: String, default: lazy { node['consul']['config']['group'] }) # @!attribute type # @return [String] diff --git a/test/spec/libraries/consul_definition_spec.rb b/test/spec/libraries/consul_definition_spec.rb index b7c4978c..846eb9c8 100644 --- a/test/spec/libraries/consul_definition_spec.rb +++ b/test/spec/libraries/consul_definition_spec.rb @@ -6,6 +6,10 @@ let(:chefspec_options) { {platform: 'ubuntu', version: '14.04'} } before do default_attributes['consul'] = { + 'config' => { + 'owner' => 'consul', + 'group' => 'consul' + }, 'service' => { 'config_dir' => '/etc/consul/conf.d' } @@ -62,6 +66,42 @@ end end + context 'service definition with owner and group from node config' do + before do + default_attributes['consul'] = { + 'config' => { + 'owner' => 'root', + '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 From 56e77edc2aabe990e576fd106b30698ad4d3ac77 Mon Sep 17 00:00:00 2001 From: Tim Petter Date: Thu, 3 Nov 2016 14:58:12 +0100 Subject: [PATCH 2/2] Use node['consul']['service_user'] and node['consul']['service_group'] --- libraries/consul_config.rb | 4 ++-- libraries/consul_definition.rb | 4 ++-- test/spec/libraries/consul_config_spec.rb | 2 ++ test/spec/libraries/consul_definition_spec.rb | 12 ++++-------- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/libraries/consul_config.rb b/libraries/consul_config.rb index 9fe8dc69..b674f2ad 100644 --- a/libraries/consul_config.rb +++ b/libraries/consul_config.rb @@ -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'] }) diff --git a/libraries/consul_definition.rb b/libraries/consul_definition.rb index 0eae214e..3c1d7023 100644 --- a/libraries/consul_definition.rb +++ b/libraries/consul_definition.rb @@ -21,11 +21,11 @@ class ConsulDefinition < Chef::Resource # @!attribute user # @return [String] - attribute(:user, kind_of: String, default: lazy { node['consul']['config']['owner'] }) + attribute(:user, kind_of: String, default: lazy { node['consul']['service_user'] }) # @!attribute group # @return [String] - attribute(:group, kind_of: String, default: lazy { node['consul']['config']['group'] }) + attribute(:group, kind_of: String, default: lazy { node['consul']['service_group'] }) # @!attribute type # @return [String] diff --git a/test/spec/libraries/consul_config_spec.rb b/test/spec/libraries/consul_config_spec.rb index 70f9e98c..2db826ac 100644 --- a/test/spec/libraries/consul_config_spec.rb +++ b/test/spec/libraries/consul_config_spec.rb @@ -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' } diff --git a/test/spec/libraries/consul_definition_spec.rb b/test/spec/libraries/consul_definition_spec.rb index 846eb9c8..896c7907 100644 --- a/test/spec/libraries/consul_definition_spec.rb +++ b/test/spec/libraries/consul_definition_spec.rb @@ -6,10 +6,8 @@ let(:chefspec_options) { {platform: 'ubuntu', version: '14.04'} } before do default_attributes['consul'] = { - 'config' => { - 'owner' => 'consul', - 'group' => 'consul' - }, + 'service_user' => 'consul', + 'service_group' => 'consul', 'service' => { 'config_dir' => '/etc/consul/conf.d' } @@ -69,10 +67,8 @@ context 'service definition with owner and group from node config' do before do default_attributes['consul'] = { - 'config' => { - 'owner' => 'root', - 'group' => 'root' - }, + 'service_user' => 'root', + 'service_group' => 'root', 'service' => { 'config_dir' => '/etc/consul/conf.d' }