C0 code coverage information generated on Wed Mar 15 10:52:01 MST 2006


Marked code looks like this.
This line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this.
Finally, here's a line marked as not executed.
Name Total lines Lines of code Total coverage Code coverage
./test/../bin/kvmcheck.rb 265 265
64.5% 
59.0% 
  1 #!/usr/bin/ruby
  2 
  3 # == Synopsis
  4 # Check entries in the kvm vpdbase.script against the equinix.psv
  5 #
  6 # == Usage
  7 #
  8 # kvmcheck.rb
  9 #   [ -h | --help ]
 10 #   [ -v | --verbose ]
 11 #   <-s | --script vpdbase.script>
 12 #   <-i | --input equinix.psv
 13 #
 14 # == Author
 15 # Jared Harper
 16 
 17 require 'rdoc/usage'
 18 require 'getoptlong'
 19 require 'rubygems'
 20 require 'psvparse'
 21 
 22 class Script
 23   attr_reader :host, :interfaces_header, :servers_header, :appliances_header
 24 
 25   class KVM
 26     # kmv host name
 27     attr_reader :host_name
 28     # kvm dongle
 29     attr_reader :name
 30     # kvm port
 31     attr_reader :port
 32     # kvm ip
 33     attr_reader :ip
 34 
 35     def initialize(host_name, name, port, ip)
 36       @host_name = host_name
 37       @name = name
 38       @port = port
 39       @ip = ip
 40     end
 41 
 42     def to_s
 43       "#{@host_name}|#{@name}|#{@port}|#{@ip}"
 44     end
 45   end
 46 
 47   def initialize(file)
 48     @interfaces = Array.new
 49     @interfaces_header = Array.new
 50 
 51     @servers = Array.new
 52     @servers_header = Array.new
 53 
 54     @appliances = Array.new
 55     @appliances_header = Array.new
 56 
 57     # host is keyed by the server host name
 58     @host = Hash.new
 59 
 60     @vpdbase = File.open(file)
 61     @vpdbase.each do |line|
 62       if (line.slice(/^CREATE TABLE/))
 63         header = line.split(/[()]/)
 64         table = header[0].split(' ')
 65         header[1].gsub!(/INTEGER|NOT|NULL|PRIMARY|KEY|VARCHAR/, '')
 66         case table[2]
 67           when 'APPLIANCES'
 68             fields = header[1].split(',')
 69             fields.each do |field|
 70               @appliances_header.push(field.strip.downcase.to_sym)
 71             end
 72           when 'SERVERS'
 73             fields = header[1].split(',')
 74             fields.each do |field|
 75               @servers_header.push(field.strip.downcase.to_sym)
 76             end
 77           when 'INTERFACES'
 78             fields = header[1].split(',')
 79             fields.each do |field|
 80               @interfaces_header.push(field.strip.downcase.to_sym)
 81             end
 82         end
 83       elsif line.slice(/^INSERT INTO/)
 84         header = line.split(/[()]/)
 85         table = header[0].split(' ')
 86         case table[2]
 87           when 'APPLIANCES'
 88             data = FasterCSV.new(header[1])
 89             row = FasterCSV::Row.new(@appliances_header, data.shift)
 90             @appliances[Integer(row[:id])] = row
 91           when 'SERVERS'
 92             data = FasterCSV.new(header[1])
 93             row = FasterCSV::Row.new(@servers_header, data.shift())
 94             @servers[Integer(row[:id])] = row
 95           when 'INTERFACES'
 96             data = FasterCSV.new(header[1])
 97             row = FasterCSV::Row.new(@interfaces_header, data.shift())
 98             @interfaces[Integer(row[:id])] = row
 99         end
100       end
101     end
102     @vpdbase.close()
103 
104     @interfaces.each do |record|
105       if record
106         if not @host.has_key?(@servers[Integer(record[:conn_id])][:name])
107           @host[@servers[Integer(record[:conn_id])][:name][1...-1]] =
108             KVM.new(
109                 @appliances[Integer(record[:appliance_id])][:name][1...-1],
110                 record[:name][1...-1],
111                 record[:port_num],
112                 @appliances[Integer(record[:appliance_id])][:address][1...-1])
113         else
114           print "Error: host #{@servers[Integer(record[:conn_id])][:name]} in list twice\n"
115         end
116       end
117     end
118   end
119 
120   def output()
121     printf("host|kvm|name|port|ip\n")
122     @host.keys.sort.each do |key|
123       printf("%s|%s|%s|%s|%s\n",
124         key,
125         @host[key].host_name,
126         @host[key].name,
127         @host[key].port,
128         @host[key].ip)
129     end
130   end
131 
132   def has_host?(host)
133     @host[host]
134   end
135 
136   def name?(host, name)
137     name == @host[host].host_name
138   end
139 
140   def port?(host, port)
141     port == @host[host].port
142   end
143 
144   def ip?(host, ip)
145     ip == @host[host].ip
146   end
147 
148 end
149 
150 class AllHosts
151   attr_reader :hosts, :parser
152 
153   NO_KVM_NAME = '?'
154   NO_KVM_PORT = '?'
155   NO_KVM_IP = '?'
156 
157   class Host
158     attr_reader :host_name, :kvm_name, :kvm_port, :kvm_ip
159 
160     def initialize(host_name, kvm_name, kvm_port, kvm_ip)
161       @host_name = host_name
162       @kvm_name = kvm_name
163       @kvm_port = kvm_port
164       @kvm_ip = kvm_ip
165     end
166 
167     def to_s
168       "#{@host_name}|#{@kvm_name}|#{@kvm_port}|#{@kvm_ip}"
169     end
170   end
171 
172   def initialize(file)
173     @hosts = Array.new
174     @parser = PSVParse.new(file)
175 
176     psvHost = @parser.search(
177       {:search_term => 'HOST_NAME',
178        :search_field => :host_name,
179        :result_field => :all,
180        :negated => true})
181     psvHost.each do |row|
182       @hosts.push(Host.new(
183           row[:host_name],
184           row[:kvm_name] || NO_KVM_NAME,
185           row[:kvm_port] || NO_KVM_PORT,
186           row[:kvm_ip] || NO_KVM_IP))
187      end
188   end
189 
190   def output()
191     hosts.each do |row|
192       printf("host: %s\tkvm: %s\tport: %s\tip: %s\n",
193         row[:host_name],
194         row[:kvm_name],
195         row[:kvm_port],
196         row[:kvm_ip])
197     end
198   end
199 
200   def has_kvm?(host)
201     host.kvm_name != 'NO_KVM_NAME'
202   end
203 end
204 
205 ################# main ################
206 if __FILE__ == $0
207 @verbose = false
208 @script = 'vpdbase.script'
209 @filename = 'equinix.psv'
210 @list = false
211 
212   opts = GetoptLong.new(
213       ["--verbose", "-v", GetoptLong::NO_ARGUMENT],
214       ["--script", "-s", GetoptLong::REQUIRED_ARGUMENT],
215       ["--input", "-i", GetoptLong::REQUIRED_ARGUMENT],
216       ["--help", "-h", GetoptLong::NO_ARGUMENT],
217       ["--list", "-l", GetoptLong::NO_ARGUMENT])
218 
219   opts.each do |opt, arg|
220     case opt
221       when '--verbose' then
222         @verbose = true
223       when '--list' then
224         @list = true
225       when '--script' then
226         @script = arg
227       when '--input' then
228         @filename = arg
229       when '--help' then
230         RDoc::usage
231     end
232   end
233 
234   script = Script.new(@script)
235 
236   script.output if @verbose == true
237 
238   if @list
239     script.output
240   else
241     hosts = AllHosts.new(@filename)
242     hosts.output if @verbose == true
243     hosts.hosts.each do |host|
244       if script.has_host?(host.host_name)
245 	if not script.name?(host.host_name, host.kvm_name)
246 	  print "Error: Name '#{host.to_s}' not equal '#{script.host[host.host_name].to_s}'\n"
247 	end
248 	if not script.port?(host.host_name, host.kvm_port)
249 	  print "Error: Port '#{host.to_s}' not equal '#{script.host[host.host_name].to_s}'\n"
250 	end
251 	if not script.ip?(host.host_name, host.kvm_ip)
252 	  print "Error: IP '#{host.to_s}' not equal '#{script.host[host.host_name].to_s}'\n"
253 	end
254       else
255 	if !hosts.has_kvm?(host)
256 	  print "Error: '#{host.to_s}' has no configured kvm\n"
257 	else
258 	  print "Error: '#{host.to_s}' not found in vpdbase.script\n"
259 	end
260       end
261     end
262   end
263 end
264 
265 # vim:ts=2:sw=2:sts=2:et

Valid XHTML 1.0!Valid CSS!