Skip to content

Commit 8ba3907

Browse files
committed
Wrapped up the config provider and tests
1 parent 8cd907f commit 8ba3907

13 files changed

Lines changed: 89 additions & 135 deletions

File tree

.kitchen.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ platforms:
1111

1212
suites:
1313
- name: default
14-
run_list:
14+
run_list:
1515
- recipe[minitest-handler]
1616
- recipe[ssh_test]
17-
attributes: {}

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ MethodLength:
1616
Severity: refactor
1717
SingleSpaceBeforeFirstArg:
1818
Enabled: false
19+
Documentation:
20+
Enabled: false

attributes/default.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
default['ssh']['known_hosts_path'] = '/etc/ssh/ssh_known_hosts'
2-
default['ssh']['config_path'] = '/etc/ssh/ssh_config'
2+
default['ssh']['config_path'] = '/etc/ssh/ssh_config'

libraries/ssh_config_helpers.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
class Chef
55
module SSH
66
module ConfigHelpers
7-
87
def default_or_user_path(username = nil)
9-
if username
10-
user_dir(username)
11-
else
12-
ssh_path = node['ssh']['config_path']
13-
end
8+
username ? "#{user_dir(username)}/.ssh/config" : node['ssh']['config_path']
149
end
1510

1611
def default?(path)
@@ -38,13 +33,13 @@ def parse_file(path)
3833
def to_config(existing_entries)
3934
existing_entries.map do |name, options|
4035
if options
41-
body = options.map{ |key, value| " #{key} #{value}" }.join("\n")
36+
body = options.map { |key, value| " #{key} #{value}" }.join("\n")
4237
else
4338
body = ''
4439
end
45-
[ "Host #{name}", body ].join("\n")
40+
["Host #{name}", body].join("\n")
4641
end.join("\n\n") + "\n"
4742
end
4843
end
4944
end
50-
end
45+
end

libraries/ssh_helpers.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ class Chef
22
module SSH
33
module Helpers
44
require 'etc'
5-
65
def user_dir(username)
76
pwent = pwent_for(username)
87
pwent.dir
@@ -14,4 +13,3 @@ def pwent_for(uid)
1413
end
1514
end
1615
end
17-

metadata.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
name "ssh"
2-
maintainer "Mark Olson"
3-
maintainer_email "theothermarkolson@gmail.com"
4-
license "Apache 2.0"
5-
description "LWRPs for managing SSH known_hosts and config files"
1+
name 'ssh'
2+
maintainer 'Tejay Cardon'
3+
maintainer_email 'tejay.cardon@gmail.com'
4+
license 'Apache 2.0'
5+
description 'LWRPs for managing SSH known_hosts and config files'
66
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
7-
version "0.6.6"
7+
version '0.10.0'

providers/config.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,20 @@ def whyrun_supported?
3030
end
3131

3232
def create_directory
33-
directory "Creating #{::File.dirname(@path)} for #{@user}" do
34-
owner @user
35-
group @group if @group
36-
mode default?(@path) ? 00755 : 00700
37-
path ::File.dirname(@path)
38-
recursive true
39-
end
33+
d = directory ::File.dirname(@path)
34+
d.owner @user
35+
d.group @group if @group
36+
d.mode default?(@path) ? 00755 : 00700
37+
d.path ::File.dirname(@path)
38+
d.recursive true
4039
end
4140

4241
def create_file
43-
existing_entries = @existing_entries
44-
file @path do
45-
user @user if @user
46-
group @group if @group
47-
mode default?(@path) ? 00644 : 00600
48-
content "# Created by Chef for #{node.name}\n\n#{to_config(existing_entries)}"
49-
end
42+
f = file @path
43+
f.owner @user if @user
44+
f.group @group if @group
45+
f.mode default?(@path) ? 00644 : 00600
46+
f.content "# Created by Chef for #{node.name}\n\n#{to_config(@existing_entries)}"
5047
end
5148

5249
def load_current_resource
@@ -58,4 +55,3 @@ def load_current_resource
5855
@current_resource = Chef::Resource::SshConfig.new(@new_resource.name)
5956
@current_resource.exists = @existing_entries.key? @new_resource.name
6057
end
61-

recipes/default.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
package 'ssh' do
66
action :install
7-
end
7+
end
Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
require 'spec_helper'
22

3-
describe "ssh_config resource" do
3+
describe 'ssh_config resource' do
44

55
let(:chef_run) do
66
runner = ChefSpec::SoloRunner.new(:step_into => :ssh_config)
77
runner.converge('ssh_test::config')
88
end
99

1010
let(:default_config) { '/etc/ssh/ssh_config' }
11-
let(:vagrant_config) { '/home/vagrant' }
11+
let(:vagrant_config) { '/home/vagrant/.ssh/config' }
1212

1313
let(:test_user) { 'someone' }
1414
let(:test_group) { 'other_group' }
15-
let(:test_config) { '/some/random/path' }
15+
let(:test_config) { '/some/random/path/config' }
1616

1717
let(:partial_start) do
1818
content = []
@@ -54,7 +54,7 @@
5454

5555
let(:test_end) do
5656
content = []
57-
content << 'Host github.com'
57+
content << 'Host test.io'
5858
content << ' User testuser'
5959
content << ' DummyKey I was allowed'
6060
content << ''
@@ -77,6 +77,7 @@
7777
allow(IO).to receive(:foreach)
7878
allow(IO).to partial_start.reduce(receive(:foreach).with(vagrant_config), :and_yield)
7979
allow(IO).to partial_start.reduce(receive(:foreach).with(default_config), :and_yield)
80+
allow(IO).to partial_start.reduce(receive(:foreach).with(test_config), :and_yield)
8081

8182
allow(Etc).to receive(:getpwnam)
8283
allow(Etc).to receive(:getpwnam).with('vagrant').and_return(
@@ -162,4 +163,56 @@
162163
break if found_one
163164
end
164165
end
166+
167+
it 'can create user ssh configs' do
168+
expect(chef_run).to create_file(vagrant_config).with(
169+
:owner => 'vagrant',
170+
:group => 200,
171+
:mode => 00600
172+
).with_content(
173+
(common_end + github_and_partial_end).join("\n")
174+
)
175+
end
176+
177+
it 'can create the global ssh config' do
178+
expect(chef_run).to create_file(default_config).with(
179+
:owner => 'root',
180+
:group => 0,
181+
:mode => 00644
182+
).with_content(
183+
(common_end + github_and_partial_end).join("\n")
184+
)
185+
end
186+
187+
it 'creates the /etc/ssh directory if it is missing' do
188+
expect(chef_run).to create_directory(::File.dirname(default_config)).with(
189+
:owner => 'root',
190+
:group => 0,
191+
:mode => 00755
192+
)
193+
end
194+
195+
it "creates vagrant's ~/.ssh/config file" do
196+
expect(chef_run).to create_directory(::File.dirname(vagrant_config)).with(
197+
:owner => 'vagrant',
198+
:group => 200,
199+
:mode => 00700
200+
)
201+
end
202+
203+
context 'when non-default attributes are used' do
204+
it 'can handle a custom path' do
205+
expect(chef_run).to render_file(test_config).with_content(
206+
(common_end + test_and_partial_end).join("\n")
207+
)
208+
end
209+
210+
it 'can handle a custom owner and group for the config file' do
211+
expect(chef_run).to create_file(test_config).with(
212+
:owner => test_user,
213+
:group => test_group,
214+
:mode => 00600
215+
)
216+
end
217+
end
165218
end

test/cookbooks/ssh_test/files/default/tests/minitest/default_test.rb

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)