This repository was archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathos_detector_spec.rb
More file actions
220 lines (167 loc) · 7.08 KB
/
os_detector_spec.rb
File metadata and controls
220 lines (167 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# frozen_string_literal: true
require 'rbconfig'
describe OsDetector do
let(:os_hierarchy) { instance_spy(Facter::OsHierarchy) }
let(:logger) { instance_spy(Facter::Log) }
before do
Singleton.__init__(OsDetector)
allow(Facter::Log).to receive(:new).and_return(logger)
allow(Facter::OsHierarchy).to receive(:new).and_return(os_hierarchy)
end
describe 'initialize' do
context 'when os is macosx' do
before do
RbConfig::CONFIG['host_os'] = 'darwin'
allow(os_hierarchy).to receive(:construct_hierarchy).with(:macosx).and_return(['macosx'])
end
it 'detects os as macosx' do
expect(OsDetector.instance.identifier).to eq(:macosx)
end
it 'calls hierarchy construction with macosx identifier' do
OsDetector.instance
expect(os_hierarchy).to have_received(:construct_hierarchy).with(:macosx)
end
it 'construct hierarchy with darwin identifier' do
expect(OsDetector.instance.hierarchy).to eq(['macosx'])
end
end
context 'when os is windows' do
before do
RbConfig::CONFIG['host_os'] = 'mingw'
allow(os_hierarchy).to receive(:construct_hierarchy).with(:windows).and_return(['windows'])
end
it 'detects os as windows' do
expect(OsDetector.instance.identifier).to eq(:windows)
end
it 'calls hierarchy construction with windows identifier' do
OsDetector.instance
expect(os_hierarchy).to have_received(:construct_hierarchy).with(:windows)
end
it 'construct hierarchy with windows identifier' do
expect(OsDetector.instance.hierarchy).to eq(['windows'])
end
end
context 'when os is solaris' do
before do
RbConfig::CONFIG['host_os'] = 'solaris'
allow(os_hierarchy).to receive(:construct_hierarchy).with(:solaris).and_return(['solaris'])
end
it 'detects os as solaris' do
expect(OsDetector.instance.identifier).to eq(:solaris)
end
it 'calls hierarchy construction with solaris identifier' do
OsDetector.instance
expect(os_hierarchy).to have_received(:construct_hierarchy).with(:solaris)
end
it 'construct hierarchy with solaris identifier' do
expect(OsDetector.instance.hierarchy).to eq(['solaris'])
end
end
context 'when os is aix' do
before do
RbConfig::CONFIG['host_os'] = 'aix'
allow(os_hierarchy).to receive(:construct_hierarchy).with(:aix).and_return(['aix'])
end
it 'detects os as aix' do
expect(OsDetector.instance.identifier).to eq(:aix)
end
it 'calls hierarchy construction with aix identifier' do
OsDetector.instance
expect(os_hierarchy).to have_received(:construct_hierarchy).with(:aix)
end
it 'construct hierarchy with aix identifier' do
expect(OsDetector.instance.hierarchy).to eq(['aix'])
end
end
context 'when os cannot be detected' do
before do
RbConfig::CONFIG['host_os'] = 'my_custom_os'
end
it 'raises error if it could not detect os' do
expect { OsDetector.instance.identifier }.to raise_error(RuntimeError, 'unknown os: "my_custom_os"')
end
end
context 'when host_os is linux' do
before do
RbConfig::CONFIG['host_os'] = 'linux'
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:identifier)
allow(Facter::Resolvers::RedHatRelease).to receive(:resolve).with(:identifier).and_return(:redhat)
allow(Facter::Resolvers::SuseRelease).to receive(:resolve).with(:identifier)
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:version)
allow(Facter::Resolvers::RedHatRelease).to receive(:resolve).with(:version)
allow(Facter::Resolvers::SuseRelease).to receive(:resolve).with(:version)
allow(os_hierarchy).to receive(:construct_hierarchy).with(:redhat).and_return(%w[linux redhat])
end
it 'detects linux distro' do
expect(OsDetector.instance.identifier).to be(:redhat)
end
it 'calls Facter::OsHierarchy with construct_hierarchy' do
OsDetector.instance
expect(os_hierarchy).to have_received(:construct_hierarchy).with(:redhat)
end
it 'calls Facter::Resolvers::OsRelease with identifier' do
OsDetector.instance
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:identifier)
end
it 'calls Facter::Resolvers::RedHatRelease with identifier' do
OsDetector.instance
expect(Facter::Resolvers::RedHatRelease).to have_received(:resolve).with(:identifier)
end
it 'calls Facter::Resolvers::OsRelease with version' do
OsDetector.instance
expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:version)
end
it 'calls Facter::Resolvers::RedHatRelease with version' do
OsDetector.instance
expect(Facter::Resolvers::RedHatRelease).to have_received(:resolve).with(:version)
end
context 'when distribution is not known' do
before do
allow(Facter::Resolvers::RedHatRelease).to receive(:resolve).with(:identifier).and_return('my_linux_distro')
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:id_like).and_return(nil)
allow(os_hierarchy).to receive(:construct_hierarchy).and_return([])
allow(os_hierarchy).to receive(:construct_hierarchy).with(:linux).and_return(['linux'])
Singleton.__init__(OsDetector)
end
it 'falls back to linux' do
expect(OsDetector.instance.identifier).to eq(:my_linux_distro)
end
context 'when no hierarchy for os identifier' do
it 'logs debug message' do
OsDetector.instance
expect(logger)
.to have_received(:debug)
.with('Could not detect hierarchy using os identifier: my_linux_distro , trying with family')
end
end
context 'when no os family detected' do
it 'logs debug message' do
OsDetector.instance
expect(logger)
.to have_received(:debug)
.with('Could not detect hierarchy using family , falling back to Linux')
end
end
it 'constructs hierarchy with linux' do
expect(OsDetector.instance.hierarchy).to eq(['linux'])
end
context 'when family is known' do
before do
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:id_like).and_return(:ubuntu)
allow(os_hierarchy).to receive(:construct_hierarchy).with(:ubuntu).and_return(%w[Linux Debian Ubuntu])
Singleton.__init__(OsDetector)
end
it 'constructs hierarchy with linux' do
expect(OsDetector.instance.hierarchy).to eq(%w[Linux Debian Ubuntu])
end
it 'logs debug message' do
OsDetector.instance
expect(logger)
.to have_received(:debug)
.with('Could not detect hierarchy using os identifier: my_linux_distro , trying with family')
end
end
end
end
end
end