Skip to content

Commit b1b4dd0

Browse files
author
Kevin Paulisse
committed
Add rspec test covering both files and directories
1 parent 170233f commit b1b4dd0

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"document_type": "Catalog",
3+
"tags": ["settings","test"],
4+
"name": "my.rspec.node",
5+
"version": "production",
6+
"environment": "production",
7+
"resources": [
8+
{
9+
"type": "Stage",
10+
"title": "main",
11+
"tags": ["stage"],
12+
"exported": false,
13+
"parameters": {
14+
"name": "main"
15+
}
16+
},
17+
{
18+
"type": "Class",
19+
"title": "Settings",
20+
"tags": ["class","settings"],
21+
"exported": false
22+
},
23+
{
24+
"type": "File",
25+
"title": "/tmp/foo",
26+
"tags": ["file","class"],
27+
"file": "/x/modules/modulestest/manifests/init.pp",
28+
"line": 37,
29+
"exported": false,
30+
"parameters": {
31+
"backup": false,
32+
"mode": "0440",
33+
"owner": "root",
34+
"group": "root",
35+
"source": "puppet:///modules/modulestest/tmp/modulestest"
36+
}
37+
},
38+
{
39+
"type": "File",
40+
"title": "/tmp/foobaz",
41+
"tags": ["file","class"],
42+
"file": "/x/modules/modulestest/manifests/init.pp",
43+
"line": 37,
44+
"exported": false,
45+
"parameters": {
46+
"backup": false,
47+
"ensure": "directory",
48+
"mode": "0755",
49+
"owner": "root",
50+
"group": "root",
51+
"source": "puppet:///modules/modulestest/foo"
52+
}
53+
}
54+
],
55+
"classes": [
56+
"test"
57+
]
58+
}

spec/octocatalog-diff/tests/catalog-util/fileresources_spec.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def catalog_from_fixture(path)
2020
end
2121

2222
it 'should return path if file is found' do
23-
allow(File).to receive(:file?).with('/a/foo/files/bar').and_return(true)
23+
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(true)
2424
result = OctocatalogDiff::CatalogUtil::FileResources.file_path('puppet:///modules/foo/bar', ['/a'])
2525
expect(result).to eq('/a/foo/files/bar')
2626
end
2727

2828
it 'should return nil if file is not found' do
29-
allow(File).to receive(:file?).with('/a/foo/files/bar').and_return(false)
29+
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(false)
3030
result = OctocatalogDiff::CatalogUtil::FileResources.file_path('puppet:///modules/foo/bar', ['/a'])
3131
expect(result).to eq(nil)
3232
end
@@ -54,6 +54,46 @@ def catalog_from_fixture(path)
5454
end
5555
end
5656

57+
context 'with mixed files and directories' do
58+
describe '#convert_file_resources' do
59+
before(:each) do
60+
@tmpdir = Dir.mktmpdir
61+
FileUtils.cp_r OctocatalogDiff::Spec.fixture_path('repos/modulepath/manifests'), @tmpdir
62+
FileUtils.cp_r OctocatalogDiff::Spec.fixture_path('repos/modulepath/modules'), @tmpdir
63+
Dir.mkdir File.join(@tmpdir, 'environments')
64+
File.symlink @tmpdir, File.join(@tmpdir, 'environments', 'production')
65+
File.open(File.join(@tmpdir, 'manifests', 'site.pp'), 'w') { |f| f.write "include modulestest\n" }
66+
67+
@obj = catalog_from_fixture('catalogs/catalog-modules-test.json')
68+
@obj.compilation_dir = @tmpdir
69+
@resources_save = @obj.resources.dup
70+
OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(@obj)
71+
end
72+
73+
after(:each) do
74+
FileUtils.remove_entry_secure @tmpdir if File.directory?(@tmpdir)
75+
end
76+
77+
it 'should populate content of a file' do
78+
r = @obj.resources.select { |x| x['type'] == 'File' && x['title'] == '/tmp/foo' }
79+
expect(r).to be_a_kind_of(Array)
80+
expect(r.size).to eq(1)
81+
expect(r.first).to be_a_kind_of(Hash)
82+
expect(r.first['parameters'].key?('source')).to eq(false)
83+
expect(r.first['parameters']['content']).to eq("Modules Test\n")
84+
end
85+
86+
it 'should leave a directory unmodified' do
87+
r = @obj.resources.select { |x| x['type'] == 'File' && x['title'] == '/tmp/foobaz' }
88+
expect(r).to be_a_kind_of(Array)
89+
expect(r.size).to eq(1)
90+
expect(r.first).to be_a_kind_of(Hash)
91+
expect(r.first['parameters'].key?('content')).to eq(false)
92+
expect(r.first['parameters']['source']).to eq('puppet:///modules/modulestest/foo')
93+
end
94+
end
95+
end
96+
5797
describe '#convert_file_resources' do
5898
before(:each) do
5999
@tmpdir = Dir.mktmpdir

0 commit comments

Comments
 (0)