summaryrefslogtreecommitdiff
path: root/spec/unit/plugins/c_spec.rb
blob: 33daa21c1b9b7cb3f9259c1155452415fc7dde07 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

# Author:: Doug MacEachern <dougm@vmware.com>
# Copyright:: Copyright (c) 2010 VMware, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'rbconfig'

require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb'))

C_GCC = <<EOF
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr ... --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)
EOF

C_GLIBC_2_3_4 = <<EOF
GNU C Library stable release version 2.3.4, by Roland McGrath et al.
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 3.4.6 20060404 (Red Hat 3.4.6-3).
Compiled on a Linux 2.4.20 system on 2006-08-12.
Available extensions:
        GNU libio by Per Bothner
        crypt add-on version 2.1 by Michael Glad and others
        linuxthreads-0.10 by Xavier Leroy
        The C stubs add-on version 2.1.2.
        BIND-8.2.3-T5B
        NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
        Glibc-2.0 compatibility add-on by Cristian Gafton 
        GNU Libidn by Simon Josefsson
        libthread_db work sponsored by Alpha Processor Inc
Thread-local storage support included.
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
EOF

C_GLIBC_2_5 = <<EOF
GNU C Library stable release version 2.5, by Roland McGrath et al.
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.1.2 20080704 (Red Hat 4.1.2-44).
Compiled on a Linux 2.6.9 system on 2009-09-02.
Available extensions:
	The C stubs add-on version 2.1.2.
	crypt add-on version 2.1 by Michael Glad and others
	GNU Libidn by Simon Josefsson
	GNU libio by Per Bothner
	NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
	Native POSIX Threads Library by Ulrich Drepper et al
	BIND-8.2.3-T5B
	RT using linux kernel aio
Thread-local storage support included.
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
EOF

C_CL = <<EOF
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.
EOF

C_VS = <<EOF

Microsoft (R) Visual Studio Version 8.0.50727.762.
Copyright (C) Microsoft Corp 1984-2005. All rights reserved.
EOF

C_XLC = <<EOF
IBM XL C/C++ Enterprise Edition for AIX, V9.0
Version: 09.00.0000.0000
EOF

C_SUN = <<EOF
cc: Sun C 5.8 Patch 121016-06 2007/08/01
EOF

C_HPUX = <<EOF
/opt/ansic/bin/cc:
        $Revision: 92453-07 linker linker crt0.o B.11.47 051104 $
        LINT B.11.11.16 CXREF B.11.11.16
        HP92453-01 B.11.11.16 HP C Compiler
         $ PATCH/11.00:PHCO_27774  Oct  3 2002 09:45:59 $ 
EOF

describe Ohai::System, "plugin c" do

  before(:each) do
    @plugin = get_plugin("c")

    @plugin[:languages] = Mash.new
    #gcc
    @plugin.stub(:shell_out).with("gcc -v").and_return(mock_shell_out(0, "", C_GCC))
    #glibc
    @plugin.stub(:shell_out).with("/lib/libc.so.6").and_return(mock_shell_out(0, C_GLIBC_2_3_4, ""))
    #ms cl
    @plugin.stub(:shell_out).with("cl /\?").and_return(mock_shell_out(0, "", C_CL))
    #ms vs
    @plugin.stub(:shell_out).with("devenv.com /\?").and_return(mock_shell_out(0, C_VS, ""))
    #ibm xlc
    @plugin.stub(:shell_out).with("xlc -qversion").and_return(mock_shell_out(0, C_XLC, ""))
    #sun pro
    @plugin.stub(:shell_out).with("cc -V -flags").and_return(mock_shell_out(0, "", C_SUN))
    #hpux cc
    @plugin.stub(:shell_out).with("what /opt/ansic/bin/cc").and_return(mock_shell_out(0, C_HPUX, ""))
  end

  #gcc
  it "should get the gcc version from running gcc -v" do
    @plugin.should_receive(:shell_out).with("gcc -v").and_return(mock_shell_out(0, "", C_GCC))
    @plugin.run
  end

  it "should set languages[:c][:gcc][:version]" do
    @plugin.run
    @plugin.languages[:c][:gcc][:version].should eql("3.4.6")
  end

  it "should set languages[:c][:gcc][:description]" do
    @plugin.run
    @plugin.languages[:c][:gcc][:description].should eql(C_GCC.split($/).last)
  end

  it "should not set the languages[:c][:gcc] tree up if gcc command fails" do
    @plugin.stub(:shell_out).with("gcc -v").and_return(mock_shell_out(1, "", ""))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:gcc) if @plugin[:languages][:c]
  end

  #glibc
  it "should get the glibc x.x.x version from running /lib/libc.so.6" do
    @plugin.should_receive(:shell_out).with("/lib/libc.so.6").and_return(mock_shell_out(0, C_GLIBC_2_3_4, ""))
    @plugin.run
  end

  it "should set languages[:c][:glibc][:version]" do
    @plugin.run
    @plugin.languages[:c][:glibc][:version].should eql("2.3.4")
  end

  it "should set languages[:c][:glibc][:description]" do
    @plugin.run
    @plugin.languages[:c][:glibc][:description].should eql(C_GLIBC_2_3_4.split($/).first)
  end

  it "should not set the languages[:c][:glibc] tree up if glibc command fails" do
    @plugin.stub(:shell_out).with("/lib/libc.so.6").and_return(mock_shell_out(1, "", ""))
    @plugin.stub(:shell_out).with("/lib64/libc.so.6").and_return(mock_shell_out(1, "", ""))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:glibc) if @plugin[:languages][:c]
  end

  it "should get the glibc x.x version from running /lib/libc.so.6" do
    @plugin.stub(:shell_out).with("/lib/libc.so.6").and_return(mock_shell_out(0, C_GLIBC_2_5, ""))
    @plugin.should_receive(:shell_out).with("/lib/libc.so.6").and_return(mock_shell_out(0, C_GLIBC_2_5, ""))
    @plugin.run
    @plugin.languages[:c][:glibc][:version].should eql("2.5")
  end

  #ms cl
  it "should get the cl version from running cl /?" do
    @plugin.should_receive(:shell_out).with("cl /\?").and_return(mock_shell_out(0, "", C_CL))
    @plugin.run
  end

  it "should set languages[:c][:cl][:version]" do
    @plugin.run
    @plugin.languages[:c][:cl][:version].should eql("14.00.50727.762")
  end

  it "should set languages[:c][:cl][:description]" do
    @plugin.run
    @plugin.languages[:c][:cl][:description].should eql(C_CL.split($/).first)
  end

  it "should not set the languages[:c][:cl] tree up if cl command fails" do
    @plugin.stub(:shell_out).with("cl /\?").and_return(mock_shell_out(1, "", ""))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:cl) if @plugin[:languages][:c]
  end

  #ms vs
  it "should get the vs version from running devenv.com /?" do
    @plugin.should_receive(:shell_out).with("devenv.com /\?").and_return(mock_shell_out(0, C_VS, ""))
    @plugin.run
  end

  it "should set languages[:c][:vs][:version]" do
    @plugin.run
    @plugin.languages[:c][:vs][:version].should eql("8.0.50727.762")
  end

  it "should set languages[:c][:vs][:description]" do
    @plugin.run
    @plugin.languages[:c][:vs][:description].should eql(C_VS.split($/)[1])
  end

  it "should not set the languages[:c][:vs] tree up if devenv command fails" do
    @plugin.stub(:shell_out).with("devenv.com /\?").and_return(mock_shell_out(1, "", ""))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:vs) if @plugin[:languages][:c]
  end

  #ibm xlc
  it "should get the xlc version from running xlc -qversion" do
    @plugin.should_receive(:shell_out).with("xlc -qversion").and_return(mock_shell_out(0, C_XLC, ""))
    @plugin.run
  end

  it "should set languages[:c][:xlc][:version]" do
    @plugin.run
    @plugin.languages[:c][:xlc][:version].should eql("9.0")
  end

  it "should set languages[:c][:xlc][:description]" do
    @plugin.run
    @plugin.languages[:c][:xlc][:description].should eql(C_XLC.split($/).first)
  end

  it "should not set the languages[:c][:xlc] tree up if xlc command fails" do
    @plugin.stub(:shell_out).with("xlc -qversion").and_return(mock_shell_out(1, "", ""))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:xlc) if @plugin[:languages][:c]
  end

  it "should set the languages[:c][:xlc] tree up if xlc exit status is 249" do
    @plugin.stub(:shell_out).with("xlc -qversion").and_return(mock_shell_out(63744, "", ""))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:xlc) if @plugin[:languages][:c]
  end

  #sun pro
  it "should get the cc version from running cc -V -flags" do
    @plugin.should_receive(:shell_out).with("cc -V -flags").and_return(mock_shell_out(0, "", C_SUN))
    @plugin.run
  end

  it "should set languages[:c][:sunpro][:version]" do
    @plugin.run
    @plugin.languages[:c][:sunpro][:version].should eql("5.8")
  end

  it "should set languages[:c][:sunpro][:description]" do
    @plugin.run
    @plugin.languages[:c][:sunpro][:description].should eql(C_SUN.chomp)
  end

  it "should not set the languages[:c][:sunpro] tree up if cc command fails" do
    @plugin.stub(:shell_out).with("cc -V -flags").and_return(mock_shell_out(1, "", ""))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:sunpro) if @plugin[:languages][:c]
  end

  it "should not set the languages[:c][:sunpro] tree if the corresponding cc command fails on linux" do
    fedora_error_message = "cc: error trying to exec 'i686-redhat-linux-gcc--flags': execvp: No such file or directory"

    @plugin.stub(:shell_out).with("cc -V -flags").and_return(mock_shell_out(0, "", fedora_error_message))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:sunpro) if @plugin[:languages][:c]
  end

  it "should not set the languages[:c][:sunpro] tree if the corresponding cc command fails on hpux" do
    hpux_error_message = "cc: warning 901: unknown option: `-flags': use +help for online documentation.\ncc: HP C/aC++ B3910B A.06.25 [Nov 30 2009]"
    @plugin.stub(:shell_out).with("cc -V -flags").and_return(mock_shell_out(0, "", hpux_error_message))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:sunpro) if @plugin[:languages][:c]
  end

  #hpux cc
  it "should get the cc version from running what cc" do
    @plugin.should_receive(:shell_out).with("what /opt/ansic/bin/cc").and_return(mock_shell_out(0, C_HPUX, ""))
    @plugin.run
  end

  it "should set languages[:c][:hpcc][:version]" do
    @plugin.run
    @plugin.languages[:c][:hpcc][:version].should eql("B.11.11.16")
  end

  it "should set languages[:c][:hpcc][:description]" do
    @plugin.run
    @plugin.languages[:c][:hpcc][:description].should eql(C_HPUX.split($/)[3].strip)
  end

  it "should not set the languages[:c][:hpcc] tree up if cc command fails" do
    @plugin.stub(:shell_out).with("what /opt/ansic/bin/cc").and_return(mock_shell_out(1, "", ""))
    @plugin.run
    @plugin[:languages][:c].should_not have_key(:hpcc) if @plugin[:languages][:c]
  end

  require File.expand_path( File.join( File.dirname( __FILE__ ), '..', 'path', 'ohai_plugin_common.rb' ))

  test_plugin([ "languages", "c" ], [ "/lib/libc.so.6", "/lib64/libc.so.6", "gcc", "cl", "devenv.com", "xlc", "cc", "what" ]) do | p |
    p.test([ "centos-5.5" ], [ "x64" ], [[]], { "languages" => { "c" => {
                 "gcc" => { "version" => "4.1.2", "description" => "gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)" },
                 "glibc" => { "version" => "2.5" , "description" => "GNU C Library stable release version 2.5, by Roland McGrath et al." },
                 "cl" => nil, "vs" => nil, "xlc" => nil, "sunpro" => nil, "hpcc" => nil }}})
    p.test([ "centos-6.2" ], [ "x86", "x64" ], [[]], { "languages" => { "c" => {
                 "gcc" => { "version" => "4.4.6", "description" => "gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) " },
                 "glibc" => { "version" => "2.12", "description" => "GNU C Library stable release version 2.12, by Roland McGrath et al." },
                 "cl" => nil, "vs" => nil, "xlc" => nil, "sunpro" => nil, "hpcc" => nil }}})
    p.test([ "ubuntu-10.04" ], [ "x86", "x64" ], [[]],
           { "languages" => { "c" => {
                 "glibc" => { "version" => "2.11.1", "description" => "GNU C Library (Ubuntu EGLIBC 2.11.1-0ubuntu7.12) stable release version 2.11.1, by Roland McGrath et al." },
                 "gcc" => nil, "cl" => nil, "vs" => nil, "xlc" => nil, "sunpro" => nil, "hpcc" => nil }}})
    p.test([ "ubuntu-12.04", "ubuntu-13.04" ], [ "x86", "x64" ], [[]], { "languages" => { "c" => nil }}, "OC-9993")
    p.test([ "ubuntu-12.10" ], [ "x64" ], [[]], { "languages" => { "c" => nil }}, "OC-9993")
    p.test([ "centos-5.5" ], [ "x64" ], [[ "gcc" ]], { "languages" => { "c" => {
                 "gcc" => { "version" => "4.1.2", "description" => "gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)" },
                 "glibc" => { "version" => "2.5", "description" => "GNU C Library stable release version 2.5, by Roland McGrath et al." },
                 "cl" => nil, "vs" => nil, "xlc" => nil, "sunpro" => nil, "hpcc" => nil }}})
    p.test([ "centos-6.2" ], [ "x86", "x64" ], [[ "gcc" ]], { "languages" => { "c" => {
                 "gcc" => { "version" => "4.4.7", "description" => "gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) " },
                 "glibc" => { "version" => "2.12", "description" => "GNU C Library stable release version 2.12, by Roland McGrath et al." },
                 "cl" => nil, "vs" => nil, "xlc" => nil, "sunpro" => nil, "hpcc" => nil }}})
    p.test([ "ubuntu-10.04" ], [ "x86", "x64" ], [[ "gcc" ]], { "languages" => { "c" => {
                 "gcc" => { "version" => "4.4.3", "description" => "gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) " },
                 "glibc" => { "version" => "2.11.1", "description" => "GNU C Library (Ubuntu EGLIBC 2.11.1-0ubuntu7.12) stable release version 2.11.1, by Roland McGrath et al." },
                 "cl" => nil, "vs" => nil, "xlc" => nil, "sunpro" => nil, "hpcc" => nil }}})
    p.test([ "ubuntu-12.04" ], [ "x86", "x64" ], [[ "gcc" ]], { "languages" => { "c" => {
                 "gcc" => { "version" => "4.6.3", "description" => "gcc verison 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) " },
                 "glibc" => nil, "cl" => nil, "vs" => nil, "xlc" => nil, "sunpro" => nil, "hpcc" => nil }}}, "OC-9993" )
  end
end