Skip to content

Commit 7bfac67

Browse files
authored
Merge pull request #131 from github/kpaulisse-fact-file-should-not-be-global
Fact file should not be global
2 parents 3f3c654 + e99be82 commit 7bfac67

File tree

3 files changed

+133
-7
lines changed

3 files changed

+133
-7
lines changed

lib/octocatalog-diff/cli/options/fact_file.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ def parse(parser, options)
3636
break
3737
end
3838
end
39-
40-
options[:facts] ||= options[:to_facts]
4139
end
4240
)
4341
end

spec/octocatalog-diff/integration/fact_file_by_branch_spec.rb

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
require 'json'
66

7-
describe 'fact files by branch' do
7+
describe 'fact files by branch, with two fact files' do
88
before(:all) do
99
@result = OctocatalogDiff::Integration.integration_cli(
1010
[
1111
'-n', 'rspec-node.github.net',
1212
'--bootstrapped-to-dir', OctocatalogDiff::Spec.fixture_path('repos/fact-overrides'),
1313
'--bootstrapped-from-dir', OctocatalogDiff::Spec.fixture_path('repos/fact-overrides'),
1414
'--output-format', 'json',
15-
'--fact-file', OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
15+
'--from-fact-file', OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'),
1616
'--to-fact-file', OctocatalogDiff::Spec.fixture_path('facts/valid-facts-different-ip.yaml'),
1717
'--puppet-binary', OctocatalogDiff::Spec::PUPPET_BINARY,
1818
'--fact-override', 'foofoo=barbar',
@@ -44,3 +44,101 @@
4444
expect(@result.stderr).to match(/Diffs computed for rspec-node.github.net/)
4545
end
4646
end
47+
48+
describe 'fact files by branch, with only a to fact file' do
49+
before(:all) do
50+
@tmpdir = Dir.mktmpdir
51+
ENV['PUPPET_FACT_DIR'] = @tmpdir
52+
FileUtils.cp OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'), File.join(@tmpdir, 'rspec-node.github.net.yaml')
53+
54+
@result = OctocatalogDiff::Integration.integration_cli(
55+
[
56+
'-n', 'rspec-node.github.net',
57+
'--bootstrapped-to-dir', OctocatalogDiff::Spec.fixture_path('repos/fact-overrides'),
58+
'--bootstrapped-from-dir', OctocatalogDiff::Spec.fixture_path('repos/fact-overrides'),
59+
'--output-format', 'json',
60+
'--to-fact-file', OctocatalogDiff::Spec.fixture_path('facts/valid-facts-different-ip.yaml'),
61+
'--puppet-binary', OctocatalogDiff::Spec::PUPPET_BINARY,
62+
'--fact-override', 'foofoo=barbar',
63+
'-d'
64+
]
65+
)
66+
end
67+
68+
after(:all) do
69+
ENV['PUPPET_FACT_DIR'] = nil
70+
OctocatalogDiff::Spec.clean_up_tmpdir(@tmpdir)
71+
end
72+
73+
it 'should exit with status 2' do
74+
expect(@result.exitcode).to eq(2), @result.stderr
75+
end
76+
77+
it 'should contain the correct diffs' do
78+
parse_result = JSON.parse(@result.stdout)['diff'].map { |x| OctocatalogDiff::Spec.remove_file_and_line(x) }
79+
expect(parse_result.size).to eq(1)
80+
expect(parse_result).to include(
81+
'diff_type' => '~',
82+
'type' => 'File',
83+
'title' => '/tmp/ipaddress',
84+
'structure' => %w(parameters content),
85+
'old_value' => '10.20.30.40',
86+
'new_value' => '10.30.50.70'
87+
)
88+
end
89+
90+
it 'should log the correct messages' do
91+
expect(@result.stderr).to match(/Catalog for . will be built with OctocatalogDiff::Catalog::Computed/)
92+
expect(@result.stderr).to match(/Override foofoo from nil to "barbar"/)
93+
expect(@result.stderr).to match(/Diffs computed for rspec-node.github.net/)
94+
end
95+
end
96+
97+
describe 'fact files by branch, with only a from fact file' do
98+
before(:all) do
99+
@tmpdir = Dir.mktmpdir
100+
ENV['PUPPET_FACT_DIR'] = @tmpdir
101+
FileUtils.cp OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml'), File.join(@tmpdir, 'rspec-node.github.net.yaml')
102+
103+
@result = OctocatalogDiff::Integration.integration_cli(
104+
[
105+
'-n', 'rspec-node.github.net',
106+
'--bootstrapped-to-dir', OctocatalogDiff::Spec.fixture_path('repos/fact-overrides'),
107+
'--bootstrapped-from-dir', OctocatalogDiff::Spec.fixture_path('repos/fact-overrides'),
108+
'--output-format', 'json',
109+
'--from-fact-file', OctocatalogDiff::Spec.fixture_path('facts/valid-facts-different-ip.yaml'),
110+
'--puppet-binary', OctocatalogDiff::Spec::PUPPET_BINARY,
111+
'--fact-override', 'foofoo=barbar',
112+
'-d'
113+
]
114+
)
115+
end
116+
117+
after(:all) do
118+
ENV['PUPPET_FACT_DIR'] = nil
119+
OctocatalogDiff::Spec.clean_up_tmpdir(@tmpdir)
120+
end
121+
122+
it 'should exit with status 2' do
123+
expect(@result.exitcode).to eq(2), @result.stderr
124+
end
125+
126+
it 'should contain the correct diffs' do
127+
parse_result = JSON.parse(@result.stdout)['diff'].map { |x| OctocatalogDiff::Spec.remove_file_and_line(x) }
128+
expect(parse_result.size).to eq(1)
129+
expect(parse_result).to include(
130+
'diff_type' => '~',
131+
'type' => 'File',
132+
'title' => '/tmp/ipaddress',
133+
'structure' => %w(parameters content),
134+
'new_value' => '10.20.30.40',
135+
'old_value' => '10.30.50.70'
136+
)
137+
end
138+
139+
it 'should log the correct messages' do
140+
expect(@result.stderr).to match(/Catalog for . will be built with OctocatalogDiff::Catalog::Computed/)
141+
expect(@result.stderr).to match(/Override foofoo from nil to "barbar"/)
142+
expect(@result.stderr).to match(/Diffs computed for rspec-node.github.net/)
143+
end
144+
end

spec/octocatalog-diff/tests/cli/options/fact_file_spec.rb

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
fact_file = OctocatalogDiff::Spec.fixture_path('facts/facts.yaml')
99
result = run_optparse(['--fact-file', fact_file])
1010
expect(result[:node]).to eq('rspec-node.xyz.github.net')
11-
result_facts = result[:facts].facts
11+
result_facts = result[:to_facts].facts
1212
expect(result_facts).to be_a_kind_of(Hash)
1313
expect(result_facts['name']).to eq('rspec-node.xyz.github.net')
1414
expect(result_facts['values']).to be_a_kind_of(Hash)
@@ -20,7 +20,7 @@
2020
fact_file = OctocatalogDiff::Spec.fixture_path('facts/facts.json')
2121
result = run_optparse(['--fact-file', fact_file])
2222
expect(result[:node]).to eq('rspec-node.xyz.github.net')
23-
result_facts = result[:facts].facts
23+
result_facts = result[:to_facts].facts
2424
expect(result_facts).to be_a_kind_of(Hash)
2525
expect(result_facts['name']).to eq('rspec-node.xyz.github.net')
2626
expect(result_facts['values']).to be_a_kind_of(Hash)
@@ -50,6 +50,20 @@
5050
end
5151

5252
describe '#opt_to_fact_file' do
53+
let(:fact_file) { OctocatalogDiff::Spec.fixture_path('facts/facts.yaml') }
54+
55+
let(:fact_answer) do
56+
{
57+
'name' => 'rspec-node.xyz.github.net',
58+
'values' => {
59+
'apt_update_last_success' => 1_458_162_123,
60+
'architecture' => 'amd64',
61+
'datacenter' => 'xyz',
62+
'fqdn' => 'rspec-node.xyz.github.net'
63+
}
64+
}
65+
end
66+
5367
it 'should distinguish between the to-facts and from-facts' do
5468
fact_file_1 = OctocatalogDiff::Spec.fixture_path('facts/facts.yaml')
5569
fact_file_2 = OctocatalogDiff::Spec.fixture_path('facts/valid-facts.yaml')
@@ -70,8 +84,24 @@
7084
expect(result_facts_2['values']['fqdn']).to eq('rspec-node.xyz.github.net')
7185
expect(result_facts_2['values']['ipaddress']).to eq('10.20.30.40')
7286
expect(result_facts_2['values'].keys).not_to include('expiration')
87+
end
7388

74-
expect(result[:facts]).to eq(result[:to_facts])
89+
it 'should only define from-fact-file when only --from-fact-file is given' do
90+
result = run_optparse(['--from-fact-file', fact_file])
91+
expect(result[:from_facts].facts).to eq(fact_answer)
92+
expect(result[:to_facts]).to be_nil
93+
end
94+
95+
it 'should only define to-fact-file when only --to-fact-file is given' do
96+
result = run_optparse(['--to-fact-file', fact_file])
97+
expect(result[:from_facts]).to be_nil
98+
expect(result[:to_facts].facts).to eq(fact_answer)
99+
end
100+
101+
it 'should define both from and to fact file when --fact-file is given' do
102+
result = run_optparse(['--fact-file', fact_file])
103+
expect(result[:from_facts].facts).to eq(fact_answer)
104+
expect(result[:to_facts].facts).to eq(fact_answer)
75105
end
76106
end
77107
end

0 commit comments

Comments
 (0)